rclcpp  master
C++ ROS Client Library API
time_source.hpp
Go to the documentation of this file.
1 // Copyright 2017 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__TIME_SOURCE_HPP_
16 #define RCLCPP__TIME_SOURCE_HPP_
17 
18 #include <memory>
19 #include <vector>
20 
21 #include "rcl/time.h"
22 
23 #include "builtin_interfaces/msg/time.hpp"
24 #include "rosgraph_msgs/msg/clock.hpp"
25 #include "rcl_interfaces/msg/parameter_event.hpp"
26 
27 #include "rclcpp/node.hpp"
30 
31 
32 namespace rclcpp
33 {
34 class Clock;
35 
37 {
38 public:
40  explicit TimeSource(rclcpp::Node::SharedPtr node);
41 
43  TimeSource();
44 
46  void attachNode(rclcpp::Node::SharedPtr node);
47 
49  void attachNode(
50  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface,
51  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_interface,
52  rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_interface,
53  rclcpp::node_interfaces::NodeServicesInterface::SharedPtr node_services_interface,
54  rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_interface,
55  rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock_interface,
56  rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_interface);
57 
59  void detachNode();
60 
62 
66  void attachClock(rclcpp::Clock::SharedPtr clock);
67 
69  void detachClock(rclcpp::Clock::SharedPtr clock);
70 
72  ~TimeSource();
73 
74 private:
75  // Preserve the node reference
76  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_;
77  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_;
78  rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_;
79  rclcpp::node_interfaces::NodeServicesInterface::SharedPtr node_services_;
80  rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
81  rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock_;
82  rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_;
83 
84  // Store (and update on node attach) logger for logging.
85  Logger logger_;
86 
87  // The subscription for the clock callback
88  using MessageT = rosgraph_msgs::msg::Clock;
91  std::shared_ptr<SubscriptionT> clock_subscription_;
92  std::mutex clock_sub_lock_;
93 
94  // The clock callback itself
95  void clock_cb(const rosgraph_msgs::msg::Clock::SharedPtr msg);
96 
97  // Create the subscription for the clock topic
98  void create_clock_sub();
99 
100  // Destroy the subscription for the clock topic
101  void destroy_clock_sub();
102 
103  // Parameter Client pointer
105 
106  // Parameter Event subscription
107  using ParamMessageT = rcl_interfaces::msg::ParameterEvent;
109  std::shared_ptr<ParamSubscriptionT> parameter_subscription_;
110 
111  // Callback for parameter updates
112  void on_parameter_event(const rcl_interfaces::msg::ParameterEvent::SharedPtr event);
113 
114  // An enum to hold the parameter state
115  enum UseSimTimeParameterState {UNSET, SET_TRUE, SET_FALSE};
116  UseSimTimeParameterState parameter_state_;
117 
118  // An internal method to use in the clock callback that iterates and enables all clocks
119  void enable_ros_time();
120  // An internal method to use in the clock callback that iterates and disables all clocks
121  void disable_ros_time();
122 
123  // Internal helper functions used inside iterators
124  static void enable_ros_time(rclcpp::Clock::SharedPtr clock);
125  static void disable_ros_time(rclcpp::Clock::SharedPtr clock);
126  static void set_clock(
127  const builtin_interfaces::msg::Time::SharedPtr msg,
128  bool set_ros_time_enabled,
129  rclcpp::Clock::SharedPtr clock);
130 
131  // Local storage of validity of ROS time
132  // This is needed when new clocks are added.
133  bool ros_time_active_;
134  // Last set message to be passed to newly registered clocks
135  rosgraph_msgs::msg::Clock::SharedPtr last_msg_set_;
136 
137  // A lock to protect iterating the associated_clocks_ field.
138  std::mutex clock_list_lock_;
139  // A vector to store references to associated clocks.
140  std::vector<rclcpp::Clock::SharedPtr> associated_clocks_;
141 };
142 
143 } // namespace rclcpp
144 
145 #endif // RCLCPP__TIME_SOURCE_HPP_
Definition: logger.hpp:77
Definition: allocator_common.hpp:24
Subscription implementation, templated on the type of message this subscription receives.
Definition: subscription.hpp:148
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
void detachClock(rclcpp::Clock::SharedPtr clock)
Definition: time_source.hpp:36
void attachClock(rclcpp::Clock::SharedPtr clock)
Attach a clock to the time source to be updated.
void attachNode(rclcpp::Node::SharedPtr node)