Ask Your Question
5

Publishing to a topic via subscriber callback function

asked 2013-04-01 15:39:56 -0500

joy gravatar image

updated 2013-04-02 00:29:29 -0500

felix k gravatar image

Hello, I am wondering if anybody could help me with this problem. I have a listener that just publishes to a topic when another topic is published data, so basically the topic it is listening on gets data published. I have tried putting global variable in the code whose change can map into the main function but ros is not allowing global variables. I also tried to define the nodehandle as global but ros is not allowing this either. (I think before rosinit no ros components will be recognized). My code is in C++. Any help as to what could be done would be really appreciated. I am including a basic skeleton of the code I am trying:

void func_cb(std_msgs type param as defined in ros chatter example)
{
    //*need some code here to publish to a topic*
    //*problem:nodehandle out of scope,cannot define new nodehandle*
}

int main(... )
{
    //ros initilization
    //ros node handle initialization
    //ros subscription initialization
    //callback spinner
    return 0;
}
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
35

answered 2013-04-01 20:54:02 -0500

updated 2016-07-06 05:37:25 -0500

You could define a class that handles everything and avoid using ugly global variables:

#include <ros/ros.h>

class SubscribeAndPublish
{
public:
  SubscribeAndPublish()
  {
    //Topic you want to publish
    pub_ = n_.advertise<PUBLISHED_MESSAGE_TYPE>("/published_topic", 1);

    //Topic you want to subscribe
    sub_ = n_.subscribe("/subscribed_topic", 1, &SubscribeAndPublish::callback, this);
  }

  void callback(const SUBSCRIBED_MESSAGE_TYPE& input)
  {
    PUBLISHED_MESSAGE_TYPE output;
    //.... do something with the input and generate the output...
    pub_.publish(output);
  }

private:
  ros::NodeHandle n_; 
  ros::Publisher pub_;
  ros::Subscriber sub_;

};//End of class SubscribeAndPublish

int main(int argc, char **argv)
{
  //Initiate ROS
  ros::init(argc, argv, "subscribe_and_publish");

  //Create an object of class SubscribeAndPublish that will take care of everything
  SubscribeAndPublish SAPObject;

  ros::spin();

  return 0;
}
edit flag offensive delete link more

Comments

Thanx Martin...One question though: If I create multiple objects how will ROS handle multiple instances of class publishing in the same topic especially if it is some topic like geomtry_msgs::Twist type? Is it a sequential/interleaved type execution?

joy gravatar imagejoy ( 2013-04-02 15:48:57 -0500 )edit

That is a good question. If I am not mistaken, when you create multiple instances of SubscribeAndPublish object, lets say n, then for each message received on the topic "/subscribed_topic" you will generate n messages on the topic "/published_topic". No guarantees about the order though.

Martin Peris gravatar imageMartin Peris ( 2013-04-02 17:06:02 -0500 )edit

So the message is tied with the object id. Thanx so much for clearing that up. :)

joy gravatar imagejoy ( 2013-04-05 10:41:22 -0500 )edit

Yeah, I was also going to warn that none the published messages won't actually be sent until after the callback returns. I landed here looking for a workaround.

drewm1980 gravatar imagedrewm1980 ( 2015-10-20 02:08:04 -0500 )edit

I just wanted to warn you that I think there is a semicolon missing at the end of the class declaration. Thank you for your suggestion!

OMC gravatar imageOMC ( 2016-06-29 08:54:14 -0500 )edit

Thanks @Billie, fixed.

Martin Peris gravatar imageMartin Peris ( 2016-07-06 05:39:50 -0500 )edit

I used this example fpr oublishing velocity from my joystick to my motor. But there is a delay. Maybe I can solve the problem by changing the publsih rate. How can I cahanged the publish rate at this example?

Milliau gravatar imageMilliau ( 2016-12-06 05:45:09 -0500 )edit

You can substitute the call to ros::spin() with: while(true) { ros::Rate(YOUR_DESIRED_RATE).sleep(); ros::spinOnce(); }

Martin Peris gravatar imageMartin Peris ( 2016-12-11 00:40:19 -0500 )edit
0

answered 2017-04-02 23:51:37 -0500

RaoBrother gravatar image
  1. sub_ = n_.subscribe("/subscribed_topic", 1, &SubscribeAndPublish::callback, this);

if u pass &SubscribeAndPublish::callback all so like static , in this conditopn i dont want use static.

Please tell me how to solve the problem .

2 . sub = n.subscribe("/camera/depth_registered/points", 1000, boost::bind(&Example::callBack, this, _1));

this also the same.

Thanks

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2013-04-01 15:39:56 -0500

Seen: 18,647 times

Last updated: Dec 06 '16