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