ROS学习笔记九:用C++编写ROS发布与订阅

本文档详细介绍了如何使用C++在ROS中创建发布者和订阅者节点。首先,创建并展示了发布节点的代码实现,接着编写订阅节点,并对CMakeLists.txt的修改进行了解释。最后,通过编译和运行节点,验证了ROS通信的正确性。
摘要由CSDN通过智能技术生成

ROS学习笔记九:用C++编写ROS发布与订阅

本节主要介绍如何用C++编写一个简单的ROS发布与订阅。

编写发布节点

在之前创建的例子beginner_tutorials软件包中,在其目录下的src目录中,创建talker.cpp文件:

#include "ros/ros.h"  //ROS中最常用片段的头文件
#include "std_msgs/String.h"   //由String.msg自动产生的头文件

#include <sstream>   //stringstream头文件

/**
 * This tutorial demonstrates simple sending of messages over the ROS system.
 */
int main(int argc, char **argv)
{
  /**
   * The ros::init() function needs to see argc and argv so that it can perform
   * any ROS arguments and name remapping that were provided at the command line.
   * For programmatic remappings you can use a different version of init() which takes
   * remappings directly, but for most command-line programs, passing argc and argv is
   * the easiest way to do it.  The third argument to init() is the name of the node.
   *
   * You must call one of the versions of ros::init() before using any other
   * part of the ROS system.
   */
  ros::init(argc, argv, "talker");   //初始化ROS,在这里可以指定我们节点的名称,一个ROS系统中节点的名称必须是唯一的

  /**
   * NodeHandle is the main access point to communications with the ROS system.
   * The first NodeHandle constructed
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值