利用ROS2实现话题的发布与订阅


1.创建节点

home下新建一个ros2的文件夹

mkdir ros2

依次输入下面的命令,创建mytest_ws工作空间、subscribe_and_publish功能包和publisher.cpp

cd ros2
mkdir -p mytest_ws/src
cd mytest_ws/src
ros2 pkg create subscribe_and_publish --build-type ament_cmake --dependencies rclcpp
touch subscribe_and_publish/src/publisher.cpp

2.编写发布与订阅节点

2.1 发布节点

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;
}

2.2 订阅节点

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;
}

2.3 修改CmakeLists.txtpackage.xml

打开CmakeLists.txt文件,在中间添加下面两行

find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

在这里插入图片描述

在最后面添加下面内容

add_executable(publisher src/publisher.cpp)
ament_target_dependencies(publisher rclcpp std_msgs)
add_executable(subscribe1 src/subscribe1.cpp)
ament_target_dependencies(subscribe1 rclcpp std_msgs

install(TARGETS
  publisher
  subscribe1
  DESTINATION lib/${PROJECT_NAME}
)

打开package.xml文件,在中间添加

<depend>rclcpp</depend>
<depend>std_msgs</depend>

在这里插入图片描述


3.话题发布与订阅运行测试

3.1重开一个终端,启动订阅节点

cd ros2/mytest_ws
source install/setup.bash
ros2 run subscribe_and_publish subscribe1

3.2再开一个终端,启动发布节点

cd ros2/mytest_ws
source install/setup.bash
ros2 run subscribe_and_publish publisher1

4.运行结果

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_60355964/article/details/126628733