3434def two_joint_arm (GOAL_TH = 0.0 , theta1 = 0.0 , theta2 = 0.0 ):
3535 """
3636 Computes the inverse kinematics for a planar 2DOF arm
37+ When out of bounds, rewrite x and y with last correct values
3738 """
39+ global x , y
3840 while True :
3941 try :
42+ if x is not None and y is not None :
43+ x_prev = x
44+ y_prev = y
4045 if np .sqrt (x ** 2 + y ** 2 ) > (l1 + l2 ):
4146 theta2_goal = 0
4247 else :
@@ -54,11 +59,15 @@ def two_joint_arm(GOAL_TH=0.0, theta1=0.0, theta2=0.0):
5459 theta2 = theta2 + Kp * ang_diff (theta2_goal , theta2 ) * dt
5560 except ValueError as e :
5661 print ("Unreachable goal" )
62+ except TypeError :
63+ x = x_prev
64+ y = y_prev
5765
5866 wrist = plot_arm (theta1 , theta2 , x , y )
5967
6068 # check goal
61- d2goal = np .hypot (wrist [0 ] - x , wrist [1 ] - y )
69+ if x is not None and y is not None :
70+ d2goal = np .hypot (wrist [0 ] - x , wrist [1 ] - y )
6271
6372 if abs (d2goal ) < GOAL_TH and x is not None :
6473 return theta1 , theta2
@@ -118,8 +127,8 @@ def main(): # pragma: no cover
118127 fig = plt .figure ()
119128 fig .canvas .mpl_connect ("button_press_event" , click )
120129 # for stopping simulation with the esc key.
121- fig .canvas .mpl_connect ('key_release_event' ,
122- lambda event : [ exit (0 ) if event .key == 'escape' else None ])
130+ fig .canvas .mpl_connect ('key_release_event' , lambda event : [
131+ exit (0 ) if event .key == 'escape' else None ])
123132 two_joint_arm ()
124133
125134
0 commit comments