time_sequencer.h
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 #ifndef MESSAGE_FILTERS_TIME_SEQUENCER_H
36 #define MESSAGE_FILTERS_TIME_SEQUENCER_H
37 
38 #include <ros/ros.h>
39 
40 #include "connection.h"
41 #include "simple_filter.h"
42 
43 namespace message_filters
44 {
45 
74 template<class M>
75 class TimeSequencer : public SimpleFilter<M>
76 {
77 public:
80 
89  template<class F>
90  TimeSequencer(F& f, ros::Duration delay, ros::Duration update_rate, uint32_t queue_size, ros::NodeHandle nh = ros::NodeHandle())
91  : delay_(delay)
92  , update_rate_(update_rate)
93  , queue_size_(queue_size)
94  , nh_(nh)
95  {
96  init();
97  connectInput(f);
98  }
99 
110  TimeSequencer(ros::Duration delay, ros::Duration update_rate, uint32_t queue_size, ros::NodeHandle nh = ros::NodeHandle())
111  : delay_(delay)
112  , update_rate_(update_rate)
113  , queue_size_(queue_size)
114  , nh_(nh)
115  {
116  init();
117  }
118 
122  template<class F>
123  void connectInput(F& f)
124  {
126  incoming_connection_ = f.registerCallback(typename SimpleFilter<M>::EventCallback(boost::bind(&TimeSequencer::cb, this, boost::placeholders::_1)));
127  }
128 
130  {
133  }
134 
135  void add(const EventType& evt)
136  {
137  namespace mt = ros::message_traits;
138 
139  boost::mutex::scoped_lock lock(messages_mutex_);
140  if (mt::TimeStamp<M>::value(*evt.getMessage()) < last_time_)
141  {
142  return;
143  }
144 
145  messages_.insert(evt);
146 
147  if (queue_size_ != 0 && messages_.size() > queue_size_)
148  {
149  messages_.erase(*messages_.begin());
150  }
151  }
152 
156  void add(const MConstPtr& msg)
157  {
158  EventType evt(msg);
159  add(evt);
160  }
161 
162 private:
163  class MessageSort
164  {
165  public:
166  bool operator()(const EventType& lhs, const EventType& rhs) const
167  {
168  namespace mt = ros::message_traits;
169  return mt::TimeStamp<M>::value(*lhs.getMessage()) < mt::TimeStamp<M>::value(*rhs.getMessage());
170  }
171  };
172  typedef std::multiset<EventType, MessageSort> S_Message;
173  typedef std::vector<EventType> V_Message;
174 
175  void cb(const EventType& evt)
176  {
177  add(evt);
178  }
179 
180  void dispatch()
181  {
182  namespace mt = ros::message_traits;
183 
184  V_Message to_call;
185 
186  {
187  boost::mutex::scoped_lock lock(messages_mutex_);
188 
189  while (!messages_.empty())
190  {
191  const EventType& e = *messages_.begin();
192  ros::Time stamp = mt::TimeStamp<M>::value(*e.getMessage());
193  if (stamp + delay_ <= ros::Time::now())
194  {
195  last_time_ = stamp;
196  to_call.push_back(e);
197  messages_.erase(messages_.begin());
198  }
199  else
200  {
201  break;
202  }
203  }
204  }
205 
206  {
207  typename V_Message::iterator it = to_call.begin();
208  typename V_Message::iterator end = to_call.end();
209  for (; it != end; ++it)
210  {
211  this->signalMessage(*it);
212  }
213  }
214  }
215 
216  void update(const ros::SteadyTimerEvent&)
217  {
218  dispatch();
219  }
220 
221  void init()
222  {
224  }
225 
228  uint32_t queue_size_;
230 
232 
233  Connection incoming_connection_;
234 
235 
237  boost::mutex messages_mutex_;
239 };
240 
241 }
242 
243 #endif
message_filters::TimeSequencer::TimeSequencer
TimeSequencer(F &f, ros::Duration delay, ros::Duration update_rate, uint32_t queue_size, ros::NodeHandle nh=ros::NodeHandle())
Constructor.
Definition: time_sequencer.h:122
message_filters::TimeSequencer::update_timer_
ros::SteadyTimer update_timer_
Definition: time_sequencer.h:263
message_filters::TimeSequencer::~TimeSequencer
~TimeSequencer()
Definition: time_sequencer.h:161
boost::shared_ptr
message_filters::TimeSequencer::MessageSort::operator()
bool operator()(const EventType &lhs, const EventType &rhs) const
Definition: time_sequencer.h:198
message_filters::TimeSequencer::connectInput
void connectInput(F &f)
Connect this filter's input to another filter's output.
Definition: time_sequencer.h:155
ros.h
message_filters::TimeSequencer::MessageSort
Definition: time_sequencer.h:195
message_filters::TimeSequencer::EventType
ros::MessageEvent< M const > EventType
Definition: time_sequencer.h:111
message_filters::TimeSequencer::queue_size_
uint32_t queue_size_
Definition: time_sequencer.h:260
simple_filter.h
message_filters::SimpleFilter::EventCallback
boost::function< void(const EventType &)> EventCallback
Definition: simple_filter.h:130
message_filters::TimeSequencer::last_time_
ros::Time last_time_
Definition: time_sequencer.h:270
ros::message_traits
f
f
ros::SteadyTimer::stop
void stop()
ros::SteadyTimer
message_filters::TimeSequencer::cb
void cb(const EventType &evt)
Definition: time_sequencer.h:207
ros::SteadyTimerEvent
message_filters::TimeSequencer::messages_
S_Message messages_
Definition: time_sequencer.h:268
message_filters::TimeSequencer::MConstPtr
boost::shared_ptr< M const > MConstPtr
Definition: time_sequencer.h:110
message_filters::TimeSequencer::update_rate_
ros::Duration update_rate_
Definition: time_sequencer.h:259
connection.h
ros::Time
ros::NodeHandle::createSteadyTimer
SteadyTimer createSteadyTimer(SteadyTimerOptions &ops) const
message_filters::TimeSequencer::incoming_connection_
Connection incoming_connection_
Definition: time_sequencer.h:265
message_filters::TimeSequencer::messages_mutex_
boost::mutex messages_mutex_
Definition: time_sequencer.h:269
message_filters::TimeSequencer::S_Message
std::multiset< EventType, MessageSort > S_Message
Definition: time_sequencer.h:204
message_filters::TimeSequencer::delay_
ros::Duration delay_
Definition: time_sequencer.h:258
message_filters
Definition: cache.h:47
ros::MessageEvent::getMessage
boost::shared_ptr< M > getMessage() const
DurationBase< Duration >::toSec
double toSec() const
message_filters::TimeSequencer::update
void update(const ros::SteadyTimerEvent &)
Definition: time_sequencer.h:248
ros::WallDuration
message_filters::SimpleFilter::signalMessage
void signalMessage(const MConstPtr &msg)
Call all registered callbacks, passing them the specified message.
Definition: simple_filter.h:188
message_filters::TimeSequencer::V_Message
std::vector< EventType > V_Message
Definition: time_sequencer.h:205
ros::Duration
message_filters::TimeSequencer::init
void init()
Definition: time_sequencer.h:253
message_filters::TimeSequencer::nh_
ros::NodeHandle nh_
Definition: time_sequencer.h:261
ros::NodeHandle
ros::MessageEvent
ros::Time::now
static Time now()
message_filters::TimeSequencer::dispatch
void dispatch()
Definition: time_sequencer.h:212
message_filters::TimeSequencer::add
void add(const EventType &evt)
Definition: time_sequencer.h:167
message_filters::Connection::disconnect
void disconnect()
disconnects this connection
Definition: connection.cpp:84


message_filters
Author(s): Josh Faust, Vijay Pradeep, Dirk Thomas , Jacob Perron
autogenerated on Thu Nov 23 2023 04:01:54