55Author: Daniel Ingram (daniel-s-ingram)
66 Atsushi Sakai (@Atsushi_twi)
77
8- Ref: P. I. Corke, "Robotics, Vision & Control", Springer 2017, ISBN 978-3-319-54413-7 p102
9- - [Robotics, Vision and Control \| SpringerLink](https://link.springer.com/book/10.1007/978-3-642-20144-8)
8+ Ref: P. I. Corke, "Robotics, Vision & Control", Springer 2017,
9+ ISBN 978-3-319-54413-7 p102
10+ - [Robotics, Vision and Control]
11+ (https://link.springer.com/book/10.1007/978-3-642-20144-8)
1012
1113"""
1214
@@ -37,6 +39,7 @@ def two_joint_arm(GOAL_TH=0.0, theta1=0.0, theta2=0.0):
3739 When out of bounds, rewrite x and y with last correct values
3840 """
3941 global x , y
42+ x_prev , y_prev = None , None
4043 while True :
4144 try :
4245 if x is not None and y is not None :
@@ -47,33 +50,36 @@ def two_joint_arm(GOAL_TH=0.0, theta1=0.0, theta2=0.0):
4750 else :
4851 theta2_goal = np .arccos (
4952 (x ** 2 + y ** 2 - l1 ** 2 - l2 ** 2 ) / (2 * l1 * l2 ))
50- theta1_goal = np .math .atan2 (y , x ) - np .math .atan2 (l2 *
51- np .sin (theta2_goal ), (l1 + l2 * np .cos (theta2_goal )))
53+ tmp = np .math .atan2 (l2 * np .sin (theta2_goal ),
54+ (l1 + l2 * np .cos (theta2_goal )))
55+ theta1_goal = np .math .atan2 (y , x ) - tmp
5256
5357 if theta1_goal < 0 :
5458 theta2_goal = - theta2_goal
55- theta1_goal = np .math .atan2 (
56- y , x ) - np .math .atan2 (l2 * np .sin (theta2_goal ), (l1 + l2 * np .cos (theta2_goal )))
59+ tmp = np .math .atan2 (l2 * np .sin (theta2_goal ),
60+ (l1 + l2 * np .cos (theta2_goal )))
61+ theta1_goal = np .math .atan2 (y , x ) - tmp
5762
5863 theta1 = theta1 + Kp * ang_diff (theta1_goal , theta1 ) * dt
5964 theta2 = theta2 + Kp * ang_diff (theta2_goal , theta2 ) * dt
6065 except ValueError as e :
61- print ("Unreachable goal" )
66+ print ("Unreachable goal" + e )
6267 except TypeError :
6368 x = x_prev
6469 y = y_prev
6570
6671 wrist = plot_arm (theta1 , theta2 , x , y )
6772
6873 # check goal
74+ d2goal = None
6975 if x is not None and y is not None :
7076 d2goal = np .hypot (wrist [0 ] - x , wrist [1 ] - y )
7177
7278 if abs (d2goal ) < GOAL_TH and x is not None :
7379 return theta1 , theta2
7480
7581
76- def plot_arm (theta1 , theta2 , x , y ): # pragma: no cover
82+ def plot_arm (theta1 , theta2 , target_x , target_y ): # pragma: no cover
7783 shoulder = np .array ([0 , 0 ])
7884 elbow = shoulder + np .array ([l1 * np .cos (theta1 ), l1 * np .sin (theta1 )])
7985 wrist = elbow + \
@@ -89,8 +95,8 @@ def plot_arm(theta1, theta2, x, y): # pragma: no cover
8995 plt .plot (elbow [0 ], elbow [1 ], 'ro' )
9096 plt .plot (wrist [0 ], wrist [1 ], 'ro' )
9197
92- plt .plot ([wrist [0 ], x ], [wrist [1 ], y ], 'g--' )
93- plt .plot (x , y , 'g*' )
98+ plt .plot ([wrist [0 ], target_x ], [wrist [1 ], target_y ], 'g--' )
99+ plt .plot (target_x , target_y , 'g*' )
94100
95101 plt .xlim (- 2 , 2 )
96102 plt .ylim (- 2 , 2 )
0 commit comments