Skip to content

Commit e3e8106

Browse files
committed
fix(ros2): salvage reentrant timer callback group (credit @nikola-j #4559)
Reentrant callback group + MultiThreadedExecutor for img/lidar/control timers. Signed-off-by: Bartok9 <[email protected]>
1 parent d109f0d commit e3e8106

3 files changed

Lines changed: 13 additions & 24 deletions

File tree

ros2/src/airsim_ros_pkgs/include/airsim_ros_wrapper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class AirsimROSWrapper
119119
CAR
120120
};
121121

122-
AirsimROSWrapper(const std::shared_ptr<rclcpp::Node> nh, const std::shared_ptr<rclcpp::Node> nh_img, const std::shared_ptr<rclcpp::Node> nh_lidar, const std::string& host_ip);
122+
AirsimROSWrapper(const std::shared_ptr<rclcpp::Node> nh, const std::shared_ptr<rclcpp::Node> nh_img, const std::shared_ptr<rclcpp::Node> nh_lidar, const std::string& host_ip, const std::shared_ptr<rclcpp::CallbackGroup> callback_group);
123123
~AirsimROSWrapper(){};
124124

125125
void initialize_airsim();
@@ -317,6 +317,7 @@ class AirsimROSWrapper
317317
std::shared_ptr<rclcpp::Node> nh_;
318318
std::shared_ptr<rclcpp::Node> nh_img_;
319319
std::shared_ptr<rclcpp::Node> nh_lidar_;
320+
std::shared_ptr<rclcpp::CallbackGroup> callback_group_;
320321

321322
// todo not sure if async spinners shuold be inside this class, or should be instantiated in airsim_node.cpp, and cb queues should be public
322323
// todo for multiple drones with multiple sensors, this won't scale. make it a part of VehicleROS?

ros2/src/airsim_ros_pkgs/src/airsim_node.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,12 @@ int main(int argc, char** argv)
1111
std::shared_ptr<rclcpp::Node> nh_lidar = nh->create_sub_node("lidar");
1212
std::string host_ip;
1313
nh->get_parameter("host_ip", host_ip);
14-
AirsimROSWrapper airsim_ros_wrapper(nh, nh_img, nh_lidar, host_ip);
14+
auto callback_group = nh->create_callback_group(rclcpp::CallbackGroupType::Reentrant);
15+
AirsimROSWrapper airsim_ros_wrapper(nh, nh_img, nh_lidar, host_ip, callback_group);
1516

16-
if (airsim_ros_wrapper.is_used_img_timer_cb_queue_) {
17-
rclcpp::executors::SingleThreadedExecutor executor;
18-
executor.add_node(nh_img);
19-
while (rclcpp::ok()) {
20-
executor.spin();
21-
}
22-
}
23-
24-
if (airsim_ros_wrapper.is_used_lidar_timer_cb_queue_) {
25-
rclcpp::executors::SingleThreadedExecutor executor;
26-
executor.add_node(nh_lidar);
27-
while (rclcpp::ok()) {
28-
executor.spin();
29-
}
30-
}
31-
32-
rclcpp::spin(nh);
17+
rclcpp::executors::MultiThreadedExecutor executor;
18+
executor.add_node(nh);
19+
executor.spin();
3320

3421
return 0;
35-
}
22+
}

ros2/src/airsim_ros_pkgs/src/airsim_ros_wrapper.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const std::unordered_map<int, std::string> AirsimROSWrapper::image_type_int_to_s
2424
{ 7, "Infrared" }
2525
};
2626

27-
AirsimROSWrapper::AirsimROSWrapper(const std::shared_ptr<rclcpp::Node> nh, const std::shared_ptr<rclcpp::Node> nh_img, const std::shared_ptr<rclcpp::Node> nh_lidar, const std::string& host_ip)
27+
AirsimROSWrapper::AirsimROSWrapper(const std::shared_ptr<rclcpp::Node> nh, const std::shared_ptr<rclcpp::Node> nh_img, const std::shared_ptr<rclcpp::Node> nh_lidar, const std::string& host_ip, const std::shared_ptr<rclcpp::CallbackGroup> callback_group)
2828
: is_used_lidar_timer_cb_queue_(false)
2929
, is_used_img_timer_cb_queue_(false)
3030
, airsim_settings_parser_(host_ip)
@@ -35,6 +35,7 @@ AirsimROSWrapper::AirsimROSWrapper(const std::shared_ptr<rclcpp::Node> nh, const
3535
, nh_(nh)
3636
, nh_img_(nh_img)
3737
, nh_lidar_(nh_lidar)
38+
, callback_group_(callback_group)
3839
, isENU_(false)
3940
, publish_clock_(false)
4041
{
@@ -109,7 +110,7 @@ void AirsimROSWrapper::initialize_ros()
109110

110111
nh_->declare_parameter("vehicle_name", rclcpp::ParameterValue(""));
111112
create_ros_pubs_from_settings_json();
112-
airsim_control_update_timer_ = nh_->create_wall_timer(std::chrono::duration<double>(update_airsim_control_every_n_sec), std::bind(&AirsimROSWrapper::drone_state_timer_cb, this));
113+
airsim_control_update_timer_ = nh_->create_wall_timer(std::chrono::duration<double>(update_airsim_control_every_n_sec), std::bind(&AirsimROSWrapper::drone_state_timer_cb, this), callback_group_);
113114
}
114115

115116
void AirsimROSWrapper::create_ros_pubs_from_settings_json()
@@ -310,15 +311,15 @@ void AirsimROSWrapper::create_ros_pubs_from_settings_json()
310311
double update_airsim_img_response_every_n_sec;
311312
nh_->get_parameter("update_airsim_img_response_every_n_sec", update_airsim_img_response_every_n_sec);
312313

313-
airsim_img_response_timer_ = nh_img_->create_wall_timer(std::chrono::duration<double>(update_airsim_img_response_every_n_sec), std::bind(&AirsimROSWrapper::img_response_timer_cb, this));
314+
airsim_img_response_timer_ = nh_img_->create_wall_timer(std::chrono::duration<double>(update_airsim_img_response_every_n_sec), std::bind(&AirsimROSWrapper::img_response_timer_cb, this), callback_group_);
314315
is_used_img_timer_cb_queue_ = true;
315316
}
316317

317318
// lidars update on their own callback/thread at a given rate
318319
if (lidar_cnt > 0) {
319320
double update_lidar_every_n_sec;
320321
nh_->get_parameter("update_lidar_every_n_sec", update_lidar_every_n_sec);
321-
airsim_lidar_update_timer_ = nh_lidar_->create_wall_timer(std::chrono::duration<double>(update_lidar_every_n_sec), std::bind(&AirsimROSWrapper::lidar_timer_cb, this));
322+
airsim_lidar_update_timer_ = nh_lidar_->create_wall_timer(std::chrono::duration<double>(update_lidar_every_n_sec), std::bind(&AirsimROSWrapper::lidar_timer_cb, this), callback_group_);
322323
is_used_lidar_timer_cb_queue_ = true;
323324
}
324325

0 commit comments

Comments
 (0)