Subscriber won't update constantly/won't call callback function
Hello,
I need to constantly know the gps coordinates of my robot as it moves through its world. To do this I am trying to use a simple subscriber, that will subscribe to the topic which this data is being published to. My understanding of a subscriber was that the callback would be called every time a message is published to the topic. So I put the declaration of the subscriber in the "main" and assign the message received to a variable. However, this only calls the callback function twice. I then tried the code below, where I placed the declaration within the function I need to use the message data. this however doesn't even call the callback function for some reason
geometry_msgs::Twist srv_velocity;
ros::NodeHandle n;
ros::Subscriber gps_sub = n.subscribe<sensor_msgs::NavSatFix>
("/mavros/global_position/raw/fix", 10, gps_cb);
double current_lat = current_coordinates.latitude, current_long = current_coordinates.longitude;
while(current_lat != target_lat && current_long != target_long){
if(((target_lat - current_lat)< 0.0001)&&((target_long - current_long)<0.0001)){
if(((target_lat - current_lat)<0.00001)&&((target_long-current_long)<0.00001))){
ROS_INFO("STOPPING");
srv_velocity.linear.x=0;
srv_velocity.linear.y=0;
velocity_pub.publish(srv_velocity);
}else{
ROS_INFO("CHANGING SPEED");
srv_velocity.linear.y = 1*cos(yaw);
srv_velocity.linear.x = 1*sin(yaw);
velocity_pub.publish(srv_velocity);
}
}else{
//setting speed to max
srv_velocity.linear.x = 5*cos(yaw);
srv_velocity.linear.y = 5*sine(yaw);
velocity_pub.publish(srv_velocity);
}
ros::Subscriber gps_sub = n.subscribe<sensor_msgs::NavSatFix>
("mavros/global_position/raw/fix",10,gps_cb);
current_lat = current_coordinates.latitude;
current_long = current_coordinates.longitude;
}
This is what my code is right now, I realize this isn't a very good way of doing it but i can't work out a better way. What I asking is, is there a way to update my current_lat and current_long variables from the message published to the "mavros/global_position/raw/fix" topic.