お世話になってます。
現在、マニピュレーターの順運動学を勉強しているところです。
6軸アームロボットをイメージしてもらえるとわかりやすいかと思います。
どのアームをどのくらい曲げたら先端の座標はどこにあるのかをpythonで計算することが目的です
このサイトを参考にしました。このサイトでは2次元空間でのグラフ(というよりアームの形状)しかありません。
2次元ではなく3次元空間での計算をしたいと思っています。
参考にしたサイトのコードと私が作成したコードの違いはグラフの次元とアームは1個だけです。
問題点
以下のコードを実行してもエラー文は出てきませんが、スライダーを動かしてもグラフの形状が変わらないことが問題点です。
この問題をクリアするために力を貸していただけないでしょうか?
試したこと
- 関数
def update_th1(slider_val):
内のfig.canvas.draw_idle()
が違うのかなと思いあらゆる代案を実行してみましたが、いずれもダメでした。(代案が何かは忘れてしまいました。。 - アームの角度から座標を求める計算式の再確認
実行環境
ubuntu 16.04
python 3.5.2
matplotlib 2.2.2
python3
1##Reference source : https://algorithm.joho.info/programming/python/forward-kinematics-simulation/ 2 3import numpy as np 4import matplotlib.pyplot as plt 5from mpl_toolkits.mplot3d import Axes3D 6from matplotlib.widgets import Slider 7import math 8 9# 順運動学の計算 10def fk(L, th): 11 # 各リンクの長さと関節角度の取得 12 l1 = L 13 th1 = th 14 15 # リンク1の手先 16 x1 = l1 * math.cos(th1) 17 y1 = l1 * math.sin(th1) 18 19 # 手先位置をnumpy配列に格納して返す 20 return np.array([[0, 0],[x1, y1]]) 21 22def main(): 23 # リンク1、2の長さ 24 L = 0.5 25 26 # 第1、2の関節角度 27 th = np.radians([90]) 28 29 # 順運動学の計算 30 p = fk(L, th) 31 32 # グラフ描画位置の設定 33 #fig, ax = plt.subplots() 34 fig = plt.figure() 35 ax = Axes3D(fig) 36 37 plt.axis('equal') 38 plt.subplots_adjust(left=0.1, bottom=0.15) 39 plt.xlim([-1, 1]) 40 plt.ylim([-0.3 ,1.3]) 41 # グラフ描画 42 plt.grid() 43 graph, = plt.plot(p.T[0], p.T[1]) 44 45 def update_th1(slider_val): 46 # 関節1の角度を更新 47 th[0] = np.radians([slider_val]) 48 49 # 順運動学の計算 50 p = fk(L, th) 51 52 # 手先位置を更新 53 graph.set_data(p.T[0], p.T[1]) 54 graph.set_linestyle('-') 55 graph.set_linewidth(5) 56 graph.set_marker('o') 57 graph.set_markerfacecolor('g') 58 graph.set_markeredgecolor('g') 59 graph.set_markersize(15) 60 61 # グラフ再描画 62 fig.canvas.draw_idle() 63 64 # スライダーの表示位置 65 slider1_pos = plt.axes([0.1, 0.05, 0.8, 0.03]) 66 67 # Sliderオブジェクトのインスタンス作成 68 threshold_slider1 = Slider(slider1_pos, 'th1', 0, 180) 69 70 # スライダーの値が変更された場合の処理を呼び出し 71 threshold_slider1.on_changed(update_th1) 72 graph.set_linestyle('-') 73 graph.set_linewidth(5) 74 graph.set_marker('o') 75 graph.set_markerfacecolor('g') 76 graph.set_markeredgecolor('g') 77 graph.set_markersize(15) 78 plt.grid() 79 plt.show() 80 81if __name__ == '__main__': 82 main() 83
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。