Ask Your Question

ROSCPP - reading HZ from code

asked 2018-05-21 13:23:51 -0600

Gil404 gravatar image

Hi Guys,

trying to figure out a way to read a HZ data of a rostopic in my C++ code. I see that there is no API for it, so I am trying to execute the cmd by using popen and reading from the file. However since it is a constant stream it makes the process to halt. Any idea what is the right way to do it? I need only one "iteration" of the data.

this is my current method that works for other calls:

std::string MyClass::InvokeCmd(std::string command)
    std::string result;
    std::array<char, 128> buffer;
    FILE *pipe = popen(command.c_str(), "r");

        while (fgets(, 128, pipe) != NULL)
            result +=;
    return result;
edit retag flag offensive close merge delete


read a HZ data of a rostopic


gvdhoorn gravatar imagegvdhoorn ( 2018-05-21 14:07:34 -0600 )edit

such as the avg rate, the min and the max....@gvdhoorn, do have any suggestion or tip?

Gil404 gravatar imageGil404 ( 2018-05-21 14:12:21 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2018-05-21 13:52:51 -0600

It would be easier to subscribe to the topic from your own node and then record the time between callbacks. After two callbacks you could unsubscribe and the job's done.

It's best to avoid using command line tools from within programs, it's a very ugly solution.

edit flag offensive delete link more



After two callbacks you could unsubscribe and the job's done.

Theoretically, this should be true. In reality though, you'll need a bit more events than two (to account for transient networking effects, OS scheduling, ROS event processing, etc).

gvdhoorn gravatar imagegvdhoorn ( 2018-05-21 14:07:06 -0600 )edit

I do understand that it is ugly, however, as also @gvdhoorn mentioned, 2 or 3 callbacks are not reliable enough.

Gil404 gravatar imageGil404 ( 2018-05-21 14:20:55 -0600 )edit

That's true, the longer you can sample the topic the more precise the frequency measurement will be.

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2018-05-21 17:04:26 -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: 2018-05-21 13:23:51 -0600

Seen: 460 times

Last updated: May 21 '18