ROS消息, 服务, 主题, 订阅 2

#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");
  //一开始要初始化ros, 没啥可说的.

  /**
   * 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.
   */
  ros::NodeHandle n;
  //获取节点的handle 

  /**
   * The advertise() function is how you tell ROS that you want to
   * publish on a given topic name. This invokes a call to the ROS
   * master node, which keeps a registry of who is publishing and who
   * is subscribing. After this advertise() call is made, the master
   * node will notify anyone who is trying to subscribe to this topic name,
   * and they will in turn negotiate a peer-to-peer connection with this
   * node.  advertise() returns a Publisher object which allows you to
   * publish messages on that topic through a call to publish().  Once
   * all copies of the returned Publisher object are destroyed, the topic
   * will be automatically unadvertised.
   *
   * The second parameter to advertise() is the size of the message queue
   * used for publishing messages.  If messages are published more quickly
   * than we can send them, the number here specifies how many messages to
   * buffer up before throwing some away.
   */
  ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
  //advertise()函数, 用来告诉ROS, 你要发布一个topic, 名字叫chatter, ROS会注册对应的发布者跟订阅者, 一旦发现订阅者, 直接让他们建立点对点通信.一旦所有的发布者都不
  //在了, 主题会被自动不再广告.这个函数的第二个参数是message的缓存数量, 一旦消息被发布得太快, 来不及发送, 超过这个数量的消息就被扔掉了.

  ros::Rate loop_rate(10);
  //建一个间隔大意是1秒10次

  /**
   * A count of how many messages we have sent. This is used to create
   * a unique string for each message.
   */
  int count = 0;
  //用来放消息尾部, 定义消息的唯一性.
  while (ros::ok())
  {
    /**
     * This is a message object. You stuff it with data, and then publish it.
     */
    std_msgs::String msg;
    //new 一个标准的String对象.

    std::stringstream ss;
    ss << "hello world " << count;
    msg.data = ss.str();
    //这个有点像java的bufferredString

    ROS_INFO("%s", msg.data.c_str());
    //给ROS发个infor, 类似打印log.
    /**
     * The publish() function is how you send messages. The parameter
     * is the message object. The type of this object must agree with the type
     * given as a template parameter to the advertise<>() call, as was done
     * in the constructor above.
     */
    chatter_pub.publish(msg);
    //发布者开始发布msg

    ros::spinOnce();
    //spin可能就是执行一次吧.

    loop_rate.sleep();
    //休眠一个间隔
    ++count;

  }

  return 0;
}

wget https://raw.github.com/ros/ros_tutorials/kinetic-devel/roscpp_tutorials/talker/talker.cpp

时间: 2024-10-10 13:45:23

ROS消息, 服务, 主题, 订阅 2的相关文章

ROS消息, 服务, 主题, 订阅 3

#include "ros/ros.h" #include "std_msgs/String.h" /** * This tutorial demonstrates simple receipt of messages over the ROS system. */ void chatterCallback(const std_msgs::String::ConstPtr& msg) { ROS_INFO("I heard: [%s]",

ROS消息, 服务, 主题, 订阅 4

发布者跟订阅者都ok了, 现在要建立节点: cmake_minimum_required(VERSION 2.8.3) project(beginner_tutorials) ## Find catkin and any catkin packages find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg) ## Declare ROS messages and services add_message_file

ROS消息, 服务, 主题, 订阅 5

#include "ros/ros.h" #include "geometry_msgs/Twist.h" #include "geometry_msgs/Vector3.h" int main(int argc, char ** argv){ ros::init(argc, argv, "sendCMD2Turtule"); ros::NodeHandle nodeHandle; ros::Publisher cmdPub

【EJB四】——JMS消息服务之P2P和Pub/Sub

JMS:java消息服务,JMS客户端可以通过JMS服务进行异步消息传输.可以支持两种模型:P2P和Pub/Sub P2P 点对点消息传输模式,这种模式主要包括发送者,消息队列和接收者 特点: 1.每个消息只有一个消费者,一旦被接收(消费),此消息就不存在于消息队列中了. 2.发送者和接收者在时间上没有依赖性(当消息发送后,无论接收者有没有在接收,都不会影响消息进入消息队列) 3.接收者在成功接收消息之后,需要向队列应答成功. Pub/Sub 发布/订阅的模式.主要包括:发布者,主题,订阅者三部

Java消息服务

Java消息服务(Java Message Service,JMS)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信. Java消息服务是一个与具体平台无关的API,绝大多数MOM提供商都对JMS提供支持. 消息中间件的传递模式                   消息中间件一般有两种传递模型:点对点模型(PTP)和发布-订阅模型(Pub/Sub)[2].             点对点模型(PTP) 点对点模型用

【Microsoft Azure学习之旅】消息服务Service Bus的学习笔记及Demo示例

今年项目组做的是Cloud产品,有幸接触到了云计算的知识,也了解并使用了当今流行的云计算平台Amazon AWS与Microsoft Azure.我们的产品最初只部署在AWS平台上,现在产品决定同时支持Azure,所以有幸学习下Azure,并在查看文档资料以及写Demo过程中发现了其中的一些不同.虽然AWS与Azure是两款旗鼓相当的竞争产品,但是还是有很多区别. 本文主要是自己学习Service Bus中的学习笔记,自己有些结论也都跟微软技术支持确认过.个人观点,抛砖引玉:-) 消息服务对于云

三:JMS消息服务规范

一:JMS是什么?--->JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API--->用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信.--->Java消息服务是一个与具体平台无关的API,绝大多数MOM提供商都对JMS提供支持.---> JMS 使您能够通过消息收发服务(有时称为消息中介程序或路由器)从一个 JMS 客户机向另一个 JMS客户机发送消息.--->JMS(Java

Java消息服务初步学习(基于Spring In Action的整理)

几个名词 Java消息服务(Java Message Service)是一个Java标准,定义了使用消息代理的通用API. 消息代理(message broker):类似于邮局的作用,确保消息被投递到指定的目的地. ActiveMQ Kafka 目的地(destination) 队列(queue,点对点模型):消息可以有多个接收者,但每一条消息只能被一个接收者取走. 主题(topic,点对线模型):订阅此主题的订阅者都会接收到此消息的副本. 异步消息相较于同步消息的优点 时间:异步消息不需要等待

JAVA消息服务JMS规范及原理详解

一.简介 JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信.Java消息服务是一个与具体平台无关的API,绝大多数MOM提供商都对JMS提供支持. JMS允许应用程序组件基于JavaEE平台创建.发送.接收和读取消息.它使分布式通信耦合度更低,消息服务更加可靠以及异步性. 二.常用术语介绍 在提到JMS时,我们通常会说到一些术语,解释如下: 消息