Skip to content

Commit

Permalink
Apply some simplifications and deduplications to ExactTime sync policy (
Browse files Browse the repository at this point in the history
#142)

Signed-off-by: Christopher Wecht <[email protected]>
  • Loading branch information
cwecht authored Aug 19, 2024
1 parent 7121cb1 commit 5e71178
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions include/message_filters/sync_policies/exact_time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ struct ExactTime : public PolicyBase<Ms...>
assert(parent_);

namespace mt = message_filters::message_traits;
using Message = typename std::tuple_element<i, Messages>::type;

std::lock_guard<std::mutex> lock(mutex_);

Tuple & t =
tuples_[mt::TimeStamp<typename std::tuple_element<i,
Messages>::type>::value(*evt.getMessage())];
Tuple & t = tuples_[mt::TimeStamp<Message>::value(*evt.getMessage())];
std::get<i>(t) = evt;

checkTuple(t);
Expand Down Expand Up @@ -154,40 +153,39 @@ struct ExactTime : public PolicyBase<Ms...>
clearOldTuples();
}

if (queue_size_ > 0) {
while (tuples_.size() > queue_size_) {
Tuple & t2 = tuples_.begin()->second;
std::apply([this](auto &&... args) {this->drop_signal_.call(args ...);}, t2);
tuples_.erase(tuples_.begin());
}
if (queue_size_ > 0 && tuples_.size() > queue_size_) {
const auto n_exceeding_tuples = tuples_.size() - queue_size_;
const auto oldest_to_keep = std::next(tuples_.begin(), n_exceeding_tuples);
drop_range(tuples_.begin(), oldest_to_keep);
}
}


// assumes mutex_ is already locked
void clearOldTuples()
{
typename M_TimeToTuple::iterator it = tuples_.begin();
typename M_TimeToTuple::iterator end = tuples_.end();
for (; it != end; ) {
if (it->first <= last_signal_time_) {
typename M_TimeToTuple::iterator old = it;
++it;

Tuple & t = old->second;
std::apply([this](auto &&... args) {this->drop_signal_.call(args ...);}, t);
tuples_.erase(old);
} else {
// the map is sorted by time, so we can ignore anything after this if this one's time is ok
break;
}
}
const auto first_arrived_after = tuples_.upper_bound(last_signal_time_);
drop_range(tuples_.begin(), first_arrived_after);
}

private:
using M_TimeToTuple = std::map<rclcpp::Time, Tuple>;
using map_iterator = typename M_TimeToTuple::iterator;

void call_drop_signal(Tuple & t)
{
std::apply([this](auto &&... args) {this->drop_signal_.call(args ...);}, t);
}

void drop_range(map_iterator begin, map_iterator end)
{
std::for_each(begin, end, [this](auto & e) {call_drop_signal(e.second);});
tuples_.erase(begin, end);
}

Sync * parent_;

uint32_t queue_size_;
typedef std::map<rclcpp::Time, Tuple> M_TimeToTuple;
M_TimeToTuple tuples_;
rclcpp::Time last_signal_time_;

Expand Down

0 comments on commit 5e71178

Please sign in to comment.