I just want to subscribe once to get data then do not want to subscribe anymore.
But after that, I want to continue to subscribe once then stopping again and again
I see I can use subsciber.unregister() but it will create and destroy many times? I worried after many times, my program will have a lot of trash? I do not have experience when run the program for a long time. Could you share experience for this case?
I do not have the hardware to test now. It would be great if you can share your experience with me.
Or could you share how to monitor trash from a program in Linux?
Hi @NguyenDuyDuc,
You can create a Rosject in using theconstruct website and give it a try. Even if you don’t have the hardware, you should be able to test it like that. I don’t know about your specific use case.
I rather use detroy_subscription() method. This is in Node class.
I think, I have used it once, but not in production just for learning.
It worked, as I remember. Unfortunately, I lost the source.
I do not know how does it work when you are doing this in a callback for the subscription.
As I suspected, destroy_subscription() did not work for me from the callback.
So I created a timer and destroyed the subscription from a timer. Very poor design.
Usually, when a framework does not allow me to do something with clean code, that means,
I try to do something against the framework (so it is discouraged) or I do it totally wrong or the framework needs a fix.
I want to get the data from the laser scan to detect whether the front of AMR is a person or a machine. It works but it inferences every time. I want to infer three times if the flag is on and then stopping. And again, again
Would you have some ideas to do it the best?
There are two ways to do this: blocking and non blocking way.
Blocking
there’s a function available rclcpp::wait_for_message(), which will block the thread and wait until a message is received in the topic. I’m sharing an old code I used for you as a reference:
This might be useful if the rate at which you want the message is extremely low, and if you want to have the latest data available. But you wont be able to anything while the thread is running and if the rate is significant then it might cause a bit of inefficiency as it creates and destroys a subscriber every time wait_for_message is being called. In other cases ill recommend the non blocking one.
2. Non Blocking
This is really simple, just use a subscriber and a update the message variable which you store locally in your object or anywhere you want. Whenever you need the message read from the said variable. Really simple. Although you might have to make the implementation thread safe depending on your spinner (using mutex while accessing or updating it).