planning_pipeline_tutorial.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2012, Willow Garage, Inc.
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of Willow Garage, Inc. nor the names of its
00019 *     contributors may be used to endorse or promote products derived
00020 *     from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Sachin Chitta
00036 *********************************************************************/
00037 
00038 #include <pluginlib/class_loader.h>
00039 #include <ros/ros.h>
00040 
00041 // MoveIt!
00042 #include <moveit/robot_model_loader/robot_model_loader.h>
00043 #include <moveit/planning_pipeline/planning_pipeline.h>
00044 #include <moveit/planning_interface/planning_interface.h>
00045 #include <moveit/kinematic_constraints/utils.h>
00046 #include <moveit_msgs/DisplayTrajectory.h>
00047 #include <moveit_msgs/PlanningScene.h>
00048 
00049 int main(int argc, char **argv)
00050 {
00051   ros::init (argc, argv, "move_group_tutorial");
00052   ros::AsyncSpinner spinner(1);
00053   spinner.start();
00054   ros::NodeHandle node_handle("~");
00055 
00056   /* SETUP A PLANNING SCENE*/
00057   /* Load the robot model */
00058   robot_model_loader::RobotModelLoader robot_model_loader("robot_description");
00059 
00060   /* Get a shared pointer to the model */
00061   robot_model::RobotModelPtr robot_model = robot_model_loader.getModel();
00062 
00063   /* Construct a planning scene - NOTE: this is for illustration purposes only.
00064      The recommended way to construct a planning scene is to use the planning_scene_monitor
00065      to construct it for you.*/
00066   planning_scene::PlanningScenePtr planning_scene(new planning_scene::PlanningScene(robot_model));
00067 
00068   /* SETUP THE PLANNING PIPELINE*/
00069   planning_pipeline::PlanningPipelinePtr planning_pipeline(new planning_pipeline::PlanningPipeline(robot_model, node_handle, "planning_plugin", "request_adapters"));
00070 
00071   /* Sleep a little to allow time to startup rviz, etc. */
00072   ros::WallDuration sleep_time(8.0);
00073   sleep_time.sleep();
00074 
00075   /* CREATE A MOTION PLAN REQUEST FOR THE RIGHT ARM OF THE PR2 */
00076   /* We will ask the end-effector of the PR2 to go to a desired location */
00077   planning_interface::MotionPlanRequest req;
00078   planning_interface::MotionPlanResponse res;
00079 
00080   /* A desired pose */
00081   geometry_msgs::PoseStamped pose;
00082   pose.header.frame_id = "torso_lift_link";
00083   pose.pose.position.x = 0.75;
00084   pose.pose.position.y = 0.0;
00085   pose.pose.position.z = 0.0;
00086   pose.pose.orientation.w = 1.0;
00087 
00088   /* A desired tolerance */
00089   std::vector<double> tolerance_pose(3, 0.01);
00090   std::vector<double> tolerance_angle(3, 0.01);
00091 
00092   /*Create the request */
00093   req.group_name = "right_arm";
00094   moveit_msgs::Constraints pose_goal = kinematic_constraints::constructGoalConstraints("r_wrist_roll_link", pose, tolerance_pose, tolerance_angle);
00095   req.goal_constraints.push_back(pose_goal);
00096 
00097   /* Call the pipeline */
00098   //  planning_pipeline->generatePlan((planning_scene::PlanningSceneConstPtr) planning_scene, req, res);
00099   planning_pipeline->generatePlan(planning_scene, req, res);
00100 
00101   /* Check that the planning was successful */
00102   if(res.error_code_.val != res.error_code_.SUCCESS)
00103   {
00104     ROS_ERROR("Could not compute plan successfully");
00105     return 0;
00106   }
00107 
00108   /* Visualize the generated plan */
00109   /* Publisher for display */
00110   ros::Publisher display_publisher = node_handle.advertise<moveit_msgs::DisplayTrajectory>("/move_group/display_planned_path", 1, true);
00111   moveit_msgs::DisplayTrajectory display_trajectory;
00112 
00113   /* Visualize the trajectory */
00114   ROS_INFO("Visualizing the trajectory");
00115   moveit_msgs::MotionPlanResponse response;
00116   res.getMessage(response);
00117 
00118   display_trajectory.trajectory_start = response.trajectory_start;
00119   display_trajectory.trajectory.push_back(response.trajectory);
00120   display_publisher.publish(display_trajectory);
00121 
00122   sleep_time.sleep();
00123 
00124   /* NOW TRY A JOINT SPACE GOAL */
00125   /* First, set the state in the planning scene to the final state of the last plan */
00126   robot_state::RobotState& robot_state = planning_scene->getCurrentStateNonConst();
00127   planning_scene->setCurrentState(response.trajectory_start);
00128   robot_state::JointStateGroup* joint_state_group = robot_state.getJointStateGroup("right_arm");
00129   joint_state_group->setVariableValues(response.trajectory.joint_trajectory.points.back().positions);
00130 
00131   /* Now, setup a joint space goal*/
00132   robot_state::RobotState goal_state(robot_model);
00133   robot_state::JointStateGroup* goal_group = goal_state.getJointStateGroup("right_arm");
00134   std::vector<double> joint_values(7, 0.0);
00135   joint_values[0] = -2.0;
00136   joint_values[3] = -0.2;
00137   joint_values[5] = -0.15;
00138   goal_group->setVariableValues(joint_values);
00139   moveit_msgs::Constraints joint_goal = kinematic_constraints::constructGoalConstraints(goal_group);
00140 
00141   req.goal_constraints.clear();
00142   req.goal_constraints.push_back(joint_goal);
00143 
00144   /* Call the pipeline */
00145   planning_pipeline->generatePlan(planning_scene, req, res);
00146 
00147   /* Check that the planning was successful */
00148   if(res.error_code_.val != res.error_code_.SUCCESS)
00149   {
00150     ROS_ERROR("Could not compute plan successfully");
00151     return 0;
00152   }
00153 
00154   /* Visualize the trajectory */
00155   ROS_INFO("Visualizing the trajectory");
00156   res.getMessage(response);
00157 
00158   display_trajectory.trajectory_start = response.trajectory_start;
00159   display_trajectory.trajectory.push_back(response.trajectory);
00160   //Now you should see two planned trajectories in series
00161   display_publisher.publish(display_trajectory);
00162 
00163   sleep_time.sleep();
00164 
00165 
00166   /* PRE-PROCESSING THE MOTION PLAN REQUEST */
00167   /* Let's purposefully set the initial state to be outside the joint limits and let the
00168      planning request adapter deal with it */
00169   /* First, set the state in the planning scene to the final state of the last plan */
00170   robot_state = planning_scene->getCurrentStateNonConst();
00171   planning_scene->setCurrentState(response.trajectory_start);
00172   joint_state_group = robot_state.getJointStateGroup("right_arm");
00173   joint_state_group->setVariableValues(response.trajectory.joint_trajectory.points.back().positions);
00174 
00175   //Now, set one of the joints slightly outside its upper limit
00176   robot_state::JointState* joint_state = joint_state_group->getJointState("r_shoulder_pan_joint");
00177   const std::vector<std::pair<double, double> >& joint_bounds = joint_state->getVariableBounds();
00178   std::vector<double> tmp_values(1, 0.0);
00179   tmp_values[0] = joint_bounds[0].first - 0.01;
00180   joint_state->setVariableValues(tmp_values);
00181 
00182   req.goal_constraints.clear();
00183   req.goal_constraints.push_back(pose_goal);
00184   planning_pipeline->generatePlan(planning_scene, req, res);
00185   if(res.error_code_.val != res.error_code_.SUCCESS)
00186   {
00187     ROS_ERROR("Could not compute plan successfully");
00188     return 0;
00189   }
00190   /* Visualize the trajectory */
00191   ROS_INFO("Visualizing the trajectory");
00192   res.getMessage(response);
00193   display_trajectory.trajectory_start = response.trajectory_start;
00194   display_trajectory.trajectory.push_back(response.trajectory);
00195   //Now you should see three planned trajectories in series
00196   display_publisher.publish(display_trajectory);
00197 
00198   sleep_time.sleep();
00199   ROS_INFO("Done");
00200   return 0;
00201 }


pr2_moveit_tutorials
Author(s): Sachin Chitta
autogenerated on Mon Oct 6 2014 11:15:31