赞
踩
为所有的callback分了组别
类型有
Node::create_subscription(
const std::string & topic_name, //topic名称
const rclcpp::QoS & qos,
CallbackT && callback,
const SubscriptionOptionsWithAllocator<AllocatorT> & options, //选项
typename rclcpp::message_memory_strategy::MessageMemoryStrategy<
typename rclcpp::subscription_traits::has_message_type<CallbackT>::type, AllocatorT>::SharedPtr
msg_mem_strat)
this->create_subscription<std_msgs::msg::Int32>("int1",qos,
std::bind(&PathSearcherNode::int1Sub, this,
std::placeholders::_1),
rclcpp::SubscriptionOptions());
这里的SubscriptionOptions()可以设置callback_group组别;如果不设置,默认为node初始化时的callback_group
#include <rclcpp/rclcpp.hpp> #include <std_msgs/msg/int32.hpp> using namespace std::chrono_literals; std::string getTime(){ time_t tick = (time_t)(rclcpp::Clock().now().seconds()); struct tm tm; char s[100]; tm = *localtime(&tick); strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm); return std::string(s); } std::string string_thread_id() { auto hashed = std::hash<std::thread::id>()(std::this_thread::get_id()); return std::to_string(hashed); } class MyNode: public rclcpp::Node{ public: MyNode(const rclcpp::NodeOptions & options); ~MyNode(){} void int1Sub(std_msgs::msg::Int32::SharedPtr msg); void int2Sub(std_msgs::msg::Int32::SharedPtr msg); private: rclcpp::Subscription<std_msgs::msg::Int32>::SharedPtr int_sub1_; rclcpp::Subscription<std_msgs::msg::Int32>::SharedPtr int_sub2_; rclcpp::callback_group::CallbackGroup::SharedPtr callback_group_sub1_; rclcpp::callback_group::CallbackGroup::SharedPtr callback_group_sub2_; } MyNode::MyNode(const rclcpp::NodeOptions & options): rclcpp::Node("my_node",options){ //两个组别,设置组别类型为MutuallyExclusive callback_group_sub1_ = this->create_callback_group(rclcpp::callback_group::CallbackGroupType::MutuallyExclusive); callback_group_sub2_ = this->create_callback_group(rclcpp::callback_group::CallbackGroupType::MutuallyExclusive); auto sub1_opt = rclcpp::SubscriptionOptions(); sub1_opt.callback_group = callback_group_sub1_; auto sub2_opt = rclcpp::SubscriptionOptions(); sub2_opt.callback_group = callback_group_sub2_; int_sub1_ = nh_->create_subscription<std_msgs::msg::Int32>("int1",10, std::bind(&MyNode::int1Sub,this,std::placeholders::_1), sub1_opt); int_sub2_ = nh_->create_subscription<std_msgs::msg::Int32>("int2",10, std::bind(&MyNode::int2Sub,this,std::placeholders::_1), sub2_opt); } void MyNode::int1Sub(std_msgs::msg::Int32::SharedPtr msg){ printf("int1 sub time:%s\n",getTime().c_str()); printf("thread1 id:%s\n",string_thread_id().c_str()); rclcpp::sleep_for(10s); printf("int1 sub time:%s\n",getTime().c_str()); } void MyNode::int2Sub(std_msgs::msg::Int32::SharedPtr msg){ printf("int2 sub time:%s\n",getTime().c_str()); printf("thread2 id:%s\n",string_thread_id().c_str()); rclcpp::sleep_for(10s); printf("int2 sub time:%s\n",getTime().c_str()); } int main(int argc, char **argv) { rclcpp::init(argc, argv); rclcpp::executors::MultiThreadedExecutor executor(rclcpp::executor::ExecutorArgs(),5,true); auto node = std::make_shared<MyNode>(rclcpp::NodeOptions()); executor.add_node(node); printf("threads %d\n",executor.get_number_of_threads()); executor.spin(); rclcpp::shutdown(); return EXIT_SUCCESS; }
这时,如果同时收到了/int1和/int2 的消息,两个callback都能执行,但如果接连收到了两个/int1,只能调1个int1Sub;
//同一个组别,类型设置为Reentrant
callback_group_sub_ = this->create_callback_group(rclcpp::callback_group::CallbackGroupType::Reentrant);
auto sub_opt = rclcpp::SubscriptionOptions();
sub_opt.callback_group = callback_group_sub_;
int_sub1_ = nh_->create_subscription<std_msgs::msg::Int32>("int1",10,
std::bind(&MyNode::int1Sub,this,std::placeholders::_1),
sub_opt);
int_sub2_ = nh_->create_subscription<std_msgs::msg::Int32>("int2",10,
std::bind(&MyNode::int2Sub,this,std::placeholders::_1),
sub_opt);
这时,如果同时收到多个/int1和/int2,开启的线程数会受电脑内核和初始化MultiThreadedExecutor时设置的线程数决
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。