Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
clock_publisher.hpp
1 // Copyright (c) 2024, Open Navigation LLC
2 // Copyright (c) 2026, Dexory (Tony Najjar)
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef NAV2_LOOPBACK_SIM__CLOCK_PUBLISHER_HPP_
17 #define NAV2_LOOPBACK_SIM__CLOCK_PUBLISHER_HPP_
18 
19 #include <chrono>
20 #include <memory>
21 
22 #include "nav2_ros_common/lifecycle_node.hpp"
23 #include "rclcpp/rclcpp.hpp"
24 #include "rosgraph_msgs/msg/clock.hpp"
25 
26 namespace nav2_loopback_sim
27 {
28 
36 {
37 public:
44  nav2::LifecycleNode::WeakPtr node,
45  double speed_factor = 1.0);
46 
50  void start();
54  void stop();
59  void setSpeedFactor(double speed_factor);
60 
61 protected:
65  void timerCallback();
69  void resetTimer();
70 
71  nav2::LifecycleNode::WeakPtr node_;
72  rclcpp::Logger logger_;
73 
74  rclcpp::Publisher<rosgraph_msgs::msg::Clock>::SharedPtr clock_pub_; // nosemgrep
75  rclcpp::TimerBase::SharedPtr timer_;
76 
77  static constexpr double kResolution = 0.01; // 10ms sim-time step
78  static constexpr double kMinWallPeriod = 0.001; // 1ms / 1000 Hz max
79  double speed_factor_;
80  rclcpp::Time sim_time_;
81  std::chrono::steady_clock::time_point last_wall_time_;
82 };
83 
84 } // namespace nav2_loopback_sim
85 
86 #endif // NAV2_LOOPBACK_SIM__CLOCK_PUBLISHER_HPP_
Publishes simulated clock to /clock using wall time. Uses wall timers so that it works correctly even...
void start()
Start publishing /clock.
void setSpeedFactor(double speed_factor)
Update the simulation speed factor.
void resetTimer()
(Re)create the wall timer based on current speed_factor
void stop()
Stop publishing /clock.
ClockPublisher(nav2::LifecycleNode::WeakPtr node, double speed_factor=1.0)
Construct a ClockPublisher.
void timerCallback()
Wall-timer callback that advances sim time and publishes /clock.