当前位置: 首页 > news >正文

【ROS2】【分步讲解】节点的使用以及引入消息接口的方法

写在前面

主要参考了小鱼的ROS2教程,其他教程在分部拆解上有些模棱两可

建立完整功能节点的步骤概述

  1. 创建节点
  2. 定义该节点的身份
  3. 为节点分配具体任务
  4. ros2 run

打个比方,今天要完成送快递的任务,将任务拆解后可分为:

  1. 招人
  2. 让这个人变成快递员
  3. 给TA分配具体任务(把城北的快递派送完)
  4. 让TA干活

完整程序展示(以publisher为例)

class TopicPublisher01 : public rclcpp::Node
{
public:// 构造函数,有一个参数为节点名称TopicPublisher01(std::string name) : Node(name){RCLCPP_INFO(this->get_logger(), "发布者节点%s 已启动.", name.c_str());command_publisher_ = this->create_publisher<std_msgs::msg::String>("command", 10);timer_ = this->create_wall_timer(std::chrono::milliseconds(500), std::bind(&TopicPublisher01::timer_callback, this));}private:void timer_callback(){// 创建消息std_msgs::msg::String message;message.data = "forward";// 日志打印RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());// 发布消息command_publisher_->publish(message);}// 声名定时器指针rclcpp::TimerBase::SharedPtr timer_;// 声明节点rclcpp::Publisher<std_msgs::msg::String>::SharedPtr command_publisher_;
};

分部拆解

  • 创建节点
    • 创建节点发生在auto node = std::make_shared(“topic_publisher_01”);
      当构造函数TopicPublisher01被调用,节点名称参数写入Node(name)时,节点就被创建完成了
      相当于在没有人的情况下,招了个人进来,但是TA现在还不知道自己是来干嘛的,只知道自己是人,是一个劳动力
    • 仅仅被创建的节点本身不具有任何功能,具体功能是加在上面的程序基础上的
    class TopicPublisher01 : public rclcpp::Node
    {
    public:// 构造函数,有一个参数为节点名称TopicPublisher01(std::string name) : Node(name){RCLCPP_INFO(this->get_logger(), "发布者节点%s 已启动.", name.c_str());}private:
    };int main(int argc, char **argv)
    {rclcpp::init(argc, argv);/*创建对应节点的共享指针对象*/auto node = std::make_shared<TopicPublisher01>("topic_publisher_01");/* 运行节点,并检测退出信号*/rclcpp::spin(node);rclcpp::shutdown();return 0;
    }
    
  • 节点功能定义
    • 相比上面的程序两行代码,将该节点定义成publisher,作为消息的发布者
      相当于从无业游民变成了快递员
      public:command_publisher_ = this->create_publisher<std_msgs::msg::String>("command", 10);
      private:rclcpp::Publisher<std_msgs::msg::String>::SharedPtr command_publisher_;
      
  • 分配具体任务
    • 为发布者节点增加定时发送的功能,主要增加了下面的代码
      相当于为快递员分配的具体的工作任务,招人了总是要用起来的
      timer_ = this->create_wall_timer(std::chrono::milliseconds(500), std::bind(&TopicPublisher01::timer_callback, this));void timer_callback()
      {// 创建消息std_msgs::msg::String message;message.data = "forward";// 日志打印RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());// 发布消息command_publisher_->publish(message);
      }
      // 声名定时器指针
      rclcpp::TimerBase::SharedPtr timer_;
      // 声明节点
      rclcpp::Publisher<std_msgs::msg::String>::SharedPtr command_publisher_;
      
  • 运行程序
    • 使用下面的指令运行对应节点即可
      cd xxx/工作空间_ws/
      colcon build --packages-select 功能包名称			# 编译功能包
      source install/setup.bash						# 安装编译后的结果,防止运行时找不到相关依赖
      ros2 run 功能包名称 功能包中的节点名称				# 运行节点
      

完整程序参考最上面的完整程序展示,下面的分部拆解并不是正确的语法

订阅者节点代码展示

实现过程和发布者完全相同

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"class TopicSubscriber : public rclcpp::Node
{
public:TopicSubscriber(std::string name) : Node(name){RCLCPP_INFO(this->get_logger(), "this is %s", name.c_str());command_subscriber = this->create_subscription<std_msgs::msg::String>("command", 10, std::bind(&TopicSubscriber::command_callback, this, std::placeholders::_1));}
private:rclcpp::Subscription<std_msgs::msg::String>::SharedPtr command_subscriber;void command_callback(const std_msgs::msg::String::SharedPtr msg){double speed = 0.0f;if(msg->data == "forward"){speed = 0.2f;}RCLCPP_INFO(this->get_logger(), "receive command [%s], the speed is %f", msg->data.c_str(), speed);}
};int main(int argc, char **argv)
{rclcpp::init(argc, argv);/*创建对应节点的共享指针对象*/auto node = std::make_shared<TopicSubscriber>("topic_subscriber_0");/* 运行节点,并检测退出信号*/rclcpp::spin(node);rclcpp::shutdown();return 0;
}

最后补充一下引入消息接口的方法 (以std_msgs为例)

  1. 在CMakeLists.txt中,增加
    find_package(std_msgs REQUIRED)
    ament_target_dependencies(topic_publisher_01 rclcpp std_msgs)
    
  2. 在packages.xml中增加
     <depend>std_msgs</depend>
    
  3. 在cpp程序中,使用#include “std_msgs/msg/string.hpp”,调用这些接口

补充一点语法

rclcpp::Subscription<std_msgs::msg::String>::SharedPtr command_subscriber;

使用模板类Subscription中的类型别名SharedPtr,创建一个变量command_subscriber
这是c++特有的长难句,也特意记录一下

相关文章:

  • win11 安装 wsl ubuntu 18.04后换源失败!
  • PostgreSQL数据库有什么独特之处?为什么它成为了后起之秀?
  • OpenCV CUDA模块中逐元素操作------逻辑运算
  • 1669上什么课
  • Linux系统中,创建一个xxx.service服务单元文件,方便快速启动、停止java应用程序
  • 310. 最小高度树
  • Java高频面试之并发编程-17
  • PHP黑白胶卷底片图转彩图功能 V2025.05.15
  • 深圳无人机展览即将开始,无人机舵机为什么选择伟创动力
  • NHANES稀有指标推荐:PWI
  • LeetCode_sql刷题(3482.分析组织层级)
  • GitHub 趋势日报 (2025年05月13日)
  • 蓝桥杯 17. 修改数组
  • 01、基础入门-SpringBoot2课程介绍
  • 制作一款打飞机游戏48:敌人转向
  • 【氮化镓】AlGaN合金中成分相关的辐射响应
  • AI数字赋能,点亮精神心理康复之路 —— 华南精神心理数字化诊疗体系3.0发布会圆满举办成功!
  • 抽奖系统-奖品-活动
  • android实现USB通讯
  • Go语言中的函数类型参数:深入理解`func()`
  • 做磁力搜索网站好吗/外链怎么做
  • 个人服务器搭建做网站/百度网站首页网址
  • 专业建站商/产品全网营销推广
  • 富阳网站建设 优帮云/aso优化运营
  • ps网站导航制作/快速排名seo软件
  • 劫持网站挂广告是个人做的吗/新业务在线软件下载