test_hand_and_arm_sim.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Copyright 2020 Shadow Robot Company Ltd.
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation version 2 of the License.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 # more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 from __future__ import absolute_import
18 import rospy
19 import rostest
20 from moveit_msgs.msg import PlanningScene
21 from sr_robot_commander.sr_arm_commander import SrArmCommander
22 from sr_robot_commander.sr_hand_commander import SrHandCommander
23 from sr_robot_commander.sr_robot_commander import SrRobotCommander
24 from geometry_msgs.msg import PoseStamped, Pose
25 from rospy import get_rostime
26 from actionlib_msgs.msg import GoalStatusArray
27 from unittest import TestCase
28 
29 
30 class TestHandAndArmSim(TestCase):
31  """
32  Tests the Hand and Arm in Sim
33  """
34 
35  @classmethod
36  def setUpClass(cls):
37  rospy.wait_for_message('/move_group/status', GoalStatusArray)
38  cls.hand_type = rospy.get_param('~test_hand_and_arm_sim/hand_type')
39  cls.scene = rospy.get_param('~test_hand_and_arm_sim/scene')
40 
41  # ur-specific launch files do not accept 'side' param as it is already set
42  # for phantom hands use hand finder
43  try:
44  cls.side = rospy.get_param('~test_hand_and_arm_sim/side')
45  if cls.side == 'right':
46  cls.hand_id = 'rh'
47  elif cls.side == 'left':
48  cls.hand_id = 'lh'
49  except rospy.ROSException:
50  rospy.loginfo("No side param for this test type")
51  cls.hand_id = rospy.get_param('/hand/mapping/1082')
52 
53  if cls.hand_id == 'rh':
54  cls.arm_id = 'ra'
55  cls.robot_commander = SrRobotCommander(name="right_arm_and_hand")
56  cls.hand_commander = SrHandCommander(name='right_hand')
57  cls.arm_commander = SrArmCommander(name='right_arm', set_ground=False)
58  elif cls.hand_id == 'lh':
59  cls.arm_id = 'la'
60  cls.robot_commander = SrRobotCommander(name="left_arm_and_hand")
61  cls.hand_commander = SrHandCommander(name='left_hand')
62  cls.arm_commander = SrArmCommander(name='left_arm', set_ground=False)
63 
64  rospy.Subscriber('/move_group/monitored_planning_scene', PlanningScene, cls.scene_data_cb)
65 
66  rospy.sleep(10)
67 
68  @classmethod
69  def tearDownClass(cls):
70  pass
71 
72  @classmethod
73  def scene_data_cb(cls, result):
74  scene_data = ()
75  cls.scene_data = result.world.collision_objects
76 
77  def joints_error_check(self, expected_joint_values, recieved_joint_values):
78  expected_and_final_joint_value_diff = 0
79  for expected_value, recieved_value in zip(sorted(expected_joint_values), sorted(recieved_joint_values)):
80  expected_and_final_joint_value_diff += abs(expected_joint_values[expected_value] -
81  recieved_joint_values[recieved_value])
82  return expected_and_final_joint_value_diff
83 
84  def wait_for_topic_with_scene(self, timeout=50):
85  counter = 0
86  while counter < timeout:
87  current_value = self.scene_data
88  if len(current_value) != 0:
89  return current_value
90  rospy.sleep(0.1)
91  counter += 1
92 
94  start_arm_angles = self.arm_commander.get_current_state()
95 
96  if self.arm_id == 'ra':
97  self.expected_home_angles = {'shoulder_pan_joint': 0.00, 'elbow_joint': 2.0,
98  'shoulder_lift_joint': -1.25, 'wrist_1_joint': -1,
99  'wrist_2_joint': 1.5708, 'wrist_3_joint': -2}
100 
101  elif self.arm_id == 'la':
102  self.expected_home_angles = {'shoulder_pan_joint': 0.0, 'elbow_joint': -2.0,
103  'shoulder_lift_joint': -1.89, 'wrist_1_joint': -2.1,
104  'wrist_2_joint': -1.5708, 'wrist_3_joint': 2}
105 
106  home_angles_no_id = self.expected_home_angles
107  expected_start_angles = {}
108  for key, value in home_angles_no_id.items():
109  expected_start_angles[self.arm_id + '_' + key] = value
110 
111  expected_and_actual_home_angles = self.joints_error_check(expected_start_angles, start_arm_angles)
112  self.assertAlmostEqual(expected_and_actual_home_angles, 0, delta=0.2)
113 
114  def test_2_scene(self):
115  scene = ()
116  self.scene = rospy.get_param('~test_hand_and_arm_sim/scene')
118  if self.scene is True:
119  self.assertNotEqual(len(self.scene_value), 0)
120  elif self.scene is False:
121  self.assertTrue(self.scene_value is None)
122 
123  def test_4_hand(self):
124  hand_joints_target = {
125  'hand_e': {'THJ1': 0.52, 'THJ2': 0.61, 'THJ3': 0.0, 'THJ4': 1.20, 'THJ5': 0.17,
126  'FFJ1': 1.5707, 'FFJ2': 1.5707, 'FFJ3': 1.5707, 'FFJ4': 0.0,
127  'MFJ1': 1.5707, 'MFJ2': 1.5707, 'MFJ3': 1.5707, 'MFJ4': 0.0,
128  'RFJ1': 1.5707, 'RFJ2': 1.5707, 'RFJ3': 1.5707, 'RFJ4': 0.0,
129  'LFJ1': 0.0, 'LFJ2': 0.0, 'LFJ3': 0.0, 'LFJ4': 0.0,
130  'LFJ5': 0.0, 'WRJ1': 0.0, 'WRJ2': 0.0},
131  'hand_lite': {'THJ1': 0.52, 'THJ2': 0.61, 'THJ4': 1.20, 'THJ5': 0.17,
132  'FFJ1': 1.5707, 'FFJ2': 1.5707, 'FFJ3': 1.5707, 'FFJ4': 0.0,
133  'MFJ1': 1.5707, 'MFJ2': 1.5707, 'MFJ3': 1.5707, 'MFJ4': 0.0,
134  'RFJ1': 0.0, 'RFJ2': 0.0, 'RFJ3': 0.0, 'RFJ4': 0.0},
135  'hand_extra_lite': {'THJ1': 0.52, 'THJ2': 0.61, 'THJ4': 1.20, 'THJ5': 0.17,
136  'FFJ1': 1.5707, 'FFJ2': 1.5707, 'FFJ3': 1.5707, 'FFJ4': 0.0,
137  'RFJ1': 0.0, 'RFJ2': 0.0, 'RFJ3': 0.0, 'RFJ4': 0.0}
138  }
139 
140  hand_joints_target_no_id = hand_joints_target[self.hand_type]
141  hand_joints_target = {}
142  for key, value in hand_joints_target_no_id.items():
143  hand_joints_target[self.hand_id + '_' + key] = value
144 
145  self.hand_commander.move_to_joint_value_target(hand_joints_target, wait=True)
146  rospy.sleep(15)
147  final_hand_joint_values = self.hand_commander.get_current_state()
148 
149  expected_and_final_joint_value_diff_hand = self.joints_error_check(hand_joints_target, final_hand_joint_values)
150 
151  self.assertAlmostEqual(expected_and_final_joint_value_diff_hand, 0, delta=0.4)
152 
153  def test_3_arm(self):
154 
155  if self.arm_id == 'la':
156  arm_joints_target = {'shoulder_pan_joint': 0.00, 'elbow_joint': -1.43,
157  'shoulder_lift_joint': -1.82, 'wrist_1_joint': 3.24,
158  'wrist_2_joint': -1.57, 'wrist_3_joint': 3.13}
159  elif self.arm_id == 'ra':
160  arm_joints_target = {'shoulder_pan_joint': 0.00, 'elbow_joint': 1.43,
161  'shoulder_lift_joint': -1.27, 'wrist_1_joint': -0.1,
162  'wrist_2_joint': 1.57, 'wrist_3_joint': 3.13}
163 
164  arm_joints_target_no_id = arm_joints_target
165  arm_joints_target = {}
166  for key, value in arm_joints_target_no_id.items():
167  arm_joints_target[self.arm_id + '_' + key] = value
168 
169  self.arm_commander.move_to_joint_value_target(arm_joints_target, wait=True)
170  rospy.sleep(5)
171  final_arm_joint_values = self.arm_commander.get_current_state()
172 
173  expected_and_final_joint_value_diff_arm = self.joints_error_check(arm_joints_target, final_arm_joint_values)
174 
175  self.assertAlmostEqual(expected_and_final_joint_value_diff_arm, 0, delta=0.2)
176 
178  hand_joints_target = {
179  'hand_e': {'THJ1': 0.52, 'THJ2': 0.61, 'THJ3': 0.0, 'THJ4': 1.20, 'THJ5': 0.17,
180  'FFJ1': 1.5707, 'FFJ2': 1.5707, 'FFJ3': 1.5707, 'FFJ4': 0.0,
181  'MFJ1': 1.5707, 'MFJ2': 1.5707, 'MFJ3': 1.5707, 'MFJ4': 0.0,
182  'RFJ1': 0.0, 'RFJ2': 0.0, 'RFJ3': 0.0, 'RFJ4': 0.0,
183  'LFJ1': 0.0, 'LFJ2': 0.0, 'LFJ3': 0.0, 'LFJ4': 0.0,
184  'LFJ5': 0.0, 'WRJ1': 0.0, 'WRJ2': 0.0},
185  'hand_lite': {'THJ1': 0.52, 'THJ2': 0.61, 'THJ4': 1.20, 'THJ5': 0.17,
186  'FFJ1': 1.5707, 'FFJ2': 1.5707, 'FFJ3': 0.0, 'FFJ4': 0.0,
187  'MFJ1': 1.5707, 'MFJ2': 1.5707, 'MFJ3': 1.5707, 'MFJ4': 0.0,
188  'RFJ1': 0.0, 'RFJ2': 0.0, 'RFJ3': 0.0, 'RFJ4': 0.0},
189  'hand_extra_lite': {'THJ1': 0.52, 'THJ2': 0.61, 'THJ4': 1.20, 'THJ5': 0.17,
190  'FFJ1': 1.5707, 'FFJ2': 1.5707, 'FFJ3': 1.5707, 'FFJ4': 0.0,
191  'RFJ1': 0.0, 'RFJ2': 0.0, 'RFJ3': 0.0, 'RFJ4': 0.0}
192  }
193 
194  hand_joints_target_no_id = hand_joints_target[self.hand_type]
195  hand_joints_target = {}
196  for key, value in hand_joints_target_no_id.items():
197  hand_joints_target[self.hand_id + '_' + key] = value
198 
199  if self.arm_id == 'ra':
200  arm_joints_target = {'shoulder_pan_joint': 0.00, 'elbow_joint': 2.0,
201  'shoulder_lift_joint': -1.25, 'wrist_1_joint': -0.733,
202  'wrist_2_joint': 1.578, 'wrist_3_joint': -3.1416}
203  elif self.arm_id == 'la':
204  arm_joints_target = {'shoulder_pan_joint': 0.0, 'elbow_joint': -2.0,
205  'shoulder_lift_joint': -1.89, 'wrist_1_joint': 3.8,
206  'wrist_2_joint': -1.5708, 'wrist_3_joint': 3.1416}
207 
208  arm_joints_target_no_id = arm_joints_target
209  arm_joints_target = {}
210  for key, value in arm_joints_target_no_id.items():
211  arm_joints_target[self.arm_id + '_' + key] = value
212 
213  hand_and_arm_joints_target = {**hand_joints_target.items, **arm_joints_target}
214  self.robot_commander.move_to_joint_value_target_unsafe(hand_and_arm_joints_target, 10.0, True)
215 
216  rospy.sleep(5)
217 
218  final_hand_and_arm_joint_values = self.robot_commander.get_current_state()
219 
220  joint_value_diff_arm_and_hand = self.joints_error_check(hand_and_arm_joints_target,
221  final_hand_and_arm_joint_values)
222 
223  self.assertAlmostEqual(joint_value_diff_arm_and_hand, 0, delta=0.4)
224 
225 
226 if __name__ == "__main__":
227  PKGNAME = 'sr_robot_launch'
228  NODENAME = 'test_hand_and_arm_sim'
229 
230  rospy.init_node(NODENAME, anonymous=True)
231  rostest.rosrun(PKGNAME, NODENAME, TestHandAndArmSim)
def joints_error_check(self, expected_joint_values, recieved_joint_values)


sr_robot_launch
Author(s): Kirsty Ellis
autogenerated on Mon Feb 28 2022 23:52:27