altos: Perform time comparisons using 16-bit arithmetic to handle wrap
[fw/altos] / src / kernel / ao_kalman.c
index 7b0f8145d807ce74bd5db66c0d23408251a8d745..d401a3c8ff5bd04a88c7dea0216d53abcd145589 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -54,14 +55,14 @@ static void
 ao_kalman_predict(void)
 {
 #ifdef AO_FLIGHT_TEST
-       if (ao_sample_tick - ao_sample_prev_tick > 50) {
+       if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) {
                ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_1 +
                                (ao_k_t) ao_accel * AO_K_STEP_2_2_1) >> 4;
                ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_1;
 
                return;
        }
-       if (ao_sample_tick - ao_sample_prev_tick > 5) {
+       if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
                ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_10 +
                                (ao_k_t) ao_accel * AO_K_STEP_2_2_10) >> 4;
                ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_10;
@@ -140,13 +141,13 @@ ao_kalman_correct_baro(void)
 {
        ao_kalman_err_height();
 #ifdef AO_FLIGHT_TEST
-       if (ao_sample_tick - ao_sample_prev_tick > 50) {
+       if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) {
                ao_k_height += (ao_k_t) AO_BARO_K0_1 * ao_error_h;
                ao_k_speed  += (ao_k_t) AO_BARO_K1_1 * ao_error_h;
                ao_k_accel  += (ao_k_t) AO_BARO_K2_1 * ao_error_h;
                return;
        }
-       if (ao_sample_tick - ao_sample_prev_tick > 5) {
+       if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
                ao_k_height += (ao_k_t) AO_BARO_K0_10 * ao_error_h;
                ao_k_speed  += (ao_k_t) AO_BARO_K1_10 * ao_error_h;
                ao_k_accel  += (ao_k_t) AO_BARO_K2_10 * ao_error_h;
@@ -179,7 +180,7 @@ ao_kalman_correct_both(void)
        ao_kalman_err_accel();
 
 #ifdef AO_FLIGHT_TEST
-       if (ao_sample_tick - ao_sample_prev_tick > 50) {
+       if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) {
                if (ao_flight_debug) {
                        printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
                                ao_k_speed / (65536.0 * 16.0),
@@ -200,7 +201,7 @@ ao_kalman_correct_both(void)
                        (ao_k_t) AO_BOTH_K21_1 * ao_error_a;
                return;
        }
-       if (ao_sample_tick - ao_sample_prev_tick > 5) {
+       if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
                if (ao_flight_debug) {
                        printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
                                ao_k_speed / (65536.0 * 16.0),
@@ -249,7 +250,7 @@ ao_kalman_correct_accel(void)
 {
        ao_kalman_err_accel();
 
-       if (ao_sample_tick - ao_sample_prev_tick > 5) {
+       if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
                ao_k_height +=(ao_k_t) AO_ACCEL_K0_10 * ao_error_a;
                ao_k_speed  += (ao_k_t) AO_ACCEL_K1_10 * ao_error_a;
                ao_k_accel  += (ao_k_t) AO_ACCEL_K2_10 * ao_error_a;
@@ -284,9 +285,9 @@ ao_kalman(void)
                ao_max_height = ao_height;
        ao_avg_height_scaled = ao_avg_height_scaled - ao_avg_height + ao_sample_height;
 #ifdef AO_FLIGHT_TEST
-       if (ao_sample_tick - ao_sample_prev_tick > 50)
+       if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50)
                ao_avg_height = (ao_avg_height_scaled + 1) >> 1;
-       else if (ao_sample_tick - ao_sample_prev_tick > 5)
+       else if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5)
                ao_avg_height = (ao_avg_height_scaled + 7) >> 4;
        else 
 #endif