move_group_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 <ros/ros.h>
00039 #include <geometry_msgs/Pose.h>
00040 
00041 // MoveIt!
00042 #include <moveit_msgs/PlanningScene.h>
00043 #include <moveit_msgs/AttachedCollisionObject.h>
00044 #include <moveit_msgs/GetStateValidity.h>
00045 #include <moveit_msgs/DisplayRobotState.h>
00046 
00047 #include <moveit/robot_model_loader/robot_model_loader.h>
00048 #include <moveit/robot_state/robot_state.h>
00049 #include <moveit/robot_state/conversions.h>
00050 
00051 int main(int argc, char **argv)
00052 {
00053   ros::init (argc, argv, "move_group_tutorial");
00054   ros::AsyncSpinner spinner(1);
00055   spinner.start();
00056   ros::NodeHandle node_handle;
00057 
00058   /* First put an object into the scene*/
00059   /* Advertise the collision object message publisher*/
00060   ros::Publisher collision_object_publisher = node_handle.advertise<moveit_msgs::CollisionObject>("collision_object", 1);
00061   while(collision_object_publisher.getNumSubscribers() < 1)
00062   {
00063     ros::WallDuration sleep_t(0.5);
00064     sleep_t.sleep();
00065   }
00066   /* Define the object message */
00067   moveit_msgs::CollisionObject object;
00068   /* The header must contain a valid TF frame */
00069   object.header.frame_id = "r_wrist_roll_link";
00070   /* The id of the object */
00071   object.id = "box";
00072 
00073   /* A default pose */
00074   geometry_msgs::Pose pose;
00075   pose.orientation.w = 1.0;
00076 
00077   /* Define a box to be attached */
00078   shape_msgs::SolidPrimitive primitive;
00079   primitive.type = primitive.BOX;
00080   primitive.dimensions.resize(3);
00081   primitive.dimensions[0] = 0.1;
00082   primitive.dimensions[1] = 0.1;
00083   primitive.dimensions[2] = 0.1;
00084 
00085   object.primitives.push_back(primitive);
00086   object.primitive_poses.push_back(pose);
00087 
00088   /* An attach operation requires an ADD */
00089   object.operation = attached_object.object.ADD;
00090 
00091   /* Publish and sleep (to view the visualized results) */
00092   collision_object_publisher.publish(object);
00093   ros::WallDuration sleep_time(1.0);
00094   sleep_time.sleep();
00095 
00096   /* CHECK IF A STATE IS VALID */
00097   /* PUT THE OBJECT IN THE ENVIRONMENT */
00098   ROS_INFO("Putting the object back into the environment");
00099   planning_scene.robot_state.attached_collision_objects.clear();
00100   planning_scene.world.collision_objects.clear();
00101   planning_scene.world.collision_objects.push_back(object);
00102   planning_scene.is_diff = true;
00103   planning_scene_diff_publisher.publish(planning_scene);
00104   sleep_time.sleep();
00105 
00106   /* Load the robot model */
00107   robot_model_loader::RDFLoader robot_model_loader("robot_description");
00108   /* Get a shared pointer to the model and construct a state */
00109   robot_model::RobotModelPtr kinematic_model = robot_model_loader.getModel();
00110   robot_state::RobotState current_state(kinematic_model);
00111   current_state.getJointStateGroup("right_arm")->setToRandomValues();
00112 
00113   /* Construct a robot state message */
00114   moveit_msgs::RobotState robot_state;
00115   robot_state::robotStateToRobotStateMsg(current_state, robot_state);
00116 
00117   /* Construct the service request */
00118   moveit_msgs::GetStateValidity::Request get_state_validity_request;
00119   moveit_msgs::GetStateValidity::Response get_state_validity_response;
00120   get_state_validity_request.robot_state = robot_state;
00121   get_state_validity_request.group_name = "right_arm";
00122 
00123   /* Service client for checking state validity */
00124   ros::ServiceClient service_client =  node_handle.serviceClient<moveit_msgs::GetStateValidity>("/check_state_validity");
00125 
00126   /* Publisher for display */
00127   ros::Publisher display_publisher = node_handle.advertise<moveit_msgs::DisplayRobotState>("/display_robot_state", 1);
00128   moveit_msgs::DisplayRobotState display_state;
00129 
00130   for(std::size_t i=0; i < 20; ++i)
00131   {
00132     /* Make the service call */
00133     service_client.call(get_state_validity_request, get_state_validity_response);
00134     if(get_state_validity_response.valid)
00135       ROS_INFO("State %d was valid", (int) i);
00136     else
00137       ROS_ERROR("State %d was invalid", (int) i);
00138 
00139     /* Visualize the state */
00140     display_state.state = robot_state;
00141     display_publisher.publish(display_state);
00142 
00143     /* Generate a new state and put it into the request */
00144     current_state.getJointStateGroup("right_arm")->setToRandomValues();
00145     robot_state::robotStateToRobotStateMsg(current_state, robot_state);
00146     get_state_validity_request.robot_state = robot_state;
00147     sleep_time.sleep();
00148   }
00149 }


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