Planning a trajectory

First of all, we'll need to launch the MoveIt RViz environment. Type the following command:

$ roslaunch fetch_moveit_config fetch_planning_execution.launch

This command may vary depending on how you've named your MoveIt package and your launch file. In this example command, it is assumed that they're named fetch_moveit_config and fetch_planning_execution.launch, respectively:

#! /usr/bin/env python 
import sys 
import copy 
import rospy 
import moveit_commander 
import moveit_msgs.msg 
import geometry_msgs.msg 
moveit_commander.roscpp_initialize(sys.argv) 
rospy.init_node('move_group_python_interface_tutorial', anonymous=True) 
 
robot = moveit_commander.RobotCommander() 
scene = moveit_commander.PlanningSceneInterface()     
group = moveit_commander.MoveGroupCommander("arm") 
display_trajectory_publisher = rospy.Publisher('/move_group/display_planned_path', moveit_msgs.msg.DisplayTrajectory) 
 
pose_target = geometry_msgs.msg.Pose() 
pose_target.orientation.w = 1.0 
pose_target.position.x = 0.96 
pose_target.position.y = 0 
pose_target.position.z = 1.18 
group.set_pose_target(pose_target) 
plan1 = group.plan() 
rospy.sleep(5) 
moveit_commander.roscpp_shutdown() 
 

After a few seconds, we'll see how the robot is planning the specified motion that was described in the preceding code:

Planning a trajectory

That's great! But, how does this code work? What does each part mean? Let's break it down into smaller pieces:

import sys 
import copy 
import rospy 
import moveit_commander 
import moveit_msgs.msg 
import geometry_msgs.msg 

In this section of the code, we are just importing some modules and messages that we'll need for the program. The most important one here is the moveit_commander module, which will allow us to communicate with the MoveIt RViz interface:

moveit_commander.roscpp_initialize(sys.argv) 

Here, we are just initializing the moveit_commander module:

rospy.init_node('move_group_python_interface_tutorial', anonymous=True) 

Here, we are just initializing a ROS node:

robot = moveit_commander.RobotCommander() 

Here, we are creating a RobotCommander object, which is, basically, an interface to our robot:

scene = moveit_commander.PlanningSceneInterface() 

Here, we are creating a PlanningSceneInterface object, which is, basically, an interface to the world that surrounds the robot:

group = moveit_commander.MoveGroupCommander("arm") 

Here, we create a MoveGroupCommander object, which is an interface to the manipulator group of joints that we defined when we created the MoveIt package, back in the first section of this chapter. This will allow us to interact with this set of joints, which, in this case, is the full arm:

display_trajectory_publisher = rospy.Publisher('/move_group/display_planned_path', moveit_msgs.msg.DisplayTrajectory) 

Here, we are defining a topic Publisher, which will publish to the /move_group/display_planned_path topic. By publishing into this topic, we will be able to visualize the planned motion through the MoveIt RViz interface:

pose_target = geometry_msgs.msg.Pose() 
pose_target.orientation.w = 1.0 
pose_target.position.x = 0.7 
pose_target.position.y = -0.05 
pose_target.position.z = 1.1 

Here, we are creating a pose object, which is the type of message that we will send as a goal. Then, we just give values to the variables that will define the goal pose:

plan1 = group.plan() 

Finally, we are telling the manipulator group that we created previously to calculate the plan. If the plan is successfully computed, it will be displayed through MoveIt RViz:

moveit_commander.roscpp_shutdown() 

Finally, we just shut down the moveit_commander module.

For executing the preceding code, we will create a new package called my_motion_scripts. Inside this package, we will create a new directory called src, with a file named planning_script.py. Finally, copy the code we've just discussed inside this file. Inside the package, we will also create a launch directory that contains a launch file in order to launch the planning_script.py file.

Then, we can modify the values assigned to the pose_target variable. Afterwards, launch our code and check if the new pose was achieved successfully. We can repeat this process and try it again with different poses.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.243.32