tf2  master
tf2 maintains the relationship between coordinate frames in a tree structure buffered in time, and lets the user transform points, vectors, etc between any two coordinate frames at any desired point in time.
time_cache.h
Go to the documentation of this file.
1 // Copyright 2008, Willow Garage, Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 //
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 //
13 // * Neither the name of the Willow Garage 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 HOLDER 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.
30 #ifndef TF2__TIME_CACHE_H_
31 #define TF2__TIME_CACHE_H_
32 
33 #include <chrono>
34 #include <memory>
35 #include <list>
36 #include <sstream>
37 #include <string>
38 #include <utility>
39 
40 #include "tf2/visibility_control.h"
41 #include "tf2/transform_storage.h"
42 
43 namespace tf2
44 {
46 
48 {
49 public:
51  virtual ~TimeCacheInterface() = default;
52 
57  virtual bool getData(
58  tf2::TimePoint time, tf2::TransformStorage & data_out,
59  std::string * error_str = 0) = 0;
60 
63  virtual bool insertData(const tf2::TransformStorage & new_data) = 0;
64 
67  virtual void clearList() = 0;
68 
71  virtual CompactFrameID getParent(tf2::TimePoint time, std::string * error_str) = 0;
72 
78 
80 
82  virtual unsigned int getListLength() = 0;
83 
86  virtual tf2::TimePoint getLatestTimestamp() = 0;
87 
90  virtual tf2::TimePoint getOldestTimestamp() = 0;
91 };
92 
94 
97 
103 {
104 public:
106  TF2_PUBLIC
107  static const int MIN_INTERPOLATION_DISTANCE = 5;
108 
110  TF2_PUBLIC
111  static const unsigned int MAX_LENGTH_LINKED_LIST = 1000000;
112  TF2_PUBLIC
113  explicit TimeCache(tf2::Duration max_storage_time = TIMECACHE_DEFAULT_MAX_STORAGE_TIME);
114 
116 
117  TF2_PUBLIC
118  virtual bool getData(
119  tf2::TimePoint time, tf2::TransformStorage & data_out,
120  std::string * error_str = 0);
121  TF2_PUBLIC
122  virtual bool insertData(const tf2::TransformStorage & new_data);
123  TF2_PUBLIC
124  virtual void clearList();
125  TF2_PUBLIC
126  virtual tf2::CompactFrameID getParent(tf2::TimePoint time, std::string * error_str);
127  TF2_PUBLIC
129 
131  TF2_PUBLIC
132  virtual unsigned int getListLength();
133  TF2_PUBLIC
134  virtual TimePoint getLatestTimestamp();
135  TF2_PUBLIC
136  virtual TimePoint getOldestTimestamp();
137 
138 private:
140  L_TransformStorage storage_;
141 
142  tf2::Duration max_storage_time_;
143 
144 
145  // A helper function for getData
146  // Assumes storage is already locked for it
147  inline uint8_t findClosest(
148  tf2::TransformStorage * & one, TransformStorage * & two,
149  tf2::TimePoint target_time, std::string * error_str);
150 
151  inline void interpolate(
152  const tf2::TransformStorage & one, const tf2::TransformStorage & two,
153  tf2::TimePoint time, tf2::TransformStorage & output);
154 
155  void pruneList();
156 };
157 
159 {
160 public:
162  TF2_PUBLIC
163  virtual bool getData(TimePoint time, TransformStorage & data_out, std::string * error_str = 0);
164  // returns false if data unavailable (should be thrown as lookup exception
165  TF2_PUBLIC
166  virtual bool insertData(const TransformStorage & new_data);
167  TF2_PUBLIC
168  virtual void clearList();
169  TF2_PUBLIC
170  virtual CompactFrameID getParent(TimePoint time, std::string * error_str);
171  TF2_PUBLIC
173 
175  TF2_PUBLIC
176  virtual unsigned int getListLength();
177  TF2_PUBLIC
178  virtual TimePoint getLatestTimestamp();
179  TF2_PUBLIC
180  virtual TimePoint getOldestTimestamp();
181 
182 private:
183  TransformStorage storage_;
184 };
185 } // namespace tf2
186 #endif // TF2__TIME_CACHE_H_
tf2::StaticCache::getLatestTimeAndParent
virtual P_TimeAndFrameID getLatestTimeAndParent()
Get the latest time stored in this cache, and the parent associated with it. Returns parent = 0 if no...
tf2::TimeCache::MIN_INTERPOLATION_DISTANCE
static const int MIN_INTERPOLATION_DISTANCE
Number of nano-seconds to not interpolate below.
Definition: time_cache.h:107
tf2::StaticCache::getParent
virtual CompactFrameID getParent(TimePoint time, std::string *error_str)
Retrieve the parent at a specific time.
std::string
std::shared_ptr
tf2::TimeCacheInterface::~TimeCacheInterface
virtual ~TimeCacheInterface()=default
tf2::TimeCacheInterface::getData
virtual bool getData(tf2::TimePoint time, tf2::TransformStorage &data_out, std::string *error_str=0)=0
Access data from the cache returns false if data unavailable (should be thrown as lookup exception)
std::list< TransformStorage >
tf2::TimeCacheInterface::getListLength
virtual unsigned int getListLength()=0
Debugging information methods.
tf2::TimeCache::insertData
virtual bool insertData(const tf2::TransformStorage &new_data)
Insert data into the cache.
std::pair
std::chrono::nanoseconds
tf2::P_TimeAndFrameID
std::pair< TimePoint, CompactFrameID > P_TimeAndFrameID
Definition: buffer_core.h:56
tf2::TimeCache::clearList
virtual void clearList()
Clear the list of stored values.
tf2::StaticCache::insertData
virtual bool insertData(const TransformStorage &new_data)
Insert data into the cache.
tf2::TimeCache
A class to keep a sorted linked list in time This builds and maintains a list of timestamped data....
Definition: time_cache.h:102
tf2::TimeCache::getOldestTimestamp
virtual TimePoint getOldestTimestamp()
Get the oldest timestamp cached.
tf2::CompactFrameID
uint32_t CompactFrameID
Definition: transform_storage.h:40
tf2::TimeCacheInterface::getLatestTimestamp
virtual tf2::TimePoint getLatestTimestamp()=0
Get the latest timestamp cached.
tf2::TimeCache::getParent
virtual tf2::CompactFrameID getParent(tf2::TimePoint time, std::string *error_str)
Retrieve the parent at a specific time.
tf2::TimeCache::MAX_LENGTH_LINKED_LIST
static const unsigned int MAX_LENGTH_LINKED_LIST
Maximum length of linked list, to make sure not to be able to use unlimited memory.
Definition: time_cache.h:111
tf2::TransformStorage
Storage for transforms and their parent.
Definition: transform_storage.h:43
tf2::TimeCache::getData
virtual bool getData(tf2::TimePoint time, tf2::TransformStorage &data_out, std::string *error_str=0)
Virtual methods.
tf2::TimeCache::getLatestTimeAndParent
virtual P_TimeAndFrameID getLatestTimeAndParent()
Get the latest time stored in this cache, and the parent associated with it. Returns parent = 0 if no...
tf2::StaticCache
Definition: time_cache.h:158
tf2::TimeCacheInterface::clearList
virtual void clearList()=0
Clear the list of stored values.
tf2::StaticCache::getData
virtual bool getData(TimePoint time, TransformStorage &data_out, std::string *error_str=0)
Virtual methods.
tf2::TIMECACHE_DEFAULT_MAX_STORAGE_TIME
constexpr tf2::Duration TIMECACHE_DEFAULT_MAX_STORAGE_TIME
default value of 10 seconds storage
Definition: time_cache.h:96
std::chrono::time_point< std::chrono::system_clock, Duration >
transform_storage.h
tf2::TimeCacheInterface::getParent
virtual CompactFrameID getParent(tf2::TimePoint time, std::string *error_str)=0
Retrieve the parent at a specific time.
tf2::TimeCacheInterface
Definition: time_cache.h:47
tf2::StaticCache::getLatestTimestamp
virtual TimePoint getLatestTimestamp()
Get the latest timestamp cached.
tf2::StaticCache::getOldestTimestamp
virtual TimePoint getOldestTimestamp()
Get the oldest timestamp cached.
visibility_control.h
tf2::TimeCache::getLatestTimestamp
virtual TimePoint getLatestTimestamp()
Get the latest timestamp cached.
tf2::TimeCacheInterface::insertData
virtual bool insertData(const tf2::TransformStorage &new_data)=0
Insert data into the cache.
tf2::StaticCache::clearList
virtual void clearList()
Clear the list of stored values.
tf2::TimeCacheInterface::getOldestTimestamp
virtual tf2::TimePoint getOldestTimestamp()=0
Get the oldest timestamp cached.
tf2
Definition: buffer_core.h:53
tf2::TimeCache::TimeCache
TimeCache(tf2::Duration max_storage_time=TIMECACHE_DEFAULT_MAX_STORAGE_TIME)
TF2_PUBLIC
#define TF2_PUBLIC
Definition: visibility_control.h:57
tf2::TimeCache::getListLength
virtual unsigned int getListLength()
Debugging information methods.
tf2::TimeCacheInterface::getLatestTimeAndParent
virtual P_TimeAndFrameID getLatestTimeAndParent()=0
Get the latest time stored in this cache, and the parent associated with it. Returns parent = 0 if no...
tf2::StaticCache::getListLength
virtual unsigned int getListLength()
Debugging information methods.