How to count the messages received (or the times a callback function is called) from a sensor
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
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.
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...
...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
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.
I cannot understand what you mean...If I call ros::spin() I won't get reinitialization of the counter ?
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.
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?
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.