Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Just declare your publisher in the global scope. So, above your callback. Then, in main, you can loop the way you mentioned, but use ros::spinOnce() instead of ros::spin()

while (ros::ok()) {
  ros::spinOnce();
}

Just declare your publisher in the global scope. So, above your callback. Then, in main, you can loop the way you mentioned, but use ros::spinOnce() instead of ros::spin()

while (ros::ok()) {
  ros::spinOnce();
  loop_rate.sleep(); // Don't forget this! *
}

* Thanks @gvdhoorn: Without this, your node would use 100% CPU, even if it has nothing to do.

EDIT: loop_rate.sleep()