Understanding async spinner in ROS1
Hi,
so I am trying to understand async spinner in ROS1. I know ROS1 is obsolete but unfortunately, my institution is still using it...
I have noticed some weird behaviour which I would like that someone explains to me. I made a node which listens on a topic which publishes with rate 10 Hz. My callback function intentionally takes 1s to run. In the callback, I am printing the seq id of the received msg. My callback queue is just 1.
When I use ros::spin(), it is single-threaded, blocking behaviour, so I am getting msg: 1, 11, 21, 31 and so on...
When I use:
ros::AsyncSpinner spinner(10);
spinner.start();
ros::waitForShutdown();
instead of ross::spin(), I would expect to see msg: 1,2,3,4,5, and so on... but I am still getting 1, 11, 21, 31 and so on... Practically it looks that the async spinner is not launching new threads. The only issue I can think off is if the sleep in my callback is somehow blocking all threads. I tried to use ros::Duration(1.0).sleep(); and std::this_thread::sleep_for (std::chrono::seconds(1)); and both gives me the same behaviour.
So can someone please tell me what I am doing wrong with the AsyncSpinner?
PS: If you wonder why my code is so silly, it is part of a bigger project, I just tried my best to strip it to necessary basics to understand the AsyncSpinner.
I am using Ubuntu 20.04 and I am running Noetic.
The code:
#include <chrono>
#include <thread>
#include <ros/ros.h>
#include "sensor_msgs/BatteryState.h"
void BatteryCallback(const sensor_msgs::BatteryState::ConstPtr& msg)
{
ROS_INFO("Msg id:%d", msg->header.seq);
//ros::Duration(1.0).sleep();
std::this_thread::sleep_for (std::chrono::seconds(1));
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "battery_monitor");
ros::NodeHandle nh;
ros::Subscriber battery_sub = nh.subscribe("/fcs/battery_state", 1, BatteryCallback);
ROS_INFO("Battery monitor is running");
//ros::spin();
ros::AsyncSpinner spinner(10);
spinner.start();
ros::waitForShutdown();
return 0;
}
CmakeList:
cmake_minimum_required(VERSION 3.0.2)
project(battery_monitor)
find_package(catkin REQUIRED COMPONENTS
roscpp
sensor_msgs
)
catkin_package(
CATKIN_DEPENDS roscpp sensor_msgs
)
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
add_executable(battery_monitor src/battery_monitor.cpp)
target_link_libraries(battery_monitor ${catkin_LIBRARIES})
Package.xml
<?xml version="1.0"?>
<package format="2">
<name>battery_monitor</name>
<version>1.0.0</version>
<description>This package ...</description>
<maintainer email="L@M.com">L</maintainer>
<license>MIT</license>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>sensor_msgs</build_depend>
<build_export_depend>roscpp</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>sensor_msgs</exec_depend>
</package>