Ask Your Question

How can I know the rate the publisher is sending a msg with c++?

asked 2020-01-22 04:09:52 -0600

drone_lab gravatar image

updated 2020-01-22 04:27:52 -0600

Delb gravatar image

I want to have a subscriber that is able to print the rate at which a message is being sent using C++, what is the best way to do it?

The subscriber works in a really slower rate than the publisher.

Edit :

The subscriber is defined in a different process that has its own loop rate and the publisher has its own loop rate too, so basically, the callback function of a subscriber is called when a message is published but with a maximum rate of the loop of the subscriber process. (I don't know if I've explained myself well)

edit retag flag offensive close merge delete


The subscriber works in a really slower rate than the publisher.

Can you explain this ? The callback function of a subscriber is called when a message is published on the subscribed topic. Unless you do operations that take time in your callback (which you shouldn't), it should be the same rate as the publisher.

Delb gravatar imageDelb ( 2020-01-22 04:20:33 -0600 )edit

If I understand correctly you want to get the rate of a publisher using your current subscriber that has its loop rate slower than the publisher's one ? Can you change anything or is it fixed ? If you can't you could simply add a new node only subscribing to your publisher with a higher spin rate.By recording the time between two message received you could find the rate of your publisher.

Delb gravatar imageDelb ( 2020-01-22 04:31:42 -0600 )edit

That's a good approach, but the publisher rate is unknown to me and I don't want to create a node with a high spin rate (computational cost). Is there any other aproach? (I'll think about your idea, it is actually something that could work)

drone_lab gravatar imagedrone_lab ( 2020-01-22 04:42:45 -0600 )edit

A node with only a subscriber doesn't add a lot of computational cost since you are either sleeping or either in your callback function.

But i forgot to ask : Do you want to get the rate to actually use the value found or is it simply to find out the rate because it's currently unknown ?

Delb gravatar imageDelb ( 2020-01-22 04:49:17 -0600 )edit

Well, we want to get the rate as an information value, we work with drones and sometimes it's important to us to know that value for some publishers. I'm creating an interface that shows data from the drone and different process. Each different drone has different rates and sometimes things are not working properly because of that.

drone_lab gravatar imagedrone_lab ( 2020-01-22 05:03:49 -0600 )edit

If you don't want to use a very high loop rate, you can then write a node using ros::spin and a timer. It will answer to callbacks of your subscriber as soon as a message is received, but will only execute your main function in the rate you have specified for your timer. Take a look here:

lucascoelho gravatar imagelucascoelho ( 2020-01-22 12:01:43 -0600 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2020-01-22 06:37:11 -0600

gvdhoorn gravatar image

updated 2020-01-22 06:38:33 -0600

I believe the canonical way to do this -- even from a roscpp application -- would be to use Topic statistics (and the support in rqt_graph).

This comes with a bit of overhead though: ros/ros_comm#1626.

The advantage would be this requires no additional subscribers ( @drone_lab : the "computational cost" is not in spinning, it's in serialising the messages to an additional subscriber).

edit flag offensive delete link more

answered 2020-01-22 05:20:01 -0600

cpetersmeier gravatar image

updated 2020-01-22 05:21:35 -0600

I found a similar question in this thread. You may be able to use the rostopic and ROSTopicHz classes to get access to the publishing / subscription rates. But I agree with the answer in the linked thread that since this is an internal library, maybe the API can get changed, which would break your code. Anyway, let us know how it goes!

edit flag offensive delete link more

answered 2020-01-22 04:13:01 -0600

lucascoelho gravatar image

No need to write a subscriber, there is a command that does that for you, take a look

edit flag offensive delete link more


Yes, I know about that command, but what I'm trying to do is an interface that shows specific information to the user. I'm already showing some data of the messages I'm receiving but it is important to us to know the rate too of some of these messages and being shown in that interface.

drone_lab gravatar imagedrone_lab ( 2020-01-22 04:28:22 -0600 )edit

Yes, I know about that command

could you please include information like this (ie: things you've already found and/or tried) in your OP next time? It would save everyone time and effort if we already know what you know.

gvdhoorn gravatar imagegvdhoorn ( 2020-01-22 06:39:16 -0600 )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: 2020-01-22 04:09:52 -0600

Seen: 33 times

Last updated: Jan 22