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

Revision history [back]

click to hide/show revision 1
initial version

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.

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()
  {

  }
};