fixed time timer

asked 2022-12-29 18:56:02 -0500

Young gravatar image

ros2 example recommends to use create_wall_timer to make a periodic task. But I think it waits the time after doing the callback. So it will wait longer time when the processing of callback takes longer time than usual.

I want to wait the fixed time no matter time the callback takes. What is the best way to do this in ros2? This is the sample code in c++ where it waits 1 ms to publish data:

    auto next = std::chrono::steady_clock::now();
    while (IsAlive()) {
        next += std::chrono::microseconds(1000);
        Publish(data);
        std::this_thread::sleep_until(next);
    }
edit retag flag offensive close merge delete