[ros2] Multithreaded Executors and parameters
I'm running into an issue in attaching a node to a MultiThreadedExecutor. The basic code is structured as follows:
#include <rclcpp/rclcpp.hpp>
#include <health/health.h>
int main(int argc, char* argv[])
{
rclcpp::init(argc, argv);
// make multithreaded executor
rclcpp::executors::MultiThreadedExecutor executor;
// make nodehandle
auto nh = std::make_shared<rclcpp::Node>("health_status");
// add to the executor
executor.add_node(nh);
// declare some parameters
nh->declare_parameter("warning1",0.0);
// create an object that sets up some callbacks
Health h(nh);
// wait for shutdown and then clean up
executor.spin();
RCLCPP_INFO(nh->get_logger(),"Exiting health_status node");
rclcpp::shutdown();
return 0;
}
The code compiles and appears to set up the correct publishers and subscribers (which are all abstracted into the "Health" object). However, if I call "ros2 param list" while the node is running, the call appears to hang forever. If I change my executor to a SingleThreadedExecutor or use rclcpp::spin() instead, "ros2 param list" works as it's supposed to. The code (as currently structured) does not actually use the MultiThreadedExecutor's functionality to run multiple callback threads; however, we would like to eventually support that functionality.
I have come across this thread, which appears to address a related problem. However, I'm not sure how to modify my code. Any help would be appreciated.
Asked by fatadama on 2019-09-23 16:55:15 UTC
Answers
You may need to add two CallbackGroups and pass then as arguments while calling create_subscription()
or create_publisher()
. This multithread_executor.cpp will help you.
Asked by ignacio on 2019-10-17 21:54:25 UTC
Comments
that link is dead now. Is this what you were referring to: https://github.com/ros2/rclcpp/blob/master/rclcpp/src/rclcpp/executors/multi_threaded_executor.cpp ?
Asked by mysteriousmonkey29 on 2020-06-03 12:53:25 UTC
this link is actual implementation of MultiThreadedExecutor
. I think it should be https://github.com/ros2/examples/blob/master/rclcpp/executors/multithreaded_executor/multithreaded_executor.cpp
Asked by ipa-hsd on 2020-06-14 13:48:35 UTC
just updated. Thanks guys!
Asked by ignacio on 2020-08-04 02:38:13 UTC
With OP not making it clear what's in "Health" object (we don't know e.g. if there are multiple callback hooks in it), I'm not sure if making multiple CallbackGroup
s can be a solution to OP's case?
Asked by 130s on 2023-05-25 16:11:22 UTC
Comments