first cut at turnon scripts for EasyTimer v2
[fw/altos] / src / kernel / ao_microkalman.c
index 0684ea2bbaad4b03e747ab6421b83dce885a78b3..a9b2c7092aa33e20c3716857f45e1b7550551d26 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
 
 #define FIX_BITS       16
 
-#define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
-#define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
+#define to_fix_v(x) ((int16_t) ((x) * 65536.0 + 0.5))
+#define to_fix_k(x) ((int32_t) ((x) * 65536.0 + 0.5))
 #define from_fix8(x)   ((x) >> 8)
 #define from_fix(x)    ((x) >> 16)
-#define fix8_to_fix16(x)       ((x) << 8)
+#define fix8_to_fix_v(x)       ((x) << 8)
 #define fix16_to_fix8(x)       ((x) >> 8)
 
 #include <ao_kalman.h>
 
+#ifndef AO_MK_STEP_96MS
+#define AO_MK_STEP_96MS        1
+
+#endif
+
+#if AO_MK_STEP_100MS
+#define AO_MK_TIME_STEP        0.1
+
+#define AO_K0_10       AO_MK2_BARO_K0_10
+#define AO_K1_10       AO_MK2_BARO_K1_10
+#define AO_K2_10       AO_MK2_BARO_K2_10
+#endif
+
+#if AO_MK_STEP_96MS
+#define AO_MK_TIME_STEP        0.096
+
+#define AO_K0_10       AO_MK_BARO_K0_10
+#define AO_K1_10       AO_MK_BARO_K1_10
+#define AO_K2_10       AO_MK_BARO_K2_10
+#endif
+
 /* Basic time step (96ms) */
-#define AO_MK_STEP     to_fix16(0.096)
+#define AO_MK_STEP     to_fix_v(AO_MK_TIME_STEP)
+
 /* step ** 2 / 2 */
-#define AO_MK_STEP_2_2 to_fix16(0.004608)
+#define AO_MK_STEP_2_2 to_fix_v(AO_MK_TIME_STEP * AO_MK_TIME_STEP / 2.0)
 
 uint32_t       ao_k_pa;                /* 24.8 fixed point */
 int32_t                ao_k_pa_speed;          /* 16.16 fixed point */
@@ -47,14 +70,14 @@ int16_t             ao_pa_accel;            /* integer portion */
 void
 ao_microkalman_init(void)
 {
-       ao_pa = pa;
-       ao_k_pa = pa << 8;
-}      
+       ao_pa = (uint32_t) pa;
+       ao_k_pa = (uint32_t) (pa << 8);
+}
 
 void
 ao_microkalman_predict(void)
 {
-       ao_k_pa       += fix16_to_fix8((int32_t) ao_pa_speed * AO_MK_STEP + (int32_t) ao_pa_accel * AO_MK_STEP_2_2);
+       ao_k_pa       += (uint32_t) (int32_t) fix16_to_fix8((int32_t) ao_pa_speed * AO_MK_STEP + (int32_t) ao_pa_accel * AO_MK_STEP_2_2);
        ao_k_pa_speed += (int32_t) ao_pa_accel * AO_MK_STEP;
 }
 
@@ -63,12 +86,12 @@ ao_microkalman_correct(void)
 {
        int16_t e;      /* Height error in Pa */
 
-       e = pa - from_fix8(ao_k_pa);
+       e = (int16_t) (pa - from_fix8(ao_k_pa));
 
-       ao_k_pa       += fix16_to_fix8((int32_t) e * AO_MK_BARO_K0_10);
-       ao_k_pa_speed += (int32_t) e * AO_MK_BARO_K1_10;
-       ao_k_pa_accel += (int32_t) e * AO_MK_BARO_K2_10;
+       ao_k_pa       += (uint32_t)(int32_t)fix16_to_fix8(e * AO_K0_10);
+       ao_k_pa_speed += (int32_t) e * AO_K1_10;
+       ao_k_pa_accel += (int32_t) e * AO_K2_10;
        ao_pa = from_fix8(ao_k_pa);
-       ao_pa_speed = from_fix(ao_k_pa_speed);
-       ao_pa_accel = from_fix(ao_k_pa_accel);
+       ao_pa_speed = (int16_t) from_fix(ao_k_pa_speed);
+       ao_pa_accel = (int16_t) from_fix(ao_k_pa_accel);
 }