test_cancel_before_plan_execution.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import sys
4 import rospy
5 import unittest
6 from actionlib import SimpleActionClient
7 import moveit_commander
8 from moveit_msgs.msg import MoveItErrorCodes, MoveGroupGoal, Constraints, JointConstraint, MoveGroupAction
9 
10 
11 class TestMoveActionCancelDrop(unittest.TestCase):
12 
13  def setUp(self):
14  # create a action client of move group
15  self._move_client = SimpleActionClient('move_group', MoveGroupAction)
16  self._move_client.wait_for_server()
17 
18  moveit_commander.roscpp_initialize(sys.argv)
19  group_name = moveit_commander.RobotCommander().get_group_names()[0]
20  group = moveit_commander.MoveGroupCommander(group_name)
21 
22  # prepare a joint goal
23  self._goal = MoveGroupGoal()
24  self._goal.request.group_name = group_name
25  self._goal.request.max_velocity_scaling_factor = 0.1
26  self._goal.request.max_acceleration_scaling_factor = 0.1
27  self._goal.request.start_state.is_diff = True
28 
29  goal_constraint = Constraints()
30  joint_values = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
31  joint_names = group.get_active_joints()
32  for i in range(len(joint_names)):
33  joint_constraint = JointConstraint()
34  joint_constraint.joint_name = joint_names[i]
35  joint_constraint.position = joint_values[i]
36  joint_constraint.weight = 1.0
37  goal_constraint.joint_constraints.append(joint_constraint)
38 
39  self._goal.request.goal_constraints.append(goal_constraint)
40  self._goal.planning_options.planning_scene_diff.robot_state.is_diff = True
41 
43  # send the goal
44  self._move_client.send_goal(self._goal)
45 
46  # cancel the goal
47  self._move_client.cancel_goal()
48 
49  # wait for result
50  self._move_client.wait_for_result()
51 
52  # polling the result, since result can come after the state is Done
53  result = None
54  while result is None:
55  result = self._move_client.get_result()
56  rospy.sleep(0.1)
57 
58  # check the error code in result
59  # error code is 0 if the server ends with RECALLED status
60  self.assertTrue(result.error_code.val == MoveItErrorCodes.PREEMPTED or result.error_code.val == 0)
61 
63  # set the plan only flag
64  self._goal.planning_options.plan_only = True
65 
66  # send the goal
67  self._move_client.send_goal(self._goal)
68 
69  # cancel the goal
70  self._move_client.cancel_goal()
71 
72  # wait for result
73  self._move_client.wait_for_result()
74 
75  # polling the result, since result can come after the state is Done
76  result = None
77  while result is None:
78  result = self._move_client.get_result()
79  rospy.sleep(0.1)
80 
81  # check the error code in result
82  # error code is 0 if the server ends with RECALLED status
83  self.assertTrue(result.error_code.val == MoveItErrorCodes.PREEMPTED or result.error_code.val == 0)
84 
85  def test_cancel_resend(self):
86  # send the goal
87  self._move_client.send_goal(self._goal)
88 
89  # cancel the goal
90  self._move_client.cancel_goal()
91 
92  # send the goal again
93  self._move_client.send_goal(self._goal)
94 
95  # wait for result
96  self._move_client.wait_for_result()
97 
98  # polling the result, since result can come after the state is Done
99  result = None
100  while result is None:
101  result = self._move_client.get_result()
102  rospy.sleep(0.1)
103 
104  # check the error code in result
105  self.assertEqual(result.error_code.val, MoveItErrorCodes.SUCCESS)
106 
107 
108 if __name__ == '__main__':
109  import rostest
110  rospy.init_node('cancel_before_plan_execution')
111  rostest.rosrun('moveit_ros_move_group', 'test_cancel_before_plan_execution', TestMoveActionCancelDrop)


move_group
Author(s): Ioan Sucan , Sachin Chitta
autogenerated on Wed Jul 10 2019 04:03:52