3
$\begingroup$

The issue I'm reporting is quite common looking at similar questions:

Shutdown a node from another node in ROS CPP

Start or stop the ROS node in another node

Could a node can be stoped by another node?

launch a node within another node

None of these answer to my case: ros2, cpp, no system calls allowed, component nodes. The first link suggests that a possible solution would be implementing a service but the old code snippets are no longer available...

My use case is this node to capture sound from microphones

https://github.com/ros-drivers/audio_common/blob/ros2/audio_capture/src/audio_capture_node.cpp

I'd like to record only when some conditions become true...

Thank you!

$\endgroup$

1 Answer 1

3
$\begingroup$

lifecycle nodes are the default solution for this. Activate your node when conditions are met, and deactivate it when it should not be active anymore. In your activate and deactivate transitions you can then implement the logic required. (see the official documentation and demo at github.com/ros2/demos/blob/foxy/lifecycle/README.rst)

An easy way to activate or deactivate a node is to only construct the subscription when a node should be active. If it should not do anything anymore, this subscription is destroyed again.

$\endgroup$
3
  • 1
    $\begingroup$ Thanks for sharing this answer! It's very good, but could be improved with a link to the relevant documentation or by providing a demo (github.com/ros2/demos/blob/foxy/lifecycle/README.rst). $\endgroup$
    – cst0
    Commented Sep 6, 2023 at 14:49
  • $\begingroup$ Thank you very much! lifecycle nodes are quite a new and powerful tool but not sure how to encapsulate an already released code into a lifecycle node... As for the topic-based solution: I am not sure about you suggestion. I thought about a topic where publish an true false message so that the node can execute or not the code in its listener callback... $\endgroup$ Commented Sep 7, 2023 at 9:06
  • 2
    $\begingroup$ @AntonioBono take a look at the link @cst0 sent, and github.com/ros2/demos/blob/foxy/lifecycle/src/… for an example of how a lifecycle node could be implemented. If you fork the node you already have, replace rclcpp::Node with rclcpp_lifecycle::LifecycleNode and implement the on_* functions. Then, in the on_activate function, you initialize the subscriber, isntead of initializing it in the constructor. $\endgroup$ Commented Sep 7, 2023 at 12:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.