ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Subscriber won't update constantly/won't call callback function

asked 2021-04-15 08:30:40 -0500

steveJobless gravatar image

updated 2021-04-15 13:22:34 -0500


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("CHANGING SPEED");
                                  srv_velocity.linear.y = 1*cos(yaw);
                                  srv_velocity.linear.x = 1*sin(yaw);
                     //setting speed to max
                     srv_velocity.linear.x = 5*cos(yaw);
                     srv_velocity.linear.y = 5*sine(yaw);
            ros::Subscriber gps_sub = n.subscribe<sensor_msgs::NavSatFix>
            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.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2021-04-15 15:05:21 -0500

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 the while loop.

  • Add ros::SpinOnce() at the end of your while loop.

edit flag offensive delete link more


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.

steveJobless gravatar image steveJobless  ( 2021-04-15 18:43:21 -0500 )edit

Did you try the changes suggested above?

skpro19 gravatar image skpro19  ( 2021-04-15 18:52:52 -0500 )edit

Sorry yeah forgot to say, this work really well

steveJobless gravatar image steveJobless  ( 2021-04-16 06:45:00 -0500 )edit

answered 2021-04-15 08:37:20 -0500

TzviH gravatar image

I think you should read this basic example how to subscribe to msg

If you still have problems, please upload your full code.

edit flag offensive delete link more


I guess what im trying to ask is how do you send a subscriber into a function

steveJobless gravatar image steveJobless  ( 2021-04-15 12:25:54 -0500 )edit

Question Tools

1 follower


Asked: 2021-04-15 08:30:40 -0500

Seen: 628 times

Last updated: Apr 15 '21