Skip to content

Commit 532bc51

Browse files
committed
Adding timestamp manager
1 parent 5b06df7 commit 532bc51

File tree

3 files changed

+47
-49
lines changed

3 files changed

+47
-49
lines changed

fuse_core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ add_library(
4949
src/graph_deserializer.cpp
5050
src/loss.cpp
5151
src/serialization.cpp
52-
# src/timestamp_manager.cpp
52+
src/timestamp_manager.cpp
5353
src/transaction.cpp
5454
src/transaction_deserializer.cpp
5555
src/uuid.cpp

fuse_core/include/fuse_core/timestamp_manager.h

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
#include <fuse_core/fuse_macros.h>
3939
#include <fuse_core/transaction.h>
4040
#include <fuse_core/variable.h>
41-
#include <ros/duration.h>
42-
#include <ros/time.h>
41+
#include <rclcpp/time.hpp>
4342

4443
#include <boost/range/any_range.hpp>
4544

@@ -65,7 +64,7 @@ namespace fuse_core
6564
class TimestampManager
6665
{
6766
public:
68-
FUSE_SMART_PTR_DEFINITIONS(TimestampManager);
67+
FUSE_SMART_PTR_DEFINITIONS(TimestampManager)
6968

7069
/**
7170
* @brief Function that generates motion model constraints between the requested timestamps
@@ -80,8 +79,8 @@ class TimestampManager
8079
* @param[out] variables One or more variables at both the \p beginning_stamp and \p ending_stamp. The
8180
* variables should include initial values for the optimizer.
8281
*/
83-
using MotionModelFunction = std::function<void(const ros::Time& beginning_stamp,
84-
const ros::Time& ending_stamp,
82+
using MotionModelFunction = std::function<void(const rclcpp::Time& beginning_stamp,
83+
const rclcpp::Time& ending_stamp,
8584
std::vector<Constraint::SharedPtr>& constraints,
8685
std::vector<Variable::SharedPtr>& variables)>;
8786

@@ -90,9 +89,9 @@ class TimestampManager
9089
*
9190
* An object representing a range defined by two iterators. It has begin() and end() methods (which means it can
9291
* be used in range-based for loops), an empty() method, and a front() method for directly accessing the first
93-
* member. When dereferenced, an iterator returns a const ros::Time&.
92+
* member. When dereferenced, an iterator returns a const rclcpp::Time&.
9493
*/
95-
using const_stamp_range = boost::any_range<const ros::Time, boost::forward_traversal_tag>;
94+
using const_stamp_range = boost::any_range<const rclcpp::Time, boost::forward_traversal_tag>;
9695

9796
/**
9897
* @brief Constructor that accepts the motion model generator as a std::function object, probably constructed using
@@ -102,7 +101,7 @@ class TimestampManager
102101
* @param[in] buffer_length The length of the motion model history. If queries arrive involving timestamps
103102
* that are older than the buffer length, an exception will be thrown.
104103
*/
105-
explicit TimestampManager(MotionModelFunction generator, const ros::Duration& buffer_length = ros::DURATION_MAX);
104+
explicit TimestampManager(MotionModelFunction generator, const rclcpp::Duration& buffer_length = rclcpp::Duration::max());
106105

107106
/**
108107
* @brief Constructor that accepts the motion model generator as a member function pointer and object pointer
@@ -116,12 +115,12 @@ class TimestampManager
116115
* that are older than the buffer length, an exception will be thrown.
117116
*/
118117
template<class T>
119-
TimestampManager(void(T::*fp)(const ros::Time& beginning_stamp,
120-
const ros::Time& ending_stamp,
118+
TimestampManager(void(T::*fp)(const rclcpp::Time& beginning_stamp,
119+
const rclcpp::Time& ending_stamp,
121120
std::vector<Constraint::SharedPtr>& constraints,
122121
std::vector<Variable::SharedPtr>& variables),
123122
T* obj,
124-
const ros::Duration& buffer_length = ros::DURATION_MAX);
123+
const rclcpp::Duration& buffer_length = rclcpp::Duration::max());
125124

126125
/**
127126
* @brief Constructor that accepts the motion model generator as a const member function pointer and object pointer
@@ -135,12 +134,12 @@ class TimestampManager
135134
* that are older than the buffer length, an exception will be thrown.
136135
*/
137136
template<class T>
138-
TimestampManager(void(T::*fp)(const ros::Time& beginning_stamp,
139-
const ros::Time& ending_stamp,
137+
TimestampManager(void(T::*fp)(const rclcpp::Time& beginning_stamp,
138+
const rclcpp::Time& ending_stamp,
140139
std::vector<Constraint::SharedPtr>& constraints,
141140
std::vector<Variable::SharedPtr>& variables) const,
142141
T* obj,
143-
const ros::Duration& buffer_length = ros::DURATION_MAX);
142+
const rclcpp::Duration& buffer_length = rclcpp::Duration::max());
144143

145144
/**
146145
* @brief Destructor
@@ -150,15 +149,15 @@ class TimestampManager
150149
/**
151150
* @brief Read-only access to the buffer length
152151
*/
153-
const ros::Duration& bufferLength() const
152+
const rclcpp::Duration& bufferLength() const
154153
{
155154
return buffer_length_;
156155
}
157156

158157
/**
159158
* @brief Write access to the buffer length
160159
*/
161-
void bufferLength(const ros::Duration& buffer_length)
160+
void bufferLength(const rclcpp::Duration& buffer_length)
162161
{
163162
buffer_length_ = buffer_length;
164163
}
@@ -200,16 +199,16 @@ class TimestampManager
200199
*/
201200
struct MotionModelSegment
202201
{
203-
ros::Time beginning_stamp;
204-
ros::Time ending_stamp;
202+
rclcpp::Time beginning_stamp;
203+
rclcpp::Time ending_stamp;
205204
std::vector<Constraint::SharedPtr> constraints;
206205
std::vector<Variable::SharedPtr> variables;
207206

208207
MotionModelSegment() = default;
209208

210209
MotionModelSegment(
211-
const ros::Time& beginning_stamp,
212-
const ros::Time& ending_stamp,
210+
const rclcpp::Time& beginning_stamp,
211+
const rclcpp::Time& ending_stamp,
213212
const std::vector<Constraint::SharedPtr>& constraints,
214213
const std::vector<Variable::SharedPtr>& variables) :
215214
beginning_stamp(beginning_stamp),
@@ -226,10 +225,10 @@ class TimestampManager
226225
* The MotionModelHistory will always contain all represented timestamps; the very last entry will be the ending
227226
* time of the previous MotionModelSegment, and the very last entry will be an empty MotionModelSegment.
228227
*/
229-
using MotionModelHistory = std::map<ros::Time, MotionModelSegment>;
228+
using MotionModelHistory = std::map<rclcpp::Time, MotionModelSegment>;
230229

231230
MotionModelFunction generator_; //!< Users upplied function that generates motion model constraints
232-
ros::Duration buffer_length_; //!< The length of the motion model history. Segments older than \p buffer_length_
231+
rclcpp::Duration buffer_length_; //!< The length of the motion model history. Segments older than \p buffer_length_
233232
//!< will be removed from the motion model history
234233
MotionModelHistory motion_model_history_; //!< Container that stores all previously generated motion models
235234

@@ -243,8 +242,8 @@ class TimestampManager
243242
* @param[out] transaction A transaction object to be updated with the changes caused by addSegment
244243
*/
245244
void addSegment(
246-
const ros::Time& beginning_stamp,
247-
const ros::Time& ending_stamp,
245+
const rclcpp::Time& beginning_stamp,
246+
const rclcpp::Time& ending_stamp,
248247
Transaction& transaction);
249248

250249
/**
@@ -271,7 +270,7 @@ class TimestampManager
271270
*/
272271
void splitSegment(
273272
MotionModelHistory::iterator& iter,
274-
const ros::Time& stamp,
273+
const rclcpp::Time& stamp,
275274
Transaction& transaction);
276275

277276
/**
@@ -281,12 +280,12 @@ class TimestampManager
281280
};
282281

283282
template<class T>
284-
TimestampManager::TimestampManager(void(T::*fp)(const ros::Time& beginning_stamp,
285-
const ros::Time& ending_stamp,
283+
TimestampManager::TimestampManager(void(T::*fp)(const rclcpp::Time& beginning_stamp,
284+
const rclcpp::Time& ending_stamp,
286285
std::vector<Constraint::SharedPtr>& constraints,
287286
std::vector<Variable::SharedPtr>& variables),
288287
T* obj,
289-
const ros::Duration& buffer_length) :
288+
const rclcpp::Duration& buffer_length) :
290289
TimestampManager(std::bind(fp,
291290
obj,
292291
std::placeholders::_1,
@@ -298,12 +297,12 @@ TimestampManager::TimestampManager(void(T::*fp)(const ros::Time& beginning_stamp
298297
}
299298

300299
template<class T>
301-
TimestampManager::TimestampManager(void(T::*fp)(const ros::Time& beginning_stamp,
302-
const ros::Time& ending_stamp,
300+
TimestampManager::TimestampManager(void(T::*fp)(const rclcpp::Time& beginning_stamp,
301+
const rclcpp::Time& ending_stamp,
303302
std::vector<Constraint::SharedPtr>& constraints,
304303
std::vector<Variable::SharedPtr>& variables) const,
305304
T* obj,
306-
const ros::Duration& buffer_length) :
305+
const rclcpp::Duration& buffer_length) :
307306
TimestampManager(std::bind(fp,
308307
obj,
309308
std::placeholders::_1,

fuse_core/src/timestamp_manager.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
#include <fuse_core/constraint.h>
3737
#include <fuse_core/transaction.h>
3838
#include <fuse_core/variable.h>
39-
#include <ros/duration.h>
40-
#include <ros/time.h>
39+
#include <rclcpp/time.hpp>
4140

4241
#include <boost/iterator/transform_iterator.hpp>
4342

@@ -52,7 +51,7 @@
5251
namespace fuse_core
5352
{
5453

55-
TimestampManager::TimestampManager(MotionModelFunction generator, const ros::Duration& buffer_length) :
54+
TimestampManager::TimestampManager(MotionModelFunction generator, const rclcpp::Duration& buffer_length) :
5655
generator_(generator),
5756
buffer_length_(buffer_length)
5857
{
@@ -70,7 +69,7 @@ void TimestampManager::query(
7069
}
7170
// Verify the query is within the buffer length
7271
if ( (!motion_model_history_.empty())
73-
&& (buffer_length_ != ros::DURATION_MAX)
72+
&& (buffer_length_ != rclcpp::Duration::max())
7473
&& (stamps.front() < motion_model_history_.begin()->first)
7574
&& (stamps.front() < (motion_model_history_.rbegin()->first - buffer_length_)))
7675
{
@@ -79,7 +78,7 @@ void TimestampManager::query(
7978
// Create a list of all the required timestamps involved in motion model segments that must be created
8079
// Add all of the existing timestamps between the first and last input stamp
8180
Transaction motion_model_transaction;
82-
std::set<ros::Time> augmented_stamps(stamps.begin(), stamps.end());
81+
std::set<rclcpp::Time> augmented_stamps(stamps.begin(), stamps.end());
8382
auto first_stamp = *augmented_stamps.begin();
8483
auto last_stamp = *augmented_stamps.rbegin();
8584
{
@@ -99,14 +98,14 @@ void TimestampManager::query(
9998
}
10099
}
101100
// Convert the sequence of stamps into stamp pairs that must be generated
102-
std::vector<std::pair<ros::Time, ros::Time>> stamp_pairs;
101+
std::vector<std::pair<rclcpp::Time, rclcpp::Time>> stamp_pairs;
103102
{
104103
for (auto previous_iter = augmented_stamps.begin(), current_iter = std::next(augmented_stamps.begin());
105104
current_iter != augmented_stamps.end();
106105
++previous_iter, ++current_iter)
107106
{
108-
const ros::Time& previous_stamp = *previous_iter;
109-
const ros::Time& current_stamp = *current_iter;
107+
const rclcpp::Time& previous_stamp = *previous_iter;
108+
const rclcpp::Time& current_stamp = *current_iter;
110109
// Check if the timestamp pair is exactly an existing pair. If so, don't add it.
111110
auto history_iter = motion_model_history_.lower_bound(previous_stamp);
112111
if ((history_iter != motion_model_history_.end()) &&
@@ -173,7 +172,7 @@ void TimestampManager::query(
173172

174173
TimestampManager::const_stamp_range TimestampManager::stamps() const
175174
{
176-
auto extract_stamp = +[](const MotionModelHistory::value_type& element) -> const ros::Time&
175+
auto extract_stamp = +[](const MotionModelHistory::value_type& element) -> const rclcpp::Time&
177176
{
178177
return element.first;
179178
};
@@ -183,8 +182,8 @@ TimestampManager::const_stamp_range TimestampManager::stamps() const
183182
}
184183

185184
void TimestampManager::addSegment(
186-
const ros::Time& beginning_stamp,
187-
const ros::Time& ending_stamp,
185+
const rclcpp::Time& beginning_stamp,
186+
const rclcpp::Time& ending_stamp,
188187
Transaction& transaction)
189188
{
190189
// Generate the set of constraints and variables to add
@@ -228,11 +227,11 @@ void TimestampManager::removeSegment(
228227

229228
void TimestampManager::splitSegment(
230229
MotionModelHistory::iterator& iter,
231-
const ros::Time& stamp,
230+
const rclcpp::Time& stamp,
232231
Transaction& transaction)
233232
{
234-
ros::Time removed_beginning_stamp = iter->second.beginning_stamp;
235-
ros::Time removed_ending_stamp = iter->second.ending_stamp;
233+
rclcpp::Time removed_beginning_stamp = iter->second.beginning_stamp;
234+
rclcpp::Time removed_ending_stamp = iter->second.ending_stamp;
236235
// We need to remove the existing constraint.
237236
removeSegment(iter, transaction);
238237
// And add a new constraint from the beginning of the removed constraint to the provided stamp
@@ -244,17 +243,17 @@ void TimestampManager::splitSegment(
244243
void TimestampManager::purgeHistory()
245244
{
246245
// Purge any motion model segments that are more than buffer_length_ seconds older than the most recent entry
247-
// A setting of ros::DURATION_MAX means "keep everything"
246+
// A setting of rclcpp::Duration::max() means "keep everything"
248247
// And we want to keep at least one entry in motion model history, regardless of the stamps.
249-
if ((buffer_length_ == ros::DURATION_MAX) || (motion_model_history_.size() <= 1))
248+
if ((buffer_length_ == rclcpp::Duration::max()) || (motion_model_history_.size() <= 1))
250249
{
251250
return;
252251
}
253252
// Continue to remove the first entry from the history until we:
254253
// (a) are left with only one entry, OR
255254
// (b) the time delta between the beginning and end is within the buffer_length_
256255
// We compare with the ending timestamp of each segment to be conservative
257-
ros::Time ending_stamp = motion_model_history_.rbegin()->first;
256+
rclcpp::Time ending_stamp = motion_model_history_.rbegin()->first;
258257
while ( (motion_model_history_.size() > 1)
259258
&& ((ending_stamp - motion_model_history_.begin()->second.ending_stamp) > buffer_length_))
260259
{

0 commit comments

Comments
 (0)