sr_right_hand_partial_traj_grasp.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright 2019 Shadow Robot Company Ltd.
3 #
4 # This program is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the Free
6 # Software Foundation version 2 of the License.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 # more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program. If not, see <http://www.gnu.org/licenses/>.
15 
16 # This example demonstrates how you can send a partial trajectory to a joint or set of joints.
17 # The partial trajectory can then be run during an existing motion and define a new goal for
18 # the joints specified in this sub list
19 
20 import rospy
21 from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint
22 
23 from sr_robot_commander.sr_hand_commander import SrHandCommander
24 from sr_utilities.hand_finder import HandFinder
25 
26 rospy.init_node("partial_traj_example", anonymous=True)
27 rospy.sleep(1) # Do not start at time zero
28 
29 
30 def construct_trajectory_point(posture, duration):
31  trajectory_point = JointTrajectoryPoint()
32  trajectory_point.time_from_start = rospy.Duration.from_sec(float(duration))
33  for key in joint_trajectory.joint_names:
34  trajectory_point.positions.append(posture[key])
35  return trajectory_point
36 
37 hand_finder = HandFinder()
38 
39 hand_parameters = hand_finder.get_hand_parameters()
40 hand_serial = hand_parameters.mapping.keys()[0]
41 
42 hand_commander = SrHandCommander(hand_parameters=hand_parameters,
43  hand_serial=hand_serial)
44 
45 hand_mapping = hand_parameters.mapping[hand_serial]
46 
47 # Hand joints are detected
48 joints = hand_finder.get_hand_joints()[hand_mapping]
49 
50 open_hand = {'rh_FFJ1': 0.0, 'rh_FFJ2': 0.0, 'rh_FFJ3': 0.0, 'rh_FFJ4': 0.0,
51  'rh_MFJ1': 0.0, 'rh_MFJ2': 0.0, 'rh_MFJ3': 0.0, 'rh_MFJ4': 0.0,
52  'rh_RFJ1': 0.0, 'rh_RFJ2': 0.0, 'rh_RFJ3': 0.0, 'rh_RFJ4': 0.0,
53  'rh_LFJ1': 0.0, 'rh_LFJ2': 0.0, 'rh_LFJ3': 0.0, 'rh_LFJ4': 0.0,
54  'rh_LFJ5': 0.0, 'rh_THJ1': 0.0, 'rh_THJ2': 0.0, 'rh_THJ3': 0.0,
55  'rh_THJ4': 0.0, 'rh_THJ5': 0.0}
56 
57 keys = ['rh_FFJ1', 'rh_FFJ2', 'rh_FFJ3', 'rh_FFJ4', 'rh_LFJ1', 'rh_LFJ2',
58  'rh_LFJ3', 'rh_LFJ4', 'rh_LFJ5', 'rh_MFJ1', 'rh_MFJ2', 'rh_MFJ3',
59  'rh_MFJ4', 'rh_RFJ1', 'rh_RFJ2', 'rh_RFJ3', 'rh_RFJ4', 'rh_THJ1',
60  'rh_THJ2', 'rh_THJ3', 'rh_THJ4', 'rh_THJ5']
61 
62 position = [1.07, 0.26, 0.88, -0.34, 0.85, 0.60,
63  0.21, -0.23, 0.15, 1.06, 0.16, 1.04,
64  0.05, 1.04, 0.34, 0.68, -0.24, 0.35,
65  0.69, 0.18, 1.20, -0.11]
66 
67 grasp_pose = dict(zip(keys, position))
68 
69 # Adjust poses according to the hand loaded
70 open_hand_current = dict([(i, open_hand[i]) for i in joints if i in open_hand])
71 grasp_pose_current = dict([(i, grasp_pose[i]) for i in joints if i in grasp_pose])
72 
73 # Partial list of goals
74 grasp_partial_1 = {'rh_FFJ3': 0.50}
75 
76 start_time = rospy.Time.now()
77 
78 # Move hand using move_to_joint_value_target_unsafe to 1st position
79 hand_commander.move_to_joint_value_target_unsafe(open_hand_current, 1.0, True)
80 
81 rospy.sleep(2)
82 
83 # Move hand using run_joint_trajectory_unsafe to joint angles specified in 'position' list
84 hand_commander.move_to_joint_value_target_unsafe(grasp_pose_current, 1.0, False)
85 
86 trajectory_start_time = 2.0
87 joint_trajectory = JointTrajectory()
88 
89 # Construct and send partial trajectory for joint listed in grasp_partial_1
90 joint_trajectory.header.stamp = start_time + rospy.Duration.from_sec(float(trajectory_start_time))
91 joint_trajectory.joint_names = list(grasp_partial_1.keys())
92 joint_trajectory.points = []
93 trajectory_point = construct_trajectory_point(grasp_partial_1, 1.0)
94 joint_trajectory.points.append(trajectory_point)
95 
96 hand_commander.run_joint_trajectory_unsafe(joint_trajectory, True)
97 rospy.sleep(2)


sr_example
Author(s): Ugo Cupcic
autogenerated on Wed Oct 14 2020 04:05:12