Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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 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

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