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

ROSCPP - reading HZ from code

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

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(buffer.data(), 128, pipe) != NULL)
        {
            result += buffer.data();
        }
    }
    pclose(pipe);
    return result;
}
edit retag flag offensive close merge delete

Comments

read a HZ data of a rostopic

?

gvdhoorn gravatar image gvdhoorn  ( 2018-05-21 14:07:34 -0500 )edit

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

Gil404 gravatar image Gil404  ( 2018-05-21 14:12:21 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

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

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

Comments

1

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 image gvdhoorn  ( 2018-05-21 14:07:06 -0500 )edit

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

Gil404 gravatar image Gil404  ( 2018-05-21 14:20:55 -0500 )edit

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

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-05-21 17:04:26 -0500 )edit

Question Tools

Stats

Asked: 2018-05-21 13:23:51 -0500

Seen: 1,453 times

Last updated: May 21 '18