[ros-users] [Discourse.ros.org] [Next Generation ROS] ROS 2 Time vs std::chrono

andreaspasternak ros.discourse at gmail.com
Mon Oct 8 21:34:42 UTC 2018



@sloretz:  You can wrap the clock provided by the node again in a `std::chrono` clock wrapper and you would have simulation time available.:

```
struct ros2_node_clock
{
  ros2_node_clock(std::shared_ptr<Clock> clock) : m_clock(clock) {}
  typedef std::chrono::nanoseconds 				duration;
  typedef duration::rep	  				rep;
  typedef duration::period	  				period;
  typedef std::chrono::time_point<ros2_clock, duration> 	time_point;

  static constexpr bool is_steady = false;

  static time_point
  now() noexcept {
    return time_point(duration(clock->now().nanoseconds()));
  }


  std::shared_ptr<Clock> m_clock;
};

ros2_node_clock rclcpp::None::get_std_clock() {
   return ros2_node_clock(get_clock());
}
```

The additional safety of not mixing clocks is lost in this case, but ROS 2 time is also not safe in this regard and not likely solvable.





---
[Visit Topic](https://discourse.ros.org/t/ros-2-time-vs-std-chrono/6293/5) or reply to this email to respond.




More information about the ros-users mailing list