Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
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
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
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
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
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;
00128 color.g = 1.0;
00129 color.b = 1.0;
00130 color.a = 1;
00131
00132 ROS_DEBUG("Creating interactive marker for link %s (%s)", link_name.c_str(), mesh_filename.c_str());
00133
00134
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
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
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
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
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