22 from control_msgs.msg
import JointTrajectoryControllerState,\
23 FollowJointTrajectoryActionResult, FollowJointTrajectoryActionGoal
24 import matplotlib.pyplot
as plt
25 from trajectory_msgs.msg
import JointTrajectory, JointTrajectoryPoint
27 from sr_robot_commander.sr_hand_commander
import SrHandCommander
28 from sr_utilities.hand_finder
import HandFinder
55 rospy.Subscriber(
"/rh_trajectory_controller/state",
56 JointTrajectoryControllerState, self.
callback)
58 "/rh_trajectory_controller/follow_joint_trajectory/result",
61 "/rh_trajectory_controller/follow_joint_trajectory/goal",
70 self.joints_time.append(state.header.stamp.to_sec())
71 self.ffj3_actual.append(state.actual.positions[self.
ffj3_index])
72 self.ffj3_desired.append(state.desired.positions[self.
ffj3_index])
73 self.ffj3_error.append(state.error.positions[self.
ffj3_index])
74 self.rfj3_actual.append(state.actual.positions[self.
rfj3_index])
75 self.rfj3_desired.append(state.desired.positions[self.
rfj3_index])
76 self.rfj3_error.append(state.error.positions[self.
rfj3_index])
78 self.ffj3_vel_actual.append(state.actual.velocities[self.
ffj3_index])
79 self.rfj3_vel_actual.append(state.actual.velocities[self.
rfj3_index])
80 self.ffj3_vel_desired.append(state.desired.velocities[self.
ffj3_index])
81 self.rfj3_vel_desired.append(state.desired.velocities[self.
rfj3_index])
82 self.ffj3_vel_error.append(state.error.velocities[self.
ffj3_index])
83 self.rfj3_vel_error.append(state.error.velocities[self.
rfj3_index])
86 print (
"Trajectory Goal: " + result.status.goal_id.id +
87 " finished with status: " + str(result.status.status))
91 self.goal_joint_names.append(goal.goal.trajectory.joint_names)
92 self.start_time_goals.append(goal.header.stamp.to_sec())
93 self.start_time_goals_trajectory.append(goal.goal.trajectory.header.stamp.to_sec())
94 self.trajectories.append(goal.goal.trajectory.points)
98 plt.grid(which=
'both', axis=
'both')
99 plt.setp(ax.get_xticklabels(), fontsize=8)
100 plt.setp(ax.get_yticklabels(), fontsize=8)
101 plt.xlabel(
'Time (s)')
102 ax.xaxis.label.set_size(10)
103 ax.yaxis.label.set_size(10)
121 if ffj3_goal_index > -1:
122 time_ffj3_traj.append(point.time_from_start.to_sec() +
125 angle_ffj3_traj.append(point.positions[ffj3_goal_index])
126 if rfj3_goal_index > -1:
127 time_rfj3_traj.append(point.time_from_start.to_sec() +
130 angle_rfj3_traj.append(point.positions[rfj3_goal_index])
132 if ffj3_goal_index > -1:
134 plt.plot(time_ffj3_traj, angle_ffj3_traj,
'o',
135 label=
"Traj " + str(i + 1))
137 if rfj3_goal_index > -1:
139 plt.plot(time_rfj3_traj, angle_rfj3_traj,
'o',
140 label=
"Traj " + str(i + 1))
149 plt.plot(time, self.
ffj3_actual,
'black', label=
"Actual traj")
150 plt.plot(time, self.
ffj3_desired,
'green', label=
"Desired traj")
151 plt.ylabel(
'FFJ3 Actual position (rad)')
153 plt.ylim(ymax=2.2, ymin=-0.1)
154 plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=5,
155 mode=
"expand", borderaxespad=0., prop={
'size': 8})
158 plt.plot(time, self.
rfj3_actual,
'black', label=
"Actual traj")
159 plt.plot(time, self.
rfj3_desired,
'green', label=
"Desired traj")
160 plt.ylabel(
'RFJ3 Actual position (rad)')
162 plt.ylim(ymax=2.2, ymin=-0.1)
163 plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=4,
164 mode=
"expand", borderaxespad=0., prop={
'size': 8})
169 plt.ylabel(
'FFJ3 Actual velocity')
170 plt.xlim(xmax=16, xmin=0)
172 plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=4,
173 mode=
"expand", borderaxespad=0., prop={
'size': 8})
178 plt.ylabel(
'RFJ3 Actual velocity')
179 plt.xlim(xmax=16, xmin=0)
181 plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=4,
182 mode=
"expand", borderaxespad=0., prop={
'size': 8})
186 plt.ylabel(
'FFJ3 Velocity Error')
187 plt.xlim(xmax=16, xmin=0)
192 plt.ylabel(
'RFJ3 Velocity Error')
193 plt.xlim(xmax=16, xmin=0)
196 plt.subplots_adjust(left=0.07, right=0.96, bottom=0.083, top=0.90)
201 trajectory_point = JointTrajectoryPoint()
202 trajectory_point.time_from_start = rospy.Duration.from_sec(float(duration))
203 for key
in joint_trajectory.joint_names:
204 trajectory_point.positions.append(posture[key])
205 return trajectory_point
208 open_hand = {
'rh_FFJ1': 0.0,
'rh_FFJ2': 0.0,
'rh_FFJ3': 0.0,
'rh_FFJ4': 0.0,
209 'rh_MFJ1': 0.0,
'rh_MFJ2': 0.0,
'rh_MFJ3': 0.0,
'rh_MFJ4': 0.0,
210 'rh_RFJ1': 0.0,
'rh_RFJ2': 0.0,
'rh_RFJ3': 0.0,
'rh_RFJ4': 0.0,
211 'rh_LFJ1': 0.0,
'rh_LFJ2': 0.0,
'rh_LFJ3': 0.0,
'rh_LFJ4': 0.0,
212 'rh_LFJ5': 0.0,
'rh_THJ1': 0.0,
'rh_THJ2': 0.0,
'rh_THJ3': 0.0,
213 'rh_THJ4': 0.0,
'rh_THJ5': 0.0}
215 grasp1 = {
'rh_FFJ1': 0.0,
'rh_FFJ2': 0.0,
'rh_FFJ3': 0.5235,
'rh_FFJ4': 0.0,
216 'rh_MFJ1': 0.0,
'rh_MFJ2': 0.0,
'rh_MFJ3': 0.0,
'rh_MFJ4': 0.0,
217 'rh_RFJ1': 0.0,
'rh_RFJ2': 0.0,
'rh_RFJ3': 0.5235,
'rh_RFJ4': 0.0,
218 'rh_LFJ1': 0.0,
'rh_LFJ2': 0.0,
'rh_LFJ3': 0.0,
'rh_LFJ4': 0.0,
219 'rh_LFJ5': 0.0,
'rh_THJ1': 0.0,
'rh_THJ2': 0.0,
'rh_THJ3': 0.0,
220 'rh_THJ4': 0.0,
'rh_THJ5': 0.0}
222 grasp2 = {
'rh_FFJ1': 0.0,
'rh_FFJ2': 0.0,
'rh_FFJ3': 1.0472,
'rh_FFJ4': 0.0,
223 'rh_MFJ1': 0.0,
'rh_MFJ2': 0.0,
'rh_MFJ3': 0.0,
'rh_M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': 1.5707,
'rh_FFJ4': 0.0,
244 'rh_MFJ1': 0.0,
'rh_MFJ2': 0.0,
'rh_MFJ3': 0.0,
'rh_MFJ4': 0.0,
245 'rh_RFJ1': 0.0,
'rh_RFJ2': 0.0,
'rh_RFJ3': 1.5707,
'rh_RFJ4': 0.0,
246 'rh_LFJ1': 0.0,
'rh_LFJ2': 0.0,
'rh_LFJ3': 0.0,
'rh_LFJ4': 0.0,
247 'rh_LFJ5': 0.0,
'rh_THJ1': 0.0,
'rh_THJ2': 0.0,
'rh_THJ3': 0.0,
248 'rh_THJ4': 0.0,
'rh_THJ5': 0.0}
251 grasp_partial_1 = {
'rh_FFJ3': 1.06}
252 grasp_partial_2 = {
'rh_FFJ3': 1.2}
254 if __name__ ==
'__main__':
255 rospy.init_node(
"partial_traj_example_advanced", anonymous=
True)
259 hand_finder = HandFinder()
261 hand_parameters = hand_finder.get_hand_parameters()
262 hand_serial = hand_parameters.mapping.keys()[0]
264 hand_commander = SrHandCommander(hand_parameters=hand_parameters,
265 hand_serial=hand_serial)
267 hand_mapping = hand_parameters.mapping[hand_serial]
270 joints = hand_finder.get_hand_joints()[hand_mapping]
273 open_hand_current = dict([(i, open_hand[i])
for i
in joints
if i
in open_hand])
274 grasp1_current = dict([(i, grasp1[i])
for i
in joints
if i
in grasp1])
275 grasp2_current = dict([(i, grasp2[i])
for i
in joints
if i
in grasp2])
276 grasp3_current = dict([(i, grasp3[i])
for i
in joints
if i
in grasp3])
277 grasp4_current = dict([(i, grasp4[i])
for i
in joints
if i
in grasp4])
278 grasp5_current = dict([(i, grasp5[i])
for i
in joints
if i
in grasp5])
280 start_time = rospy.Time.now()
283 rospy.loginfo(
"Moving hand to open position")
284 trajectory_start_time = 1.0
285 joint_trajectory = JointTrajectory()
286 joint_trajectory.header.stamp = start_time + rospy.Duration.from_sec(float(trajectory_start_time))
287 joint_trajectory.joint_names = list(open_hand_current.keys())
288 joint_trajectory.points = []
290 joint_trajectory.points.append(trajectory_point)
291 hand_commander.run_joint_trajectory_unsafe(joint_trajectory,
True)
294 rospy.loginfo(
"Closing index and ring fingers")
295 trajectory_start_time = 4.0
296 joint_trajectory = JointTrajectory()
297 joint_trajectory.header.stamp = start_time + rospy.Duration.from_sec(float(trajectory_start_time))
298 joint_trajectory.joint_names = list(grasp1_current.keys())
299 joint_trajectory.points = []
301 joint_trajectory.points.append(trajectory_point)
303 joint_trajectory.points.append(trajectory_point)
305 joint_trajectory.points.append(trajectory_point)
307 joint_trajectory.points.append(trajectory_point)
309 joint_trajectory.points.append(trajectory_point)
310 hand_commander.run_joint_trajectory_unsafe(joint_trajectory,
False)
313 rospy.loginfo(
"Moving index finger to partial trajectory goals")
315 trajectory_start_time = 8.0
316 joint_trajectory = JointTrajectory()
317 joint_trajectory.header.stamp = start_time + rospy.Duration.from_sec(float(trajectory_start_time))
318 joint_trajectory.joint_names = list(grasp_partial_1.keys())
319 joint_trajectory.points = []
321 joint_trajectory.points.append(trajectory_point)
323 joint_trajectory.points.append(trajectory_point)
324 hand_commander.run_joint_trajectory_unsafe(joint_trajectory,
False)
326 graphs_finished =
False 328 rate = rospy.Rate(10)
331 while not rospy.is_shutdown():
332 if len(listener.joints_time) > 5
and\
333 (listener.joints_time[-1] - listener.joints_time[0]) > 15
and\
335 listener.finish_goals =
True 337 graphs_finished =
True start_time_goals_trajectory
def callback_result(self, result)
def callback_goal(self, goal)
def construct_trajectory_point(posture, duration)
def plot_settings(self, plt)
def callback(self, state)