Search code examples
c++c++14rosroboticsgazebo-simu

Callback fuction is not getting called


I am trying to implement ROS GotoGoal in c++, here is the code

#include "ros/ros.h"
#include "geometry_msgs/Twist.h"
#include "geometry_msgs/Pose2D.h"
#include "turtlesim/Pose.h"

class Turtle {

public :
  Turtle(int argc,char** argv){
    ros::init(argc,argv,"mover");
    ros::NodeHandle n;
    pose = turtlesim::Pose();
    pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 100);
    sub = n.subscribe("/turtle1/pose", 100, &Turtle::Update, this);
  }

  void Update(const turtlesim::Pose::ConstPtr& msg){
    ROS_INFO("Pose recieved : x = %f y = %f\n", msg->x, msg->y );
    pose = *msg;
  }

  void move2goal(){
    turtlesim::Pose goalPose= turtlesim::Pose() ;
    
    std::cout<<"Enter goal x : "<<" ";
    std::cin>>goalPose.x ;
    std::cout<<"Enter goal y : "<<" ";
    std::cin>>goalPose.y ;
    
    float d ;
    std::cout<<"Enter distance tolerance d : "<<" ";
    std::cin>>d ;
    
    auto vel_msg = geometry_msgs::Twist() ;
    ros::Rate loop_rate(2.0);
    while(distance(goalPose)>=d && ros::ok()){
      vel_msg.linear.x = linear_velocity(goalPose,1.5); 
      vel_msg.linear.y =0 ;
      vel_msg.linear.z = 0 ;
      
      vel_msg.angular.x = 0 ;
      vel_msg.angular.y= 0 ;
      vel_msg.angular.z = angular_velocity(goalPose,6) ;
      
      pub.publish(vel_msg) ;
      loop_rate.sleep() ; 
 ROS_INFO("current : %f %f\n",pose.x,pose.y) ;  
 }
    vel_msg.angular.z=0 ;
    vel_msg.linear.x =0 ;
    pub.publish(vel_msg) ;
    ros::spin();
  }

  ros::Publisher pub;
  ros::Subscriber sub;
  turtlesim::Pose pose;
  int ch = 0;
};

int main(int argc, char** argv) {
  Turtle turtle = Turtle(argc,argv);
  turtle.move2goal();
  return 0;
}

But the Update callback function is not getting called and the turtle is moving in a circle as pose is not getting updated. I tried using ROS_INFO for debugging the issue but nothing worked. What am I doing wrong here? Note: implementations of few functions have been removed from the code snippet due to stackoverflow's policy.

[Output][1] [1]:https://i.sstatic.net/eL9Sr.png


Solution

  • I think you misunderstood what the sleep does. Unlike spin it doesn't actually perform all the ROS communication events. It's just a convenience for accurate sleep. See Difference between spin and rate.sleep in ROS.

    Fortunately the fix is really easy, just add a spinOnce:

    while( distance(goalPose) >= d && ros::ok()) {
      // (..)
      ros::spinOnce();
      loop_rate.sleep();
    }