Browse Source

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.
master
Gustavo Jose de Sousa 9 years ago committed by Lucas De Marchi
parent
commit
2246343ec7
  1. 4
      libraries/AP_HAL_SITL/sitl_barometer.cpp
  2. 3
      libraries/AP_HAL_SITL/sitl_compass.cpp
  3. 4
      libraries/AP_HAL_SITL/sitl_ins.cpp

4
libraries/AP_HAL_SITL/sitl_barometer.cpp

@ -76,7 +76,9 @@ void SITL_State::_update_barometer(float altitude) @@ -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;

3
libraries/AP_HAL_SITL/sitl_compass.cpp

@ -73,7 +73,8 @@ void SITL_State::_update_compass(float rollDeg, float pitchDeg, float yawDeg) @@ -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;

4
libraries/AP_HAL_SITL/sitl_ins.cpp

@ -62,7 +62,9 @@ uint16_t SITL_State::_airspeed_sensor(float airspeed) @@ -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;

Loading…
Cancel
Save