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  void
63  reset();
64 
66  virtual void
67  execute_callback() = 0;
68 
72 
74 
78 
80 
81  virtual bool is_steady() = 0;
82 
84 
90  bool is_ready();
91 
92 protected:
93  Clock::SharedPtr clock_;
95 };
96 
97 
100 
102 template<
103  typename FunctorT,
104  typename std::enable_if<
107  >::type * = nullptr
108 >
109 class GenericTimer : public TimerBase
110 {
111 public:
113 
114 
115 
120  explicit GenericTimer(
121  Clock::SharedPtr clock, std::chrono::nanoseconds period, FunctorT && callback,
122  rclcpp::Context::SharedPtr context
123  )
124  : TimerBase(clock, period, context), callback_(std::forward<FunctorT>(callback))
125  {
126  }
127 
129  virtual ~GenericTimer()
130  {
131  // Stop the timer from running.
132  cancel();
133  }
134 
135  void
136  execute_callback() override
137  {
139  if (ret == RCL_RET_TIMER_CANCELED) {
140  return;
141  }
142  if (ret != RCL_RET_OK) {
143  throw std::runtime_error("Failed to notify timer that callback occurred");
144  }
145  execute_callback_delegate<>();
146  }
147 
148  // void specialization
149  template<
150  typename CallbackT = FunctorT,
151  typename std::enable_if<
153  >::type * = nullptr
154  >
155  void
157  {
158  callback_();
159  }
160 
161  template<
162  typename CallbackT = FunctorT,
163  typename std::enable_if<
165  >::type * = nullptr
166  >
167  void
169  {
170  callback_(*this);
171  }
172 
173  bool
174  is_steady() override
175  {
176  return clock_->get_clock_type() == RCL_STEADY_TIME;
177  }
178 
179 protected:
181 
182  FunctorT callback_;
183 };
184 
185 template<
186  typename FunctorT,
187  typename std::enable_if<
188  rclcpp::function_traits::same_arguments<FunctorT, VoidCallbackType>::value ||
189  rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value
190  >::type * = nullptr
191 >
192 class WallTimer : public GenericTimer<FunctorT>
193 {
194 public:
196 
198  std::chrono::nanoseconds period,
199  FunctorT && callback,
200  rclcpp::Context::SharedPtr context)
201  : GenericTimer<FunctorT>(
202  std::make_shared<Clock>(RCL_STEADY_TIME), period, std::move(callback), context)
203  {}
204 
205 protected:
207 };
208 
209 } // namespace rclcpp
210 
211 #endif // RCLCPP__TIMER_HPP_
virtual void execute_callback()=0
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
Clock::SharedPtr clock_
Definition: timer.hpp:93
rmw_ret_t rcl_ret_t
Definition: timer.hpp:192
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:156
Definition: allocator_common.hpp:24
virtual ~GenericTimer()
Default destructor.
Definition: timer.hpp:129
Generic timer. Periodically executes a user-specified callback.
Definition: timer.hpp:109
#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:48
#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:174
T get(T... args)
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
void execute_callback() override
Definition: timer.hpp:136
std::shared_ptr< rcl_timer_t > timer_handle_
Definition: timer.hpp:94
Definition: timer.hpp:43