5
5
Author: Daniel Ingram (daniel-s-ingram)
6
6
Atsushi Sakai (@Atsushi_twi)
7
7
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)
10
12
11
13
"""
12
14
@@ -37,6 +39,7 @@ def two_joint_arm(GOAL_TH=0.0, theta1=0.0, theta2=0.0):
37
39
When out of bounds, rewrite x and y with last correct values
38
40
"""
39
41
global x , y
42
+ x_prev , y_prev = None , None
40
43
while True :
41
44
try :
42
45
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):
47
50
else :
48
51
theta2_goal = np .arccos (
49
52
(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
52
56
53
57
if theta1_goal < 0 :
54
58
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
57
62
58
63
theta1 = theta1 + Kp * ang_diff (theta1_goal , theta1 ) * dt
59
64
theta2 = theta2 + Kp * ang_diff (theta2_goal , theta2 ) * dt
60
65
except ValueError as e :
61
- print ("Unreachable goal" )
66
+ print ("Unreachable goal" + e )
62
67
except TypeError :
63
68
x = x_prev
64
69
y = y_prev
65
70
66
71
wrist = plot_arm (theta1 , theta2 , x , y )
67
72
68
73
# check goal
74
+ d2goal = None
69
75
if x is not None and y is not None :
70
76
d2goal = np .hypot (wrist [0 ] - x , wrist [1 ] - y )
71
77
72
78
if abs (d2goal ) < GOAL_TH and x is not None :
73
79
return theta1 , theta2
74
80
75
81
76
- def plot_arm (theta1 , theta2 , x , y ): # pragma: no cover
82
+ def plot_arm (theta1 , theta2 , target_x , target_y ): # pragma: no cover
77
83
shoulder = np .array ([0 , 0 ])
78
84
elbow = shoulder + np .array ([l1 * np .cos (theta1 ), l1 * np .sin (theta1 )])
79
85
wrist = elbow + \
@@ -89,8 +95,8 @@ def plot_arm(theta1, theta2, x, y): # pragma: no cover
89
95
plt .plot (elbow [0 ], elbow [1 ], 'ro' )
90
96
plt .plot (wrist [0 ], wrist [1 ], 'ro' )
91
97
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*' )
94
100
95
101
plt .xlim (- 2 , 2 )
96
102
plt .ylim (- 2 , 2 )
0 commit comments