赞
踩
ROS2探索(二)executor_rclcpp::executors::multithreadedexecutor-CSDN博客
多线程运行多个节点:
- int main(int argc, char * argv[])
- {
- rclcpp::init(argc, argv);
- rclcpp::executors::MultiThreadedExecutor executor;
- auto pubnode = std::make_shared<PublisherNode>();
- auto subnode = std::make_shared<DualThreadedNode>();
- executor.add_node(pubnode);
- executor.add_node(subnode);
- executor.spin();
- rclcpp::shutdown();
- return 0;
- }
多线程处理回调函数:
- rclcpp::CallbackGroup::SharedPtr callback_group_subscriber_;
-
- rclcpp::Subscription<sensor_msgs::msg::Imu>::SharedPtr imu_sub_;
- rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr point_cloud_sub_;
-
- // 创建回调组。
- //Reentrant表示可以同时处理多个回调函数,MutuallyExclusive表示此组的回调函数是互斥的。
- callback_group_subscriber_ = create_callback_group(rclcpp::CallbackGroupType::Reentrant);
- auto sub_opt = rclcpp::SubscriptionOptions();
- sub_opt.callback_group = callback_group_subscriber_;
-
- // 订阅IMU消息
- imu_sub_ = create_subscription<sensor_msgs::msg::Imu>(
- params_.imuTopic, 2000, std::bind(&OnlineMapping::imuCallback, this, std::placeholders::_1), sub_opt);
-
- // 订阅lidar消息
- point_cloud_sub_ = create_subscription<sensor_msgs::msg::PointCloud2>(
- params_.pointCloudTopic, 1, std::bind(&OnlineMapping::pointCloud2Callback, this, std::placeholders::_1), sub_opt);
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。