Ask Your Question
0

Making Subscriber and Publisher work in the same node

asked 2013-12-19 01:11:47 -0500

Safeer gravatar image

updated 2013-12-21 07:31:24 -0500

tfoote gravatar image

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;
}
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2013-12-19 01:51:49 -0500

Ben_S gravatar image

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... :)

edit flag offensive delete link more
0

answered 2013-12-19 03:59:57 -0500

David Galdeano gravatar image

I think that your: pub_data.publish(tw); and publisher related stuff must go in tour callback function.

Something like that:

class PublishData
{
  ros::NodeHandle n;
  ros::NodeHandle n1;
  geometry_msgs::Twist tw;
public:
  PublishData()
  {
    tw.linear.x = 0.1;
    ros::Publisher pub_data = n.advertise<geometry_msgs::Twist>("/mobile_base/commands/velocity", 10);
    ros::Subscriber bumper = n.subscribe("/mobile_base/events/bumper",1,callback);
    ROS_INFO("DataPublished");
    while(ros::ok())
    {
        //ROS_WARN("In Loop.");
        ros::spin();
    }
  }
  void callback(const kobuki_msgs::BumperEventConstPtr msg)
  {
     ROS_ERROR("MOVE!!! Bumper pressed.");
     ROS_INFO("MOVE!!! Bumper pressed.");
     pub_data.publish(tw);
  }
};
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-12-19 01:11:47 -0500

Seen: 1,019 times

Last updated: Dec 19 '13