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

How to count the messages received (or the times a callback function is called) from a sensor

asked 2016-10-08 09:02:38 -0500

patrchri gravatar image

updated 2016-10-09 07:42:42 -0500

What I am really looking for is a way to count the messages that a node receives (or the times a node subscribes to a topic) and print this result on screen, like:

We have listened to 1 message/s
We have listened to 2 message/s
We have listened to 3 message/s

(and so on)

and somewhere around these prints, when we reach for example 20 messages the node will print a differenent message like for example:

We have listened to 20 message/s
We reached 20 messages !!!
We have listened to 21 message/s
We have listened to 22 message/s

(and so on)

I thought of putting a counter at the start of the node, but everytime the node will start I will reinitialize the counter, so it won't work. I also thought of starting another node at the same time that the listener will start and the other node will count the messages of the listener, but I doubt this will work, so I am looking for a faster way if this is possible.

Thanks for answering and for your time in advance,

Chris

edit retag flag offensive close merge delete

Comments

Could you perhaps describe what you really want to do? I ask because it sounds like your asking for something that isn't actually really what you're trying to solve, but instead is a potential solution that you want vetted by us.

gvdhoorn gravatar image gvdhoorn  ( 2016-10-08 09:24:52 -0500 )edit

I want to capture the following frames of a Kinect v2 sensor in a way that I can later do something with their difference, e.g. find a moving object, implement visual odometry etc.Outside ROS is simpler.I was thinking that if I could have a counter of messages/frames of the sensor,I could do that...

patrchri gravatar image patrchri  ( 2016-10-08 10:13:38 -0500 )edit

...by the number of the counter. In other words,when the counter is an even number capture in the cv_bridge pointer one frame/message/image and when it is an odd number capture the following frame in another pointer while keeping the other pointer intact.And this in the loop of the ROS data exchange

patrchri gravatar image patrchri  ( 2016-10-08 10:17:44 -0500 )edit

What I would do is create a global counter and increment it every time you enter the subscriber callback function. Like you were saying though, if you restart the node, it will reset. You can just call spin() though to keep the node running, if that is fine.

JoshMarino gravatar image JoshMarino  ( 2016-10-08 14:44:32 -0500 )edit

I cannot understand what you mean...If I call ros::spin() I won't get reinitialization of the counter ?

patrchri gravatar image patrchri  ( 2016-10-09 07:41:39 -0500 )edit

In your main function, you initialize the node, subscribe to the topic of interest, and then call ros spin. Outside of the main function, you would have the callback function and initialization of the global variable. All spin does is keep the node running until node shutdown.

JoshMarino gravatar image JoshMarino  ( 2016-10-09 11:01:30 -0500 )edit

I will try tomorrow...In a ros listener how many times does the initialization of the node take place? I mean, the node runs again and again, but does this mean the whole code of the node is being executed again and again?

patrchri gravatar image patrchri  ( 2016-10-09 15:44:16 -0500 )edit
1

After initializing the node, it will continue to run until the program ends, the node is closed, or ROS is shutdown. Spin() will stop the node from being closed.

Each time the callback function is entered inside the node, the node does not need to be restarted each time.

JoshMarino gravatar image JoshMarino  ( 2016-10-09 20:26:18 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-10-10 13:34:48 -0500

patrchri gravatar image

Ok after JohnMarino's advice, I found it.

You just have to initialize a global variable and increment this value at the callback function and not outside of it.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-10-08 09:02:38 -0500

Seen: 2,028 times

Last updated: Oct 10 '16