tf.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
32 #ifndef TF_TF_H
33 #define TF_TF_H
34 
35 #include <iostream>
36 #include <iomanip>
37 #include <cmath>
38 #include <vector>
39 #include <sstream>
40 #include <map>
41 #include <memory>
42 
43 #include <tf/exceptions.h>
44 #include "tf/time_cache.h"
45 #include <boost/unordered_map.hpp>
46 #include <boost/signals2.hpp>
47 #include "geometry_msgs/TwistStamped.h"
48 
49 #include <tf2_ros/buffer.h>
50 
51 namespace tf
52 {
54 std::string resolve(const std::string& prefix, const std::string& frame_name);
55 
57 std::string strip_leading_slash(const std::string& frame_name);
58 
60 __attribute__((deprecated)) static inline std::string remap(const std::string& prefix, const std::string& frame_name) { return tf::resolve(prefix, frame_name);} ;
61 
63 
67 typedef struct
68 {
69  std::vector<TransformStorage > inverseTransforms;
70  std::vector<TransformStorage > forwardTransforms;
72 
92 {
93 public:
94  /************* Constants ***********************/
95  static const unsigned int MAX_GRAPH_DEPTH = 100UL;
96  static const double DEFAULT_CACHE_TIME;
97  static const int64_t DEFAULT_MAX_EXTRAPOLATION_DISTANCE = 0ULL;
98 
99 
105  Transformer(bool interpolating = true,
106  ros::Duration cache_time_ = ros::Duration(DEFAULT_CACHE_TIME));
107  virtual ~Transformer(void);
108 
110  void clear();
111 
117  bool setTransform(const StampedTransform& transform, const std::string & authority = "default_authority");
118 
119  /*********** Accessors *************/
120 
130  void lookupTransform(const std::string& target_frame, const std::string& source_frame,
131  const ros::Time& time, StampedTransform& transform) const;
143  void lookupTransform(const std::string& target_frame, const ros::Time& target_time,
144  const std::string& source_frame, const ros::Time& source_time,
145  const std::string& fixed_frame, StampedTransform& transform) const;
146 
167  void lookupTwist(const std::string& tracking_frame, const std::string& observation_frame, const std::string& reference_frame,
168  const tf::Point & reference_point, const std::string& reference_point_frame,
169  const ros::Time& time, const ros::Duration& averaging_interval,
170  geometry_msgs::Twist& twist) const;
171 
182  void lookupTwist(const std::string& tracking_frame, const std::string& observation_frame,
183  const ros::Time& time, const ros::Duration& averaging_interval,
184  geometry_msgs::Twist& twist) const;
185 
194  bool waitForTransform(const std::string& target_frame, const std::string& source_frame,
195  const ros::Time& time, const ros::Duration& timeout, const ros::Duration& polling_sleep_duration = ros::Duration(0.01),
196  std::string* error_msg = NULL) const;
203  bool canTransform(const std::string& target_frame, const std::string& source_frame,
204  const ros::Time& time,
205  std::string* error_msg = NULL) const;
206 
215  bool canTransform(const std::string& target_frame, const ros::Time& target_time,
216  const std::string& source_frame, const ros::Time& source_time,
217  const std::string& fixed_frame,
218  std::string* error_msg = NULL) const;
219 
230  bool waitForTransform(const std::string& target_frame, const ros::Time& target_time,
231  const std::string& source_frame, const ros::Time& source_time,
232  const std::string& fixed_frame,
233  const ros::Duration& timeout, const ros::Duration& polling_sleep_duration = ros::Duration(0.01),
234  std::string* error_msg = NULL) const;
235 
238  int getLatestCommonTime(const std::string &source_frame, const std::string &target_frame, ros::Time& time, std::string* error_string) const;
239 
242  void transformQuaternion(const std::string& target_frame, const Stamped<tf::Quaternion>& stamped_in, Stamped<tf::Quaternion>& stamped_out) const;
245  void transformVector(const std::string& target_frame, const Stamped<tf::Vector3>& stamped_in, Stamped<tf::Vector3>& stamped_out) const;
248  void transformPoint(const std::string& target_frame, const Stamped<tf::Point>& stamped_in, Stamped<tf::Point>& stamped_out) const;
251  void transformPose(const std::string& target_frame, const Stamped<tf::Pose>& stamped_in, Stamped<tf::Pose>& stamped_out) const;
252 
255  void transformQuaternion(const std::string& target_frame, const ros::Time& target_time,
256  const Stamped<tf::Quaternion>& stamped_in,
257  const std::string& fixed_frame,
258  Stamped<tf::Quaternion>& stamped_out) const;
261  void transformVector(const std::string& target_frame, const ros::Time& target_time,
262  const Stamped<tf::Vector3>& stamped_in,
263  const std::string& fixed_frame,
264  Stamped<tf::Vector3>& stamped_out) const;
267  void transformPoint(const std::string& target_frame, const ros::Time& target_time,
268  const Stamped<tf::Point>& stamped_in,
269  const std::string& fixed_frame,
270  Stamped<tf::Point>& stamped_out) const;
273  void transformPose(const std::string& target_frame, const ros::Time& target_time,
274  const Stamped<tf::Pose>& stamped_in,
275  const std::string& fixed_frame,
276  Stamped<tf::Pose>& stamped_out) const;
277 
282  //std::string chainAsString(const std::string & target_frame, ros::Time target_time, const std::string & source_frame, ros::Time source_time, const std::string & fixed_frame) const;
283 
288  void 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;
289 
293  std::string allFramesAsString() const;
294 
298  std::string allFramesAsDot(double current_time = 0) const;
299 
301  void getFrameStrings(std::vector<std::string>& ids) const;
302 
307  bool getParent(const std::string& frame_id, ros::Time time, std::string& parent) const;
308 
311  bool frameExists(const std::string& frame_id_str) const;
312 
317 
319  ros::Duration getCacheLength() { return tf2_buffer_ptr_->getCacheLength();}
320 
328  boost::signals2::connection addTransformsChangedListener(boost::function<void(void)> callback);
329  void removeTransformsChangedListener(boost::signals2::connection c);
330 
334  std::string getTFPrefix() const { return tf_prefix_;};
335 
336  //Declare that it is safe to call waitForTransform
337  void setUsingDedicatedThread(bool value) { tf2_buffer_ptr_->setUsingDedicatedThread(value);};
338  // Get the state of using_dedicated_thread_ from the buffer
339  bool isUsingDedicatedThread() { return tf2_buffer_ptr_->isUsingDedicatedThread();};
340 
341  // Get a copy of the shared_ptr containing the tf2_ros::Buffer object
342  std::shared_ptr<tf2_ros::Buffer> getTF2BufferPtr() { return tf2_buffer_ptr_;};
343 
344 protected:
345 
346 
347 
359  /******************** Internal Storage ****************/
360 
361 
363  std::string tf_prefix_;
364 
365 
366  public:
367  // A flag to allow falling back to wall time
369 
370  protected:
372  ros::Time now() const {
373  if (!fall_back_to_wall_time_)
374  return ros::Time::now() ;
375  else {
377  return ros::Time(wt.sec, wt.nsec);
378  };
379  }
380 
381  // Allows broadcaster to check ok() before wait for transform
382  // Always returns true in base class
383  virtual bool ok() const;
384 
385  /************************* Internal Functions ****************************/
386 
387 
388 
389 
390 protected:
391  std::shared_ptr<tf2_ros::Buffer> tf2_buffer_ptr_;
392 
393 };
394 
395 
397 inline void assertQuaternionValid(const tf::Quaternion & q)
398 {
399  if(std::isnan(q.x()) || std::isnan(q.y()) || std::isnan(q.z()) || std::isnan(q.w()))
400  {
401  std::stringstream ss;
402  ss << "Quaternion contains a NaN" << std::endl;
403  throw tf::InvalidArgument(ss.str());
404  }
405 
406  if(std::fabs(q.x()*q.x() + q.y()*q.y() + q.z()*q.z() + q.w()*q.w() - 1) > 0.01)
407  {
408  std::stringstream ss;
409  ss << "Quaternion malformed, magnitude: " << q.x()*q.x() + q.y()*q.y() + q.z()*q.z() + q.w()*q.w() << " should be 1.0" <<std::endl;
410  throw tf::InvalidArgument(ss.str());
411  } // ROS_ASSERT(std::fabs(q.x()*q.x() + q.y()*q.y() + q.z*q.z() + q.w()*q.w() - 1 < 0.01));
412 };
413 
415 inline void assertQuaternionValid(const geometry_msgs::Quaternion & q)
416 {
417  if(std::isnan(q.x) || std::isnan(q.y) || std::isnan(q.z) || std::isnan(q.w))
418  {
419  std::stringstream ss;
420  ss << "Quaternion contains a NaN" << std::endl;
421  throw tf::InvalidArgument(ss.str());
422  }
423 
424  if(std::fabs(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w - 1) > 0.01)
425  {
426  std::stringstream ss;
427  ss << "Quaternion malformed, magnitude: " << q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w << " should be 1.0" <<std::endl;
428  throw tf::InvalidArgument(ss.str());
429  } // ROS_ASSERT(std::fabs(q.x()*q.x() + q.y()*q.y() + q.z*q.z() + q.w()*q.w() - 1 < 0.01));
430 };
431 }
432 #endif //TF_TF_H
bool getParent(const std::string &frame_id, ros::Time time, std::string &parent) const
Fill the parent of a frame.
Definition: tf.cpp:386
ros::Duration getCacheLength()
Get the duration over which this transformer will cache.
Definition: tf.h:319
void assertQuaternionValid(const tf::Quaternion &q)
Throw InvalidArgument if quaternion is malformed.
Definition: tf.h:397
std::string allFramesAsString() const
A way to see what frames have been cached Useful for debugging.
Definition: tf.cpp:434
The Quaternion implements quaternion to perform linear algebra rotations in combination with Matrix3x...
Definition: Quaternion.h:28
bool canTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, std::string *error_msg=NULL) const
Test if a transform is possible.
Definition: tf.cpp:358
std::string remap(const std::string &frame_id) __attribute__((deprecated))
resolve names
std::string allFramesAsDot(double current_time=0) const
A way to see what frames have been cached Useful for debugging.
Definition: tf.cpp:439
bool 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=ros::Duration(0.01), std::string *error_msg=NULL) const
Block until a transform is possible or it times out.
Definition: tf.cpp:348
bool setTransform(const StampedTransform &transform, const std::string &authority="default_authority")
Add transform information to the tf data structure.
Definition: tf.cpp:229
static const double DEFAULT_CACHE_TIME
10.0 is the default amount of time to cache data in seconds, set in cpp file.
Definition: tf.h:96
Definition: exceptions.h:38
std::string tf_prefix_
The internal storage class for ReferenceTransform.
Definition: tf.h:342
void 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
Debugging function that will print the spanning chain of transforms. Possible exceptions tf::LookupEx...
Definition: tf.cpp:427
virtual bool ok() const
Definition: tf.cpp:445
ErrorValues
Definition: tf.h:62
void setUsingDedicatedThread(bool value)
Definition: tf.h:337
std::string resolve(const std::string &prefix, const std::string &frame_name)
resolve tf names
Definition: tf.cpp:159
virtual ~Transformer(void)
Definition: tf.cpp:217
void removeTransformsChangedListener(boost::signals2::connection c)
Definition: tf.cpp:578
void transformPose(const std::string &target_frame, const Stamped< tf::Pose > &stamped_in, Stamped< tf::Pose > &stamped_out) const
Transform a Stamped Pose into the target frame This can throw anything a lookupTransform can throw as...
Definition: tf.cpp:494
void 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
Lookup the twist of the tracking_frame with respect to the observation frame in the reference_frame u...
Definition: tf.cpp:270
std::string getTFPrefix() const
Get the tf_prefix this is running with.
Definition: tf.h:334
The data type which will be cross compatable with geometry_msgs This is the tf datatype equivilant of...
bool isUsingDedicatedThread()
Definition: tf.h:339
bool fall_back_to_wall_time_
Definition: tf.h:368
tf2::InvalidArgumentException InvalidArgument
Definition: exceptions.h:45
bool frameExists(const std::string &frame_id_str) const
Check if a frame exists in the tree.
Definition: tf.cpp:392
std::shared_ptr< tf2_ros::Buffer > tf2_buffer_ptr_
Definition: tf.h:391
static const int64_t DEFAULT_MAX_EXTRAPOLATION_DISTANCE
The default amount of time to extrapolate //deprecated since integration with tf2.
Definition: tf.h:97
static const unsigned int MAX_GRAPH_DEPTH
The maximum number of time to recurse before assuming the tree has a loop.
Definition: tf.h:95
void getFrameStrings(std::vector< std::string > &ids) const
A way to get a std::vector of available frame ids.
Definition: tf.cpp:447
void transformQuaternion(const std::string &target_frame, const Stamped< tf::Quaternion > &stamped_in, Stamped< tf::Quaternion > &stamped_out) const
Transform a Stamped Quaternion into the target frame This can throw anything a lookupTransform can th...
Definition: tf.cpp:453
An internal representation of transform chains.
Definition: tf.h:67
std::string strip_leading_slash(const std::string &frame_name)
Definition: tf.cpp:195
int getLatestCommonTime(const std::string &source_frame, const std::string &target_frame, ros::Time &time, std::string *error_string) const
Return the latest rostime which is common across the spanning set zero if fails to cross...
Definition: tf.cpp:417
std::shared_ptr< tf2_ros::Buffer > getTF2BufferPtr()
Definition: tf.h:342
Transformer(bool interpolating=true, ros::Duration cache_time_=ros::Duration(DEFAULT_CACHE_TIME))
Definition: tf.cpp:209
static WallTime now()
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
Get the transform between two frames by frame ID.
Definition: tf.cpp:238
TFSIMD_FORCE_INLINE tfScalar distance(const Vector3 &v) const
Return the distance between the ends of this and another vector This is symantically treating the vec...
void transformPoint(const std::string &target_frame, const Stamped< tf::Point > &stamped_in, Stamped< tf::Point > &stamped_out) const
Transform a Stamped Point into the target frame This can throw anything a lookupTransform can throw a...
Definition: tf.cpp:484
ros::Time now() const
Definition: tf.h:372
static Time now()
void clear()
Clear all data.
Definition: tf.cpp:223
std::vector< TransformStorage > inverseTransforms
Definition: tf.h:69
std::vector< TransformStorage > forwardTransforms
Definition: tf.h:70
void transformVector(const std::string &target_frame, const Stamped< tf::Vector3 > &stamped_in, Stamped< tf::Vector3 > &stamped_out) const
Transform a Stamped Vector3 into the target frame This can throw anything a lookupTransform can throw...
Definition: tf.cpp:466
void setExtrapolationLimit(const ros::Duration &distance)
Set the distance which tf is allow to extrapolate.
Definition: tf.cpp:397
boost::signals2::connection addTransformsChangedListener(boost::function< void(void)> callback)
Add a callback that happens when a new transform has arrived.
Definition: tf.cpp:573
The Stamped Transform datatype used by tf.
A Class which provides coordinate transforms between any two frames in a system.
Definition: tf.h:91
Vector3 can be used to represent 3D points and vectors. It has an un-used w component to suit 16-byte...
Definition: Vector3.h:38
tf::tfVector4 __attribute__


tf
Author(s): Tully Foote, Eitan Marder-Eppstein, Wim Meeussen
autogenerated on Sat Dec 1 2018 03:27:35