GPS: handle negative and high altitudes; safer macros in maths.h

Fixes underflows at negative altitude (below MSL) and overflows at altitude higher than 655.35m
Corrected parenthesis in maths.h avoid incorrect equations if arguments contain expressions.
This commit is contained in:
AirBreak69 2018-06-12 00:02:29 +02:00
parent a5ba01666b
commit 536ad399e6
4 changed files with 11 additions and 8 deletions

View File

@ -1,3 +1,4 @@
/*
/* /*
* This file is part of Cleanflight and Betaflight. * This file is part of Cleanflight and Betaflight.
* *
@ -33,13 +34,13 @@
#define M_PIf 3.14159265358979323846f #define M_PIf 3.14159265358979323846f
#define RAD (M_PIf / 180.0f) #define RAD (M_PIf / 180.0f)
#define DEGREES_TO_DECIDEGREES(angle) (angle * 10) #define DEGREES_TO_DECIDEGREES(angle) ((angle) * 10)
#define DECIDEGREES_TO_DEGREES(angle) (angle / 10) #define DECIDEGREES_TO_DEGREES(angle) ((angle) / 10)
#define DECIDEGREES_TO_RADIANS(angle) ((angle / 10.0f) * 0.0174532925f) #define DECIDEGREES_TO_RADIANS(angle) ((angle) / 10.0f * 0.0174532925f)
#define DEGREES_TO_RADIANS(angle) ((angle) * 0.0174532925f) #define DEGREES_TO_RADIANS(angle) ((angle) * 0.0174532925f)
#define CM_S_TO_KM_H(centimetersPerSecond) (centimetersPerSecond * 36 / 1000) #define CM_S_TO_KM_H(centimetersPerSecond) ((centimetersPerSecond) * 36 / 1000)
#define CM_S_TO_MPH(centimetersPerSecond) (((centimetersPerSecond * 10000) / 5080) / 88) #define CM_S_TO_MPH(centimetersPerSecond) ((centimetersPerSecond) * 10000 / 5080 / 88)
#define MIN(a,b) \ #define MIN(a,b) \
__extension__ ({ __typeof__ (a) _a = (a); \ __extension__ ({ __typeof__ (a) _a = (a); \

View File

@ -1899,7 +1899,7 @@ static mspResult_e mspProcessInCommand(uint8_t cmdMSP, sbuf_t *src)
gpsSol.numSat = sbufReadU8(src); gpsSol.numSat = sbufReadU8(src);
gpsSol.llh.lat = sbufReadU32(src); gpsSol.llh.lat = sbufReadU32(src);
gpsSol.llh.lon = sbufReadU32(src); gpsSol.llh.lon = sbufReadU32(src);
gpsSol.llh.alt = sbufReadU16(src); gpsSol.llh.alt = sbufReadU32(src);
gpsSol.groundSpeed = sbufReadU16(src); gpsSol.groundSpeed = sbufReadU16(src);
GPS_update |= 2; // New data signalisation to GPS functions // FIXME Magic Numbers GPS_update |= 2; // New data signalisation to GPS functions // FIXME Magic Numbers
break; break;

View File

@ -662,7 +662,7 @@ typedef struct gpsDataNmea_s {
int32_t latitude; int32_t latitude;
int32_t longitude; int32_t longitude;
uint8_t numSat; uint8_t numSat;
uint16_t altitude; int32_t altitude;
uint16_t speed; uint16_t speed;
uint16_t hdop; uint16_t hdop;
uint16_t ground_course; uint16_t ground_course;
@ -734,6 +734,8 @@ static bool gpsNewFrameNMEA(char c)
break; break;
case 9: case 9:
gps_Msg.altitude = grab_fields(string, 0); // altitude in meters added by Mis gps_Msg.altitude = grab_fields(string, 0); // altitude in meters added by Mis
if (string[0] == '-')
gps_Msg.altitude = -gps_Msg.altitude; // handle negative altitudes
break; break;
} }
break; break;

View File

@ -91,7 +91,7 @@ typedef struct gpsLocation_s {
typedef struct gpsSolutionData_s { typedef struct gpsSolutionData_s {
gpsLocation_t llh; gpsLocation_t llh;
uint16_t GPS_altitude; // altitude in 0.1m // int32_t GPS_altitude; // altitude in 0.1m: UNUSED!
uint16_t groundSpeed; // speed in 0.1m/s uint16_t groundSpeed; // speed in 0.1m/s
uint16_t groundCourse; // degrees * 10 uint16_t groundCourse; // degrees * 10
uint16_t hdop; // generic HDOP value (*100) uint16_t hdop; // generic HDOP value (*100)