urdf_model_marker.cpp
Go to the documentation of this file.
00001 #include "urdf_parser/urdf_parser.h"
00002 #include <iostream>
00003 #include <interactive_markers/tools.h>
00004 #include <jsk_interactive_marker/urdf_model_marker.h>
00005 #include <jsk_interactive_marker/interactive_marker_utils.h>
00006 #include <jsk_interactive_marker/interactive_marker_helpers.h>
00007 #include <dynamic_tf_publisher/SetDynamicTF.h>
00008 #include <Eigen/Geometry>
00009 
00010 #include <kdl/frames_io.hpp>
00011 #include <tf_conversions/tf_kdl.h>
00012 
00013 using namespace urdf;
00014 using namespace std;
00015 
00016 void UrdfModelMarker::addMoveMarkerControl(visualization_msgs::InteractiveMarker &int_marker, boost::shared_ptr<const Link> link, bool root){
00017   visualization_msgs::InteractiveMarkerControl control;
00018   if(root){
00019     im_helpers::add6DofControl(int_marker,false);
00020     for(int i=0; i<int_marker.controls.size(); i++){
00021       int_marker.controls[i].always_visible = true;
00022     }
00023   }else{
00024     boost::shared_ptr<Joint> parent_joint = link->parent_joint;
00025     Eigen::Vector3f origin_x(1,0,0);
00026     Eigen::Vector3f dest_x(parent_joint->axis.x, parent_joint->axis.y, parent_joint->axis.z);
00027     Eigen::Quaternionf qua;
00028 
00029     qua.setFromTwoVectors(origin_x, dest_x);
00030     control.orientation.x = qua.x();
00031     control.orientation.y = qua.y();
00032     control.orientation.z = qua.z();
00033     control.orientation.w = qua.w();
00034 
00035     int_marker.scale = 0.5;
00036 
00037     switch(parent_joint->type){
00038     case Joint::REVOLUTE:
00039     case Joint::CONTINUOUS:
00040       control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
00041       int_marker.controls.push_back(control);
00042       break;
00043     case Joint::PRISMATIC:
00044       control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
00045       int_marker.controls.push_back(control);
00046       break;
00047     default:
00048       break;
00049     }
00050   }
00051 }
00052 
00053 void UrdfModelMarker::addInvisibleMeshMarkerControl(visualization_msgs::InteractiveMarker &int_marker, boost::shared_ptr<const Link> link, const std_msgs::ColorRGBA &color){
00054   visualization_msgs::InteractiveMarkerControl control;
00055   //    ps.pose = UrdfPose2Pose(link->visual->origin);
00056   visualization_msgs::Marker marker;
00057 
00058   //if (use_color) marker.color = color;
00059   marker.type = visualization_msgs::Marker::CYLINDER;
00060   double scale=0.01;
00061   marker.scale.x = scale;
00062   marker.scale.y = scale * 1;
00063   marker.scale.z = scale * 40;
00064   marker.color = color;
00065   //marker.pose = stamped.pose;
00066   //visualization_msgs::InteractiveMarkerControl control;
00067   boost::shared_ptr<Joint> parent_joint = link->parent_joint;
00068   Eigen::Vector3f origin_x(0,0,1);
00069   Eigen::Vector3f dest_x(parent_joint->axis.x, parent_joint->axis.y, parent_joint->axis.z);
00070   Eigen::Quaternionf qua;
00071 
00072   qua.setFromTwoVectors(origin_x, dest_x);
00073   marker.pose.orientation.x = qua.x();
00074   marker.pose.orientation.y = qua.y();
00075   marker.pose.orientation.z = qua.z();
00076   marker.pose.orientation.w = qua.w();
00077 
00078   control.markers.push_back( marker );
00079   control.interaction_mode = visualization_msgs::InteractiveMarkerControl::BUTTON;
00080   control.always_visible = true;
00081 
00082   int_marker.controls.push_back(control);
00083   return;
00084 }
00085 
00086 
00087 void UrdfModelMarker::addGraspPointControl(visualization_msgs::InteractiveMarker &int_marker, std::string link_frame_name_){
00088   //yellow sphere
00089   visualization_msgs::InteractiveMarkerControl control;
00090   control.interaction_mode = visualization_msgs::InteractiveMarkerControl::BUTTON;
00091   visualization_msgs::Marker marker;
00092   marker.type = visualization_msgs::Marker::SPHERE;
00093   marker.scale.x = 0.05;
00094   marker.scale.y = 0.05;
00095   marker.scale.z = 0.05;
00096 
00097   marker.color.a = 1.0;
00098   marker.color.r = 1.0;
00099   marker.color.g = 1.0;
00100   marker.color.b = 0.0;
00101 
00102   control.markers.push_back(marker);
00103   control.always_visible = true;
00104   int_marker.controls.push_back(control);
00105 
00106   if(linkMarkerMap[link_frame_name_].gp.displayMoveMarker){
00107     im_helpers::add6DofControl(int_marker,false);
00108   }
00109 }
00110 
00111 
00112 
00113 geometry_msgs::Transform UrdfModelMarker::Pose2Transform( const geometry_msgs::Pose pose_msg){
00114   geometry_msgs::Transform tf_msg;
00115   tf_msg.translation.x = pose_msg.position.x;
00116   tf_msg.translation.y = pose_msg.position.y;
00117   tf_msg.translation.z = pose_msg.position.z;
00118   tf_msg.rotation = pose_msg.orientation;
00119   return tf_msg;
00120 }
00121 
00122 geometry_msgs::Pose UrdfModelMarker::Transform2Pose( const geometry_msgs::Transform tf_msg){
00123   geometry_msgs::Pose pose_msg;
00124   pose_msg.position.x =  tf_msg.translation.x;
00125   pose_msg.position.y = tf_msg.translation.y;
00126   pose_msg.position.z = tf_msg.translation.z;
00127   pose_msg.orientation = tf_msg.rotation;
00128   return pose_msg;
00129 }
00130 
00131 
00132 geometry_msgs::Pose UrdfModelMarker::UrdfPose2Pose( const urdf::Pose pose){
00133   geometry_msgs::Pose p_msg;
00134   double x, y, z, w;
00135   pose.rotation.getQuaternion(x,y,z,w);
00136   p_msg.orientation.x = x;
00137   p_msg.orientation.y = y;
00138   p_msg.orientation.z = z;
00139   p_msg.orientation.w = w;
00140   
00141   p_msg.position.x = pose.position.x;
00142   p_msg.position.y = pose.position.y;
00143   p_msg.position.z = pose.position.z;
00144 
00145   return p_msg;
00146 }
00147 
00148 void UrdfModelMarker::CallSetDynamicTf(string parent_frame_id, string frame_id, geometry_msgs::Transform transform){
00149   jsk_topic_tools::ScopedTimer timer = dynamic_tf_check_time_acc_.scopedTimer();
00150   dynamic_tf_publisher::SetDynamicTF SetTf;
00151   //SetTf.request.freq = 10;
00152   SetTf.request.freq = 20;
00153   //SetTf.request.freq = 100;
00154   SetTf.request.cur_tf.header.stamp = ros::Time::now();
00155   SetTf.request.cur_tf.header.frame_id = parent_frame_id;
00156   SetTf.request.cur_tf.child_frame_id = frame_id;
00157   SetTf.request.cur_tf.transform = transform;
00158   if (use_dynamic_tf_ || parent_frame_id == frame_id_){
00159     std::cout << parent_frame_id << frame_id << std::endl;
00160     dynamic_tf_publisher_client.call(SetTf);
00161   }
00162 }
00163 
00164 void UrdfModelMarker::publishBasePose( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback ){
00165   publishBasePose(feedback->pose, feedback->header);
00166 }
00167 
00168 void UrdfModelMarker::publishBasePose( geometry_msgs::Pose pose, std_msgs::Header header){
00169   geometry_msgs::PoseStamped ps;
00170   ps.pose = pose;
00171   ps.header = header;
00172   pub_base_pose_.publish(ps);
00173 }
00174 
00175 
00176 
00177 void UrdfModelMarker::publishMarkerPose( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback ){
00178   publishMarkerPose(feedback->pose, feedback->header, feedback->marker_name);
00179 }
00180 
00181 void UrdfModelMarker::publishMarkerPose( geometry_msgs::Pose pose, std_msgs::Header header, std::string marker_name){
00182   jsk_interactive_marker::MarkerPose mp;
00183   mp.pose.header = header;
00184   mp.pose.pose = pose;
00185   mp.marker_name = marker_name;
00186   mp.type = jsk_interactive_marker::MarkerPose::GENERAL;
00187   pub_.publish( mp );
00188 }
00189 
00190 
00191 void UrdfModelMarker::publishMarkerMenu( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback, int menu ){
00192   jsk_interactive_marker::MarkerMenu m;
00193   m.marker_name = feedback->marker_name;
00194   m.menu=menu;
00195   pub_move_.publish(m);
00196 }
00197 
00198 void UrdfModelMarker::publishMoveObject( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback ){
00199 
00200   //jsk_interactive_marker::MoveObject m;
00201   jsk_interactive_marker::MarkerMenu m;
00202   /*
00203     m.goal.pose.header = feedback->header;
00204     m.goal.pose.pose = feedback->pose;
00205 
00206     m.origin.pose.header = feedback->header;
00207     m.origin.pose.pose = linkMarkerMap[feedback->marker_name].origin;
00208 
00209     m.graspPose = linkMarkerMap[feedback->marker_name].gp.pose;
00210     pub_move_.publish(m);*/
00211 }
00212 
00213 void UrdfModelMarker::publishJointState( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback ){
00214   sensor_msgs::JointState js;
00215   js.header = feedback->header;
00216   getJointState(model->getRoot(), js);
00217   pub_joint_state_.publish( js );
00218 }
00219 
00220 void UrdfModelMarker::republishJointState( sensor_msgs::JointState js){
00221   js.header.stamp = ros::Time::now();
00222   pub_joint_state_.publish( js );
00223 }
00224 
00225 void UrdfModelMarker::setRootPoseCB( const geometry_msgs::PoseStampedConstPtr &msg ){
00226   setRootPose(*msg);
00227 }
00228 
00229 void UrdfModelMarker::setRootPose ( geometry_msgs::PoseStamped ps ){
00230   try{
00231     init_stamp_ = ps.header.stamp;
00232     tfl_.transformPose(frame_id_, ps, ps);
00233 
00234     geometry_msgs::Pose pose = getRootPose(ps.pose);
00235 
00236     string root_frame = tf_prefix_ + model->getRoot()->name;
00237     linkMarkerMap[frame_id_].pose = pose;
00238     CallSetDynamicTf(frame_id_, root_frame, Pose2Transform(pose));
00239     root_pose_ = pose;
00240     publishMarkerPose( pose, ps.header, root_frame);
00241   }
00242   catch (tf::TransformException ex){
00243     ROS_ERROR("%s",ex.what());
00244   }
00245 
00246 }
00247 
00248 
00249 void UrdfModelMarker::resetJointStatesCB( const sensor_msgs::JointStateConstPtr &msg){
00250   jsk_topic_tools::ScopedTimer timer = reset_joint_states_check_time_acc_.scopedTimer();
00251   setJointState(model->getRoot(), msg);
00252   addChildLinkNames(model->getRoot(), true, false);
00253   republishJointState(*msg);
00254 
00255   if(mode_ == "visualization"){
00256     //fix fixed_link of Marker on fixed_link of Robot
00257     string marker_name =  tf_prefix_ + model->getRoot()->name;
00258 
00259     tf::StampedTransform stf_robot;
00260     tfl_.lookupTransform(fixed_link_, model->getRoot()->name,
00261                          ros::Time(0), stf_robot);
00262 
00263     tf::StampedTransform stf_marker;
00264     tfl_.lookupTransform(marker_name, tf_prefix_ + fixed_link_,
00265                          ros::Time(0), stf_marker);
00266     
00267     KDL::Frame robotFrame;
00268     KDL::Frame markerFrame;
00269     KDL::Frame robotMarkerFrame;
00270     tf::transformTFToKDL(stf_robot, robotFrame);
00271     tf::transformTFToKDL(stf_marker, markerFrame);
00272     
00273     robotMarkerFrame = robotFrame * markerFrame;
00274 
00275     //TODO set Link
00276     geometry_msgs::Pose pose;
00277     pose.position.y = 1;
00278     pose.orientation.w = 1;
00279 
00280 
00281     server_->setPose(marker_name, pose);
00282     linkMarkerMap[marker_name].pose = pose;
00283     CallSetDynamicTf(frame_id_, marker_name, Pose2Transform(pose));
00284     addChildLinkNames(model->getRoot(), true, false);
00285   }
00286 }
00287 
00288 
00289 void UrdfModelMarker::proc_feedback( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback, string parent_frame_id, string frame_id){
00290   switch ( feedback->event_type ){
00291   case visualization_msgs::InteractiveMarkerFeedback::POSE_UPDATE:
00292   case visualization_msgs::InteractiveMarkerFeedback::MOUSE_UP:
00293     linkMarkerMap[frame_id].pose = feedback->pose;
00294     //root link
00295     if(parent_frame_id == frame_id_){
00296       root_pose_ = feedback->pose;
00297       publishBasePose(feedback);
00298     }
00299     CallSetDynamicTf(parent_frame_id, frame_id, Pose2Transform(feedback->pose));
00300     publishMarkerPose(feedback);
00301     publishJointState(feedback);
00302     break;
00303   case visualization_msgs::InteractiveMarkerFeedback::BUTTON_CLICK:
00304     cout << "clicked" << " frame:" << frame_id << mode_ << endl;
00305     //linkMarkerMap[frame_id].displayMoveMarker ^= true;
00306     if (mode_ != "visualization"){
00307       linkMarkerMap[linkMarkerMap[frame_id].movable_link].displayMoveMarker ^= true;
00308       addChildLinkNames(model->getRoot(), true, false);
00309     }else{
00310       geometry_msgs::PoseStamped ps = getOriginPoseStamped();
00311       pub_selected_.publish(ps);
00312       jsk_pcl_ros::Int32Stamped index_msg;
00313       index_msg.data = index_;
00314       index_msg.header.stamp = init_stamp_;
00315       pub_selected_index_.publish(index_msg);
00316     }
00317     break;
00318 
00319   }
00320   diagnostic_updater_->update();
00321 }
00322 
00323 
00324 
00325 void UrdfModelMarker::graspPointCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback ){
00326   //linkMarkerMap[feedback->marker_name].gp.pose = feedback->pose;
00327   //publishMarkerPose(feedback);
00328   //publishMarkerMenu(feedback, jsk_interactive_marker::MarkerMenu::MOVE);
00329 
00330   KDL::Vector graspVec(feedback->mouse_point.x, feedback->mouse_point.y, feedback->mouse_point.z);
00331   KDL::Frame parentFrame;
00332   tf::poseMsgToKDL (linkMarkerMap[feedback->marker_name].pose, parentFrame);
00333 
00334   graspVec = parentFrame.Inverse(graspVec);
00335 
00336   geometry_msgs::Pose p;
00337   p.position.x = graspVec.x();
00338   p.position.y = graspVec.y();
00339   p.position.z = graspVec.z();
00340   p.orientation = linkMarkerMap[feedback->marker_name].gp.pose.orientation;
00341   linkMarkerMap[feedback->marker_name].gp.pose = p;
00342   
00343   linkMarkerMap[feedback->marker_name].gp.displayGraspPoint = true;
00344   addChildLinkNames(model->getRoot(), true, false);
00345   //addChildLinkNames(model->getRoot(), true, false, true, 0);
00346 }
00347 
00348 
00349 void UrdfModelMarker::jointMoveCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback ){
00350   publishJointState(feedback);
00351   sleep(0.5);
00352   publishMarkerMenu(feedback, jsk_interactive_marker::MarkerMenu::JOINT_MOVE);
00353 }
00354 
00355 void UrdfModelMarker::resetMarkerCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback ){
00356   
00357   publishJointState(feedback);
00358   publishMarkerMenu(feedback, jsk_interactive_marker::MarkerMenu::RESET_JOINT);
00359 }
00360 
00361 void UrdfModelMarker::resetBaseMsgCB( const std_msgs::EmptyConstPtr &msg){
00362   resetBaseCB();
00363 }
00364 
00365 void UrdfModelMarker::resetBaseMarkerCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback ){
00366   resetBaseCB();
00367 }
00368 void UrdfModelMarker::resetBaseCB(){
00369   resetRobotBase();
00370 
00371   geometry_msgs::PoseStamped ps;
00372   ps.header.frame_id = frame_id_;
00373   ps.pose = root_pose_;
00374   setRootPose( ps );
00375 
00376   // to update root link marker
00377   addChildLinkNames(model->getRoot(), true, false);
00378 }
00379 
00380 void UrdfModelMarker::resetRobotBase(){
00381   //set root_pose_ to robot base pose
00382   try{
00383     tf::StampedTransform transform;
00384     geometry_msgs::TransformStamped ts_msg;
00385     tfl_.lookupTransform(frame_id_, model->getRoot()->name,
00386                          ros::Time(0), transform);
00387     tf::transformStampedTFToMsg(transform, ts_msg);
00388 
00389     root_pose_ = Transform2Pose(ts_msg.transform);
00390   }
00391   catch (tf::TransformException ex){
00392     ROS_ERROR("%s",ex.what());
00393   }
00394 }
00395 
00396 
00397 void UrdfModelMarker::registrationCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback ){
00398   
00399   publishJointState(feedback);
00400 }
00401 
00402 void UrdfModelMarker::moveCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback ){
00403   jsk_interactive_marker::MoveObject mo;
00404   mo.origin.header = feedback->header;
00405   mo.origin.pose = linkMarkerMap[feedback->marker_name].origin;
00406 
00407   mo.goal.header = feedback->header;
00408   mo.goal.pose = feedback->pose;
00409 
00410   mo.graspPose = linkMarkerMap[feedback->marker_name].gp.pose;
00411   
00412   pub_move_object_.publish( mo );
00413 
00414 }
00415 
00416 void UrdfModelMarker::setPoseCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback ){
00417   cout << "setPose" <<endl;
00418   setOriginalPose(model->getRoot());
00419 }
00420 
00421 void UrdfModelMarker::hideMarkerCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback ){
00422   linkMarkerMap[linkMarkerMap[feedback->marker_name].movable_link].displayMoveMarker = false;
00423   addChildLinkNames(model->getRoot(), true, false);
00424 }
00425 
00426 void UrdfModelMarker::hideAllMarkerCB( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback ){
00427   map<string, linkProperty>::iterator it = linkMarkerMap.begin();
00428   while( it != linkMarkerMap.end() )
00429     {
00430       (*it).second.displayMoveMarker = false;
00431       ++it;
00432     }
00433   addChildLinkNames(model->getRoot(), true, false);
00434 }
00435 
00436 void UrdfModelMarker::hideModelMarkerCB( const std_msgs::EmptyConstPtr &msg){
00437   map<string, linkProperty>::iterator it = linkMarkerMap.begin();
00438   while( it != linkMarkerMap.end() )
00439     {
00440       (*it).second.displayModelMarker = false;
00441       ++it;
00442     }
00443   addChildLinkNames(model->getRoot(), true, false);
00444 }
00445 
00446 void UrdfModelMarker::showModelMarkerCB( const std_msgs::EmptyConstPtr &msg){
00447   map<string, linkProperty>::iterator it = linkMarkerMap.begin();
00448   while( it != linkMarkerMap.end() )
00449     {
00450       (*it).second.displayModelMarker = true;
00451       ++it;
00452     }
00453   addChildLinkNames(model->getRoot(), true, false);
00454 
00455 }
00456 
00457 void UrdfModelMarker::setUrdfCB( const std_msgs::StringConstPtr &msg){
00458   server_->clear();
00459   model = parseURDF(msg->data);
00460   if (!model){
00461     std::cerr << "ERROR: Model Parsing the xml failed" << std::endl;
00462     return;
00463   }
00464   addChildLinkNames(model->getRoot(), true, true);
00465 
00466   // start JointState
00467   sensor_msgs::JointState js;
00468   getJointState(model->getRoot(), js);
00469   pub_joint_state_.publish( js );
00470   return;
00471 }
00472 
00473 
00474 void UrdfModelMarker::graspPoint_feedback( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback, string link_name){
00475   switch ( feedback->event_type ){
00476   case visualization_msgs::InteractiveMarkerFeedback::POSE_UPDATE:
00477     linkMarkerMap[link_name].gp.pose = feedback->pose;
00478     publishMarkerPose(feedback);
00479     break;
00480   case visualization_msgs::InteractiveMarkerFeedback::BUTTON_CLICK:
00481     cout << "clicked" << " frame:" << feedback->marker_name << endl;
00482     linkMarkerMap[link_name].gp.displayMoveMarker ^= true;
00483     addChildLinkNames(model->getRoot(), true, false);
00484     break;
00485   }
00486 }
00487 
00488 visualization_msgs::InteractiveMarkerControl UrdfModelMarker::makeMeshMarkerControl(const std::string &mesh_resource, const geometry_msgs::PoseStamped &stamped, geometry_msgs::Vector3 scale, const std_msgs::ColorRGBA &color, bool use_color){
00489   visualization_msgs::Marker meshMarker;
00490 
00491   if (use_color) meshMarker.color = color;
00492   meshMarker.mesh_resource = mesh_resource;
00493   meshMarker.mesh_use_embedded_materials = !use_color;
00494   meshMarker.type = visualization_msgs::Marker::MESH_RESOURCE;
00495   
00496   meshMarker.scale = scale;
00497   meshMarker.pose = stamped.pose;
00498   visualization_msgs::InteractiveMarkerControl control;
00499   control.markers.push_back( meshMarker );
00500   control.interaction_mode = visualization_msgs::InteractiveMarkerControl::BUTTON;
00501   control.always_visible = true;
00502   
00503   return control;
00504 }
00505 
00506 visualization_msgs::InteractiveMarkerControl UrdfModelMarker::makeMeshMarkerControl(const std::string &mesh_resource, const geometry_msgs::PoseStamped &stamped, geometry_msgs::Vector3 scale)
00507 {
00508   std_msgs::ColorRGBA color;
00509   color.r = 0;
00510   color.g = 0;
00511   color.b = 0;
00512   color.a = 0;
00513   //color.a = 0.3;
00514   return makeMeshMarkerControl(mesh_resource, stamped, scale, color, false);
00515   //return makeMeshMarkerControl(mesh_resource, stamped, scale, color, true);
00516 }
00517 
00518 visualization_msgs::InteractiveMarkerControl UrdfModelMarker::makeMeshMarkerControl(const std::string &mesh_resource,
00519                                                                                     const geometry_msgs::PoseStamped &stamped, geometry_msgs::Vector3 scale, const std_msgs::ColorRGBA &color)
00520 {
00521   return makeMeshMarkerControl(mesh_resource, stamped, scale, color, true);
00522 }
00523 
00524 visualization_msgs::InteractiveMarkerControl UrdfModelMarker::makeCylinderMarkerControl(const geometry_msgs::PoseStamped &stamped, double length,  double radius, const std_msgs::ColorRGBA &color, bool use_color){
00525   visualization_msgs::Marker cylinderMarker;
00526 
00527   if (use_color) cylinderMarker.color = color;
00528   cylinderMarker.type = visualization_msgs::Marker::CYLINDER;
00529   cylinderMarker.scale.x = radius * 2;
00530   cylinderMarker.scale.y = radius * 2;
00531   cylinderMarker.scale.z = length;
00532   cylinderMarker.pose = stamped.pose;
00533 
00534   visualization_msgs::InteractiveMarkerControl control;
00535   control.markers.push_back( cylinderMarker );
00536   control.interaction_mode = visualization_msgs::InteractiveMarkerControl::BUTTON;
00537   control.always_visible = true;
00538 
00539   return control;
00540 }
00541 
00542 visualization_msgs::InteractiveMarkerControl UrdfModelMarker::makeBoxMarkerControl(const geometry_msgs::PoseStamped &stamped, Vector3 dim, const std_msgs::ColorRGBA &color, bool use_color){
00543   visualization_msgs::Marker boxMarker;
00544 
00545   fprintf(stderr, "urdfModelMarker = %f %f %f\n", dim.x, dim.y, dim.z);
00546   if (use_color) boxMarker.color = color;
00547   boxMarker.type = visualization_msgs::Marker::CUBE;
00548   boxMarker.scale.x = dim.x;
00549   boxMarker.scale.y = dim.y;
00550   boxMarker.scale.z = dim.z;
00551   boxMarker.pose = stamped.pose;
00552 
00553   visualization_msgs::InteractiveMarkerControl control;
00554   control.markers.push_back( boxMarker );
00555   control.interaction_mode = visualization_msgs::InteractiveMarkerControl::BUTTON;
00556   control.always_visible = true;
00557 
00558   return control;
00559 }
00560 
00561 visualization_msgs::InteractiveMarkerControl UrdfModelMarker::makeSphereMarkerControl(const geometry_msgs::PoseStamped &stamped, double rad, const std_msgs::ColorRGBA &color, bool use_color){
00562   visualization_msgs::Marker sphereMarker;
00563 
00564   if (use_color) sphereMarker.color = color;
00565   sphereMarker.type = visualization_msgs::Marker::SPHERE;
00566   sphereMarker.scale.x = rad * 2;
00567   sphereMarker.scale.y = rad * 2;
00568   sphereMarker.scale.z = rad * 2;
00569   sphereMarker.pose = stamped.pose;
00570 
00571   visualization_msgs::InteractiveMarkerControl control;
00572   control.markers.push_back( sphereMarker );
00573   control.interaction_mode = visualization_msgs::InteractiveMarkerControl::BUTTON;
00574   control.always_visible = true;
00575 
00576   return control;
00577 }
00578 
00579 
00580 void UrdfModelMarker::getJointState(boost::shared_ptr<const Link> link, sensor_msgs::JointState &js)
00581 {
00582   string link_frame_name_ =  tf_prefix_ + link->name;
00583   boost::shared_ptr<Joint> parent_joint = link->parent_joint;
00584   if(parent_joint != NULL){
00585     KDL::Frame initialFrame;
00586     KDL::Frame presentFrame;
00587     KDL::Rotation rot;
00588     KDL::Vector rotVec;
00589     KDL::Vector jointVec;
00590     double jointAngle;
00591     double jointAngleAllRange;
00592     switch(parent_joint->type){
00593     case Joint::REVOLUTE:
00594     case Joint::CONTINUOUS:
00595       {
00596       linkProperty *link_property = &linkMarkerMap[link_frame_name_];
00597       tf::poseMsgToKDL (link_property->initial_pose, initialFrame);
00598       tf::poseMsgToKDL (link_property->pose, presentFrame);
00599       rot = initialFrame.M.Inverse() * presentFrame.M;
00600       jointAngle = rot.GetRotAngle(rotVec);
00601       jointVec = KDL::Vector(link_property->joint_axis.x,
00602                              link_property->joint_axis.y,
00603                              link_property->joint_axis.z);
00604       if( KDL::dot(rotVec,jointVec) < 0){
00605         jointAngle = - jointAngle;
00606       }
00607       if(link_property->joint_angle > M_PI/2 && jointAngle < -M_PI/2){
00608         link_property->rotation_count += 1;
00609       }else if(link_property->joint_angle < -M_PI/2 && jointAngle > M_PI/2){
00610         link_property->rotation_count -= 1;
00611       }
00612       link_property->joint_angle = jointAngle;
00613       jointAngleAllRange = jointAngle + link_property->rotation_count * M_PI * 2;
00614 
00615       if(parent_joint->type == Joint::REVOLUTE && parent_joint->limits != NULL){
00616         bool changeMarkerAngle = false;
00617         if(jointAngleAllRange < parent_joint->limits->lower){
00618           jointAngleAllRange = parent_joint->limits->lower + 0.001;
00619           changeMarkerAngle = true;
00620         }
00621         if(jointAngleAllRange > parent_joint->limits->upper){
00622           jointAngleAllRange = parent_joint->limits->upper - 0.001;
00623           changeMarkerAngle = true;
00624         }
00625 
00626         if(changeMarkerAngle){
00627           setJointAngle(link, jointAngleAllRange);
00628         }
00629       }
00630 
00631       js.position.push_back(jointAngleAllRange);
00632       js.name.push_back(parent_joint->name);
00633       break;
00634       }
00635     case Joint::PRISMATIC:
00636       {
00637         KDL::Vector pos;
00638         linkProperty *link_property = &linkMarkerMap[link_frame_name_];
00639         tf::poseMsgToKDL (link_property->initial_pose, initialFrame);
00640         tf::poseMsgToKDL (link_property->pose, presentFrame);
00641 
00642         pos = presentFrame.p - initialFrame.p;
00643 
00644         jointVec = KDL::Vector(link_property->joint_axis.x,
00645                                link_property->joint_axis.y,
00646                                link_property->joint_axis.z);
00647         jointVec = jointVec / jointVec.Norm(); // normalize vector
00648         jointAngle = KDL::dot(jointVec, pos);
00649 
00650         link_property->joint_angle = jointAngle;
00651         jointAngleAllRange = jointAngle;
00652 
00653         if(parent_joint->type == Joint::PRISMATIC && parent_joint->limits != NULL){
00654           bool changeMarkerAngle = false;
00655           if(jointAngleAllRange < parent_joint->limits->lower){
00656             jointAngleAllRange = parent_joint->limits->lower + 0.003;
00657             changeMarkerAngle = true;
00658           }
00659           if(jointAngleAllRange > parent_joint->limits->upper){
00660             jointAngleAllRange = parent_joint->limits->upper - 0.003;
00661             changeMarkerAngle = true;
00662           }
00663           if(changeMarkerAngle){
00664             setJointAngle(link, jointAngleAllRange);
00665           }
00666         }
00667 
00668         js.position.push_back(jointAngleAllRange);
00669         js.name.push_back(parent_joint->name);
00670         break;
00671       }
00672     case Joint::FIXED:
00673       break;
00674     default:
00675       break;
00676     }
00677   }
00678 
00679   for (std::vector<boost::shared_ptr<Link> >::const_iterator child = link->child_links.begin(); child != link->child_links.end(); child++){
00680     getJointState(*child, js);
00681   }
00682   return;
00683 }
00684 
00685 void UrdfModelMarker::setJointAngle(boost::shared_ptr<const Link> link, double joint_angle){
00686   string link_frame_name_ =  tf_prefix_ + link->name;
00687   boost::shared_ptr<Joint> parent_joint = link->parent_joint;
00688 
00689   if(parent_joint == NULL){
00690     return;
00691   }
00692 
00693   KDL::Frame initialFrame;
00694   KDL::Frame presentFrame;
00695   KDL::Rotation rot;
00696   KDL::Vector rotVec;
00697   KDL::Vector jointVec;
00698 
00699   std_msgs::Header link_header;
00700 
00701   int rotation_count = 0;
00702   
00703   switch(parent_joint->type){
00704     case Joint::REVOLUTE:
00705     case Joint::CONTINUOUS:
00706       {
00707       if(joint_angle > M_PI){
00708         rotation_count = (int)((joint_angle + M_PI) / (M_PI * 2));
00709         joint_angle -= rotation_count * M_PI * 2;
00710       }else if(joint_angle < -M_PI){
00711         rotation_count = (int)((- joint_angle + M_PI) / (M_PI * 2));
00712         joint_angle -= rotation_count * M_PI * 2;
00713       }
00714       linkProperty *link_property = &linkMarkerMap[link_frame_name_];
00715       link_property->joint_angle = joint_angle;
00716       link_property->rotation_count = rotation_count;
00717 
00718       tf::poseMsgToKDL (link_property->initial_pose, initialFrame);
00719       tf::poseMsgToKDL (link_property->initial_pose, presentFrame);
00720       jointVec = KDL::Vector(link_property->joint_axis.x,
00721                              link_property->joint_axis.y,
00722                              link_property->joint_axis.z);
00723 
00724       presentFrame.M = KDL::Rotation::Rot(jointVec, joint_angle) * initialFrame.M;
00725       tf::poseKDLToMsg(presentFrame, link_property->pose);
00726 
00727       break;
00728       }
00729     case Joint::PRISMATIC:
00730       {
00731       linkProperty *link_property = &linkMarkerMap[link_frame_name_];
00732       link_property->joint_angle = joint_angle;
00733       link_property->rotation_count = rotation_count;
00734       tf::poseMsgToKDL (link_property->initial_pose, initialFrame);
00735       tf::poseMsgToKDL (link_property->initial_pose, presentFrame);
00736       jointVec = KDL::Vector(link_property->joint_axis.x,
00737                              link_property->joint_axis.y,
00738                              link_property->joint_axis.z);
00739       jointVec = jointVec / jointVec.Norm(); // normalize vector
00740       presentFrame.p = joint_angle * jointVec + initialFrame.p;
00741       tf::poseKDLToMsg(presentFrame, link_property->pose);
00742       break;
00743       }
00744   default:
00745     break;
00746   }
00747 
00748   link_header.stamp = ros::Time(0);
00749   link_header.frame_id = linkMarkerMap[link_frame_name_].frame_id;
00750 
00751   server_->setPose(link_frame_name_, linkMarkerMap[link_frame_name_].pose, link_header);
00752   server_->applyChanges();
00753   CallSetDynamicTf(linkMarkerMap[link_frame_name_].frame_id, link_frame_name_, Pose2Transform(linkMarkerMap[link_frame_name_].pose));
00754 
00755 }
00756 
00757 void UrdfModelMarker::setJointState(boost::shared_ptr<const Link> link, const sensor_msgs::JointStateConstPtr &js)
00758 {
00759   string link_frame_name_ =  tf_prefix_ + link->name;
00760   boost::shared_ptr<Joint> parent_joint = link->parent_joint;
00761   if(parent_joint != NULL){
00762     KDL::Frame initialFrame;
00763     KDL::Frame presentFrame;
00764     KDL::Rotation rot;
00765     KDL::Vector rotVec;
00766     KDL::Vector jointVec;
00767     double jointAngle;
00768     bool changeAngle = false;
00769     std_msgs::Header link_header;
00770     switch(parent_joint->type){
00771     case Joint::REVOLUTE:
00772     case Joint::CONTINUOUS:
00773       for(int i=0; i< js->name.size(); i++){
00774         if(js->name[i] == parent_joint->name){
00775           jointAngle = js->position[i];
00776           changeAngle = true;
00777           break;
00778         }
00779       }
00780       if(!changeAngle){
00781         break;
00782       }
00783       setJointAngle(link, jointAngle);
00784       break;
00785     case Joint::PRISMATIC:
00786       for(int i=0; i< js->name.size(); i++){
00787         if(js->name[i] == parent_joint->name){
00788           jointAngle = js->position[i];
00789           changeAngle = true;
00790           break;
00791         }
00792       }
00793       if(!changeAngle){
00794         break;
00795       }
00796       setJointAngle(link, jointAngle);
00797       break;
00798     default:
00799       break;
00800     }
00801   }
00802   for (std::vector<boost::shared_ptr<Link> >::const_iterator child = link->child_links.begin(); child != link->child_links.end(); child++){
00803     setJointState(*child, js);
00804   }
00805   return;
00806 }
00807 
00808 geometry_msgs::Pose UrdfModelMarker::getRootPose(geometry_msgs::Pose pose){
00809   KDL::Frame pose_frame, offset_frame;
00810   tf::poseMsgToKDL(pose, pose_frame);
00811   tf::poseMsgToKDL(root_offset_, offset_frame);
00812   pose_frame = pose_frame * offset_frame.Inverse();
00813   tf::poseKDLToMsg(pose_frame, pose);
00814   return pose;
00815 }
00816 
00817 geometry_msgs::PoseStamped UrdfModelMarker::getOriginPoseStamped(){
00818   geometry_msgs::PoseStamped ps;
00819   geometry_msgs::Pose pose;
00820   pose = root_pose_;
00821   KDL::Frame pose_frame, offset_frame;
00822   tf::poseMsgToKDL(pose, pose_frame);
00823   tf::poseMsgToKDL(root_offset_, offset_frame);
00824   pose_frame = pose_frame * offset_frame;
00825   tf::poseKDLToMsg(pose_frame, pose);
00826   ps.pose = pose;
00827   ps.header.frame_id = frame_id_;
00828   ps.header.stamp = init_stamp_;
00829   return ps;
00830 }
00831 
00832 
00833 void UrdfModelMarker::setOriginalPose(boost::shared_ptr<const Link> link)
00834 {
00835   string link_frame_name_ =  tf_prefix_ + link->name;
00836   linkMarkerMap[link_frame_name_].origin =  linkMarkerMap[link_frame_name_].pose;
00837   for (std::vector<boost::shared_ptr<Link> >::const_iterator child = link->child_links.begin(); child != link->child_links.end(); child++){
00838     setOriginalPose(*child);
00839   }
00840 }
00841 
00842 void UrdfModelMarker::addChildLinkNames(boost::shared_ptr<const Link> link, bool root, bool init){
00843   //addChildLinkNames(link, root, init, false, 0);
00844   addChildLinkNames(link, root, init, use_visible_color_, 0);
00845 }
00846 
00847 void UrdfModelMarker::addChildLinkNames(boost::shared_ptr<const Link> link, bool root, bool init, bool use_color, int color_index)
00848 {
00849   geometry_msgs::PoseStamped ps;
00850 
00851   double scale_factor = 1.02;
00852   string link_frame_name_ =  tf_prefix_ + link->name;
00853   string parent_link_frame_name_;
00854 
00855   if(root){
00856     parent_link_frame_name_ = frame_id_;
00857     //ps.pose = root_pose_;
00858     ps.pose = getRootPose(root_pose_);
00859   }else{
00860     parent_link_frame_name_ = link->parent_joint->parent_link_name;
00861     parent_link_frame_name_ = tf_prefix_ + parent_link_frame_name_;
00862     ps.pose = UrdfPose2Pose(link->parent_joint->parent_to_joint_origin_transform);
00863   }
00864   ps.header.frame_id =  parent_link_frame_name_;
00865   ps.header.stamp = ros::Time(0);
00866 
00867   //initialize linkProperty
00868   if(init){
00869     CallSetDynamicTf(parent_link_frame_name_, link_frame_name_, Pose2Transform(ps.pose));
00870     linkProperty lp;
00871     lp.pose = ps.pose;
00872     lp.origin = ps.pose;
00873     lp.initial_pose = ps.pose;
00874     if(link->parent_joint !=NULL){
00875       lp.joint_axis = link->parent_joint->axis;
00876     }
00877     lp.joint_angle = 0;
00878     lp.rotation_count=0;
00879     if(link->parent_joint !=NULL && link->parent_joint->type == Joint::FIXED){
00880       lp.movable_link = linkMarkerMap[parent_link_frame_name_].movable_link;
00881     }else{
00882       lp.movable_link = link_frame_name_;
00883     }
00884 
00885     linkMarkerMap.insert( map<string, linkProperty>::value_type( link_frame_name_, lp ) );
00886   }
00887 
00888   linkMarkerMap[link_frame_name_].frame_id = parent_link_frame_name_;
00889 
00890   visualization_msgs::InteractiveMarker int_marker;
00891   int_marker.header = ps.header;
00892 
00893   int_marker.name = link_frame_name_;
00894   int_marker.scale = 1.0;
00895   int_marker.pose = ps.pose;
00896 
00897 
00898   if(!init && !root){
00899     visualization_msgs::InteractiveMarker old_marker;
00900     if(server_->get(link_frame_name_, old_marker)){
00901       int_marker.pose = old_marker.pose;
00902     }
00903   }
00904 
00905 
00906   //hide marker
00907   if(!linkMarkerMap[link_frame_name_].displayModelMarker){
00908     server_->erase(link_frame_name_);
00909     server_->erase(tf_prefix_ + link->name + "/grasp"); //grasp marker
00910   }else{
00911 
00912     //move Marker
00913     if(linkMarkerMap[link_frame_name_].displayMoveMarker){
00914       addMoveMarkerControl(int_marker, link, root);
00915     }
00916     //model Mesh Marker
00917     std_msgs::ColorRGBA color;
00918     if(mode_ == "visualization"){
00919       color.r = (double)0xFF / 0xFF;
00920       color.g = (double)0xFF / 0xFF;
00921       color.b = (double)0x00 / 0xFF;
00922       color.a = 0.5;
00923     }else{
00924       switch(color_index %3){
00925       case 0:
00926         color.r = (double)0xFF / 0xFF;
00927         color.g = (double)0xC3 / 0xFF;
00928         color.b = (double)0x00 / 0xFF;
00929         break;
00930       case 1:
00931         color.r = (double)0x58 / 0xFF;
00932         color.g = (double)0x0E / 0xFF;
00933         color.b = (double)0xAD / 0xFF;
00934         break;
00935       case 2:
00936         color.r = (double)0x0C / 0xFF;
00937         color.g = (double)0x5A / 0xFF;
00938         color.b = (double)0xA6 / 0xFF;
00939         break;
00940       }
00941       color.a = 1.0;
00942     }
00943 
00944     //link_array
00945     std::vector<boost ::shared_ptr<Visual> > visual_array;
00946     if(link->visual_array.size() != 0){
00947       visual_array = link->visual_array;
00948     }else if(link->visual.get() != NULL){
00949       visual_array.push_back(link->visual);
00950     }
00951     for(int i=0; i<visual_array.size(); i++){
00952       boost::shared_ptr<Visual> link_visual = visual_array[i];
00953       if(link_visual.get() != NULL && link_visual->geometry.get() != NULL){
00954         visualization_msgs::InteractiveMarkerControl meshControl;
00955         if(link_visual->geometry->type == Geometry::MESH){
00956           boost::shared_ptr<const Mesh> mesh = boost::static_pointer_cast<const Mesh>(link_visual->geometry);
00957           string model_mesh_ = mesh->filename;
00958           //model_mesh_ = getModelFilePath(model_mesh_);
00959           if(linkMarkerMap[link_frame_name_].mesh_file == ""){
00960             model_mesh_ = getRosPathFromModelPath(model_mesh_);
00961             linkMarkerMap[link_frame_name_].mesh_file = model_mesh_;
00962           }else{
00963             model_mesh_ = linkMarkerMap[link_frame_name_].mesh_file;
00964           }
00965 
00966           ps.pose = UrdfPose2Pose(link_visual->origin);
00967           cout << "mesh_file:" << model_mesh_ << endl;
00968 
00969           geometry_msgs::Vector3 mesh_scale;
00970           mesh_scale.x = mesh->scale.x;
00971           mesh_scale.y = mesh->scale.y;
00972           mesh_scale.z = mesh->scale.z;
00973           if(use_color){
00974             meshControl = makeMeshMarkerControl(model_mesh_, ps, mesh_scale, color);
00975           }else{
00976             meshControl = makeMeshMarkerControl(model_mesh_, ps, mesh_scale);
00977           }
00978         }else if(link_visual->geometry->type == Geometry::CYLINDER){
00979           boost::shared_ptr<const Cylinder> cylinder = boost::static_pointer_cast<const Cylinder>(link_visual->geometry);
00980           std::cout << "cylinder " << link->name;
00981           ps.pose = UrdfPose2Pose(link_visual->origin);
00982           double length = cylinder->length;
00983           double radius = cylinder->radius;
00984           std::cout << ", length =  " << length << ", radius " << radius << std::endl;
00985           if(use_color){
00986             meshControl = makeCylinderMarkerControl(ps, length, radius, color, true);
00987           }else{
00988             meshControl = makeCylinderMarkerControl(ps, length, radius, color, true);
00989           }
00990         }else if(link_visual->geometry->type == Geometry::BOX){
00991           boost::shared_ptr<const Box> box = boost::static_pointer_cast<const Box>(link_visual->geometry);
00992           std::cout << "box " << link->name;
00993           ps.pose = UrdfPose2Pose(link_visual->origin);
00994           Vector3 dim = box->dim;
00995           std::cout << ", dim =  " << dim.x << ", " << dim.y << ", " << dim.z << std::endl;
00996           if(use_color){
00997             meshControl = makeBoxMarkerControl(ps, dim, color, true);
00998           }else{
00999             meshControl = makeBoxMarkerControl(ps, dim, color, true);
01000           }
01001         }else if(link_visual->geometry->type == Geometry::SPHERE){
01002           boost::shared_ptr<const Sphere> sphere = boost::static_pointer_cast<const Sphere>(link_visual->geometry);
01003           ps.pose = UrdfPose2Pose(link_visual->origin);
01004           double rad = sphere->radius;
01005           if(use_color){
01006             meshControl = makeSphereMarkerControl(ps, rad, color, true);
01007           }else{
01008             meshControl = makeSphereMarkerControl(ps, rad, color, true);
01009           }
01010         }
01011         int_marker.controls.push_back( meshControl );
01012 
01013         server_->insert(int_marker);
01014         server_->setCallback( int_marker.name,
01015                               boost::bind( &UrdfModelMarker::proc_feedback, this, _1, parent_link_frame_name_, link_frame_name_) );
01016 
01017         model_menu_.apply(*server_, link_frame_name_);
01018 
01019       }else{
01020         boost::shared_ptr<Joint> parent_joint = link->parent_joint;
01021         if(parent_joint != NULL){
01022           if(parent_joint->type==Joint::REVOLUTE || parent_joint->type==Joint::REVOLUTE){
01023             addInvisibleMeshMarkerControl(int_marker, link, color);
01024             server_->insert(int_marker);
01025             server_->setCallback( int_marker.name,
01026                                   boost::bind( &UrdfModelMarker::proc_feedback, this, _1, parent_link_frame_name_, link_frame_name_) );
01027             model_menu_.apply(*server_, link_frame_name_);
01028           }
01029         }
01030       }
01031     }
01032     if(!robot_mode_){
01033       //add Grasp Point Marker
01034       if(linkMarkerMap[link_frame_name_].gp.displayGraspPoint){
01035         visualization_msgs::InteractiveMarker grasp_int_marker;
01036         double grasp_scale_factor = 1.02;
01037         string grasp_link_frame_name_ = tf_prefix_ + link->name + "/grasp";
01038         string grasp_parent_link_frame_name_ = tf_prefix_ + link->name;
01039 
01040         geometry_msgs::PoseStamped grasp_ps;
01041         grasp_ps.pose = linkMarkerMap[link_frame_name_].gp.pose;
01042         grasp_ps.header.frame_id =  grasp_parent_link_frame_name_;
01043 
01044         grasp_int_marker.header = grasp_ps.header;
01045         grasp_int_marker.name = grasp_link_frame_name_;
01046         grasp_int_marker.scale = grasp_scale_factor;
01047         grasp_int_marker.pose = grasp_ps.pose;
01048 
01049         addGraspPointControl(grasp_int_marker, link_frame_name_);
01050 
01051         server_->insert(grasp_int_marker);
01052         server_->setCallback( grasp_int_marker.name,
01053                               boost::bind( &UrdfModelMarker::graspPoint_feedback, this, _1, link_frame_name_));
01054 
01055       }
01056     }
01057   }
01058 
01059   //initialize JointState
01060   if(init){
01061     if(!root && initial_pose_map_.count(link->parent_joint->name) != 0){
01062       setJointAngle(link, initial_pose_map_[link->parent_joint->name]);
01063     }
01064   }
01065 
01066   //  cout << "Link:" << link->name << endl;
01067 
01068   for (std::vector<boost::shared_ptr<Link> >::const_iterator child = link->child_links.begin(); child != link->child_links.end(); child++){
01069     addChildLinkNames(*child, false, init, use_color, color_index + 1);
01070   }
01071   if(root){
01072     server_->applyChanges();
01073   }
01074 }
01075 
01076 
01077 
01078 UrdfModelMarker::UrdfModelMarker ()
01079 {}
01080 
01081 UrdfModelMarker::UrdfModelMarker (string model_name, string model_file, string frame_id, geometry_msgs::PoseStamped root_pose, geometry_msgs::Pose root_offset, double scale_factor, string mode, bool robot_mode, bool registration, string fixed_link, bool use_robot_description, bool use_visible_color, map<string, double> initial_pose_map, int index,  boost::shared_ptr<interactive_markers::InteractiveMarkerServer> server) : nh_(), pnh_("~"), tfl_(nh_),use_dynamic_tf_(true) {
01082   diagnostic_updater_.reset(new diagnostic_updater::Updater);
01083   diagnostic_updater_->setHardwareID(ros::this_node::getName());
01084   diagnostic_updater_->add("Modeling Stats", boost::bind(&UrdfModelMarker::updateDiagnostic, this, _1));
01085 
01086   pnh_.param("server_name", server_name, std::string ("") );
01087 
01088   if ( server_name == "" ) {
01089     server_name = ros::this_node::getName();
01090   }
01091 
01092   pnh_.getParam("use_dynamic_tf", use_dynamic_tf_);
01093   if (use_dynamic_tf_) {
01094     ros::service::waitForService("set_dynamic_tf", -1);
01095     dynamic_tf_publisher_client = nh_.serviceClient<dynamic_tf_publisher::SetDynamicTF>("set_dynamic_tf", true);
01096   }
01097   std::cerr << "use_dynamic_tf_ is " << use_dynamic_tf_ << std::endl;
01098 
01099   if(index != -1){
01100     stringstream ss;
01101     ss << model_name << index;
01102     model_name_ = ss.str();
01103   }else{
01104     model_name_ = model_name;
01105   }
01106 
01107   server_ = server;
01108   model_file_ = model_file;
01109   frame_id_ = frame_id;
01110   root_offset_ = root_offset;
01111   root_pose_ = root_pose.pose;
01112   init_stamp_ = root_pose.header.stamp;
01113   scale_factor_ = scale_factor;
01114   robot_mode_ = robot_mode;
01115   registration_ = registration;
01116   mode_ = mode;
01117   fixed_link_ = fixed_link;
01118   use_robot_description_ = use_robot_description;
01119   use_visible_color_ = use_visible_color;
01120   tf_prefix_ = server_name + "/" + model_name_ + "/";
01121   initial_pose_map_ = initial_pose_map;
01122   index_ = index;
01123 
01124   pub_ =  pnh_.advertise<jsk_interactive_marker::MarkerPose> ("pose", 1);
01125   pub_move_ =  pnh_.advertise<jsk_interactive_marker::MarkerMenu> ("marker_menu", 1);
01126   pub_move_object_ =  pnh_.advertise<jsk_interactive_marker::MoveObject> ("move_object", 1);
01127   pub_selected_ =  pnh_.advertise<geometry_msgs::PoseStamped> (model_name + "/selected", 1);
01128   pub_selected_index_ =  pnh_.advertise<jsk_pcl_ros::Int32Stamped> (model_name + "/selected_index", 1);
01129   pub_joint_state_ =  pnh_.advertise<sensor_msgs::JointState> (model_name_ + "/joint_states", 1);
01130   
01131   sub_set_root_pose_ = pnh_.subscribe<geometry_msgs::PoseStamped> (model_name_ + "/set_pose", 1, boost::bind( &UrdfModelMarker::setRootPoseCB, this, _1));
01132   sub_reset_joints_ = pnh_.subscribe<sensor_msgs::JointState> (model_name_ + "/reset_joint_states", 1, boost::bind( &UrdfModelMarker::resetJointStatesCB, this, _1));
01133   
01134   hide_marker_ = pnh_.subscribe<std_msgs::Empty> (model_name_ + "/hide_marker", 1, boost::bind( &UrdfModelMarker::hideModelMarkerCB, this, _1));
01135   show_marker_ = pnh_.subscribe<std_msgs::Empty> (model_name_ + "/show_marker", 1, boost::bind( &UrdfModelMarker::showModelMarkerCB, this, _1));
01136   sub_set_urdf_ = pnh_.subscribe<std_msgs::String>(model_name_ + "/set_urdf", 1, boost::bind( &UrdfModelMarker::setUrdfCB, this, _1));
01137 
01138   show_marker_ = pnh_.subscribe<std_msgs::Empty> (model_name_ + "/reset_root_pose", 1, boost::bind( &UrdfModelMarker::resetBaseMsgCB, this, _1));
01139 
01140   pub_base_pose_ = pnh_.advertise<geometry_msgs::PoseStamped>(model_name_ + "/base_pose", 1);
01141 
01142 
01143 
01144   /*
01145     serv_set_ = pnh_.advertiseService("set_pose",
01146     &InteractiveMarkerInterface::set_cb, this);
01147     serv_markers_set_ = pnh_.advertiseService("set_markers",
01148     &InteractiveMarkerInterface::markers_set_cb, this);
01149     serv_markers_del_ = pnh_.advertiseService("del_markers",
01150     &InteractiveMarkerInterface::markers_del_cb, this);
01151     serv_reset_ = pnh_.advertiseService("reset_pose",
01152     &InteractiveMarkerInterface::reset_cb, this);
01153   */
01154   if(mode_ == ""){
01155     if(registration_){
01156       mode_ = "registration";
01157     }else if(robot_mode_){
01158       mode_ = "robot";
01159     }else{
01160       mode_ = "model";
01161     }
01162   }
01163 
01164   if(mode_ == "registration"){
01165     model_menu_.insert( "Registration",
01166                         boost::bind( &UrdfModelMarker::registrationCB, this, _1) );
01167   }else if(mode_ == "visualization"){
01168 
01169   }else if(mode_ == "robot"){
01170     interactive_markers::MenuHandler::EntryHandle sub_menu_move_;
01171     sub_menu_move_ = model_menu_.insert( "Move" );
01172     model_menu_.insert( sub_menu_move_, "Joint",
01173                         boost::bind( &UrdfModelMarker::jointMoveCB, this, _1) );
01174     model_menu_.insert( sub_menu_move_, "Base",
01175                         boost::bind( &UrdfModelMarker::publishMarkerMenu, this, _1, jsk_interactive_marker::MarkerMenu::MOVE) );
01176 
01177     interactive_markers::MenuHandler::EntryHandle sub_menu_reset_;
01178     sub_menu_reset_ = model_menu_.insert( "Reset Marker Pose" );
01179     model_menu_.insert( sub_menu_reset_, "Joint",
01180                         boost::bind( &UrdfModelMarker::resetMarkerCB, this, _1) );
01181     model_menu_.insert( sub_menu_reset_, "Base",
01182                         boost::bind( &UrdfModelMarker::resetBaseMarkerCB, this, _1) );
01183 
01184 
01185     interactive_markers::MenuHandler::EntryHandle sub_menu_pose_;
01186     sub_menu_pose_ = model_menu_.insert( "Special Pose" );
01187 
01188     model_menu_.insert( sub_menu_pose_, "Stand Pose",
01189                         boost::bind( &UrdfModelMarker::publishMarkerMenu, this, _1, 100) );
01190 
01191     model_menu_.insert( sub_menu_pose_, "Manip Pose",
01192                         boost::bind( &UrdfModelMarker::publishMarkerMenu, this, _1, 101) );
01193 
01194     //model_menu_.insert( "Reset Marker Pose",
01195     //boost::bind( &UrdfModelMarker::resetMarkerCB, this, _1) );
01196 
01197 
01198     model_menu_.insert( "Hide Marker" ,
01199                         boost::bind( &UrdfModelMarker::hideMarkerCB, this, _1) );
01200     model_menu_.insert( "Hide All Marker" ,
01201                         boost::bind( &UrdfModelMarker::hideAllMarkerCB, this, _1) );
01202 
01203 
01204   }else if(mode_ == "model"){
01205     model_menu_.insert( "Grasp Point",
01206                         boost::bind( &UrdfModelMarker::graspPointCB, this, _1 ) );
01207     model_menu_.insert( "Move",
01208                         boost::bind( &UrdfModelMarker::moveCB, this, _1 ) );
01209     model_menu_.insert( "Set as present pose",
01210                         boost::bind( &UrdfModelMarker::setPoseCB, this, _1 ) );
01211     model_menu_.insert( "Hide Marker" ,
01212                         boost::bind( &UrdfModelMarker::hideMarkerCB, this, _1) );
01213     model_menu_.insert( "Hide All Marker" ,
01214                         boost::bind( &UrdfModelMarker::hideAllMarkerCB, this, _1) );
01215   }
01216 
01217   // get the entire file
01218   std::string xml_string;
01219   
01220   if(use_robot_description_){
01221     nh_.getParam(model_file_, xml_string);
01222 
01223   }else{
01224     model_file_ = getFilePathFromRosPath(model_file_);
01225     model_file_ = getFullPathFromModelPath(model_file_);
01226     std::fstream xml_file(model_file_.c_str(), std::fstream::in);
01227     while ( xml_file.good() )
01228       {
01229         std::string line;
01230         std::getline( xml_file, line);
01231         xml_string += (line + "\n");
01232       }
01233     xml_file.close();
01234 
01235     std::cout << "model_file:" << model_file_ << std::endl;
01236   }
01237   model = parseURDF(xml_string);
01238   if (!model){
01239     std::cerr << "ERROR: Model Parsing the xml failed" << std::endl;
01240     return;
01241   }
01242 
01243   if( mode_ == "robot"){
01244     resetRobotBase();
01245   }
01246 
01247 
01248   addChildLinkNames(model->getRoot(), true, true);
01249 
01250   // wait for joint\statelistner
01251 
01252   if ( pub_joint_state_.getNumSubscribers() == 0 ) {
01253     ROS_WARN_STREAM("urdf_model_marker wait for joint_state_subscriber .... ");
01254     ros::Duration(1.0).sleep();
01255   }
01256   ROS_WARN_STREAM("urdf_model_marker publish joint_state_subscriber, done");
01257 
01258   // start JointState
01259   sensor_msgs::JointState js;
01260   getJointState(model->getRoot(), js);
01261   pub_joint_state_.publish( js );
01262 
01263   return;
01264 
01265 }
01266 
01267 
01268 void UrdfModelMarker::updateDiagnostic(
01269                                                 diagnostic_updater::DiagnosticStatusWrapper &stat)
01270 {
01271   boost::mutex::scoped_lock(mutex_);
01272   stat.summary(diagnostic_msgs::DiagnosticStatus::OK, "UrdfModelMarker running");
01273   stat.add("Time to set dynamic tf (Avg.)",
01274            dynamic_tf_check_time_acc_.mean());
01275   stat.add("Time to set dynamic tf (Max)",
01276            dynamic_tf_check_time_acc_.max());
01277   stat.add("Time to set dynamic tf (Min)",
01278            dynamic_tf_check_time_acc_.min());
01279   stat.add("Time to set dynamic tf (Var.)",
01280            dynamic_tf_check_time_acc_.variance());
01281 
01282   stat.add("Time to set joint states (Avg.)",
01283            reset_joint_states_check_time_acc_.mean());
01284   stat.add("Time to set joint states (Max)",
01285            reset_joint_states_check_time_acc_.max());
01286   stat.add("Time to set joint states (Min)",
01287            reset_joint_states_check_time_acc_.min());
01288   stat.add("Time to set joint states (Var.)",
01289            reset_joint_states_check_time_acc_.variance());
01290 
01291 }


jsk_interactive_marker
Author(s): furuta
autogenerated on Mon Oct 6 2014 01:19:15