Ask Your Question

Cannot set ROS2 publisher rate correctly

asked 2022-04-27 03:35:24 -0500

alberto gravatar image

updated 2022-04-27 07:20:34 -0500

Hi all,

I want my publisher to publish at an high rate, for example 500 or 1000Hz. I've tried using different strategies, first:

rclcpp::Rate loop_rate(1000);
while (rclcpp::ok()) {

But with ros2 topic hz /name, I get an average rate of 125Hz.

So I tried to use a WallTimer, as suggested in the tutorial:

timer_ = this->create_wall_timer(8.5us, std::bind(&RosNode::publish_state, this));

Here I found a magic number: 8.5us that gives me an average rate of 360Hz (more or less). But there are a few things that I don't understand:

  1. if 8.5 microseconds is the period, then the frequency should be 1/8.5us, that is way bigger then 360Hz.
  2. if I set 9us, I get an average rate of 3200Hz.

I also tried to build the same code of the tutorial, changing the WallTimer period to 1ms, in order to get a rate of 1000Hz, but instead I get an average of 160Hz.

I read that ros2 topic hz /name can return a wrong rate, but this seems too much.

Did someone else have this problem? What am I doing wrong?

EDIT: I've done more testing. Trying to calculate my frequency using 2 timestamps taken with this->now(), I noticed that till 100/120Hz both ros2 topic hz and my calculations find the correct rate. Otherwise, for higher rate, I get wrong results with both!

Is this a ROS2 problem?

edit retag flag offensive close merge delete



I think you can get a lot of knowledge from this discussion on almost the same topic: (if you haven't found it yourself before, of course (: )

ljaniec gravatar image ljaniec  ( 2022-04-27 07:51:40 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2022-04-27 15:21:18 -0500

aprotyas gravatar image

updated 2022-04-27 15:26:03 -0500

ros2 topic hz is a Python-based CLI tool and has measurement problem for high publish rates. The discourse thread shared by @ljaniec brings it up.

You might want to check bandwidth with this tool instead: Please let us know how it goes here!

edit flag offensive delete link more


I tried it. I had to solve some error during build because in my Foxy version there is no rclcpp::GenericSubscription, so I used rclcpp::Subscription with my custom msg. Still the problem remains! With frequency <100Hz the tool gives me a correct esteem, but with higher ones (I tested 1000Hz) I get around 120Hz :(

alberto gravatar image alberto  ( 2022-04-28 03:33:04 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2022-04-27 03:35:24 -0500

Seen: 118 times

Last updated: Apr 27