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

How to create a custom clock using roscpp

asked 2023-04-17 04:32:09 -0600

fbelmonteklein gravatar image

I was thinking of a way of creating a custom clock that I can use to step all my nodes.

I investigated a bunch of complicated things like using the Player class from rosbag, but I think I am overcomplicating this.

Is there any example of just creating a custom clock? I was trying to see if there was a test in ros_comm or ros_comm_msgs but I could not find anything.

Thank you for your help!

edit retag flag offensive close merge delete



quick comment: just create a publisher publishing rosgraph_msgs/Clock, making sure to keep track of whatever time you want to publish.

Make sure there is only one, publish to the correct topic (ie: /clock) and set use_sim_time to true for all involved nodes.

That'd be it.

See also wiki/Clock.

gvdhoorn gravatar image gvdhoorn  ( 2023-04-17 04:55:03 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2023-04-17 06:37:01 -0600

fbelmonteklein gravatar image

I was overcomplicating it. Here is a simple example:

#include "ros/init.h"
#include "ros/node_handle.h"
#include "ros/publisher.h"
#include "rosgraph_msgs/Clock.h"
#include <boost/chrono/duration.hpp>
#include <boost/chrono/system_clocks.hpp>
#include <boost/thread/thread.hpp>

class custom_clock
        void pub_once_different_time();
        uint64_t current_time, clock_step_nanosseconds;
        boost::chrono::nanoseconds real_sim_clock_between_msgs_duration;
        ros::Publisher clock_pub;

    ros::NodeHandle n;
    clock_pub = n.advertise<rosgraph_msgs::Clock>("/clock", 10); 
    current_time = boost::chrono::system_clock::now().time_since_epoch().count();

void custom_clock::pub_once_different_time()
    rosgraph_msgs::Clock clock_msg;
    current_time += clock_step_nanosseconds;


int main(int argc, char **argv)
    ros::init(argc, argv, "custom_clock");
    custom_clock c;
    //set fake rate:
    c.real_sim_clock_between_msgs_duration = boost::chrono::milliseconds(10);
    c.clock_step_nanosseconds = 100;
        boost::this_thread::sleep_for( c.real_sim_clock_between_msgs_duration );

    return 0;
edit flag offensive delete link more

Question Tools


Asked: 2023-04-17 04:32:09 -0600

Seen: 193 times

Last updated: Apr 17