-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathtf.cpp
581 lines (467 loc) · 19.4 KB
/
tf.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
* Copyright (c) 2008, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of the Willow Garage, Inc. 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 OWNER 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.
*/
/** \author Tully Foote */
#include "tf/tf.h"
#include <sys/time.h>
#include "ros/assert.h"
#include "ros/ros.h"
#include <angles/angles.h>
using namespace tf;
// Must provide storage for non-integral static const class members.
// Otherwise you get undefined symbol errors on OS X (why not on Linux?).
// Thanks to Rob for pointing out the right way to do this.
// In C++0x this must be initialized here #5401
const double tf::Transformer::DEFAULT_CACHE_TIME = 10.0;
enum WalkEnding
{
Identity,
TargetParentOfSource,
SourceParentOfTarget,
FullPath,
};
struct CanTransformAccum
{
CompactFrameID gather(TimeCache* cache, ros::Time time, std::string* error_string)
{
return cache->getParent(time, error_string);
}
void accum(bool source)
{
}
void finalize(WalkEnding end, ros::Time _time)
{
}
TransformStorage st;
};
struct TransformAccum
{
TransformAccum()
: source_to_top_quat(0.0, 0.0, 0.0, 1.0)
, source_to_top_vec(0.0, 0.0, 0.0)
, target_to_top_quat(0.0, 0.0, 0.0, 1.0)
, target_to_top_vec(0.0, 0.0, 0.0)
, result_quat(0.0, 0.0, 0.0, 1.0)
, result_vec(0.0, 0.0, 0.0)
{
}
CompactFrameID gather(TimeCache* cache, ros::Time time, std::string* error_string)
{
if (!cache->getData(time, st, error_string))
{
return 0;
}
return st.frame_id_;
}
void accum(bool source)
{
if (source)
{
source_to_top_vec = quatRotate(st.rotation_, source_to_top_vec) + st.translation_;
source_to_top_quat = st.rotation_ * source_to_top_quat;
}
else
{
target_to_top_vec = quatRotate(st.rotation_, target_to_top_vec) + st.translation_;
target_to_top_quat = st.rotation_ * target_to_top_quat;
}
}
void finalize(WalkEnding end, ros::Time _time)
{
switch (end)
{
case Identity:
break;
case TargetParentOfSource:
result_vec = source_to_top_vec;
result_quat = source_to_top_quat;
break;
case SourceParentOfTarget:
{
tf::Quaternion inv_target_quat = target_to_top_quat.inverse();
tf::Vector3 inv_target_vec = quatRotate(inv_target_quat, -target_to_top_vec);
result_vec = inv_target_vec;
result_quat = inv_target_quat;
break;
}
case FullPath:
{
tf::Quaternion inv_target_quat = target_to_top_quat.inverse();
tf::Vector3 inv_target_vec = quatRotate(inv_target_quat, -target_to_top_vec);
result_vec = quatRotate(inv_target_quat, source_to_top_vec) + inv_target_vec;
result_quat = inv_target_quat * source_to_top_quat;
}
break;
};
time = _time;
}
TransformStorage st;
ros::Time time;
tf::Quaternion source_to_top_quat;
tf::Vector3 source_to_top_vec;
tf::Quaternion target_to_top_quat;
tf::Vector3 target_to_top_vec;
tf::Quaternion result_quat;
tf::Vector3 result_vec;
};
std::string assert_resolved(const std::string& prefix, const std::string& frame_id)
{
ROS_DEBUG("tf::assert_resolved just calls tf::resolve");
return tf::resolve(prefix, frame_id);
};
std::string tf::resolve(const std::string& prefix, const std::string& frame_name)
{
// printf ("resolveping prefix:%s with frame_name:%s\n", prefix.c_str(), frame_name.c_str());
if (frame_name.size() > 0)
if (frame_name[0] == '/')
{
return strip_leading_slash(frame_name);
}
if (prefix.size() > 0)
{
if (prefix[0] == '/')
{
std::string composite = strip_leading_slash(prefix);
composite.append("/");
composite.append(frame_name);
return composite;
}
else
{
std::string composite;
composite.append(prefix);
composite.append("/");
composite.append(frame_name);
return composite;
}
}
else
{
std::string composite;
composite.append(frame_name);
return composite;
}
};
std::string tf::strip_leading_slash(const std::string& frame_name)
{
if (frame_name.size() > 0)
if (frame_name[0] == '/')
{
std::string shorter = frame_name;
shorter.erase(0,1);
return shorter;
}
return frame_name;
}
Transformer::Transformer(bool interpolating,
ros::Duration cache_time):
fall_back_to_wall_time_(false),
tf2_buffer_(cache_time)
{
}
Transformer::~Transformer()
{
};
void Transformer::clear()
{
tf2_buffer_.clear();
}
bool Transformer::setTransform(const StampedTransform& transform, const std::string& authority)
{
geometry_msgs::TransformStamped msgtf;
transformStampedTFToMsg(transform, msgtf);
return tf2_buffer_.setTransform(msgtf, authority);
};
void Transformer::lookupTransform(const std::string& target_frame, const std::string& source_frame,
const ros::Time& time, StampedTransform& transform) const
{
geometry_msgs::TransformStamped output =
tf2_buffer_.lookupTransform(strip_leading_slash(target_frame),
strip_leading_slash(source_frame), time);
transformStampedMsgToTF(output, transform);
return;
};
void Transformer::lookupTransform(const std::string& target_frame,const ros::Time& target_time, const std::string& source_frame,
const ros::Time& source_time, const std::string& fixed_frame, StampedTransform& transform) const
{
geometry_msgs::TransformStamped output =
tf2_buffer_.lookupTransform(strip_leading_slash(target_frame), target_time,
strip_leading_slash(source_frame), source_time,
strip_leading_slash(fixed_frame));
transformStampedMsgToTF(output, transform);
};
void Transformer::lookupTwist(const std::string& tracking_frame, const std::string& observation_frame,
const ros::Time& time, const ros::Duration& averaging_interval,
geometry_msgs::Twist& twist) const
{
// ref point is origin of tracking_frame, ref_frame = obs_frame
lookupTwist(tracking_frame, observation_frame, observation_frame, tf::Point(0,0,0), tracking_frame, time, averaging_interval, twist);
};
void Transformer::lookupTwist(const std::string& tracking_frame, const std::string& observation_frame, const std::string& reference_frame,
const tf::Point & reference_point, const std::string& reference_point_frame,
const ros::Time& time, const ros::Duration& averaging_interval,
geometry_msgs::Twist& twist) const
{
ros::Time latest_time, target_time;
getLatestCommonTime(observation_frame, tracking_frame, latest_time, NULL); ///\TODO check time on reference point too
if (ros::Time() == time)
target_time = latest_time;
else
target_time = time;
ros::Time end_time = std::min(target_time + averaging_interval *0.5 , latest_time);
ros::Time start_time = std::max(ros::Time().fromSec(.00001) + averaging_interval, end_time) - averaging_interval; // don't collide with zero
ros::Duration corrected_averaging_interval = end_time - start_time; //correct for the possiblity that start time was truncated above.
StampedTransform start, end;
lookupTransform(observation_frame, tracking_frame, start_time, start);
lookupTransform(observation_frame, tracking_frame, end_time, end);
tf::Matrix3x3 temp = start.getBasis().inverse() * end.getBasis();
tf::Quaternion quat_temp;
temp.getRotation(quat_temp);
tf::Vector3 o = start.getBasis() * quat_temp.getAxis();
tfScalar ang = quat_temp.getAngle();
double delta_x = end.getOrigin().getX() - start.getOrigin().getX();
double delta_y = end.getOrigin().getY() - start.getOrigin().getY();
double delta_z = end.getOrigin().getZ() - start.getOrigin().getZ();
tf::Vector3 twist_vel ((delta_x)/corrected_averaging_interval.toSec(),
(delta_y)/corrected_averaging_interval.toSec(),
(delta_z)/corrected_averaging_interval.toSec());
tf::Vector3 twist_rot = o * (ang / corrected_averaging_interval.toSec());
// This is a twist w/ reference frame in observation_frame and reference point is in the tracking_frame at the origin (at start_time)
//correct for the position of the reference frame
tf::StampedTransform inverse;
lookupTransform(reference_frame,tracking_frame, target_time, inverse);
tf::Vector3 out_rot = inverse.getBasis() * twist_rot;
tf::Vector3 out_vel = inverse.getBasis()* twist_vel + inverse.getOrigin().cross(out_rot);
//Rereference the twist about a new reference point
// Start by computing the original reference point in the reference frame:
tf::Stamped<tf::Point> rp_orig(tf::Point(0,0,0), target_time, tracking_frame);
transformPoint(reference_frame, rp_orig, rp_orig);
// convert the requrested reference point into the right frame
tf::Stamped<tf::Point> rp_desired(reference_point, target_time, reference_point_frame);
transformPoint(reference_frame, rp_desired, rp_desired);
// compute the delta
tf::Point delta = rp_desired - rp_orig;
// Correct for the change in reference point
out_vel = out_vel + out_rot * delta;
// out_rot unchanged
/*
printf("KDL: Rotation %f %f %f, Translation:%f %f %f\n",
out_rot.x(),out_rot.y(),out_rot.z(),
out_vel.x(),out_vel.y(),out_vel.z());
*/
twist.linear.x = out_vel.x();
twist.linear.y = out_vel.y();
twist.linear.z = out_vel.z();
twist.angular.x = out_rot.x();
twist.angular.y = out_rot.y();
twist.angular.z = out_rot.z();
};
bool Transformer::waitForTransform(const std::string& target_frame, const std::string& source_frame,
const ros::Time& time,
const ros::Duration& timeout, const ros::Duration& polling_sleep_duration,
std::string* error_msg) const
{
return tf2_buffer_.canTransform(strip_leading_slash(target_frame),
strip_leading_slash(source_frame), time, timeout, error_msg);
}
bool Transformer::canTransform(const std::string& target_frame, const std::string& source_frame,
const ros::Time& time, std::string* error_msg) const
{
return tf2_buffer_.canTransform(strip_leading_slash(target_frame),
strip_leading_slash(source_frame), time, error_msg);
}
bool Transformer::canTransform(const std::string& target_frame,const ros::Time& target_time, const std::string& source_frame,
const ros::Time& source_time, const std::string& fixed_frame,
std::string* error_msg) const
{
return tf2_buffer_.canTransform(strip_leading_slash(target_frame), target_time,
strip_leading_slash(source_frame), source_time,
strip_leading_slash(fixed_frame), error_msg);
};
bool Transformer::waitForTransform(const std::string& target_frame,const ros::Time& target_time, const std::string& source_frame,
const ros::Time& source_time, const std::string& fixed_frame,
const ros::Duration& timeout, const ros::Duration& polling_sleep_duration,
std::string* error_msg) const
{
return tf2_buffer_.canTransform(strip_leading_slash(target_frame), target_time,
strip_leading_slash(source_frame), source_time,
strip_leading_slash(fixed_frame), timeout, error_msg);
};
bool Transformer::getParent(const std::string& frame_id, ros::Time time, std::string& parent) const
{
return tf2_buffer_._getParent(frame_id, time, parent);
};
bool Transformer::frameExists(const std::string& frame_id_str) const
{
return tf2_buffer_._frameExists(frame_id_str);
}
void Transformer::setExtrapolationLimit(const ros::Duration& distance)
{
ROS_WARN("Transformer::setExtrapolationLimit is deprecated and does not do anything");
}
struct TimeAndFrameIDFrameComparator
{
TimeAndFrameIDFrameComparator(CompactFrameID id)
: id(id)
{}
bool operator()(const P_TimeAndFrameID& rhs) const
{
return rhs.second == id;
}
CompactFrameID id;
};
int Transformer::getLatestCommonTime(const std::string &source_frame, const std::string &target_frame, ros::Time& time, std::string* error_string) const
{
CompactFrameID target_id = tf2_buffer_._lookupFrameNumber(strip_leading_slash(target_frame));
CompactFrameID source_id = tf2_buffer_._lookupFrameNumber(strip_leading_slash(source_frame));
return tf2_buffer_._getLatestCommonTime(source_id, target_id, time, error_string);
}
//@todo - Fix this to work with new data structures
void Transformer::chainAsVector(const std::string & target_frame, ros::Time target_time, const std::string & source_frame, ros::Time source_time, const std::string& fixed_frame, std::vector<std::string>& output) const
{
tf2_buffer_._chainAsVector(target_frame, target_time,
source_frame, source_time,
fixed_frame, output);
}
std::string Transformer::allFramesAsString() const
{
return tf2_buffer_.allFramesAsString();
}
std::string Transformer::allFramesAsDot() const
{
return tf2_buffer_._allFramesAsDot();
}
bool Transformer::ok() const { return true; }
void Transformer::getFrameStrings(std::vector<std::string> & vec) const
{
tf2_buffer_._getFrameStrings(vec);
}
void Transformer::transformQuaternion(const std::string& target_frame, const Stamped<Quaternion>& stamped_in, Stamped<Quaternion>& stamped_out) const
{
tf::assertQuaternionValid(stamped_in);
StampedTransform transform;
lookupTransform(target_frame, stamped_in.frame_id_, stamped_in.stamp_, transform);
stamped_out.setData( transform * stamped_in);
stamped_out.stamp_ = transform.stamp_;
stamped_out.frame_id_ = target_frame;
};
void Transformer::transformVector(const std::string& target_frame,
const Stamped<tf::Vector3>& stamped_in,
Stamped<tf::Vector3>& stamped_out) const
{
StampedTransform transform;
lookupTransform(target_frame, stamped_in.frame_id_, stamped_in.stamp_, transform);
/** \todo may not be most efficient */
tf::Vector3 end = stamped_in;
tf::Vector3 origin = tf::Vector3(0,0,0);
tf::Vector3 output = (transform * end) - (transform * origin);
stamped_out.setData( output);
stamped_out.stamp_ = transform.stamp_;
stamped_out.frame_id_ = target_frame;
};
void Transformer::transformPoint(const std::string& target_frame, const Stamped<Point>& stamped_in, Stamped<Point>& stamped_out) const
{
StampedTransform transform;
lookupTransform(target_frame, stamped_in.frame_id_, stamped_in.stamp_, transform);
stamped_out.setData(transform * stamped_in);
stamped_out.stamp_ = transform.stamp_;
stamped_out.frame_id_ = target_frame;
};
void Transformer::transformPose(const std::string& target_frame, const Stamped<Pose>& stamped_in, Stamped<Pose>& stamped_out) const
{
StampedTransform transform;
lookupTransform(target_frame, stamped_in.frame_id_, stamped_in.stamp_, transform);
stamped_out.setData(transform * stamped_in);
stamped_out.stamp_ = transform.stamp_;
stamped_out.frame_id_ = target_frame;
};
void Transformer::transformQuaternion(const std::string& target_frame, const ros::Time& target_time,
const Stamped<Quaternion>& stamped_in,
const std::string& fixed_frame,
Stamped<Quaternion>& stamped_out) const
{
tf::assertQuaternionValid(stamped_in);
StampedTransform transform;
lookupTransform(target_frame, target_time,
stamped_in.frame_id_,stamped_in.stamp_,
fixed_frame, transform);
stamped_out.setData( transform * stamped_in);
stamped_out.stamp_ = transform.stamp_;
stamped_out.frame_id_ = target_frame;
};
void Transformer::transformVector(const std::string& target_frame, const ros::Time& target_time,
const Stamped<Vector3>& stamped_in,
const std::string& fixed_frame,
Stamped<Vector3>& stamped_out) const
{
StampedTransform transform;
lookupTransform(target_frame, target_time,
stamped_in.frame_id_,stamped_in.stamp_,
fixed_frame, transform);
/** \todo may not be most efficient */
tf::Vector3 end = stamped_in;
tf::Vector3 origin = tf::Vector3(0,0,0);
tf::Vector3 output = (transform * end) - (transform * origin);
stamped_out.setData( output);
stamped_out.stamp_ = transform.stamp_;
stamped_out.frame_id_ = target_frame;
};
void Transformer::transformPoint(const std::string& target_frame, const ros::Time& target_time,
const Stamped<Point>& stamped_in,
const std::string& fixed_frame,
Stamped<Point>& stamped_out) const
{
StampedTransform transform;
lookupTransform(target_frame, target_time,
stamped_in.frame_id_,stamped_in.stamp_,
fixed_frame, transform);
stamped_out.setData(transform * stamped_in);
stamped_out.stamp_ = transform.stamp_;
stamped_out.frame_id_ = target_frame;
};
void Transformer::transformPose(const std::string& target_frame, const ros::Time& target_time,
const Stamped<Pose>& stamped_in,
const std::string& fixed_frame,
Stamped<Pose>& stamped_out) const
{
StampedTransform transform;
lookupTransform(target_frame, target_time,
stamped_in.frame_id_,stamped_in.stamp_,
fixed_frame, transform);
stamped_out.setData(transform * stamped_in);
stamped_out.stamp_ = transform.stamp_;
stamped_out.frame_id_ = target_frame;
};
boost::signals::connection Transformer::addTransformsChangedListener(boost::function<void(void)> callback)
{
return tf2_buffer_._addTransformsChangedListener(callback);
}
void Transformer::removeTransformsChangedListener(boost::signals::connection c)
{
tf2_buffer_._removeTransformsChangedListener(c);
}