test_moveit.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 # Software License Agreement (BSD License)
5 #
6 # Copyright (c) 2015, TORK (Tokyo Opensource Robotics Kyokai Association)
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #
13 # * Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # * Redistributions in binary form must reproduce the above
16 # copyright notice, this list of conditions and the following
17 # disclaimer in the documentation and/or other materials provided
18 # with the distribution.
19 # * Neither the name of Tokyo Opensource Robotics Kyokai Association
20 # nor the names of its contributors may be used to endorse or promote
21 # products derived from this software without specific prior written
22 # permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 # POSSIBILITY OF SUCH DAMAGE.
36 
37 # Author: Isaac I.Y. Saito
38 
39 import unittest
40 
41 from geometry_msgs.msg import Pose
42 from moveit_commander import MoveGroupCommander, MoveItCommanderException, RobotCommander
43 from moveit_msgs.msg import RobotTrajectory
44 import rospy
45 
46 import math
47 from tf.transformations import quaternion_from_euler
48 
49 class TestDualarmMoveit(unittest.TestCase):
50  _KINEMATICSOLVER_SAFE = 'RRTConnectkConfigDefault'
51 
52  _MOVEGROUP_NAME_TORSO = 'torso'
53  _JOINTNAMES_TORSO = ['CHEST_JOINT0']
54  # Set of MoveGroup name and the list of joints
55  _MOVEGROUP_ATTR_TORSO = [_MOVEGROUP_NAME_TORSO, _JOINTNAMES_TORSO]
56 
57  _MOVEGROUP_NAME_LEFTARM = 'left_arm'
58  _JOINTNAMES_LEFTARM = ['LARM_JOINT0', 'LARM_JOINT1', 'LARM_JOINT2', 'LARM_JOINT3', 'LARM_JOINT4', 'LARM_JOINT5']
59  _MOVEGROUP_ATTR_LEFTARM = [_MOVEGROUP_NAME_LEFTARM, _JOINTNAMES_LEFTARM]
60 
61  _MOVEGROUP_NAME_RIGHTARM = 'right_arm'
62  _JOINTNAMES_RIGHTARM = ['RARM_JOINT0', 'RARM_JOINT1', 'RARM_JOINT2', 'RARM_JOINT3', 'RARM_JOINT4', 'RARM_JOINT5']
63  _MOVEGROUP_ATTR_RIGHTARM = [_MOVEGROUP_NAME_RIGHTARM, _JOINTNAMES_RIGHTARM]
64 
65  _MOVEGROUP_NAME_LEFTARM_TORSO = 'left_arm_torso'
66  _JOINTNAMES_LEFTARM_TORSO = _JOINTNAMES_TORSO +_JOINTNAMES_LEFTARM
67  _MOVEGROUP_ATTR_LEFTARM_TORSO = [_MOVEGROUP_NAME_LEFTARM_TORSO, _JOINTNAMES_LEFTARM_TORSO]
68 
69  _MOVEGROUP_NAME_RIGHTARM_TORSO = 'right_arm_torso'
70  _JOINTNAMES_RIGHTARM_TORSO = _JOINTNAMES_TORSO + _JOINTNAMES_RIGHTARM
71  _MOVEGROUP_ATTR_RIGHTARM_TORSO = [_MOVEGROUP_NAME_RIGHTARM_TORSO, _JOINTNAMES_RIGHTARM_TORSO]
72 
73  _MOVEGROUP_NAME_LEFTHAND = 'left_hand'
74  _JOINTNAMES_LEFTHAND = ['LARM_JOINT5']
75  _MOVEGROUP_ATTR_LEFTHAND = [_MOVEGROUP_NAME_LEFTHAND, _JOINTNAMES_LEFTHAND]
76 
77  _MOVEGROUP_NAME_RIGHTHAND = 'right_hand'
78  _JOINTNAMES_RIGHTHAND = ['RARM_JOINT5']
79  _MOVEGROUP_ATTR_RIGHTHAND = [_MOVEGROUP_NAME_RIGHTHAND, _JOINTNAMES_RIGHTHAND]
80 
81  _MOVEGROUP_NAME_HEAD = 'head'
82  _JOINTNAMES_HEAD = ['HEAD_JOINT0', 'HEAD_JOINT1']
83  _MOVEGROUP_ATTR_HEAD = [_MOVEGROUP_NAME_HEAD, _JOINTNAMES_HEAD]
84 
85  _MOVEGROUP_NAME_BOTHARMS = 'botharms'
86  ## _JOINTNAMES_BOTHARMS = _JOINTNAMES_TORSO +_JOINTNAMES_LEFTARM + _JOINTNAMES_RIGHTARM
87  _JOINTNAMES_BOTHARMS = _JOINTNAMES_LEFTARM + _JOINTNAMES_RIGHTARM
88  _MOVEGROUP_ATTR_BOTHARMS = [_MOVEGROUP_NAME_BOTHARMS, _JOINTNAMES_BOTHARMS]
89 
90  _MOVEGROUP_NAME_UPPERBODY = 'upperbody'
91  _JOINTNAMES_UPPERBODY = _JOINTNAMES_TORSO + _JOINTNAMES_HEAD + _JOINTNAMES_LEFTARM + _JOINTNAMES_RIGHTARM
92  _MOVEGROUP_ATTR_UPPERBODY = [_MOVEGROUP_NAME_UPPERBODY, _JOINTNAMES_UPPERBODY]
93 
94  # List of all MoveGroup set
95  _MOVEGROUP_NAMES = sorted([_MOVEGROUP_NAME_TORSO, _MOVEGROUP_NAME_HEAD,
96  _MOVEGROUP_NAME_LEFTARM, _MOVEGROUP_NAME_RIGHTARM,
97  _MOVEGROUP_NAME_LEFTARM_TORSO, _MOVEGROUP_NAME_RIGHTARM_TORSO,
98  _MOVEGROUP_NAME_LEFTHAND, _MOVEGROUP_NAME_RIGHTHAND,
99  _MOVEGROUP_NAME_BOTHARMS, _MOVEGROUP_NAME_UPPERBODY])
100  _MOVEGROUP_ATTRS = [_MOVEGROUP_ATTR_TORSO, _MOVEGROUP_ATTR_HEAD,
101  _MOVEGROUP_ATTR_LEFTARM, _MOVEGROUP_ATTR_RIGHTARM,
102  _MOVEGROUP_ATTR_LEFTARM_TORSO, _MOVEGROUP_ATTR_RIGHTARM_TORSO,
103  _MOVEGROUP_ATTR_LEFTHAND, _MOVEGROUP_ATTR_RIGHTHAND,
104  _MOVEGROUP_ATTR_BOTHARMS, _MOVEGROUP_ATTR_UPPERBODY]
105 
106  _TARGETPOSE_LEFT = [] # TODO fill this
107 
108 # def __init__(self, mg_attrs_larm, mg_attrs_rarm):
109 # '''
110 # @param mg_attrs_larm: List of MoveGroup attributes.
111 # See the member variable for the semantics of the data type.
112 # @type mg_attrs_larm: [str, [str]]
113 # '''
114 
115  @classmethod
116  def setUpClass(self):
117 
118  rospy.init_node('test_moveit')
119  rospy.sleep(5) # intentinally wait for starting up hrpsys
120 
121  self.robot = RobotCommander()
122 
123  # TODO: Read groups from SRDF file ideally.
124 
125  for mg_attr in self._MOVEGROUP_ATTRS:
126  mg = MoveGroupCommander(mg_attr[0])
127  # Temporary workaround of planner's issue similar to https://github.com/tork-a/rtmros_nextage/issues/170
128  mg.set_planner_id(self._KINEMATICSOLVER_SAFE)
129  # Allow replanning to increase the odds of a solution
130  mg.allow_replanning(True)
131  # increase planning time
132  mg.set_planning_time(30.0)
133  # Append MoveGroup instance to the MoveGroup attribute list.
134  mg_attr.append(mg)
135 
136  @classmethod
137  def tearDownClass(self):
138  True # TODO impl something meaningful
139 
140  def _set_sample_pose(self):
141  '''
142  @return: Pose() with some values populated in.
143  '''
144  pose_target = Pose()
145  pose_target.orientation.x = 0.00
146  pose_target.orientation.y = 0.00
147  pose_target.orientation.z = -0.20
148  pose_target.orientation.w = 0.98
149  pose_target.position.x = 0.18
150  pose_target.position.y = -0.00
151  pose_target.position.z = 0.94
152  return pose_target
153 
154  def _plan(self):
155  '''
156  Run `clear_pose_targets` at the beginning.
157  @rtype: RobotTrajectory http://docs.ros.org/api/moveit_msgs/html/msg/RobotTrajectory.html
158  '''
159  self._mvgroup.clear_pose_targets()
160 
161  pose_target = self._set_sample_pose()
162  self._mvgroup.set_pose_target(pose_target)
163  plan = self._mvgroup.plan()
164  rospy.loginfo(' plan: {}'.format(plan))
165  return plan
166 
168  '''Check if the defined move groups are loaded.'''
169  self.assertEqual(self._MOVEGROUP_NAMES, sorted(self.robot.get_group_names()))
170 
172  '''Check if the defined joints in a move group are loaded.'''
173  for mg_attr in self._MOVEGROUP_ATTRS:
174  self.assertEqual(mg_attr[1], # joint groups for a Move Group.
175  sorted(mg_attr[2].get_active_joints()))
176 
177  def _plan_leftarm(self, movegroup):
178  '''
179  @type movegroup: MoveGroupCommander
180  @rtype: RobotTrajectory http://docs.ros.org/api/moveit_msgs/html/msg/RobotTrajectory.html
181  '''
182  movegroup.clear_pose_targets()
183 
184  pose_target = Pose()
185  # (@pazeshun) I don't know why, but the following pose causes planning failure on kinetic
186  # pose_target.orientation.x = -0.32136357
187  # pose_target.orientation.y = -0.63049522
188  # pose_target.orientation.z = 0.3206799
189  # pose_target.orientation.w = 0.62957575
190  # pose_target.position.x = 0.32529
191  # pose_target.position.y = 0.29919
192  # pose_target.position.z = 0.24389
193  pose_target.orientation.x = -0.000556712307053
194  pose_target.orientation.y = -0.706576742941
195  pose_target.orientation.z = -0.00102461782513
196  pose_target.orientation.w = 0.707635461636
197  pose_target.position.x = 0.325471850974-0.01
198  pose_target.position.y = 0.182271241593+0.3
199  pose_target.position.z = 0.0676272396419+0.3
200 
201  movegroup.set_pose_target(pose_target)
202  plan = movegroup.plan() # TODO catch exception
203  rospy.loginfo('Plan: {}'.format(plan))
204  return plan
205 
206  def test_plan_success(self):
207  '''Evaluate plan (RobotTrajectory)'''
208  plan = self._plan_leftarm(self._MOVEGROUP_ATTR_LEFTARM[2])
209  # TODO Better way to check the plan is valid.
210  # Currently the following checks if the number of waypoints is not zero,
211  # which (hopefully) indicates that a path is computed.
212  self.assertNotEqual(0, plan.joint_trajectory.points)
213 
215  '''
216  Evaluate Plan and Execute works.
217  Currently no value checking is done (, which is better to be implemented)
218  '''
219  mvgroup = self._MOVEGROUP_ATTR_LEFTARM[2]
220  self._plan_leftarm(mvgroup)
221  # TODO Better way to check the plan is valid.
222  # The following checks if plan execution was successful or not.
223  self.assertTrue(mvgroup.go() or mvgroup.go() or mvgroup.go())
224 
226  '''
227  Check if http://wiki.ros.org/rtmros_nextage/Tutorials/Programming_Hiro_NEXTAGE_OPEN_MOVEIT works
228  '''
229  larm = self._MOVEGROUP_ATTR_LEFTARM[2]
230  rarm = self._MOVEGROUP_ATTR_RIGHTARM[2]
231 
232  #Right Arm Initial Pose
233  rarm_initial_pose = rarm.get_current_pose().pose
234  print "=" * 10, " Printing Right Hand initial pose: "
235  print rarm_initial_pose
236 
237  #Light Arm Initial Pose
238  larm_initial_pose = larm.get_current_pose().pose
239  print "=" * 10, " Printing Left Hand initial pose: "
240  print larm_initial_pose
241 
242  target_pose_r = Pose()
243  target_pose_r.position.x = 0.325471850974-0.01
244  target_pose_r.position.y = -0.182271241593-0.3
245  target_pose_r.position.z = 0.0676272396419+0.3
246  target_pose_r.orientation.x = -0.000556712307053
247  target_pose_r.orientation.y = -0.706576742941
248  target_pose_r.orientation.z = -0.00102461782513
249  target_pose_r.orientation.w = 0.707635461636
250  rarm.set_pose_target(target_pose_r)
251 
252  print "=" * 10," plan1 ..."
253  self.assertTrue(rarm.go() or rarm.go() or rarm.go())
254  rospy.sleep(1)
255 
256  target_pose_l = [
257  target_pose_r.position.x,
258  -target_pose_r.position.y,
259  target_pose_r.position.z,
260  target_pose_r.orientation.x,
261  target_pose_r.orientation.y,
262  target_pose_r.orientation.z,
263  target_pose_r.orientation.w
264  ]
265  larm.set_pose_target(target_pose_l)
266 
267  print "=" * 10," plan2 ..."
268  self.assertTrue(larm.go() or larm.go() or larm.go())
269  rospy.sleep(1)
270 
271  #Clear pose
272  rarm.clear_pose_targets()
273 
274  #Right Hand
275  target_pose_r.position.x = 0.221486843301
276  target_pose_r.position.y = -0.0746407547512
277  target_pose_r.position.z = 0.642545484602
278  target_pose_r.orientation.x = 0.0669013615474
279  target_pose_r.orientation.y = -0.993519060661
280  target_pose_r.orientation.z = 0.00834224628291
281  target_pose_r.orientation.w = 0.0915122442864
282  rarm.set_pose_target(target_pose_r)
283 
284  print "=" * 10, " plan3..."
285  self.assertTrue(rarm.go() or rarm.go() or rarm.go())
286  rospy.sleep(1)
287 
288  print "=" * 10,"Initial pose ..."
289  rarm.set_pose_target(rarm_initial_pose)
290  larm.set_pose_target(larm_initial_pose)
291  self.assertTrue(rarm.go() or rarm.go() or rarm.go())
292  self.assertTrue(larm.go() or larm.go() or larm.go())
293 
295  botharms = self._MOVEGROUP_ATTR_BOTHARMS[2]
296 
297  target_pose_r = Pose()
298  target_pose_l = Pose()
299  q = quaternion_from_euler(0, -math.pi/2,0)
300  target_pose_r.position.x = 0.3
301  target_pose_r.position.y = 0.1
302  target_pose_r.position.z = 0.0
303  target_pose_r.orientation.x = q[0]
304  target_pose_r.orientation.y = q[1]
305  target_pose_r.orientation.z = q[2]
306  target_pose_r.orientation.w = q[3]
307  target_pose_l.position.x = 0.3
308  target_pose_l.position.y =-0.1
309  target_pose_l.position.z = 0.3
310  target_pose_l.orientation.x = q[0]
311  target_pose_l.orientation.y = q[1]
312  target_pose_l.orientation.z = q[2]
313  target_pose_l.orientation.w = q[3]
314  botharms.set_pose_target(target_pose_r, 'RARM_JOINT5_Link')
315  botharms.set_pose_target(target_pose_l, 'LARM_JOINT5_Link')
316  self.assertTrue(botharms.go() or botharms.go() or botharms.go())
317  rospy.sleep(1)
318 
319  target_pose_r.position.x = 0.3
320  target_pose_r.position.y =-0.2
321  target_pose_r.position.z = 0.0
322  target_pose_l.position.x = 0.3
323  target_pose_l.position.y = 0.2
324  target_pose_l.position.z = 0.0
325  botharms.set_pose_target(target_pose_r, 'RARM_JOINT5_Link')
326  botharms.set_pose_target(target_pose_l, 'LARM_JOINT5_Link')
327  self.assertTrue(botharms.go() or botharms.go() or botharms.go())
328 
329 if __name__ == '__main__':
330  import rostest
331  rostest.rosrun('nextage_ros_bridge', 'test_moveit', TestDualarmMoveit)
def _plan_leftarm(self, movegroup)
Definition: test_moveit.py:177


nextage_moveit_config
Author(s): Kei Okada, Isaac Isao Saito
autogenerated on Wed Jun 17 2020 04:15:01