Skip to content

Commit 84d72ab

Browse files
Re #65 Prevent JTAS execution when Re-Enabled
This fix ensures that the Trajectory callback of the JTAS exits whenever the robot has been disabled. This results in the robot holding its current pose when it is re-enabled, rather than attempting to return to the originally commanded location.
1 parent 7b4ffb5 commit 84d72ab

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/joint_trajectory_action/joint_trajectory_action.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def __init__(self, limb, reconfig_server, rate=100.0,
7070
auto_start=False)
7171
self._action_name = rospy.get_name()
7272
self._limb = baxter_interface.Limb(limb)
73+
self._enable = baxter_interface.RobotEnable()
7374
self._name = limb
7475
self._cuff = baxter_interface.DigitalIO('%s_lower_cuff' % (limb,))
7576
self._cuff.state_changed.connect(self._cuff_cb)
@@ -120,6 +121,9 @@ def __init__(self, limb, reconfig_server, rate=100.0,
120121
tcp_nodelay=True,
121122
queue_size=1)
122123

124+
def robot_is_enabled(self):
125+
return self._enable.state().enabled
126+
123127
def clean_shutdown(self):
124128
self._alive = False
125129
self._limb.exit_control_mode()
@@ -225,7 +229,8 @@ def _command_stop(self, joint_names, joint_angles, start_time, dimensions_dict):
225229
if self._mode == 'velocity':
226230
velocities = [0.0] * len(joint_names)
227231
cmd = dict(zip(joint_names, velocities))
228-
while (not self._server.is_new_goal_available() and self._alive):
232+
while (not self._server.is_new_goal_available() and self._alive
233+
and self.robot_is_enabled()):
229234
self._limb.set_joint_velocities(cmd)
230235
if self._cuff_state:
231236
self._limb.exit_control_mode()
@@ -240,7 +245,8 @@ def _command_stop(self, joint_names, joint_angles, start_time, dimensions_dict):
240245
pnt.velocities = [0.0] * len(joint_names)
241246
if dimensions_dict['accelerations']:
242247
pnt.accelerations = [0.0] * len(joint_names)
243-
while (not self._server.is_new_goal_available() and self._alive):
248+
while (not self._server.is_new_goal_available() and self._alive
249+
and self.robot_is_enabled()):
244250
self._limb.set_joint_positions(joint_angles, raw=raw_pos_mode)
245251
# zero inverse dynamics feedforward command
246252
if self._mode == 'position_w_id':
@@ -253,16 +259,16 @@ def _command_stop(self, joint_names, joint_angles, start_time, dimensions_dict):
253259
rospy.sleep(1.0 / self._control_rate)
254260

255261
def _command_joints(self, joint_names, point, start_time, dimensions_dict):
256-
if self._server.is_preempt_requested():
262+
if self._server.is_preempt_requested() or not self.robot_is_enabled():
257263
rospy.loginfo("%s: Trajectory Preempted" % (self._action_name,))
258264
self._server.set_preempted()
259265
self._command_stop(joint_names, self._limb.joint_angles(), start_time, dimensions_dict)
260266
return False
261267
velocities = []
262268
deltas = self._get_current_error(joint_names, point.positions)
263269
for delta in deltas:
264-
if (math.fabs(delta[1]) >= self._path_thresh[delta[0]]
265-
and self._path_thresh[delta[0]] >= 0.0):
270+
if ((math.fabs(delta[1]) >= self._path_thresh[delta[0]]
271+
and self._path_thresh[delta[0]] >= 0.0)) or not self.robot_is_enabled():
266272
rospy.logerr("%s: Exceeded Error Threshold on %s: %s" %
267273
(self._action_name, delta[0], str(delta[1]),))
268274
self._result.error_code = self._result.PATH_TOLERANCE_VIOLATED
@@ -406,7 +412,8 @@ def _on_trajectory_action(self, goal):
406412
# Keep track of current indices for spline segment generation
407413
now_from_start = rospy.get_time() - start_time
408414
end_time = trajectory_points[-1].time_from_start.to_sec()
409-
while (now_from_start < end_time and not rospy.is_shutdown()):
415+
while (now_from_start < end_time and not rospy.is_shutdown() and
416+
self.robot_is_enabled()):
410417
#Acquire Mutex
411418
now = rospy.get_time()
412419
now_from_start = now - start_time
@@ -451,7 +458,7 @@ def check_goal_state():
451458
return True
452459

453460
while (now_from_start < (last_time + self._goal_time)
454-
and not rospy.is_shutdown()):
461+
and not rospy.is_shutdown() and self.robot_is_enabled()):
455462
if not self._command_joints(joint_names, last, start_time, dimensions_dict):
456463
return
457464
now_from_start = rospy.get_time() - start_time

0 commit comments

Comments
 (0)