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

if you want to call the function motor_position in a regular basis (let's say every x Hz) you call it during a loop in the main function. In this case use a variable in the callback to save the value for later use in the main function. If you want to call it just once ever time you get a message from the subscriber then call it in the Callback function. As your code lets me guess you want to do the first solution. Your problem is that you do not have a while loop in the main.

The main should look somehow like this:

int main(int argc, char **argv) {
  ros::init(argc, argv, "subscriber");
  ros::NodeHandle n;

  ros::Subscriber sub = n.subscribe("topicname", 1000, chatterCallback);

  ros::Rate loop_rate(10);
  while (ros::ok()) {
    motor_position(a);
    ros::spinOnce();
    loop_rate.sleep();
  }
  return 0;
}