Making Subscriber and Publisher work in the same node
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;
}