ROS1入门教程2:主题发布和订阅

一、创建发布者

创建源文件publisher.cpp,并写入以下内容:

cpp 复制代码
#include <iostream>

#include "ros/ros.h"
#include "std_msgs/String.h"

int main(int argc, char* argv[]) {
    std::string nodeName = "publisher"; // 节点名称
    ROS_INFO("Publisher node name is: %s", nodeName.c_str()); // 日志打印节点名称
    ros::init(argc, argv, nodeName); // 初始化ROS1
    ros::NodeHandle nodeHandle; // 节点句柄
    std::string topicName = "demo2"; // 主题名称
    uint32_t queueSize = 1000; // 指定发布者队列的大小
    const ros::Publisher &publisher = nodeHandle.advertise<std_msgs::String>(topicName, queueSize); // 创建主题发布者
    uint32_t messageId = 0; // 消息ID
    double frequency = 1; // 消息发送频率,每秒发送1次
    ros::Rate rate(frequency); // 创建消息频率对象
    while (ros::ok() == true) { // 循环发送主题消息,直到ROS1关闭
        std_msgs::String message; // 创建消息对象
        std::string data = "publish message id " + std::to_string(messageId); // 创建消息内容
        message.data = data; // 填写消息内容
        publisher.publish(message); // 开始发布消息
        ROS_INFO("Publish topic message: %s", data.c_str());
        ++messageId; // 发布完成,消息ID自增1
        rate.sleep(); // 执行延时
    }

    return EXIT_SUCCESS;
}

二、创建订阅者

创建源文件subscriber.cpp,并写入以下内容:

cpp 复制代码
#include <iostream>

#include "ros/ros.h"
#include "std_msgs/String.h"

void topicCallback(const std_msgs::String::ConstPtr &message) { // 主题消息回调函数
    if (message.get() != nullptr) {
        ROS_INFO("Subscribe topic message: %s", message.get()->data.c_str());
    }
}

int main(int argc, char* argv[]) {
    std::string nodeName = "subscriber"; // 节点名称
    ROS_INFO("Subscriber node name is: %s", nodeName.c_str()); // 日志打印节点名称
    ros::init(argc, argv, nodeName); // 初始化ROS1
    ros::NodeHandle nodeHandle; // 节点句柄
    std::string topicName = "demo2"; // 主题名称
    uint32_t queueSize = 1000; // 指定发布者队列的大小
    const ros::Subscriber &subscriber = nodeHandle.subscribe<std_msgs::String>(topicName, queueSize, topicCallback); // 创建主题订阅者
    ros::spin(); // 阻塞线程,防止退出

    return EXIT_SUCCESS;
}

三、编译运行

1、修改编译配置

打开CMakeLists.txt文件,添加以下内容:

bash 复制代码
# 添加发布者源文件
add_executable(publisher src/publisher.cpp)

# 添加订阅者源文件
add_executable(subscriber src/subscriber.cpp)

# 发布者链接catkin库
target_link_libraries(publisher ${catkin_LIBRARIES})

# 订阅者链接catkin库
target_link_libraries(subscriber ${catkin_LIBRARIES})

2、开始编译

bash 复制代码
# 执行编译命令
catkin_make

# 新建【终端1】和【终端2】,分别执行以下命令加载运行环境:
source ./devel/setup.sh

3、运行发布者

bash 复制代码
# 打开【终端1】运行以下命令:
rosrun demo publisher

发布成功的日志打印:

4、运行订阅者

bash 复制代码
# 打开【终端2】运行以下命令:
rosrun demo subscriber

订阅成功的日志打印:

相关推荐
暂未成功人士!7 天前
点云处理的关键技术流程和常用算法
ros·pcl·点云处理·点云去噪滤波
提伯斯6469 天前
Fast-Lio和LIO-SAM分别在有gps情况下与PX4融合的过程
ros·px4·fast-lio·lio-sam
南檐巷上学11 天前
基于地平线RDK X5的智能医药机器人系统
ubuntu·机器人·ros·机械臂·openclaw
加成BUFF13 天前
第七天 ROS《 参数服务器与Launch文件》
运维·ros·参数服务器
加成BUFF13 天前
第六天 ROS 《Action 通信实验》
linux·机器人·ros
加成BUFF14 天前
第5天 ROS 《Service 通信实验指导书》
机器人·ros
提伯斯64614 天前
解决Fast-Drone-250编译相关错误
linux·ros·无人机·fast-drone
rqtz17 天前
【C++】源码编译 Qt5.15.3|Ubuntu22.04 下 ROS 开发环境搭建
开发语言·c++·qt·ros
rqtz17 天前
【机器人】ROS结合Qt开发上位机软件工作空间配置
开发语言·qt·ros
kobesdu17 天前
【ROS2实战笔记-24】ROS2 Launch 实用技巧:条件逻辑与节点动态生成
笔记·ros·slam