【ROS2】发布与订阅消息

前言

本文基于 ROS2 humble , Ubuntu 22.04 。

1. 创建工作空间

在home下新建文件夹

mkdir -p ros2_ws/src

2. 新建功能包

cd ros2_ws/src

ros2 pkg create subscribe_and_publish --build-type ament_cmake --dependencies rclcpp std_msgs --license Apache-2.0

上述命令中ros2 pkg create是 ROS 2 命令行工具 pkg 模块下,用于创建功能包的命令 。
subscribe_and_publish是功能包的名字。
–build-type ament_cmake 用于指定功能包的构建类型,可以是ament_cmake或ament_python。
–dependencies rclcpp std_msgs为功能包添加依赖。
–license Apache-2.0用于声明功能包的开源协议。

在ros2_ws/src目录下会生成如下文件及文件夹
在这里插入图片描述
subscribe_and_publish 文件夹中文件结构如下:

.
├── CmakeLists.txt
├── include
	└──  subscribe_and_publish
├── package.xml
└── src

3. 发布节点

创建publisher.cpp文件

touch subscribe_and_publish/src/publisher.cpp

文件内容如下:

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

class Publisher : public rclcpp::Node
{
    
    
public:
    // 构造函数,有一个参数为节点名称
    Publisher(std::string name) : Node(name)
    {
    
    
        RCLCPP_INFO(this->get_logger(), "大家好,我是%s.", name.c_str());
        // 创建发布者
        subscribe_and_publish_publisher_ = this->create_publisher<std_msgs::msg::String>("subscribe_and_publish", 10);
        // 创建定时器,500ms为周期,定时发布
        timer_ = this->create_wall_timer(std::chrono::milliseconds(500), std::bind(&Publisher::timer_callback, this));
    }

private:
    void timer_callback()
    {
    
    
        // 创建消息
        std_msgs::msg::String message;
        message.data = "1234";
        // 日志打印
        RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
        // 发布消息
        subscribe_and_publish_publisher_->publish(message);
    }
    // 声名定时器指针
    rclcpp::TimerBase::SharedPtr timer_;
    // 声明话题发布者指针
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr subscribe_and_publish_publisher_;
};

int main(int argc, char **argv)
{
    
    
    rclcpp::init(argc, argv);
    /*产生一个的节点*/
    auto node = std::make_shared<Publisher>("publisher");
    /* 运行节点,并检测退出信号*/
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}

4. 订阅节点

创建subscribe1.cpp文件

touch subscribe_and_publish/src/subscribe1.cpp

文件内容如下:

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

class Subscribe : public rclcpp::Node
{
    
    
public:
    Subscribe(std::string name) : Node(name)
    {
    
    
        RCLCPP_INFO(this->get_logger(), "大家好,我是%s.", name.c_str());
          // 创建一个订阅者订阅话题
         subscribe_and_publish_subscribe_ = this->create_subscription<std_msgs::msg::String>("subscribe_and_publish", 10, std::bind(&Subscribe::command_callback, this, std::placeholders::_1));
    }

private:
     // 声明一个订阅者
    rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscribe_and_publish_subscribe_;
     // 收到话题数据的回调函数
    void command_callback(const std_msgs::msg::String::SharedPtr msg)
    {
    
    
        // double speed = 0.0f;
        // if(msg->data == "1234")
        // {
    
    
        //     speed = 0.2f;
        // }
        // RCLCPP_INFO(this->get_logger(), "收到[%s]指令,发送速度 %f", msg->data.c_str(),speed);
        RCLCPP_INFO(this->get_logger(), "收到[%s]指令", msg->data.c_str());
    };
};

int main(int argc, char **argv)
{
    
    
    rclcpp::init(argc, argv);
    /*产生一个的节点*/
    auto node = std::make_shared<Subscribe>("subscribe1");
    /* 运行节点,并检测退出信号*/
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}

5. 构建项目

colcon是ros2中的构建工具,如果没有colcon工具需要先下载

sudo apt install python3-colcon-common-extensions

执行如下命令构建项目

colcon build --symlink-install

像ament_cmake这样的构建类型不支持devel空间的概念,并且需要安装package,所以colcon支持选项–symlink install 构建。

工作空间会多出来build install log 三个文件夹,如下图所示。
在这里插入图片描述

6. 发布订阅测试

在当前工作空间下新建终端执行命令

source install/setup.bash
ros2 run subscribe_and_publish publisher

再开一个终端执行命令

source install/setup.bash
ros2 run subscribe_and_publish subscribe1

如下图所示
在这里插入图片描述

7. 查看节点信息

打开rqt,如果没有则需安装

sudo apt install ros-humble-rqt*

打开rqt

rqt

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45906972/article/details/144219759