message being rounded between topic and subscriber

asked 2021-04-18 06:59:15 -0500

rosannaStevens gravatar image


I have been trying to use a subscriber node to read a latitude and longitude from a topic. When I read the topic using "rostopic echo" the lat and long are to 7 decimal places, which what i need, however when checked what my subscriber is hearing the message is being rounded off at 4 decimal places. I have checked and the message type is float64 and I tried assigning it to a "double" and a "long double", both just get rounded.

ros::init(argc, argv, "reading_gps");
ros:NodeHandle n;

ros::Subscriber gps_sub = n.subscribe<sensor::NavSatFix>("/mavros/global_position/raw/fix", 10, gps_cb);


The code above is what I have written to just to test the message that is being subscribed to, and the callback function is below.

void gps_cb(const sensor_msgs::NavSatFix::ConstPtr& msg){
         current_coordinates = *msg;
edit retag flag offensive close merge delete


It's very unlikely what you describe is happening (ie: messages being rounded between publisher and subscriber).

It's more likely the cout you're using in your subscriber is just not configured to show the number of decimals you're looking for.

Could you check what happens when you do this:

void gps_cb(const sensor_msgs::NavSatFix::ConstPtr& msg)
  std::cout << std::setprecision(16) << msg->latitude << std::endl;

I would also suggest adding something like a sleep or a ros::Rate in your while(ros::ok()) loop. As-is, this will loop at the maximum possible frequency, needlessly eating up all of your CPU. You already have a ros::Rate(20) in your program, why are you not using it (ie: have a rate.sleep() in your while loop)?

gvdhoorn gravatar image gvdhoorn  ( 2021-04-18 11:15:19 -0500 )edit

Did you figure out whether the configuration of the output stream was the cause of what you observed?

gvdhoorn gravatar image gvdhoorn  ( 2021-04-23 04:47:28 -0500 )edit