ROS基础教程学习笔记9-编写简单的消息发布器和订阅器C++

这篇博客详细介绍了如何在ROS环境中编写C++的发布器和订阅器节点。首先,创建了名为talker的发布器节点,接着编写了listener订阅器节点的代码。在编译过程中,遇到CMake错误,提示CMakeLists.txt文件存在问题。解决方法包括检查find_package()函数和取消注释的完整性。参考ROS官方教程,有助于理解ROS的节点通信基础。
摘要由CSDN通过智能技术生成

1.编写发布器节点

切换到之前用的beginner_tutorials包下:

cd catkin_ws/src/beginner_tutorials

(1)添加源码

在beginner_tutorials文件下创建src文件夹:

mkdir src

然后在src文件下创建talker.cpp文件,并将以下代码复制进去:

#include "ros/ros.h"    //引用了 ROS 系统中大部分常用的头文件
#include "std_msgs/String.h"   
#include <sstream>

/**
 * 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,节点的名称必须唯一
  ros::init(argc, argv, "talker");

  /**
   * NodeHandle is the main access point to communications with the ROS system.
   * The first NodeHandle constructed will fully initialize this node, and the last
   * NodeHandle destructed will close down the node.
   */
//为这个进程的节点创建一个句柄。第一个创建的 NodeHandle 会为节点进行初始化,最后一个销毁的 NodeHandle 则会释放该节点所占用的所有资源。
  ros::Nod
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值