ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

`rclcpp::Clock(RCL_ROS_TIME).now()` vs. `node_->get_clock()->now()`

asked 2021-12-02 04:11:19 -0600

galou gravatar image

Is rclcpp::Clock(RCL_ROS_TIME).now() always the same as node_->get_clock()->now()?

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted
1

answered 2022-02-23 07:28:07 -0600

galou gravatar image

updated 2022-02-23 07:29:13 -0600

Well, it looks that the answer is NO. I had some issues with this and wasn't satisfied with the answer so I wrote a small node to test these clocks. The test is carried out with Galactic. The complete code can be found at https://github.com/galou/test_clock and the source is below

#include <chrono>
#include <string>

#include <rclcpp/rclcpp.hpp>

using namespace std::chrono_literals;

class TestClockNode : public rclcpp::Node
{
  public:

    TestClockNode(
        const std::string & node_name,
        const rclcpp::NodeOptions & options = rclcpp::NodeOptions()) :
      rclcpp::Node{node_name, options}
    {
      timer_ = create_wall_timer(1s, [this] () {timerCallback();});
    }

  private:

    rclcpp::TimerBase::SharedPtr timer_;
    void timerCallback()
    {
      RCLCPP_INFO_STREAM(get_logger(), "now(): " << now().seconds());
      RCLCPP_INFO_STREAM(get_logger(), "  rclcpp::Clock{}.now(): " << rclcpp::Clock{}.now().seconds());
      RCLCPP_INFO_STREAM(get_logger(), "  rclcpp::Clock{RCL_ROS_TIME}.now(): " << rclcpp::Clock{RCL_ROS_TIME}.now().seconds());
      RCLCPP_INFO_STREAM(get_logger(), "  rclcpp::Clock{RCL_SYSTEM_TIME}.now(): " << rclcpp::Clock{RCL_SYSTEM_TIME}.now().seconds());
      RCLCPP_INFO_STREAM(get_logger(), "  rclcpp::Clock{RCL_STEADY_TIME}.now(): " << rclcpp::Clock{RCL_STEADY_TIME}.now().seconds());
    }

};

int
main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);

  auto node = std::make_shared<TestClockNode>("test_clock");
  if (node == nullptr)
  {
    return 1;
  }

  auto executor = rclcpp::executors::SingleThreadedExecutor{};
  executor.add_node(node);
  executor.spin();

  rclcpp::shutdown();

  return 0;
}

You need a running simulated time provider (i.e. a simulator) to test and you need to launch with

ros2 run test_clock test_clock --ros-args --param use_sim_time:=true --

Example of output:

[INFO] [1645622804.092181382] [test_clock]: now(): 2216.74
[INFO] [1645622804.092376229] [test_clock]:   rclcpp::Clock{}.now(): 1.64562e+09
[INFO] [1645622804.092445844] [test_clock]:   rclcpp::Clock{RCL_ROS_TIME}.now(): 1.64562e+09
[INFO] [1645622804.092512516] [test_clock]:   rclcpp::Clock{RCL_SYSTEM_TIME}.now(): 1.64562e+09
[INFO] [1645622804.092567862] [test_clock]:   rclcpp::Clock{RCL_STEADY_TIME}.now(): 199532
edit flag offensive delete link more

Comments

I see the results, but looking at the source I'm really not sure why there would be a difference between the two. rclcpp::Node defaults to a clock of type RCL_ROS_TIME, and the node_->now() function simply delegates to the clock's now() function instead.

aprotyas gravatar image aprotyas  ( 2022-02-27 03:02:01 -0600 )edit
1

answered 2022-09-23 06:51:22 -0600

tsingakbar gravatar image

When use_sim_time is enabled, RCL_ROS_TIME type clock will use time source from topic /clock.

But we have to attach the rcrclcpp::Clock(RCL_ROS_TIME) to one ros node(and furthor one executor) to be able subscibe to topic /clock and update simulated time stored in RCL_ROS_TIME clock instance. According to this understanding, we can also conclude that, as long as the attached node and executor is not spin()ing, the simulated clock won't be updated.

That's the behavior of node_->get_clock().

A standalone rcrclcpp::Clock(RCL_ROS_TIME) have no way to subscribe to topic /clock, then as a fallback strategy, it will behave like a RCL_SYSTEM_TIME type clock.

edit flag offensive delete link more
0

answered 2021-12-04 15:13:30 -0600

aprotyas gravatar image

Well, if the rclcpp::Clock object on which you'll call now() refers to the clock belonging to the rclcpp::Node object, then yes, they are the same. These can semantically mean different things if you're talking about different clocks.

edit flag offensive delete link more
0

answered 2021-12-02 20:50:55 -0600

osilva gravatar image

Looking at the source code for both, I believe they are the same:

For rcrclcpp::Clock(RCL_ROS_TIME).now()source code: https://docs.ros2.y org/foxy/api/rclcpp/clock_8hpp_source.html

And for node_->get_clock()->now()source code: https://docs.ros2.org/dashing/api/rcl...

Both are declared the same way:

 RCLCPP_PUBLIC
   Time
   now()

The only difference I see is the first one is more direct method .

edit flag offensive delete link more

Question Tools

5 followers

Stats

Asked: 2021-12-02 04:11:19 -0600

Seen: 7,587 times

Last updated: Sep 23 '22