[ROS2] best practice for composable node publishing data from blocking call
Hi
I am converting a node from ROS1 to ROS2. This node uses a proprietary SDK to connect to a sensor. The function which reads from the sensor performs a blocking call.
This is the code in ROS1:
while (ros::ok()) {
data = get_data_from_sensor(); // may block indefinitely or a very short time
publisher.publish(data);
ros::spinOnce();
}
For the ROS2 conversion, I want to make use of the composable node architecture. get_data_from_sensor()
will likely become a class member function, but how will I call it? When started as a component, ros::spin()
is called on the node. As far as I know, no custom entry point exists.
Since the sensor events do not occur on a regular interval, using a timer does not seem feasible. In the node's constructor, I could create and run a thread with a loop like this:
while (rclcpp::ok()) {
data = get_data_from_sensor(); // may block indefinitely or a very short time
publisher.publish(data);
}
However, this feels odd. My gut says, I am not adhering to some ROS2 paradigm. Is there a best practice for composable nodes publishing data from blocking calls at variable intervals?
Kind regards