rclcpp  master
C++ ROS Client Library API
timer.hpp
Go to the documentation of this file.
1 // Copyright 2014 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__TIMER_HPP_
16 #define RCLCPP__TIMER_HPP_
17 
18 #include <atomic>
19 #include <chrono>
20 #include <functional>
21 #include <memory>
22 #include <sstream>
23 #include <thread>
24 #include <type_traits>
25 #include <utility>
26 
27 #include "rclcpp/clock.hpp"
28 #include "rclcpp/context.hpp"
30 #include "rclcpp/macros.hpp"
31 #include "rclcpp/rate.hpp"
32 #include "rclcpp/utilities.hpp"
34 #include "tracetools/tracetools.h"
35 #include "tracetools/utils.hpp"
36 
37 #include "rcl/error_handling.h"
38 #include "rcl/timer.h"
39 
40 #include "rmw/error_handling.h"
41 #include "rmw/rmw.h"
42 
43 namespace rclcpp
44 {
45 
46 class TimerBase
47 {
48 public:
50 
51 
52 
58  explicit TimerBase(
59  Clock::SharedPtr clock,
61  rclcpp::Context::SharedPtr context);
62 
65  virtual
66  ~TimerBase();
67 
69 
73  void
74  cancel();
75 
77 
83  bool
84  is_canceled();
85 
87 
91  void
92  reset();
93 
96  virtual void
97  execute_callback() = 0;
98 
102 
104 
111 
113 
114  virtual bool is_steady() = 0;
115 
117 
124  bool is_ready();
125 
127 
137  bool
138  exchange_in_use_by_wait_set_state(bool in_use_state);
139 
140 protected:
141  Clock::SharedPtr clock_;
143 
145 };
146 
147 
150 
152 template<
153  typename FunctorT,
154  typename std::enable_if<
157  >::type * = nullptr
158 >
159 class GenericTimer : public TimerBase
160 {
161 public:
163 
164 
165 
171  explicit GenericTimer(
172  Clock::SharedPtr clock, std::chrono::nanoseconds period, FunctorT && callback,
173  rclcpp::Context::SharedPtr context
174  )
175  : TimerBase(clock, period, context), callback_(std::forward<FunctorT>(callback))
176  {
177  TRACEPOINT(
178  rclcpp_timer_callback_added,
179  (const void *)get_timer_handle().get(),
180  (const void *)&callback_);
181  TRACEPOINT(
182  rclcpp_callback_register,
183  (const void *)&callback_,
184  get_symbol(callback_));
185  }
186 
188  virtual ~GenericTimer()
189  {
190  // Stop the timer from running.
191  cancel();
192  }
193 
198  void
199  execute_callback() override
200  {
201  rcl_ret_t ret = rcl_timer_call(timer_handle_.get());
202  if (ret == RCL_RET_TIMER_CANCELED) {
203  return;
204  }
205  if (ret != RCL_RET_OK) {
206  throw std::runtime_error("Failed to notify timer that callback occurred");
207  }
208  TRACEPOINT(callback_start, (const void *)&callback_, false);
209  execute_callback_delegate<>();
210  TRACEPOINT(callback_end, (const void *)&callback_);
211  }
212 
213  // void specialization
214  template<
215  typename CallbackT = FunctorT,
216  typename std::enable_if<
218  >::type * = nullptr
219  >
220  void
222  {
223  callback_();
224  }
225 
226  template<
227  typename CallbackT = FunctorT,
228  typename std::enable_if<
230  >::type * = nullptr
231  >
232  void
234  {
235  callback_(*this);
236  }
237 
239 
240  bool
241  is_steady() override
242  {
243  return clock_->get_clock_type() == RCL_STEADY_TIME;
244  }
245 
246 protected:
248 
249  FunctorT callback_;
250 };
251 
252 template<
253  typename FunctorT,
254  typename std::enable_if<
255  rclcpp::function_traits::same_arguments<FunctorT, VoidCallbackType>::value ||
256  rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value
257  >::type * = nullptr
258 >
259 class WallTimer : public GenericTimer<FunctorT>
260 {
261 public:
263 
264 
265 
271  std::chrono::nanoseconds period,
272  FunctorT && callback,
273  rclcpp::Context::SharedPtr context)
274  : GenericTimer<FunctorT>(
275  std::make_shared<Clock>(RCL_STEADY_TIME), period, std::move(callback), context)
276  {}
277 
278 protected:
280 };
281 
282 } // namespace rclcpp
283 
284 #endif // RCLCPP__TIMER_HPP_
rclcpp::TimerBase::get_timer_handle
std::shared_ptr< const rcl_timer_t > get_timer_handle()
rclcpp::TimerBase::is_steady
virtual bool is_steady()=0
Is the clock steady (i.e. is the time between ticks constant?)
std::shared_ptr
rclcpp::TimerBase::in_use_by_wait_set_
std::atomic< bool > in_use_by_wait_set_
Definition: timer.hpp:144
RCLCPP_DISABLE_COPY
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
rclcpp::TimerBase::TimerBase
TimerBase(Clock::SharedPtr clock, std::chrono::nanoseconds period, rclcpp::Context::SharedPtr context)
TimerBase constructor.
rmw.h
rclcpp::TimerBase::execute_callback
virtual void execute_callback()=0
Call the callback function when the timer signal is emitted.
error_handling.h
std::chrono::nanoseconds
rclcpp::TimerBase::cancel
void cancel()
Cancel the timer.
rclcpp::TimerBase::clock_
Clock::SharedPtr clock_
Definition: timer.hpp:141
context.hpp
rclcpp::GenericTimer
Generic timer. Periodically executes a user-specified callback.
Definition: timer.hpp:159
std::shared_ptr::get
T get(T... args)
clock.hpp
std::function< void()>
rclcpp::TimerBase
Definition: timer.hpp:46
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
RCLCPP_SMART_PTR_DEFINITIONS
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
macros.hpp
rclcpp::TimerBase::is_ready
bool is_ready()
Check if the timer is ready to trigger the callback.
std::enable_if
rclcpp::GenericTimer::execute_callback_delegate
void execute_callback_delegate()
Definition: timer.hpp:221
rclcpp::TimerBase::is_canceled
bool is_canceled()
Return the timer cancellation state.
rclcpp::GenericTimer::~GenericTimer
virtual ~GenericTimer()
Default destructor.
Definition: timer.hpp:188
rclcpp::GenericTimer::callback_
FunctorT callback_
Definition: timer.hpp:249
rate.hpp
std::runtime_error
std::atomic< bool >
rclcpp::function_traits::same_arguments
Definition: function_traits.hpp:159
function_traits.hpp
rclcpp::TimerBase::time_until_trigger
std::chrono::nanoseconds time_until_trigger()
Check how long the timer has until its next scheduled callback.
rclcpp::Context
Context which encapsulates shared state between nodes and other similar entities.
Definition: context.hpp:56
rclcpp::Clock
Definition: clock.hpp:53
rclcpp::TimerBase::timer_handle_
std::shared_ptr< rcl_timer_t > timer_handle_
Definition: timer.hpp:142
rclcpp::TimerBase::exchange_in_use_by_wait_set_state
bool exchange_in_use_by_wait_set_state(bool in_use_state)
Exchange the "in use by wait set" state for this timer.
visibility_control.hpp
std
rclcpp::TimerBase::reset
void reset()
Reset the timer.
rclcpp::TimerBase::~TimerBase
virtual ~TimerBase()
TimerBase destructor.
rclcpp::GenericTimer::execute_callback
void execute_callback() override
Definition: timer.hpp:199
rclcpp::WallTimer
Definition: timer.hpp:259
RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE
#define RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(...)
Definition: macros.hpp:51
rclcpp::GenericTimer::is_steady
bool is_steady() override
Is the clock steady (i.e. is the time between ticks constant?)
Definition: timer.hpp:241
utilities.hpp