ROS 2 rclcpp + rcl - rolling  rolling-20536064
ROS 2 C++ Client Library with ROS Client Library
rate.hpp
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__RATE_HPP_
16 #define RCLCPP__RATE_HPP_
17 
18 #include <chrono>
19 #include <memory>
20 
21 #include "rclcpp/clock.hpp"
22 #include "rclcpp/duration.hpp"
23 #include "rclcpp/macros.hpp"
24 #include "rclcpp/visibility_control.hpp"
25 
26 namespace rclcpp
27 {
28 
29 class RateBase
30 {
31 public:
32  RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(RateBase)
33 
34  RCLCPP_PUBLIC
35  virtual ~RateBase() {}
36 
37  RCLCPP_PUBLIC
38  virtual bool sleep() = 0;
39 
40  RCLCPP_PUBLIC
41  virtual rcl_clock_type_t get_type() const = 0;
42 
43  RCLCPP_PUBLIC
44  virtual void reset() = 0;
45 };
46 
47 using std::chrono::duration;
48 using std::chrono::duration_cast;
49 using std::chrono::nanoseconds;
50 
51 class Rate : public RateBase
52 {
53 public:
54  RCLCPP_SMART_PTR_DEFINITIONS(Rate)
55 
56  RCLCPP_PUBLIC
57  explicit Rate(
58  const double rate,
59  const Clock::SharedPtr & clock = std::make_shared<Clock>(RCL_SYSTEM_TIME));
60 
61  RCLCPP_PUBLIC
62  explicit Rate(
63  const Duration & period,
64  const Clock::SharedPtr & clock = std::make_shared<Clock>(RCL_SYSTEM_TIME));
65 
66  RCLCPP_PUBLIC
67  virtual bool
68  sleep();
69 
70  RCLCPP_PUBLIC
71  virtual rcl_clock_type_t
72  get_type() const;
73 
74  RCLCPP_PUBLIC
75  virtual void
76  reset();
77 
78  RCLCPP_PUBLIC
79  std::chrono::nanoseconds
80  period() const;
81 
82 private:
83  RCLCPP_DISABLE_COPY(Rate)
84 
85  Clock::SharedPtr clock_;
86  Duration period_;
87  Time last_interval_;
88 };
89 
90 class WallRate : public Rate
91 {
92 public:
93  RCLCPP_PUBLIC
94  explicit WallRate(const double rate);
95 
96  RCLCPP_PUBLIC
97  explicit WallRate(const Duration & period);
98 };
99 
100 } // namespace rclcpp
101 
102 #endif // RCLCPP__RATE_HPP_
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
enum rcl_clock_type_e rcl_clock_type_t
Time source type, used to indicate the source of a time measurement.
@ RCL_SYSTEM_TIME
Use system time.
Definition: time.h:68