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

How to Improve Inaccurate ROS Timer

asked 2017-05-02 05:42:29 -0500

Winston gravatar image

updated 2017-05-02 07:11:17 -0500

I use a ROS timer in my progarm and plot the difference between the expected event time and the real event time as follows: event.current_real - event.current_expected;. However, I plot the data and find that the ROS Timer is NOT accurate AT ALL. In my program, the timer period is set to 0.002 second, but the largest error (0.01152 second) could be as large as more than 5.7 times of that. The plot is shown below: image description

I know from ROS wiki that ROS Timer does no intend to be accurate and is suject to the system load and other factors. So what I want to know is how to make it as accurate as possible? Is there any alternative method to substitute the ROS Timer?

I am using ROS Indigo and Ubuntu 14.04. The source code is listed below:

#include "ros/ros.h"
#include <fstream>

using namespace std;
std::ofstream time_file;

void callback1(const ros::TimerEvent& event)
    ros::Duration error_dur = event.current_real - event.current_expected;

int main(int argc, char **argv)
    ros::init(argc, argv, "talker");
    ros::NodeHandle n;

    ros::Timer timer1 = n.createTimer(ros::Duration(0.002), callback1);


    return 0;
edit retag flag offensive close merge delete



Simply ROS is not real time. This post probably would give you some idea why: There is also some new approach in ROS 2 regarding "real-time" aspect:

DavidN gravatar image DavidN  ( 2017-05-02 07:15:20 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2017-05-02 07:18:32 -0500

If you require jitter lower than millisecond level you probably need to look into using a hard real-time kernel and running your time critical loop in a thread that has been set to use hard real-time priority (or alternatively use OROCOS to hide some of the complexity). Searching for "hard real-time ROS" will provide some useful info such as this Q/A. Be warned that a hard real-time setup can be quite daunting to setup, so better be sure you actually need it ;)

Other option would be to just run your own thread and experiment with busy waiting etc., but I suspect that would be limited by OS scheduling still, similar to the ROS timer example you provided.

edit flag offensive delete link more


RTROS (mentioned in my answer on #q9330) could also be an option. Perhaps @Jan Carstensen can give an update on what the status of that is?

gvdhoorn gravatar image gvdhoorn  ( 2017-05-02 10:06:43 -0500 )edit

If I use OROCOS or RTROS, do I need to configure the real-time Linux kernel first? @Stefan Kohlbrecher.

Winston gravatar image Winston  ( 2017-05-02 20:01:39 -0500 )edit

If you want more than soft-realtime determinism, then yes.

gvdhoorn gravatar image gvdhoorn  ( 2017-05-03 07:41:24 -0500 )edit

Question Tools

1 follower


Asked: 2017-05-02 05:42:29 -0500

Seen: 1,109 times

Last updated: May 02 '17