[ROS2] Additional Executor within a Node

asked 2023-02-03 04:12:04 -0500

wienans gravatar image

Hi all, would like to have multiple robot_localization ukf's running in "parallel" to track robots in the cloud. I have some topics directly from the Device like Imu or GPS position and additional topics like a Position from a external tracking system which is connected to the cloud server.

To handle all the communication and creating of the UKFs dynamically i wanted to create a Node which handles these things. As it is in theory quite convenient to create a node in CPP and you can also do it within the Class of the already executing node in wanted to ask if it is a good idea to spawn a thread within the Node which executes a executor.spin(). Because the Node itself is already within a executor and i don't think we can access the main executor of the already existing node to add additional nodes to it.

Code of the executable adding my main node

rclcpp::executors::SingleThreadedExecutor executor;
executor.add_node(node->get_node_base_interface());
executor.spin();
executor.remove_node(node->get_node_base_interface());
rclcpp::shutdown();

Within the Main Node Class i spawn a thread within the constructor

 ukf_filter_thread_ = std::make_shared<std::thread>(&MultiUKFTracker::spinFilter, this);

Thread Function

void spinFilter()
{
  filter_executor_.spin();
}

And dynamically add new robot_localization filters to this executor like this

ukf_ = std::make_shared<robot_localization::RosUkf>(filter_options);
ukf_->getFilter().setConstants(
ukf_->declare_parameter("alpha", 0.001),
ukf_->declare_parameter("kappa", 0.0),
ukf_->declare_parameter("beta", 2.0));
ukf_->initialize();
ukf_->reset();
filter_executor_.add_node(ukf_->get_node_base_interface());

It works yes but i also had sometimes some weird things happening that the program doesn't recieve data anymore so i am not sure if there is somewhere a deadlock happening because of this hacky code or if somewhere else is a problem.

I am open to suggestions how to manage such a case or other ideas which are "saver". I need to say both executors for now are singlethreaded executors. I wanted to change the filter_executor_ to multi threaded, but in this case the program stalled immediately

edit retag flag offensive close merge delete