ros advertise 发布数据小技巧--latch配置

        在ros发布数据的advertise函数参数里面最后一个参数"latch",默认为false,一直没有注意,今天在分析其他问题时在ros wiki[Publishers and Subscribers]上偶然注意到了这个参数,

说明如下:

latch [optional]

  • Enables "latching" on a connection. When a connection is latched, the last message published is saved and automatically sent to any future subscribers that connect. This is useful for slow-changing to static data like a map. Note that if there are multiple publishers on the same topic, instantiated in the same node, then only the last published message from that node will be sent, as opposed to the last published message from each publisher on that single topic.

advertise函数定义在文件/opt/ros/melodic/include/ros/node_handle.h, 也做了说明.     

    /**
     * \brief Advertise a topic, simple version
     *
     * This call connects to the master to publicize that the node will be
     * publishing messages on the given topic.  This method returns a Publisher that allows you to
     * publish a message on this topic.
     *
     * This version of advertise is a templated convenience function, and can be used like so
     *
     *   ros::Publisher pub = handle.advertise<std_msgs::Empty>("my_topic", 1);
     *
     * \param topic Topic to advertise on
     *
     * \param queue_size Maximum number of outgoing messages to be
     * queued for delivery to subscribers
     *
     * \param latch (optional) If true, the last message published on
     * this topic will be saved and sent to new subscribers when they
     * connect
     *
     * \return On success, a Publisher that, when it goes out of scope,
     * will automatically release a reference on this advertisement.  On
     * failure, an empty Publisher.
     *
     * \throws InvalidNameException If the topic name begins with a
     * tilde, or is an otherwise invalid graph resource name, or is an
     * otherwise invalid graph resource name
     */
    template <class M>
    Publisher advertise(const std::string& topic, uint32_t queue_size, bool latch = false)
    {
      AdvertiseOptions ops;
      ops.template init<M>(topic, queue_size);
      ops.latch = latch;
      return advertise(ops);
    }

        一般接收节点要在发布节点启动后才能接收到数据,但启动顺序有时无法控制,就会导致某些数据丢失,尤其像地图这种只发布一次的. 当发布数据端设置了latch = true,后起节点也可以接收到之前的最后一帧消息.另外一个特别有用的就是发布rviz可视化数据调试程序时,根据namesapce分析某些结果时,可以重复勾选显示某些条目,只需在rviz中重新勾选一下这个Display就可以, 不需要重新播放数据.

        如下图,先播放bag数据,暂停后,在rviz勾选即可显示

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值