ROS中实现在回调函数中发布节点消息

     实际中常常需要通过对其他节点消息的内容进行处理后再发布消息,这就往往希望能够在回调函数中进行处理函数的书写和消息的发布 。最初网上说采取全局变量直接在回调函数书写发布函数,但是不知道为什么代码是不报错,但是内容发布不出来。后面在论坛中发现可以定义一个类来实现这一功能,具体过程如下:

#include "ros/ros.h"
#include "std_msgs/Float32.h"
#include "test1/msg1.h"
#include <iostream>
using namespace std;
class SubscribeAndPublish
{
public:
  SubscribeAndPublish()
  {
    //Topic you want to publish
    pub_ = n_.advertise<msg_type>("/published_topic", 1);
    //Topic you want to subscribe
    sub_ = n_.subscribe("/subscribe_topic", 1, &SubscribeAndPublish::callback, this);
  }

  void callback(const msg_type::ConstPtr& msg)
  {
    //PUBLISHED_MESSAGE_TYPE output;
    //.... do something with the input and generate the output...
    msg_type output;
    ou

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值