小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
按crtl+c之后弹出信息:[INFO] [1713769737.106169624] [rclcpp]: signal_handler(signum=2)
-
这是我在学习ros2通信基础的时候遇到的问题,以下是我的代码,我的代码和网上提供的代码整体逻辑应该是一致的,但是我在终端按crtl+c的时候会弹出signal_handler(signum=2),我不清楚这是什么意思;其次我还想请问:
if (!rclcpp::ok())
{
RCLCPP_ERROR(this->get_logger(), "等待服务的过程中被打断...");
return;
}
这段代码什么时候加上呢?我发现不加上这段代码,crtl+C就无法暂停我的程序
wang2.cpp
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "std_msgs/msg/u_int32.hpp"
#include "village_interfaces/srv/sell_novel.hpp"
#include <queue>using std::placeholders::_1;
using std::placeholders::_2;/*
创建一个类节点,名字叫做SingleDogNode,继承自Node.
*/
class SingleDogNode : public rclcpp::Node
{public:
// 构造函数,有一个参数为节点名称
SingleDogNode(std::string name) : Node(name)
{
// 打印一句自我介绍
RCLCPP_INFO(this->get_logger(), "大家好,我是单身狗%s.", name.c_str());
// 创建一个订阅者来订阅李四写的小说,通过名字sexy_girl
sub_novel = this->create_subscription<std_msgs::msg::String>("sexy_girl", 10, std::bind(&SingleDogNode::topic_callback, this, _1));
// 创建发布者
pub_money = this->create_publisher<std_msgs::msg::UInt32>("sexy_girl_money", 10);sell_novels_callback_group = this->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive); sell_server = this->create_service<village_interfaces::srv::SellNovel>("sell_novel", std::bind(&SingleDogNode::sell_novel_callback, this, _1, _2), rmw_qos_profile_services_default, sell_novels_callback_group); }
private:
std::queuestd::string novels_queue;
// 声明一个订阅者(成员变量),用于订阅小说
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub_novel;// 声明一个发布者(成员变量),用于给李四钱 rclcpp::Publisher<std_msgs::msg::UInt32>::SharedPtr pub_money; rclcpp::CallbackGroup::SharedPtr sell_novels_callback_group; rclcpp::Service<village_interfaces::srv::SellNovel>::SharedPtr sell_server; // maishu void sell_novel_callback(const village_interfaces::srv::SellNovel::Request::SharedPtr request, const village_interfaces::srv::SellNovel::Response::SharedPtr response) { RCLCPP_INFO(this->get_logger(), "shoudaole %d", request->money); unsigned int num = (int)request->money; if(num > novels_queue.size()) { RCLCPP_INFO(this->get_logger(), "%ld<%d", novels_queue.size(), num); rclcpp::Rate rate(1); while (novels_queue.size() < num) { if(!rclcpp::ok()) { RCLCPP_ERROR(this->get_logger(), "程序被终止了"); return ; } RCLCPP_INFO(this->get_logger(), "%ld", num - novels_queue.size()); rate.sleep(); } } RCLCPP_INFO(this->get_logger(), "%ld > %d", novels_queue.size(), num); for(int i = 0; i<(int)num; i++) { response->novels.push_back(novels_queue.front()); novels_queue.pop(); } } // 收到话题数据的回调函数 void topic_callback(const std_msgs::msg::String::SharedPtr msg) { // 新建一张人民币 std_msgs::msg::UInt32 money; money.data = 10; // 发送人民币给李四 pub_money->publish(money); novels_queue.push(msg->data); RCLCPP_INFO(this->get_logger(), "朕已阅:'%s',打赏李四:%d 元的稿费", msg->data.c_str(), money.data); };
};
int main(int argc, char argv)
{
rclcpp::init(argc, argv);
/产生一个Wang2的节点/
auto node = std::make_shared<SingleDogNode>("wang2");
rclcpp::executors::MultiThreadedExecutor executor;
executor.add_node(node);
/ 运行节点,并检测退出信号/
executor.spin();
rclcpp::shutdown();
return 0;
}zhang3.cpp#include "rclcpp/rclcpp.hpp"
#include "village_interfaces/srv/sell_novel.hpp"// 提前声明的占位符,留着创建客户端的时候用
using std::placeholders::_1;/*
创建一个类节点,名字叫做PoorManNode,继承自Node.
*/
class PoorManNode : public rclcpp::Node
{public:
/* 构造函数 */
PoorManNode(std::string name) : Node(name)
{
// 打印一句自我介绍
RCLCPP_INFO(this->get_logger(), "大家好,我是得了穷病的张三.");
novel_client = this->create_client<village_interfaces::srv::SellNovel>("sell_novel");
}void buy_novels() { while (!novel_client->wait_for_service(std::chrono::seconds(1))) { //等待时检测rclcpp的状态 if (!rclcpp::ok()) { RCLCPP_ERROR(this->get_logger(), "等待服务的过程中被打断..."); return; } RCLCPP_INFO(this->get_logger(), "等待服务端上线中"); } auto request = std::make_shared<village_interfaces::srv::SellNovel_Request>(); request->money = 5; novel_client->async_send_request(request, std::bind(&PoorManNode::novels_callback, this, _1)); }
private:
rclcpp::Client<village_interfaces::srv::SellNovel>::SharedPtr novel_client;
void novels_callback(rclcpp::Client<village_interfaces::srv::SellNovel>::SharedFuture response)
{
auto result = response.get();RCLCPP_INFO(this->get_logger(), "ok %ld", result->novels.size()); for(const std::string& novel:result->novels) { RCLCPP_INFO(this->get_logger(), "%s", novel.c_str()); } }
};
/主函数/
int main(int argc, char argv)
{
rclcpp::init(argc, argv);
/产生一个Zhang3的节点/
auto node = std::make_shared<PoorManNode>("zhang3");
node->buy_novels();
/ 运行节点,并检测rclcpp状态/
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
} -
@3278537301 在 按crtl+c之后弹出信息:[INFO] [1713769737.106169624] [rclcpp]: signal_handler(signum=2) 中说:
signal_handler(signum=2)
这个没什么问题,收到了退出信号