0
$\begingroup$

Rosanswers logo

I have attached my code. I am trying to make the turtlebot go backward once the bumper is pressed. I'm able to make it move forward and get the bumper pressed signal separately. The problem I'm facing is that it I cannot make them work together. It either moves forward or send the bumper pressed signal. (Depending upon where ros::Spin() is placed)

I think the issue is about the ordering/placement of the following lines in my PublishData Class.

ros::Subscriber bumper = n.subscribe("/mobile_base/events/bumper",1, callback);
pub_data.publish(tw);
ros::spin();

My Code:

void callback(const kobuki_msgs::BumperEventConstPtr msg)
{
      ROS_ERROR("MOVE!!! Bumper pressed.");
      ROS_INFO("MOVE!!! Bumper pressed.");

}
class PublishData
{
      ros::NodeHandle n;
      ros::NodeHandle n1;
    public:
      PublishData()
      {
        ros::Publisher pub_data = n.advertise<geometry_msgs::Twist>("/mobile_base/commands/velocity", 10);
        geometry_msgs::Twist tw;
        tw.linear.x = 0.1;
        ROS_INFO("DataPublished");

        while(ros::ok())
        {
            ros::Subscriber bumper = n.subscribe("/mobile_base/events/bumper",1, callback);
            //ROS_WARN("In Loop.");
            pub_data.publish(tw);
            ros::spin();
        }
      }
};


int main(int argc, char **argv)
{
  ros::init(argc, argv, "move_data");
  PublishData object;
   return 0;
}

Originally posted by Safeer on ROS Answers with karma: 66 on 2013-12-19

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

You should use a combination of spinOnce() and sleep (have a look at the tutorials). spin() blocks until an error occures or the program receives a sigint (ctrl+c).

Also it may be a good idea to move the spinning to the main loop. A blocking constructor seems like rather bad design... :)


Originally posted by Ben_S with karma: 2510 on 2013-12-19

This answer was ACCEPTED on the original site

Post score: 2

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.