test_planners.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright 2019 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 # A script to test planners, to be run on the command line with group name as the argument (left_arm or right_arm)
18 
19 import rospy
20 import sys
21 import numpy
22 from moveit_commander import RobotCommander, PlanningSceneInterface, MoveGroupCommander
23 from moveit_msgs.msg import RobotState
24 import geometry_msgs.msg
25 import copy
26 import time
27 import argparse
28 import rosgraph
29 
30 
31 class TestPlanners(object):
32  def __init__(self, group_id, planner):
33 
34  rospy.init_node('moveit_test_planners', anonymous=True)
35  self.pass_list = []
36  self.fail_list = []
37  self.planner = planner
38  self.scene = PlanningSceneInterface()
39  self.robot = RobotCommander()
40  self.group = MoveGroupCommander(group_id)
41  rospy.sleep(1)
42 
43  self.group.set_planner_id(self.planner)
44 
46  self.test_waypoints()
48 
49  for pass_test in self.pass_list:
50  print(pass_test)
51 
52  for fail_test in self.fail_list:
53  print(fail_test)
54 
56  # publish a scene
57  p = geometry_msgs.msg.PoseStamped()
58  p.header.frame_id = self.robot.get_planning_frame()
59 
60  p.pose.position.x = 0
61  p.pose.position.y = 0
62  # offset such that the box is below ground (to prevent collision with
63  # the robot itself)
64  p.pose.position.z = -0.11
65  p.pose.orientation.x = 0
66  p.pose.orientation.y = 0
67  p.pose.orientation.z = 0
68  p.pose.orientation.w = 1
69  self.scene.add_box("ground", p, (3, 3, 0.1))
70 
71  p.pose.position.x = 0.4
72  p.pose.position.y = 0.85
73  p.pose.position.z = 0.4
74  p.pose.orientation.x = 0.5
75  p.pose.orientation.y = -0.5
76  p.pose.orientation.z = 0.5
77  p.pose.orientation.w = 0.5
78  self.scene.add_box("wall_front", p, (0.8, 2, 0.01))
79 
80  p.pose.position.x = 1.33
81  p.pose.position.y = 0.4
82  p.pose.position.z = 0.4
83  p.pose.orientation.x = 0.0
84  p.pose.orientation.y = -0.707388
85  p.pose.orientation.z = 0.0
86  p.pose.orientation.w = 0.706825
87  self.scene.add_box("wall_right", p, (0.8, 2, 0.01))
88 
89  p.pose.position.x = -0.5
90  p.pose.position.y = 0.4
91  p.pose.position.z = 0.4
92  p.pose.orientation.x = 0.0
93  p.pose.orientation.y = -0.707107
94  p.pose.orientation.z = 0.0
95  p.pose.orientation.w = 0.707107
96  self.scene.add_box("wall_left", p, (0.8, 2, 0.01))
97 
98  # rospy.sleep(1)
99 
100  def _check_plan(self, plan):
101  if len(plan.joint_trajectory.points) > 0:
102  return True
103  else:
104  return False
105 
106  def _plan_joints(self, joints):
107  self.group.clear_pose_targets()
108  group_variable_values = self.group.get_current_joint_values()
109  group_variable_values[0:6] = joints[0:6]
110  self.group.set_joint_value_target(group_variable_values)
111 
112  plan = self.group.plan()
113  return self._check_plan(plan)
114 
116  # test_joint_values = [numpy.pi/2.0, numpy.pi-0.33, -numpy.pi/2]
117  test_joint_values = [numpy.pi/2.0]
118  joints = [0.0, 0.0, 0.0, -numpy.pi / 2.0, 0.0, 0.0]
119  # Joint 4th is colliding with the hand
120  # for joint in range(6):
121  for joint in [0, 1, 2, 3, 5]:
122  for value in test_joint_values:
123  joints[joint] = value
124  if not self._plan_joints(joints):
125  self.fail_list.append("Failed: test_trajectories_rotating_each_joint, " + self.planner +
126  ", joints:" + str(joints))
127  else:
128  self.pass_list.append("Passed: test_trajectories_rotating_each_joint, " + self.planner +
129  ", joints:" + str(joints))
130 
132  # Up - Does not work with sbpl but it does with ompl
133  joints = [
134  0.0, -1.99, 2.19, 0.58, 0.0, -0.79, 0.0, 0.0]
135  if not self._plan_joints(joints):
136  self.fail_list.append("Failed: test_trajectories_empty_environment, " + self.planner +
137  ", joints:" + str(joints))
138  else:
139  self.pass_list.append("Passed: test_trajectories_empty_environment, " + self.planner +
140  ", joints:" + str(joints))
141 
142  # All joints up
143  joints = [-1.67232, -2.39104, 0.264862,
144  0.43346, 2.44148, 2.48026, 0.0, 0.0]
145  if not self._plan_joints(joints):
146  self.fail_list.append("Failed: test_trajectories_empty_environment, " + self.planner +
147  ", joints:" + str(joints))
148  else:
149  self.pass_list.append("Passed: test_trajectories_empty_environment, " + self.planner +
150  ", joints:" + str(joints))
151 
152  # Down
153  joints = [
154  -0.000348431194526, 0.397651011661, 0.0766181197394, -
155  0.600353691727, -0.000441966540076,
156  0.12612019707, 0.0, 0.0]
157  if not self._plan_joints(joints):
158  self.fail_list.append("Failed: test_trajectories_empty_environment, " + self.planner +
159  ", joints:" + str(joints))
160  else:
161  self.pass_list.append("Passed: test_trajectories_empty_environment, " + self.planner +
162  ", joints:" + str(joints))
163 
164  # left
165  joints = [
166  0.146182953165, -2.6791929848, -
167  0.602721109682, -3.00575848765, 0.146075718452,
168  0.00420656698366, 0.0, 0.0]
169  if not self._plan_joints(joints):
170  self.fail_list.append("Failed: test_trajectories_empty_environment, " + self.planner +
171  ", joints:" + str(joints))
172  else:
173  self.pass_list.append("Passed: test_trajectories_empty_environment, " + self.planner +
174  ", joints:" + str(joints))
175 
176  # Front
177  joints = [
178  1.425279839, -0.110370375874, -
179  1.52548746261, -1.50659865247, -1.42700242769,
180  3.1415450794, 0.0, 0.0]
181  if not self._plan_joints(joints):
182  self.fail_list.append("Failed: test_trajectories_empty_environment, " + self.planner +
183  ", joints:" + str(joints))
184  else:
185  self.pass_list.append("Passed: test_trajectories_empty_environment, " + self.planner +
186  ", joints:" + str(joints))
187 
188  # Behind
189  joints = [
190  1.57542451065, 3.01734161219, 2.01043257686, -
191  1.14647092839, 0.694689321451,
192  -0.390769365032, 0.0, 0.0]
193  if not self._plan_joints(joints):
194  self.fail_list.append("Failed: test_trajectories_empty_environment, " + self.planner +
195  ", joints:" + str(joints))
196  else:
197  self.pass_list.append("Passed: test_trajectories_empty_environment, " + self.planner +
198  ", joints:" + str(joints))
199 
200  # Should fail because it is in self-collision
201  joints = [
202  -0.289797803762, 2.37263860495, 2.69118483159, 1.65486712181, 1.04235601797,
203  -1.69730925867, 0.0, 0.0]
204  if not self._plan_joints(joints):
205  self.fail_list.append("Failed: test_trajectories_empty_environment, " + self.planner +
206  ", joints:" + str(joints))
207  else:
208  self.pass_list.append("Passed: test_trajectories_empty_environment, " + self.planner +
209  ", joints:" + str(joints))
210 
211  def test_waypoints(self):
212  # Start planning in a given joint position
213  joints = [
214  -0.324590029242, -1.42602359749, -
215  1.02523472017, -0.754761892979, 0.344227622185,
216  -3.03250264451, 0.0, 0.0]
217  current = RobotState()
218  current.joint_state.name = self.robot.get_current_state(
219  ).joint_state.name
220  current_joints = list(
221  self.robot.get_current_state().joint_state.position)
222  current_joints[0:8] = joints
223  current.joint_state.position = current_joints
224 
225  self.group.set_start_state(current)
226 
227  waypoints = []
228 
229  initial_pose = self.group.get_current_pose().pose
230 
231  initial_pose.position.x = -0.301185959729
232  initial_pose.position.y = 0.517069787724
233  initial_pose.position.z = 1.20681710541
234  initial_pose.orientation.x = 0.0207499700474
235  initial_pose.orientation.y = -0.723943002716
236  initial_pose.orientation.z = -0.689528413407
237  initial_pose.orientation.w = 0.00515118111221
238 
239  # start with a specific position
240  waypoints.append(initial_pose)
241 
242  # first move it down
243  wpose = geometry_msgs.msg.Pose()
244  wpose.orientation = waypoints[0].orientation
245  wpose.position.x = waypoints[0].position.x
246  wpose.position.y = waypoints[0].position.y
247  wpose.position.z = waypoints[0].position.z - 0.20
248  waypoints.append(copy.deepcopy(wpose))
249 
250  # second front
251  wpose.position.y += 0.20
252  waypoints.append(copy.deepcopy(wpose))
253 
254  # third side
255  wpose.position.x -= 0.20
256  waypoints.append(copy.deepcopy(wpose))
257 
258  # fourth return to initial pose
259  wpose = waypoints[0]
260  waypoints.append(copy.deepcopy(wpose))
261 
262  (plan3, fraction) = self.group.compute_cartesian_path(
263  waypoints, 0.01, 0.0)
264  if not self._check_plan(plan3) and fraction > 0.8:
265  self.fail_list.append("Failed: test_waypoints, " + self.planner)
266  else:
267  self.pass_list.append("Passed: test_waypoints, " + self.planner)
268 
270  self._add_walls_and_ground()
271 
272  # Should fail to plan: Goal is in collision with the wall_front
273  joints = [
274  0.302173213174, 0.192487443763, -
275  1.94298265002, 1.74920382275, 0.302143499777, 0.00130280337897,
276  0.0, 0.0]
277  if not self._plan_joints(joints):
278  self.fail_list.append("Failed: test_trajectories_with_walls_and_ground, " + self.planner +
279  ", joints:" + str(joints))
280  else:
281  self.pass_list.append("Passed: test_trajectories_with_walls_and_ground, " + self.planner +
282  ", joints:" + str(joints))
283 
284  # Should fail to plan: Goal is in collision with the ground
285  joints = [
286  3.84825722288e-05, 0.643694953509, -
287  1.14391175311, 1.09463824437, 0.000133883149666, -
288  0.594498939239,
289  0.0, 0.0]
290  if not self._plan_joints(joints):
291  self.fail_list.append("Failed: test_trajectories_with_walls_and_ground, " + self.planner +
292  ", joints:" + str(joints))
293  else:
294  self.pass_list.append("Passed: test_trajectories_with_walls_and_ground, " + self.planner +
295  ", joints:" + str(joints))
296 
297  # Goal close to right corner
298  joints = [
299  0.354696232081, -0.982224980654, 0.908055961723, -
300  1.92328051116, -1.3516255551, 2.8225061435,
301  0.0, 0.0]
302  if not self._plan_joints(joints):
303  self.fail_list.append("Failed: test_trajectories_with_walls_and_ground, " + self.planner +
304  ", joints:" + str(joints))
305  else:
306  self.pass_list.append("Passed, test_trajectories_with_walls_and_ground, " + self.planner +
307  ", joints:" + str(joints))
308 
309  self.scene.remove_world_object("ground")
310  self.scene.remove_world_object("wall_left")
311  self.scene.remove_world_object("wall_right")
312  self.scene.remove_world_object("wall_front")
313 
314 
315 def main():
316  parser = argparse.ArgumentParser(description='A script to test moveit planners. '
317  'Should be run after launching the UR10 arm and shadow hand: \n '
318  'roslaunch sr_robot_launch sr_right_ur10arm_hand.launch OR \n '
319  'roslaunch sr_robot_launch sr_left_ur10arm_hand.launch',
320  add_help=True, usage='%(prog)s [group_id]',
321  formatter_class=argparse.RawTextHelpFormatter)
322 
323  parser.add_argument(dest='group_id', help="Should be either right_arm or left_arm")
324  parser.parse_args()
325 
326  group_id = str(sys.argv[1])
327 
328  try:
329  rospy.get_master().getPid()
330  except:
331  print("Please launch robot.")
332  sys.exit()
333 
334  # sleep to wait for robot description to be loaded
335  while not rospy.search_param('robot_description_semantic') and not rospy.is_shutdown():
336  print("Waiting for robot description, has robot been launched?")
337  time.sleep(0.5)
338 
339  planner_list = ["BKPIECEkConfigDefault", "ESTkConfigDefault", "KPIECEkConfigDefault", "LBKPIECEkConfigDefault",
340  "PRMkConfigDefault", "RRTkConfigDefault", "SBLkConfigDefault", "PRMstarkConfigDefault",
341  "RRTstarkConfigDefault"]
342  for planner in planner_list:
343  TestPlanners(group_id, planner)
344 
345 if __name__ == "__main__":
346  main()
def __init__(self, group_id, planner)


sr_multi_moveit_test
Author(s): Beatriz Leon
autogenerated on Wed Oct 14 2020 04:05:26