-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathrtt_tf-component.cpp
375 lines (326 loc) · 14.1 KB
/
rtt_tf-component.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* (C) 2011, Ruben Smits, ruben.smits@mech.kuleuven.be
* Department of Mechanical Engineering,
* Katholieke Universiteit Leuven, Belgium.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "rtt_tf-component.hpp"
#include <rtt/Component.hpp>
#include <rtt/Logger.hpp>
#include <ros/ros.h>
#include <tf/tf.h>
#include <tf2/exceptions.h>
#include <geometry_msgs/TransformStamped.h>
#include <algorithm>
namespace rtt_tf
{
// Functor for resolving the tf prefix when broadcasting multiple transforms.
class PrefixResolver
{
public:
PrefixResolver(const std::string& prefix) : prefix_(prefix) { }
geometry_msgs::TransformStamped operator()(const geometry_msgs::TransformStamped& elem)
{
geometry_msgs::TransformStamped result = elem;
result.header.frame_id = tf::resolve(prefix_, result.header.frame_id);
result.child_frame_id = tf::resolve(prefix_, result.child_frame_id);
return result;
}
private:
const std::string& prefix_;
};
tf2_msgs::TFMessage transformsToMessage(const std::vector<geometry_msgs::TransformStamped>& tforms, const std::string& prefix)
{
tf2_msgs::TFMessage msg;
// resolve names and copy transforms to message
msg.transforms.resize(tforms.size());
std::transform(tforms.begin(), tforms.end(), msg.transforms.begin(), PrefixResolver(prefix));
return msg;
}
using namespace RTT;
RTT_TF::RTT_TF(const std::string& name) :
TaskContext(name, PreOperational),
tf2::BufferCore( ros::Duration(BufferCore::DEFAULT_CACHE_TIME) ),
prop_cache_time( BufferCore::DEFAULT_CACHE_TIME ),
prop_buffer_size(DEFAULT_BUFFER_SIZE)
{
this->addProperty("cache_time", prop_cache_time);
this->addProperty("buffer_size", prop_buffer_size);
this->addProperty("tf_prefix", prop_tf_prefix);
this->addEventPort("tf_in", port_tf_in);
this->addEventPort("tf_static_in", port_tf_static_in);
this->addPort("tf_out", port_tf_out);
this->addTFOperations(this->provides());
this->addTFOperations(this->provides("tf"));
}
void RTT_TF::addTFOperations(RTT::Service::shared_ptr service)
{
service->addOperation("lookupTransform", &RTT_TF::lookupTransform, this)
.doc("Lookup the most recent transform from source to target.")
.arg("target", "target frame")
.arg("source", "source frame");
service->addOperation("lookupTransformAtTime", &RTT_TF::lookupTransformAtTime, this)
.doc("Lookup the most recent transform from source to target at a specific time.")
.arg("target", "Target frame")
.arg("source", "Source frame")
.arg("common_time", "[ros::Time] The common time at which the transform should be computed");
service->addOperation("broadcastTransform", &RTT_TF::broadcastTransform, this, RTT::OwnThread)
.doc("Broadcast a stamped transform immediately.")
.arg("transform", "[geometry_msgs::TransformStamped]");
service->addOperation("broadcastTransforms", &RTT_TF::broadcastTransforms, this, RTT::OwnThread)
.doc("Broadcast stamped transforms immediately.")
.arg("transforms", "[std::vector<geometry_msgs::TransformStamped>]");
service->addOperation("broadcastStaticTransform", &RTT_TF::broadcastStaticTransform, this, RTT::OwnThread)
.doc("Broadcast a stamped transform as a static transform immediately.")
.arg("transform", "[geometry_msgs::TransformStamped]");
service->addOperation("broadcastStaticTransforms", &RTT_TF::broadcastStaticTransforms, this, RTT::OwnThread)
.doc("Broadcast stamped transforms as static transforms immediately.")
.arg("transforms", "[std::vector<geometry_msgs::TransformStamped>]");
service->addOperation("canTransform", &RTT_TF::canTransform, this)
.doc("Check if the transform from source to target can be resolved.")
.arg("target", "Target frame")
.arg("source", "Source frame");
service->addOperation("canTransformAtTime", &RTT_TF::canTransformAtTime, this)
.doc("Check if the transform from source to target can be resolved for a given common time.")
.arg("target", "Target frame")
.arg("source", "Source frame")
.arg("common_time", "[ros::Time] The common time for which the transform would resolve");
service->addOperation("subscribeTransform", &RTT_TF::subscribeTransfrom, this)
.doc("Tracks a transformation and reprots the new transforms via a port")
.arg("target", "Target frame id")
.arg("source", "Source frame id");
service->addOperation("listTrackers", &RTT_TF::listTrackers, this)
.doc("Lists the TF trackers added via subscribeTransfrom()");
}
bool RTT_TF::configureHook()
{
Logger::In(this->getName());
// Get tf prefix rosparam
ros::NodeHandle nh("~");
std::string tf_prefix_param_key;
if(nh.searchParam("tf_prefix",tf_prefix_param_key)) {
nh.getParam(tf_prefix_param_key, prop_tf_prefix);
}
// Connect to tf topic
ConnPolicy cp = ConnPolicy::buffer(prop_buffer_size);
cp.transport = 3; //3=ROS
cp.name_id = "/tf";
// Connect to tf_static topic
ConnPolicy cp_static = ConnPolicy::buffer(prop_buffer_size);
cp_static.transport = 3; //3=ROS
cp_static.name_id = "/tf_static";
bool configured = port_tf_static_in.createStream(cp_static)
&& port_tf_in.createStream(cp)
&& port_tf_out.createStream(cp)
&& port_tf_static_out.createStream(cp_static);
if (!configured) {
cleanupHook();
}
return configured;
}
void RTT_TF::internalUpdate(tf2_msgs::TFMessage& msg, RTT::InputPort<tf2_msgs::TFMessage>& port, bool is_static)
{
// tf2::BufferCore::setTransform (see #68) has a non-defaulted authority argument,
// but there is no __connection_header to extract it from.
const std::string authority = "unknown_authority";
while (port.read(msg) == NewData) {
for (std::size_t i = 0; i < msg.transforms.size(); ++i) {
try {
this->setTransform(msg.transforms[i], authority, is_static);
} catch (tf2::TransformException& ex) {
log(Error) << "Failure to set received transform from "
<< msg.transforms[i].child_frame_id << " to "
<< msg.transforms[i].header.frame_id
<< " with error: " << ex.what() << endlog();
}
}
}
// Publish the geometry messages obtained from the lookupTransform on
// tracked transformations
// Range for not supported in ISO pre-C++ 11
// for (const auto& tracker : ports_trackers) {
// auto msg = lookupTransform(tracker.first.first, tracker.first.second);
// tracker.second->write(msg);
// }
for(std::map<std::pair<std::string, std::string>, OutputPortGeometryTransfromStampedPtr>::iterator
it_tracker = ports_trackers.begin(); it_tracker != ports_trackers.end(); ++it_tracker) {
geometry_msgs::TransformStamped msg = lookupTransform(it_tracker->first.first, it_tracker->first.second);
it_tracker->second->write(msg);
}
}
bool RTT_TF::startHook()
{
if (0 == getPeriod()) {
RTT::log(RTT::Warning) << "The period of the component is 0 (zero), so no"
" updates form TF will be published automatically" << RTT::endlog();
}
buffer_core.reset(new tf2::BufferCore());
transform_listener = TransformListenerPtr(new tf2_ros::TransformListener(*buffer_core));
return true;
}
void RTT_TF::updateHook()
{
Logger::In(this->getName());
#ifndef NDEBUG
//log(Debug) << "In update" << endlog();
#endif
try
{
tf2_msgs::TFMessage msg_in;
internalUpdate(msg_in, port_tf_in, false);
internalUpdate(msg_in, port_tf_static_in, true);
}
catch (std::exception& ex)
{
log(Error) << ex.what() << endlog();
}
}
void RTT_TF::stopHook()
{
transform_listener.reset();
buffer_core.reset();
}
void RTT_TF::cleanupHook()
{
port_tf_in.disconnect();
port_tf_out.disconnect();
port_tf_static_in.disconnect();
port_tf_static_out.disconnect();
}
ros::Time RTT_TF::getLatestCommonTime(
const std::string& target,
const std::string& source) const
{
ros::Time common_time;
tf2::CompactFrameID target_id = _lookupFrameNumber(target);
tf2::CompactFrameID source_id = _lookupFrameNumber(source);
_getLatestCommonTime(source_id, target_id, common_time, NULL);
return common_time;
}
geometry_msgs::TransformStamped RTT_TF::lookupTransform(
const std::string& target,
const std::string& source) const
{
return tf2::BufferCore::lookupTransform(target, source, ros::Time());
}
bool RTT_TF::canTransform(
const std::string& target,
const std::string& source) const
{
return tf2::BufferCore::canTransform(target, source, ros::Time());
}
bool RTT_TF::canTransformAtTime(
const std::string& target,
const std::string& source,
const ros::Time& common_time) const
{
return tf2::BufferCore::canTransform(target, source, common_time);
}
geometry_msgs::TransformStamped RTT_TF::lookupTransformAtTime(
const std::string& target,
const std::string& source,
const ros::Time& common_time) const
{
return tf2::BufferCore::lookupTransform(target, source, common_time);
}
void RTT_TF::broadcastTransform(const geometry_msgs::TransformStamped& tform)
{
const std::vector<geometry_msgs::TransformStamped> tforms(1, tform);
tf2_msgs::TFMessage msg_out = transformsToMessage(tforms, prop_tf_prefix);
port_tf_out.write(msg_out);
}
void RTT_TF::broadcastTransforms(const std::vector<geometry_msgs::TransformStamped>& tform)
{
tf2_msgs::TFMessage msg_out = transformsToMessage(tform, prop_tf_prefix);
port_tf_out.write(msg_out);
}
void RTT_TF::broadcastStaticTransform(const geometry_msgs::TransformStamped& tform)
{
const std::vector<geometry_msgs::TransformStamped> tforms(1, tform);
tf2_msgs::TFMessage msg_out = transformsToMessage(tforms, prop_tf_prefix);
port_tf_static_out.write(msg_out);
}
void RTT_TF::broadcastStaticTransforms(const std::vector<geometry_msgs::TransformStamped>& tform)
{
tf2_msgs::TFMessage msg_out = transformsToMessage(tform, prop_tf_prefix);
port_tf_static_out.write(msg_out);
}
bool RTT_TF::subscribeTransfrom(const std::string& target, const std::string& source)
{
Logger::In(this->getName());
const std::pair<std::string, std::string> transfrom_pair(target, source);
const std::string name_transform = source + "_" + target;
const std::string name_port = "tracker_tf_" + name_transform;
if (ports_trackers.find(transfrom_pair) != ports_trackers.end()) {
RTT::log(RTT::Warning) << "The transfrom from " << source << " to " << target <<
" is already tracked." << RTT::endlog();
return false;
}
if (!canTransform(target, source)) {
RTT::log(RTT::Warning) << "The transformation from " << source <<
" to " << target << " cannot yet be performed" << RTT::endlog();
}
ports_trackers[transfrom_pair] =
boost::make_shared<RTT::OutputPort<geometry_msgs::TransformStamped> >(name_port);
this->addPort(*ports_trackers[transfrom_pair])
.doc("Port generated by " + getName() + " for the transformation: " + name_transform);
return true;
}
void RTT_TF::listTrackers()
{
Logger::In(this->getName());
const RTT::Logger::LogLevel config_level = RTT::Logger::Instance()->getLogLevel();
RTT::Logger::Instance()->setLogLevel(RTT::Logger::Info);
RTT::log() << "Listing existing trackers" << RTT::endlog();
RTT::log(RTT::Info) << "(Source) <-> (Target) : [PortName]" << RTT::endlog();
RTT::log(RTT::Info) << "----------------------------------" << RTT::endlog();
// ISO C++ doesnt allow the next for, so need to use iterator
// for (const auto& entry : ports_trackers) {
// RTT::log(RTT::Info) << entry.first.second << " <-> " << entry.first.first <<
// " : [" <<entry.second->getName() << "]" << RTT::endlog();
// }
for(std::map<std::pair<std::string, std::string>, OutputPortGeometryTransfromStampedPtr>::iterator
it = ports_trackers.begin(); it != ports_trackers.end(); ++it) {
RTT::log(RTT::Info) << it->first.second << " <-> " << it->first.first <<
" : [" << it->second->getName() << "]" << RTT::endlog();
}
RTT::Logger::Instance()->setLogLevel(config_level);
}
}//namespace
/*
* Using this macro, only one component may live
* in one library *and* you may *not* link this library
* with another component library. Use
* ORO_CREATE_COMPONENT_TYPE()
* ORO_LIST_COMPONENT_TYPE(Rtt_tf)
* In case you want to link with another library that
* already contains components.
*
* If you have put your component class
* in a namespace, don't forget to add it here too:
*/
ORO_CREATE_COMPONENT(rtt_tf::RTT_TF)