ROS的学习(十二)用C++写一个简单的发布者

本文介绍如何在ROS环境中使用C++编写一个发布者节点,该节点在'chatter'话题上广播std_msgs/String消息。内容涵盖节点初始化、ros::Publisher的使用以及在ros::ok()检查下的消息发送循环。
摘要由CSDN通过智能技术生成

     节点是一个可执行程序,它连接到了ROS的网络系统中。我们将会创建一个发布者,也就是说话者节点,它将会持续的广播一个信息。

     改变目录到之前所建立的那个包下:

cd ~/catkin_ws/src/beginner_tutorials

     在beginner_tutorials包下面建立一个src文件夹:
mkdir -p ~/catkin_ws/src/beginner_tutorials/src

     创建文件src/talker.cpp:
vim src/talker.cpp 

     将下面的内容复制进去:
#include "ros/ros.h"
#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::init(argc, argv, "talker");

  /**
   * NodeHandl
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值