AP_HAL_SITL: pass signed value to abs()

Passing an unsigned value to abs() causes compilation error in gcc 6.1.1
(and doing that doesn't make sense too).

As a bonus, this patch fixes the code, since, for two unsigned integers
a and b, such that a > b, (a - b) without the casting to a signed
integer would produce garbage in the context of this patch. The type
int32_t is enough for the cases covered by this patch.
This commit is contained in:
Gustavo Jose de Sousa 2016-05-11 18:54:16 -03:00 committed by Lucas De Marchi
parent 421b9ef54a
commit 2246343ec7
3 changed files with 8 additions and 3 deletions

View File

@ -76,7 +76,9 @@ void SITL_State::_update_barometer(float altitude)
// find data corresponding to delayed time in buffer
for (uint8_t i=0; i<=baro_buffer_length-1; i++) {
time_delta_baro = abs(delayed_time_baro - buffer_baro[i].time); // find difference between delayed time and time stamp in buffer
// find difference between delayed time and time stamp in buffer
time_delta_baro = abs(
(int32_t)(delayed_time_baro - buffer_baro[i].time));
// if this difference is smaller than last delta, store this time
if (time_delta_baro < best_time_delta_baro) {
best_index_baro = i;

View File

@ -73,7 +73,8 @@ void SITL_State::_update_compass(float rollDeg, float pitchDeg, float yawDeg)
delayed_time_mag = now - _sitl->mag_delay; // get time corresponding to delay
// find data corresponding to delayed time in buffer
for (uint8_t i=0; i<=mag_buffer_length-1; i++) {
time_delta_mag = abs(delayed_time_mag - buffer_mag[i].time); // find difference between delayed time and time stamp in buffer
// find difference between delayed time and time stamp in buffer
time_delta_mag = abs((int32_t)(delayed_time_mag - buffer_mag[i].time));
// if this difference is smaller than last delta, store this time
if (time_delta_mag < best_time_delta_mag) {
best_index_mag = i;

View File

@ -62,7 +62,9 @@ uint16_t SITL_State::_airspeed_sensor(float airspeed)
delayed_time_wind = now - _sitl->wind_delay; // get time corresponding to delay
// find data corresponding to delayed time in buffer
for (uint8_t i=0; i<=wind_buffer_length-1; i++) {
time_delta_wind = abs(delayed_time_wind - buffer_wind[i].time); // find difference between delayed time and time stamp in buffer
// find difference between delayed time and time stamp in buffer
time_delta_wind = abs(
(int32_t)(delayed_time_wind - buffer_wind[i].time));
// if this difference is smaller than last delta, store this time
if (time_delta_wind < best_time_delta_wind) {
best_index_wind = i;