ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

How to make a class with publisher and subscriber which is a member of another class [closed]

asked 2014-03-13 12:15:33 -0500

BP gravatar image

Let's assume I have class cA, which has a node that subscribe from topic and publisher with a "constant" frequency. Class cA is a mamber of class cB. Where I should put "while(ros::ok())" for publishing purpose? Cose if i put it in constructor it'll bloc constructor of "higher" class (ofc. I can make a function cA::Publish() and run everything further in cB::Publish() or split classes to more nodes and classes, but i am looking for beter solution).

Should I use threads? I have never used them so I don't know if i should. I found this _cant_post_links_http_://wiki.ros.org/roscpp/Overview/Callbacks%20and%20Spinning

Is there an easy example?

thanks in advance

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by BP
close date 2014-03-15 10:30:59

1 Answer

Sort by ยป oldest newest most voted
2

answered 2014-03-13 21:12:48 -0500

Wolf gravatar image

updated 2014-03-13 21:24:47 -0500

I think the while(ros::ok()) should be somewhere 'global', e. g. in your main function or a single run function. Do not put it to deep into your class hierarchy, it make your code hard to understand.

If you need your class to publish at constant rate and your CPU load is not 'to high' (i. e. not threading needed due to CPU load) I would add a ros::Timer in your class, bind the timer to a class member function and publish in the member function.

Example:

#include <ros/ros.h>
#include <std_msgs/Empty>

#define TIMER_TIMEOUT (0.5F) // in sec

class MyClass
{
  private:

  ros::NodeHandle po_nh;
  ros::Timer po_timer;
  ros::Publisher po_pub;

  void onTimer()
  {
    po_pub.publish( std_msgs::Empty() );
  }

public:
  MyClass( ros::NodeHandle ao_nh ) :
    po_nh( ao_nh )
  {
    po_pub = po_nh.advertise<std_msgs::Empty>( "~empty", 10 ) ;
    po_timer = po_nh.createTimer( ros::Duration( TIMER_TIMEOUT ), boost::bind( &MyClass::onTimer,  this ) );
  }

  virtual ~MyClass()
  {

  }
};
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-03-13 12:15:33 -0500

Seen: 3,786 times

Last updated: Mar 13 '14