0
$\begingroup$

Rosanswers logo

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


Originally posted by patrchri on ROS Answers with karma: 354 on 2016-10-08

Post score: 0


Original comments

Comment by gvdhoorn on 2016-10-08:
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.

Comment by patrchri on 2016-10-08:
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...

Comment by patrchri on 2016-10-08:
...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

Comment by JoshMarino on 2016-10-08:
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.

Comment by patrchri on 2016-10-09:
I cannot understand what you mean...If I call ros::spin() I won't get reinitialization of the counter ?

Comment by JoshMarino on 2016-10-09:
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.

Comment by patrchri on 2016-10-09:
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?

Comment by JoshMarino on 2016-10-09:
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.

Comment by patrchri on 2016-10-10:
Thank you, I did it...If you would like post an answer and I will accept yours for the karma points.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by patrchri with karma: 354 on 2016-10-10

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.