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"
29 
30 
31 namespace rclcpp
32 {
33 class Clock;
34 
36 {
37 public:
39 
45  explicit TimeSource(rclcpp::Node::SharedPtr node);
46 
48 
52  TimeSource();
53 
55 
59  void attachNode(rclcpp::Node::SharedPtr node);
60 
62 
75  void attachNode(
76  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface,
77  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_interface,
78  rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_interface,
79  rclcpp::node_interfaces::NodeServicesInterface::SharedPtr node_services_interface,
80  rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_interface,
81  rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock_interface,
82  rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_interface);
83 
86  void detachNode();
87 
89 
94  void attachClock(rclcpp::Clock::SharedPtr clock);
95 
98  void detachClock(rclcpp::Clock::SharedPtr clock);
99 
102  ~TimeSource();
103 
104 private:
105  // Preserve the node reference
106  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_;
107  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_;
108  rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_;
109  rclcpp::node_interfaces::NodeServicesInterface::SharedPtr node_services_;
110  rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
111  rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock_;
112  rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_;
113 
114  // Store (and update on node attach) logger for logging.
115  Logger logger_;
116 
117  // The subscription for the clock callback
118  using MessageT = rosgraph_msgs::msg::Clock;
119  using Alloc = std::allocator<void>;
121  std::shared_ptr<SubscriptionT> clock_subscription_;
122  std::mutex clock_sub_lock_;
123 
124  // The clock callback itself
125  void clock_cb(const rosgraph_msgs::msg::Clock::SharedPtr msg);
126 
127  // Create the subscription for the clock topic
128  void create_clock_sub();
129 
130  // Destroy the subscription for the clock topic
131  void destroy_clock_sub();
132 
133  // Parameter Event subscription
134  using ParamMessageT = rcl_interfaces::msg::ParameterEvent;
136  std::shared_ptr<ParamSubscriptionT> parameter_subscription_;
137 
138  // Callback for parameter updates
139  void on_parameter_event(const rcl_interfaces::msg::ParameterEvent::SharedPtr event);
140 
141  // An enum to hold the parameter state
142  enum UseSimTimeParameterState {UNSET, SET_TRUE, SET_FALSE};
143  UseSimTimeParameterState parameter_state_;
144 
145  // An internal method to use in the clock callback that iterates and enables all clocks
146  void enable_ros_time();
147  // An internal method to use in the clock callback that iterates and disables all clocks
148  void disable_ros_time();
149 
150  // Internal helper functions used inside iterators
151  static void enable_ros_time(rclcpp::Clock::SharedPtr clock);
152  static void disable_ros_time(rclcpp::Clock::SharedPtr clock);
153  static void set_clock(
154  const builtin_interfaces::msg::Time::SharedPtr msg,
155  bool set_ros_time_enabled,
156  rclcpp::Clock::SharedPtr clock);
157 
158  // Local storage of validity of ROS time
159  // This is needed when new clocks are added.
160  bool ros_time_active_;
161  // Last set message to be passed to newly registered clocks
162  rosgraph_msgs::msg::Clock::SharedPtr last_msg_set_;
163 
164  // A lock to protect iterating the associated_clocks_ field.
165  std::mutex clock_list_lock_;
166  // A vector to store references to associated clocks.
167  std::vector<rclcpp::Clock::SharedPtr> associated_clocks_;
168  // A handler for the use_sim_time parameter callback.
169  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr sim_time_cb_handler_ = nullptr;
170 };
171 
172 } // namespace rclcpp
173 
174 #endif // RCLCPP__TIME_SOURCE_HPP_
rclcpp::TimeSource::attachClock
void attachClock(rclcpp::Clock::SharedPtr clock)
Attach a clock to the time source to be updated.
std::shared_ptr
std::vector< rclcpp::Clock::SharedPtr >
node.hpp
rclcpp::TimeSource
Definition: time_source.hpp:35
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
rclcpp::Subscription
Subscription implementation, templated on the type of message this subscription receives.
Definition: subscription.hpp:69
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rclcpp::TimeSource::TimeSource
TimeSource()
Empty constructor.
rclcpp::Logger
Definition: logger.hpp:77
rclcpp::TimeSource::detachClock
void detachClock(rclcpp::Clock::SharedPtr clock)
Detach a clock to the time source.
rclcpp::TimeSource::~TimeSource
~TimeSource()
TimeSource Destructor.
rclcpp::TimeSource::attachNode
void attachNode(rclcpp::Node::SharedPtr node)
Attack node to the time source.
rclcpp::TimeSource::detachNode
void detachNode()
Detach the node from the time source.
std::allocator
std::mutex
node_parameters_interface.hpp