generate_robot_model.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include "pr2_marker_control/generate_robot_model.h"
00031 
00032 #include <ros/ros.h>
00033 #include <std_msgs/ColorRGBA.h>
00034 #include <geometry_msgs/PoseStamped.h>
00035 #include <interactive_markers/interactive_marker_server.h>
00036 
00037 using namespace visualization_msgs;
00038 
00039 urdf::Model getRobotURDFModel()
00040 {
00041   std::string urdf_content;
00042   urdf::Model model;
00043 
00044   std::string robot_parameter = "robot_description";
00045 
00046   ros::NodeHandle nh;
00047 
00048   // obtain robot description from parameter server
00049   std::string loc;
00050   if (nh.searchParam(robot_parameter, loc))
00051   {
00052     nh.getParam(loc, urdf_content);
00053   }
00054   else
00055   {
00056     ROS_ERROR( "Parameter [ %s ] does not exist, and was not found by searchParam()", robot_parameter.c_str());
00057   }
00058 
00059   if (urdf_content.empty())
00060   {
00061     ROS_ERROR( "URDF is empty");
00062   }
00063 
00064   // parse xml-based robot description
00065   TiXmlDocument doc;
00066   doc.Parse(urdf_content.c_str());
00067   if (!doc.RootElement())
00068   {
00069     ROS_ERROR( "URDF failed XML parse");
00070   }
00071 
00072   // create robot model
00073   if (!model.initXml(doc.RootElement()))
00074   {
00075     ROS_ERROR( "URDF failed Model parse");
00076   }
00077 
00078   ROS_DEBUG( "URDF parsed OK");
00079 
00080   return model;
00081 }
00082 
00083 void addMeshMarkersFromRobotModel(boost::shared_ptr<const urdf::Link> link, std::vector<InteractiveMarker>& int_markers)
00084 {
00085   // All interactive markers will be set to time(0), so they update automatically in rviz.
00086   const std::string& link_name = link->name;
00087 
00088   geometry_msgs::PoseStamped ps;
00089   ps.header.stamp = ros::Time(0);
00090   ps.header.frame_id = "/" + link_name;
00091 
00092   if (link && link->visual && link->visual->geometry)
00093   {
00094     const urdf::Geometry &geom = *link->visual->geometry;
00095 
00096     switch (geom.type)
00097     {
00098  /*     case urdf::Geometry::SPHERE:
00099       {
00100         const urdf::Sphere& sphere = static_cast<const urdf::Sphere&>(geom);
00101 
00102         break;
00103       }
00104       case urdf::Geometry::BOX:
00105       {
00106         const urdf::Box& box = static_cast<const urdf::Box&>(geom);
00107 
00108         break;
00109       }
00110       case urdf::Geometry::CYLINDER:
00111       {
00112         const urdf::Cylinder& cylinder = static_cast<const urdf::Cylinder&>(geom);
00113 
00114         break;
00115       }*/
00116       case urdf::Geometry::MESH:
00117       {
00118         const urdf::Mesh& mesh = static_cast<const urdf::Mesh&>(geom);
00119 
00120         if (mesh.filename.empty())
00121           return;
00122 
00123         const std::string& mesh_filename = mesh.filename;
00124 
00125         std_msgs::ColorRGBA color;
00126 
00127         color.r = 1.0; //link->visual->material->color.r;
00128         color.g = 1.0; //link->visual->material->color.g;
00129         color.b = 1.0; //link->visual->material->color.b;
00130         color.a = 1; //link->visual->material->color.a;
00131 
00132         ROS_DEBUG("Creating interactive marker for link %s (%s)", link_name.c_str(), mesh_filename.c_str());
00133 
00134         // create mesh marker
00135         visualization_msgs::Marker mesh_marker;
00136 
00137         mesh_marker.pose.position.x = link->visual->origin.position.x;
00138         mesh_marker.pose.position.y = link->visual->origin.position.y;
00139         mesh_marker.pose.position.z = link->visual->origin.position.z;
00140 
00141         mesh_marker.pose.orientation.x = link->visual->origin.rotation.x;
00142         mesh_marker.pose.orientation.y = link->visual->origin.rotation.y;
00143         mesh_marker.pose.orientation.z = link->visual->origin.rotation.z;
00144         mesh_marker.pose.orientation.w = link->visual->origin.rotation.w;
00145 
00146         mesh_marker.color = color;
00147 
00148         mesh_marker.mesh_resource = mesh_filename;
00149         mesh_marker.mesh_use_embedded_materials = true;
00150         mesh_marker.type = visualization_msgs::Marker::MESH_RESOURCE;
00151         mesh_marker.scale.x = mesh.scale.x;
00152         mesh_marker.scale.y = mesh.scale.y;
00153         mesh_marker.scale.z = mesh.scale.z;
00154 
00155         // interactive marker control
00156         visualization_msgs::InteractiveMarkerControl control;
00157         control.markers.push_back(mesh_marker);
00158         control.interaction_mode = visualization_msgs::InteractiveMarkerControl::NONE;
00159         control.always_visible = true;
00160 
00161         // create interactive marker
00162         InteractiveMarker int_marker;
00163 
00164         int_marker.header = ps.header;
00165         int_marker.pose.orientation.w = 1;
00166         int_marker.name = link_name;
00167         int_marker.scale = 0.3;
00168 
00169         int_marker.controls.push_back(control);
00170 
00171         int_markers.push_back(int_marker);
00172         break;
00173       }
00174       default:
00175         //ROS_WARN("Unsupported geometry type for element: %d", geom.type);
00176         break;
00177     }
00178   }
00179   else
00180   {
00181     ROS_DEBUG("Robot model: link: %s has a null child!", link->name.c_str());
00182   }
00183 
00184   // recursively traverse the urdf tree
00185   for (std::vector<boost::shared_ptr<urdf::Link> >::const_iterator child = link->child_links.begin();
00186       child != link->child_links.end(); child++)
00187     addMeshMarkersFromRobotModel(*child,int_markers);
00188 
00189 }
00190 
00191 void addMeshMarkersFromRobotModel(std::vector<InteractiveMarker>& int_markers)
00192 {
00193   urdf::Model model = getRobotURDFModel();
00194   addMeshMarkersFromRobotModel(model.getRoot(),int_markers);
00195 }
00196 


pr2_marker_control
Author(s): Adam Leeper
autogenerated on Mon Oct 6 2014 12:48:08