moveit_tutorial.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Software License Agreement (BSD License)
4 #
5 # Copyright (c) 2013, SRI International
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
18 # * Neither the name of SRI International nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34 #
35 # Author: Acorn Pooley
36 # Additions by Max Svetlik
37 
38 ## BEGIN_SUB_TUTORIAL imports
39 
42 
43 """
44  This is a basic MoveIt tutorial with minor modifications for displaying multiple poses,
45  target poses in space and approximate IK solutions using the MoveIt API.
46 
47  The original tutorial can be found here: http://docs.ros.org/indigo/api/moveit_tutorials/html/doc/pr2_tutorials/planning/scripts/doc/move_group_python_interface_tutorial.html
48 
49  Instructions for use:
50 
51  roslaunch svenzva_moveit demo.launch simulation:=true
52 
53  in the RViz window that appears, you can add a pose plugin and fill in its topic parameter to /target_pose
54  to visualize the poses published on /target_pose. Then, run this tutorial file:
55 
56  rosrun svenzva_demo moveit_tutorial.py
57 
58 
59 """
60 
61 import sys
62 import copy
63 import rospy
64 import moveit_commander
65 import moveit_msgs.msg
66 import geometry_msgs.msg
67 import random
68 ## END_SUB_TUTORIAL
69 
70 from std_msgs.msg import String
71 
73  ## BEGIN_TUTORIAL
74 
80  print "============ Starting tutorial setup"
81  moveit_commander.roscpp_initialize(sys.argv)
82  rospy.init_node('move_group_python_interface_tutorial',
83  anonymous=True)
84 
85  ## Instantiate a RobotCommander object. This object is an interface to
86  ## the robot as a whole.
87  robot = moveit_commander.RobotCommander()
88 
89  ## Instantiate a PlanningSceneInterface object. This object is an interface
90  ## to the world surrounding the robot.
91  scene = moveit_commander.PlanningSceneInterface()
92 
93  ## Instantiate a MoveGroupCommander object. This object is an interface
94  ## to one group of joints. In this case the group is the joints in the left
95  ## arm. This interface can be used to plan and execute motions on the left
96  ## arm.
97  group = moveit_commander.MoveGroupCommander("svenzva_arm")
98 
99 
100  ## We create this DisplayTrajectory publisher which is used below to publish
101  ## trajectories for RVIZ to visualize.
102  display_trajectory_publisher = rospy.Publisher(
103  '/move_group/display_planned_path',
104  moveit_msgs.msg.DisplayTrajectory)
105 
106  target_pose_publisher = rospy.Publisher("/target_pose", geometry_msgs.msg.PoseStamped, queue_size=2)
107 
108  ## Wait for RVIZ to initialize. This sleep is ONLY to allow Rviz to come up.
109  print "============ Waiting for RVIZ..."
110  rospy.sleep(1)
111  print "============ Starting tutorial "
112 
113  ## Getting Basic Information
114  ## ^^^^^^^^^^^^^^^^^^^^^^^^^
115 
117  print "============ Reference frame: %s" % group.get_planning_frame()
118 
119  ## We can also print the name of the end-effector link for this group
120  print "============ Reference frame: %s" % group.get_end_effector_link()
121 
122  ## We can get a list of all the groups in the robot
123  print "============ Robot Groups:"
124  print robot.get_group_names()
125 
126  ## Sometimes for debugging it is useful to print the entire state of the
127  ## robot.
128  print "============ Printing robot state"
129  print robot.get_current_state()
130  print "============"
131 
132 
133  ## Planning to a Pose goal
134  ## ^^^^^^^^^^^^^^^^^^^^^^^
135  ## We can plan a motion for this group to a desired pose for the
136  ## end-effector
137  number_of_samples = 5
138  for i in range(0, number_of_samples):
139 
140  print "============ Generating plan " + str(i)
141  print "pose is chosen randomly, pose may be inherently unreachable."
142  pose_target = geometry_msgs.msg.Pose()
143  pose_target.orientation.w = 1.0
144  pose_target.position.x = random.uniform(-0.5, 0.5)
145  pose_target.position.y = random.uniform(-0.5, 0.5)
146  pose_target.position.z = random.uniform(0.3, 0.6)
147 
148  pub_pose = geometry_msgs.msg.PoseStamped()
149  pub_pose.header.frame_id = "base_link"
150  pub_pose.pose = pose_target
151 
152  #DISPLAY target pose on /target_pose topic
153  target_pose_publisher.publish(pub_pose)
154 
155  """
156  Builds with fewer than 6 DOF will only be able to plan using approximate IK solutions with the standard
157  URDF. Running the MoveIt demo, there is a checkbox for 'allow approximate IK solutions' which permits
158  dragging the interactive marker and planning.
159  If allow_approx_soln is set to True here, we enable approximate IK solutions programmatically.
160  See details on the MoveIt commander API
161  """
162  allow_approx_soln = True
163 
164  if allow_approx_soln:
165  #If your end effector link is different than link_5, change the string parameter appropriately
166  group.set_joint_value_target(pose_target, "link_5", True)
167  else:
168  group.set_pose_target(pose_target)
169 
170  ## Now, we call the planner to compute the plan
171  ## and visualize it if successful
172  ## Note that we are just planning, not asking move_group
173  ## to actually move the robot
174  plan1 = group.plan()
175 
176  print "============ Waiting while RVIZ displays plan1..."
177  rospy.sleep(5)
178 
179 
180  ## You can ask RVIZ to visualize a plan (aka trajectory) for you. But the
181  ## group.plan() method does this automatically so this is not that useful
182  ## here (it just displays the same trajectory again).
183  print "============ Visualizing plan1"
184  display_trajectory = moveit_msgs.msg.DisplayTrajectory()
185 
186  display_trajectory.trajectory_start = robot.get_current_state()
187  display_trajectory.trajectory.append(plan1)
188  display_trajectory_publisher.publish(display_trajectory);
189 
190 
191  print "============ Waiting while plan1 is visualized (again)..."
192  rospy.sleep(5)
193 
194 
195  ## Moving to a pose goal
196  ## ^^^^^^^^^^^^^^^^^^^^^
197 
205 
206  # Uncomment below line when working with a real robot
207  # group.go(wait=True)
208 
209  ## Planning to a joint-space goal
210  ## ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
211 
214 
215  group.clear_pose_targets()
216 
217  ## Then, we will get the current set of joint values for the group
218  group_variable_values = group.get_current_joint_values()
219  print "============ Joint values: ", group_variable_values
220 
221  ## Now, let's modify one of the joints, plan to the new joint
222  ## space goal and visualize the plan
223  group_variable_values[0] = 1.0
224  group.set_joint_value_target(group_variable_values)
225 
226  plan2 = group.plan()
227 
228  print "============ Waiting while RVIZ displays plan2..."
229  rospy.sleep(5)
230 
231 
232  ## Cartesian Paths
233  ## ^^^^^^^^^^^^^^^
234  ## You can plan a cartesian path directly by specifying a list of waypoints
235  ## for the end-effector to go through.
236  waypoints = []
237 
238  # start with the current pose
239  waypoints.append(group.get_current_pose().pose)
240 
241  # first orient gripper and move forward (+x)
242  wpose = geometry_msgs.msg.Pose()
243  wpose.orientation.w = 1.0
244  wpose.position.x = waypoints[0].position.x + 0.1
245  wpose.position.y = waypoints[0].position.y
246  wpose.position.z = waypoints[0].position.z
247  waypoints.append(copy.deepcopy(wpose))
248 
249  # second move down
250  wpose.position.z -= 0.10
251  waypoints.append(copy.deepcopy(wpose))
252 
253  # third move to the side
254  wpose.position.y += 0.05
255  waypoints.append(copy.deepcopy(wpose))
256 
257  ## We want the cartesian path to be interpolated at a resolution of 1 cm
258  ## which is why we will specify 0.01 as the eef_step in cartesian
259  ## translation. We will specify the jump threshold as 0.0, effectively
260  ## disabling it.
261  (plan3, fraction) = group.compute_cartesian_path(
262  waypoints, # waypoints to follow
263  0.01, # eef_step
264  0.0) # jump_threshold
265 
266  print "============ Waiting while RVIZ displays plan3..."
267  rospy.sleep(5)
268 
269 
270  ## Adding/Removing Objects and Attaching/Detaching Objects
271  ## ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
272  ## First, we will define the collision object message
273  collision_object = moveit_msgs.msg.CollisionObject()
274 
275 
276 
277  ## When finished shut down moveit_commander.
278  moveit_commander.roscpp_shutdown()
279 
280  ## END_TUTORIAL
281 
282  print "============ STOPPING"
283 
284 
285 if __name__=='__main__':
286  try:
288  except rospy.ROSInterruptException:
289  pass
290 
def move_group_python_interface_tutorial()
Setup ^^^^^ CALL_SUB_TUTORIAL imports.


svenzva_demo
Author(s): Max Svetlik
autogenerated on Wed Oct 28 2020 03:31:24