From df3fb5c0416730ec10e96751fa4734661f82312c Mon Sep 17 00:00:00 2001 From: rmackay9 Date: Sat, 2 Jun 2012 12:51:12 +0900 Subject: [PATCH] ArduCopter: fixed print_latlon bug in which it would print negative lat/lon numbers incorrectly (i.e. -1234567890 would be printed as -124.xxx). --- ArduCopter/Log.pde | 13 ++++++++++--- .../examples/GPS_AUTO_test/GPS_AUTO_test.pde | 16 ++++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/ArduCopter/Log.pde b/ArduCopter/Log.pde index 75ac303ab8..5383167578 100644 --- a/ArduCopter/Log.pde +++ b/ArduCopter/Log.pde @@ -220,13 +220,20 @@ process_logs(uint8_t argc, const Menu::arg *argv) return 0; } - // print_latlon - prints an latitude or longitude value held in an int32_t // probably this should be moved to AP_Common void print_latlon(BetterStream *s, int32_t lat_or_lon) { - int32_t dec_portion = lat_or_lon / T7; - int32_t frac_portion = labs(lat_or_lon - dec_portion*T7); + int32_t dec_portion, frac_portion; + + // extract decimal portion (special handling of negative numbers to ensure we round towards zero) + dec_portion = labs(lat_or_lon) / T7; + if( lat_or_lon < 0 ){ + dec_portion = -dec_portion; + } + + // extract fractional portion + frac_portion = labs(lat_or_lon - dec_portion*T7); s->printf("%ld.%07ld",(long)dec_portion,(long)frac_portion); } diff --git a/libraries/AP_GPS/examples/GPS_AUTO_test/GPS_AUTO_test.pde b/libraries/AP_GPS/examples/GPS_AUTO_test/GPS_AUTO_test.pde index c31447ac04..ff312843fd 100644 --- a/libraries/AP_GPS/examples/GPS_AUTO_test/GPS_AUTO_test.pde +++ b/libraries/AP_GPS/examples/GPS_AUTO_test/GPS_AUTO_test.pde @@ -17,13 +17,21 @@ AP_GPS_Auto GPS(&Serial1, &gps); #define T6 1000000 #define T7 10000000 -// print_latlon - prints an latitude or longitude value held in an int32_t +// print_latlon - prints an latitude or longitude value held in an int32_t // probably this should be moved to AP_Common void print_latlon(BetterStream *s, int32_t lat_or_lon) { - int32_t dec_portion = lat_or_lon / T7; - int32_t frac_portion = labs(lat_or_lon - dec_portion*T7); - s->printf("%ld.%07ld",dec_portion,frac_portion); + int32_t dec_portion, frac_portion; + + // extract decimal portion (special handling of negative numbers to ensure we round towards zero) + dec_portion = labs(lat_or_lon) / T7; + if( lat_or_lon < 0 ){ + dec_portion = -dec_portion; + } + + // extract fractional portion + frac_portion = labs(lat_or_lon - dec_portion*T7); + s->printf("%ld.%07ld",(long)dec_portion,(long)frac_portion); } void setup()