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
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include "rtt_tf-component.hpp"
00049 #include <rtt/Component.hpp>
00050 #include <ros/ros.h>
00051
00052 namespace rtt_tf
00053 {
00054
00055 using namespace RTT;
00056 using namespace tf;
00057
00058 RTT_TF::RTT_TF(const std::string& name) :
00059 TaskContext(name, PreOperational),
00060 tf::Transformer(true, ros::Duration(Transformer::DEFAULT_CACHE_TIME)),
00061 prop_interpolating(true),
00062 prop_cache_time(Transformer::DEFAULT_CACHE_TIME),
00063 prop_buffer_size(DEFAULT_BUFFER_SIZE)
00064 {
00065 this->addProperty("interpolating", prop_interpolating);
00066 this->addProperty("cache_time", prop_cache_time);
00067 this->addProperty("buffer_size", prop_buffer_size);
00068 this->addProperty("tf_prefix", prop_tf_prefix);
00069 this->addEventPort("tf_in", port_tf_in);
00070 this->addPort("tf_out", port_tf_out);
00071
00072 this->addOperation("lookupTransform", &RTT_TF::lookupTransformService, this)
00073 .doc("lookup the most recent transform from source to target")
00074 .arg("target", "target frame")
00075 .arg("source", "source frame");
00076
00077 this->addOperation("lookupTransformAtTime", &RTT_TF::lookupTransformAtTimeService, this)
00078 .doc("lookup the most recent transform from source to target")
00079 .arg("target", "target frame")
00080 .arg("source", "source frame")
00081 .arg("common_time", "common time at which the transform should be computed");
00082
00083 this->addOperation("broadcastTransform", &RTT_TF::broadcastTransformService, this, RTT::OwnThread)
00084 .doc("lookup the most recent transform from source to target")
00085 .arg("stamped transform", "geometry_msgs::TransformStamped");
00086
00087 this->provides("tf")->addOperation("lookupTransform", &RTT_TF::lookupTransformService, this)
00088 .doc("lookup the most recent transform from source to target")
00089 .arg("target", "target frame")
00090 .arg("source", "source frame");
00091
00092 this->provides("tf")->addOperation("lookupTransformAtTime", &RTT_TF::lookupTransformAtTimeService, this)
00093 .doc("lookup the most recent transform from source to target")
00094 .arg("target", "target frame")
00095 .arg("source", "source frame")
00096 .arg("common_time", "common time at which the transform should be computed");
00097
00098 this->provides("tf")->addOperation("broadcastTransform", &RTT_TF::broadcastTransformService, this, RTT::OwnThread)
00099 .doc("lookup the most recent transform from source to target")
00100 .arg("stamped transform", "geometry_msgs::TransformStamped");
00101 }
00102
00103 bool RTT_TF::configureHook()
00104 {
00105 Logger::In(this->getName());
00106
00107
00108 ros::NodeHandle nh("~");
00109 std::string tf_prefix_param_key;
00110 if(nh.searchParam("tf_prefix",tf_prefix_param_key)) {
00111 nh.getParam(tf_prefix_param_key, prop_tf_prefix);
00112 }
00113
00114
00115 interpolating = prop_interpolating;
00116 cache_time = ros::Duration(prop_cache_time);
00117
00118
00119 tf_prefix_ = prop_tf_prefix;
00120
00121
00122 ConnPolicy cp = ConnPolicy::buffer(prop_buffer_size);
00123 cp.transport = 3;
00124 cp.name_id = "/tf";
00125
00126 return (port_tf_in.createStream(cp) && port_tf_out.createStream(cp));
00127 }
00128
00129 void RTT_TF::updateHook()
00130 {
00131 Logger::In(this->getName());
00132 #ifndef NDEBUG
00133 log(Debug) << "In update" << endlog();
00134 #endif
00135 try
00136 {
00137 tf::tfMessage msg_in;
00138 while (port_tf_in.read(msg_in) == NewData)
00139 {
00140 for (unsigned int i = 0; i < msg_in.transforms.size(); i++)
00141 {
00142 StampedTransform trans;
00143 transformStampedMsgToTF(msg_in.transforms[i], trans);
00144 try
00145 {
00146 std::map<std::string, std::string>* msg_header_map =
00147 msg_in.__connection_header.get();
00148 std::string authority;
00149 std::map<std::string, std::string>::iterator it =
00150 msg_header_map->find("callerid");
00151 if (it == msg_header_map->end())
00152 {
00153 log(Warning) << "Message received without callerid"
00154 << endlog();
00155 authority = "no callerid";
00156 }
00157 else
00158 {
00159 authority = it->second;
00160 }
00161 this->setTransform(trans, authority);
00162 } catch (TransformException& ex)
00163 {
00164
00165 log(Error) << "Failure to set received transform from "
00166 << msg_in.transforms[i].child_frame_id << " to "
00167 << msg_in.transforms[i].header.frame_id
00168 << " with error: " << ex.what() << endlog();
00169 }
00170 }
00171 }
00172
00173 } catch (std::exception& ex)
00174 {
00175 log(Error) << ex.what() << endlog();
00176 }
00177 }
00178
00179 geometry_msgs::TransformStamped RTT_TF::lookupTransformService(
00180 const std::string& target,
00181 const std::string& source)
00182 {
00183 tf::StampedTransform stamped_tf;
00184 ros::Time common_time;
00185 this->getLatestCommonTime(source, target, common_time,NULL);
00186 this->lookupTransform(target, source, common_time, stamped_tf);
00187 geometry_msgs::TransformStamped msg;
00188 tf::transformStampedTFToMsg(stamped_tf,msg);
00189 return msg;
00190 }
00191
00192 geometry_msgs::TransformStamped RTT_TF::lookupTransformAtTimeService(
00193 const std::string& target,
00194 const std::string& source,
00195 const ros::Time& common_time)
00196 {
00197 tf::StampedTransform stamped_tf;
00198 this->lookupTransform(target, source, common_time, stamped_tf);
00199 geometry_msgs::TransformStamped msg;
00200 tf::transformStampedTFToMsg(stamped_tf,msg);
00201 return msg;
00202 }
00203
00204 void RTT_TF::broadcastTransformService(
00205 const geometry_msgs::TransformStamped &tform)
00206 {
00207
00208 tf::tfMessage msg_out;
00209 msg_out.transforms.push_back(tform);
00210
00211
00212 msg_out.transforms.back().header.frame_id = tf::resolve(prop_tf_prefix, msg_out.transforms.back().header.frame_id);
00213 msg_out.transforms.back().child_frame_id = tf::resolve(prop_tf_prefix, msg_out.transforms.back().child_frame_id);
00214
00215 port_tf_out.write(msg_out);
00216 }
00217
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 ORO_CREATE_COMPONENT(rtt_tf::RTT_TF)