此文章默认读者已经对ros有了一定的基础,明白ros之间如何进行数据通信,了解ros的文件结构,了解工作空间,功能包的概念。本篇文章参考创客智造上的教程,他们的讲解比我更详细。百度一下创客智造即可。
订阅者和发布者依托于节点,即订阅者和发布者是在节点中完成的。所以先来说一下节点的定义,步骤可分为:
1.新建代码源文件
2.写代码
3.在cmakelist.txt文件中定义
订阅者和发布者在写代码的步骤中。
下面上一段,roswiki上给出的官方demo,然后对demo做讲解。
talker.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream> //c++里的导包,对于我这种c++半路出家的,就理解为java里的import语句
//值得一说的是第一行语句,include ros/ros.h ,引入ros的相关头文件,这是所有ros开发的起点
/**
* 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"); //初始化节点,第一二个参数走main里传进来,第三个参数为节点名称
/**
* NodeHandle is the main access point to communications with the ROS system.
* The first NodeHandle constructed will fully initi