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
00031
00032
00033
00034
00035
00036
00037 #include "robot_state_chain_publisher/robot_state_publisher.h"
00038 #include <kdl/frames_io.hpp>
00039 #include <tf_conversions/tf_kdl.h>
00040
00041 using namespace std;
00042 using namespace ros;
00043 using namespace KDL;
00044
00045
00046
00047 namespace robot_state_chain_publisher{
00048
00049 RobotStatePublisher::RobotStatePublisher(const Tree& tree)
00050 :n_("~"), tree_(tree)
00051 {
00052
00053 n_.param("tf_prefix", tf_prefix_, string());
00054
00055
00056 tf_publisher_ = n_.advertise<tf::tfMessage>("/tf", 5);
00057 tf_msg_.transforms.resize(tree.getNrOfSegments()-1);
00058
00059
00060 SegmentMap::const_iterator root = tree.getRootSegment();
00061 if (root->second.children.empty())
00062 throw empty_tree_ex;
00063
00064 root_ = root->first;
00065 ignore_root = false;
00066
00067 if (root_ == "world")
00068 {
00069 ignore_root = true;
00070 root_ = (*root->second.children.begin())->first;
00071 }
00072 }
00073
00074
00075
00076 bool RobotStatePublisher::publishTransforms(map<string, RobotStatePublisher::JointState >& joint_positions, const Time& time, const ros::Time& republish_time)
00077 {
00078 int i=0;
00079 if (ignore_root)
00080 tf_msg_.transforms.resize(tree_.getNrOfSegments()-1);
00081 else
00082 tf_msg_.transforms.resize(tree_.getNrOfSegments());
00083 addChildTransforms( joint_positions, tree_.getSegment( root_ ), i, time, republish_time );
00084 tf_msg_.transforms.resize(i);
00085
00086 tf_publisher_.publish(tf_msg_);
00087
00088 return true;
00089 }
00090
00091 void RobotStatePublisher::addChildTransforms(map<string, RobotStatePublisher::JointState >& joint_positions, const SegmentMap::const_iterator segment, int &tf_index, const Time &time, const ros::Time& republish_time)
00092 {
00093 double jnt_p = 0;
00094 bool publish=true;
00095 geometry_msgs::TransformStamped trans;
00096
00097 for( vector<SegmentMap::const_iterator>::const_iterator child=segment->second.children.begin();
00098 child != segment->second.children.end(); child++ )
00099 {
00100 Time time_frame = time;
00101 if( (*child)->second.segment.getJoint().getType() != Joint::None )
00102 {
00103 map<string, RobotStatePublisher::JointState >::iterator jnt = joint_positions.find((*child)->second.segment.getJoint().getName());
00104 if (jnt == joint_positions.end()){
00105 jnt_p = 0;
00106 }
00107 else {
00108 jnt_p = jnt->second.pos;
00109
00110
00111 if( jnt->second.time < republish_time ) {
00112 jnt->second.time = time;
00113 jnt->second.published = false;
00114 }
00115
00116 time_frame = jnt->second.time;
00117 publish = ! jnt->second.published;
00118 jnt->second.published = true;
00119 }
00120 }
00121
00122
00123 if( publish ) {
00124 Frame frame = (*child)->second.segment.pose( jnt_p );
00125 tf::Transform tf_frame;
00126 tf::TransformKDLToTF(frame, tf_frame);
00127 trans.header.stamp = time_frame;
00128 trans.header.frame_id = tf::resolve(tf_prefix_, segment->first);
00129 trans.child_frame_id = tf::resolve(tf_prefix_, (*child)->first);
00130 if (trans.child_frame_id == string(""))
00131 {
00132
00133 }
00134 tf::transformTFToMsg(tf_frame, trans.transform);
00135 tf_msg_.transforms[tf_index++] = trans;
00136 }
00137
00138 addChildTransforms(joint_positions, *child, tf_index, time, republish_time);
00139 }
00140 }
00141
00142 }