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 currentlat and currentlong variables from the message published to the "mavros/global_position/raw/fix" topic.
Asked by steveJobless on 2021-04-15 08:30:40 UTC
Answers
I think you should read this basic example how to subscribe to msg http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29
If you still have problems, please upload your full code.
Asked by TzviH on 2021-04-15 08:37:20 UTC
Comments
I guess what im trying to ask is how do you send a subscriber into a function
Asked by steveJobless on 2021-04-15 12:25:54 UTC
I am assuming that the above mentioned block of code is inside the main
function. Also, please update the post with the gps_cb
function.
Could you please try making the following changes to your code -
Remove
ros::Subscriber gps_sub = n.subscribe<sensor_msgs::NavSatFix>("mavros/global_position/raw/fix",10,gps_cb);
from thewhile
loop.Add
ros::SpinOnce()
at the end of yourwhile
loop.
Asked by skpro19 on 2021-04-15 15:05:21 UTC
Comments
This code is within another function later in my code, and I don't understand why this wouldn't work in another function other than the main.
Asked by steveJobless on 2021-04-15 18:43:21 UTC
Did you try the changes suggested above?
Asked by skpro19 on 2021-04-15 18:52:52 UTC
Sorry yeah forgot to say, this work really well
Asked by steveJobless on 2021-04-16 06:45:00 UTC
Comments