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 <chrono>
19 #include <functional>
20 #include <memory>
21 #include <sstream>
22 #include <thread>
23 #include <type_traits>
24 #include <utility>
25 
26 #include "rclcpp/clock.hpp"
27 #include "rclcpp/context.hpp"
29 #include "rclcpp/macros.hpp"
30 #include "rclcpp/rate.hpp"
31 #include "rclcpp/utilities.hpp"
33 
34 #include "rcl/error_handling.h"
35 #include "rcl/timer.h"
36 
37 #include "rmw/error_handling.h"
38 #include "rmw/rmw.h"
39 
40 namespace rclcpp
41 {
42 
43 class TimerBase
44 {
45 public:
47 
49  explicit TimerBase(
50  Clock::SharedPtr clock,
52  rclcpp::Context::SharedPtr context);
53 
55  ~TimerBase();
56 
58  void
59  cancel();
60 
62 
68  bool
69  is_canceled();
70 
72  void
73  reset();
74 
76  virtual void
77  execute_callback() = 0;
78 
82 
84 
88 
90 
91  virtual bool is_steady() = 0;
92 
94 
100  bool is_ready();
101 
102 protected:
103  Clock::SharedPtr clock_;
105 };
106 
107 
110 
112 template<
113  typename FunctorT,
114  typename std::enable_if<
117  >::type * = nullptr
118 >
119 class GenericTimer : public TimerBase
120 {
121 public:
123 
124 
125 
130  explicit GenericTimer(
131  Clock::SharedPtr clock, std::chrono::nanoseconds period, FunctorT && callback,
132  rclcpp::Context::SharedPtr context
133  )
134  : TimerBase(clock, period, context), callback_(std::forward<FunctorT>(callback))
135  {
136  }
137 
139  virtual ~GenericTimer()
140  {
141  // Stop the timer from running.
142  cancel();
143  }
144 
145  void
146  execute_callback() override
147  {
149  if (ret == RCL_RET_TIMER_CANCELED) {
150  return;
151  }
152  if (ret != RCL_RET_OK) {
153  throw std::runtime_error("Failed to notify timer that callback occurred");
154  }
155  execute_callback_delegate<>();
156  }
157 
158  // void specialization
159  template<
160  typename CallbackT = FunctorT,
161  typename std::enable_if<
163  >::type * = nullptr
164  >
165  void
167  {
168  callback_();
169  }
170 
171  template<
172  typename CallbackT = FunctorT,
173  typename std::enable_if<
175  >::type * = nullptr
176  >
177  void
179  {
180  callback_(*this);
181  }
182 
183  bool
184  is_steady() override
185  {
186  return clock_->get_clock_type() == RCL_STEADY_TIME;
187  }
188 
189 protected:
191 
192  FunctorT callback_;
193 };
194 
195 template<
196  typename FunctorT,
197  typename std::enable_if<
198  rclcpp::function_traits::same_arguments<FunctorT, VoidCallbackType>::value ||
199  rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value
200  >::type * = nullptr
201 >
202 class WallTimer : public GenericTimer<FunctorT>
203 {
204 public:
206 
208  std::chrono::nanoseconds period,
209  FunctorT && callback,
210  rclcpp::Context::SharedPtr context)
211  : GenericTimer<FunctorT>(
212  std::make_shared<Clock>(RCL_STEADY_TIME), period, std::move(callback), context)
213  {}
214 
215 protected:
217 };
218 
219 } // namespace rclcpp
220 
221 #endif // RCLCPP__TIMER_HPP_
virtual void execute_callback()=0
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
Clock::SharedPtr clock_
Definition: timer.hpp:103
rmw_ret_t rcl_ret_t
Definition: timer.hpp:202
bool is_ready()
Check if the timer is ready to trigger the callback.
Context which encapsulates shared state between nodes and other similar entities. ...
Definition: context.hpp:53
#define RCL_RET_TIMER_CANCELED
std::chrono::nanoseconds time_until_trigger()
Check how long the timer has until its next scheduled callback.
void execute_callback_delegate()
Definition: timer.hpp:166
This header provides the get_node_topics_interface() template function.
Definition: allocator_common.hpp:24
virtual ~GenericTimer()
Default destructor.
Definition: timer.hpp:139
Generic timer. Periodically executes a user-specified callback.
Definition: timer.hpp:119
#define RCL_RET_OK
rcl_ret_t rcl_timer_call(rcl_timer_t *timer)
virtual bool is_steady()=0
Is the clock steady (i.e. is the time between ticks constant?)
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
RCL_STEADY_TIME
Definition: function_traits.hpp:143
TimerBase(Clock::SharedPtr clock, std::chrono::nanoseconds period, rclcpp::Context::SharedPtr context)
std::shared_ptr< const rcl_timer_t > get_timer_handle()
Definition: clock.hpp:49
#define RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(...)
Definition: macros.hpp:51
bool is_steady() override
Is the clock steady (i.e. is the time between ticks constant?)
Definition: timer.hpp:184
T get(T... args)
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
void execute_callback() override
Definition: timer.hpp:146
std::shared_ptr< rcl_timer_t > timer_handle_
Definition: timer.hpp:104
Definition: timer.hpp:43
bool is_canceled()
Return the timer cancellation state.