altos: Calibrate IMU accelerometers too
authorKeith Packard <keithp@keithp.com>
Fri, 25 Oct 2013 11:34:16 +0000 (04:34 -0700)
committerKeith Packard <keithp@keithp.com>
Fri, 25 Oct 2013 11:34:16 +0000 (04:34 -0700)
Average the IMU accelerometer values pointing up and down so that we
have a zero-g offset for all three axes. This can then be used to
compute which direction the rocket is pointing while sitting on the pad.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/core/ao.h
src/core/ao_config.c
src/core/ao_sample.c
src/test/ao_flight_test.c

index ea37885e03e4c84987cea74904ee2bbc1f5ae4d5..d12f13a08f0f3979e4cbae178b8db33133e9836b 100644 (file)
@@ -739,7 +739,7 @@ extern __xdata uint8_t ao_force_freq;
 #endif
 
 #define AO_CONFIG_MAJOR        1
 #endif
 
 #define AO_CONFIG_MAJOR        1
-#define AO_CONFIG_MINOR        14
+#define AO_CONFIG_MINOR        15
 
 #define AO_AES_LEN 16
 
 
 #define AO_AES_LEN 16
 
@@ -773,6 +773,11 @@ struct ao_config {
 #if HAS_RADIO_AMP
        uint8_t         radio_amp;              /* minor version 14 */
 #endif
 #if HAS_RADIO_AMP
        uint8_t         radio_amp;              /* minor version 14 */
 #endif
+#if HAS_GYRO
+       uint16_t        accel_zero_along;       /* minor version 15 */
+       uint16_t        accel_zero_across;      /* minor version 15 */
+       uint16_t        accel_zero_through;     /* minor version 15 */
+#endif
 };
 
 #define AO_IGNITE_MODE_DUAL            0
 };
 
 #define AO_IGNITE_MODE_DUAL            0
index b480e14c40edd3effc85b0da3f6474283bdd669a..82faf32bbb904b22517fb814f86d829f30231d3f 100644 (file)
@@ -155,6 +155,19 @@ _ao_config_get(void)
 #if HAS_RADIO_AMP
                if (minor  < 14)
                        ao_config.radio_amp = AO_CONFIG_DEFAULT_RADIO_AMP;
 #if HAS_RADIO_AMP
                if (minor  < 14)
                        ao_config.radio_amp = AO_CONFIG_DEFAULT_RADIO_AMP;
+#endif
+#if HAS_GYRO
+               if (minor < 15) {
+                       ao_config.accel_zero_along = 0;
+                       ao_config.accel_zero_across = 0;
+                       ao_config.accel_zero_through = 0;
+
+                       /* Reset the main accel offsets to force
+                        * re-calibration
+                        */
+                       ao_config.accel_plus_g = 0;
+                       ao_config.accel_minus_g = 0;
+               }
 #endif
                ao_config.minor = AO_CONFIG_MINOR;
                ao_config_dirty = 1;
 #endif
                ao_config.minor = AO_CONFIG_MINOR;
                ao_config_dirty = 1;
@@ -275,17 +288,34 @@ ao_config_accel_calibrate_show(void) __reentrant
 {
        printf("Accel cal +1g: %d -1g: %d\n",
               ao_config.accel_plus_g, ao_config.accel_minus_g);
 {
        printf("Accel cal +1g: %d -1g: %d\n",
               ao_config.accel_plus_g, ao_config.accel_minus_g);
+#if HAS_GYRO
+       printf ("IMU cal along %d across %d through %d\n",
+               ao_config.accel_zero_along,
+               ao_config.accel_zero_across,
+               ao_config.accel_zero_through);
+#endif
 }
 
 #define ACCEL_CALIBRATE_SAMPLES        1024
 #define ACCEL_CALIBRATE_SHIFT  10
 
 }
 
 #define ACCEL_CALIBRATE_SAMPLES        1024
 #define ACCEL_CALIBRATE_SHIFT  10
 
+#if HAS_GYRO
+static int16_t accel_cal_along;
+static int16_t accel_cal_across;
+static int16_t accel_cal_through;
+#endif
+
 static int16_t
 ao_config_accel_calibrate_auto(char *orientation) __reentrant
 {
        uint16_t        i;
        int32_t         accel_total;
        uint8_t         cal_data_ring;
 static int16_t
 ao_config_accel_calibrate_auto(char *orientation) __reentrant
 {
        uint16_t        i;
        int32_t         accel_total;
        uint8_t         cal_data_ring;
+#if HAS_GYRO
+       int32_t         accel_along_total = 0;
+       int32_t         accel_across_total = 0;
+       int32_t         accel_through_total = 0;
+#endif
 
        printf("Orient antenna %s and press a key...", orientation);
        flush();
 
        printf("Orient antenna %s and press a key...", orientation);
        flush();
@@ -299,10 +329,20 @@ ao_config_accel_calibrate_auto(char *orientation) __reentrant
                ao_sleep(DATA_TO_XDATA(&ao_sample_data));
                while (i && cal_data_ring != ao_sample_data) {
                        accel_total += (int32_t) ao_data_accel(&ao_data_ring[cal_data_ring]);
                ao_sleep(DATA_TO_XDATA(&ao_sample_data));
                while (i && cal_data_ring != ao_sample_data) {
                        accel_total += (int32_t) ao_data_accel(&ao_data_ring[cal_data_ring]);
+#if HAS_GYRO
+                       accel_along_total += (int32_t) ao_data_along(&ao_data_ring[cal_data_ring]);
+                       accel_across_total += (int32_t) ao_data_across(&ao_data_ring[cal_data_ring]);
+                       accel_through_total += (int32_t) ao_data_through(&ao_data_ring[cal_data_ring]);
+#endif
                        cal_data_ring = ao_data_ring_next(cal_data_ring);
                        i--;
                }
        }
                        cal_data_ring = ao_data_ring_next(cal_data_ring);
                        i--;
                }
        }
+#if HAS_GYRO
+       accel_cal_along = accel_along_total >> ACCEL_CALIBRATE_SHIFT;
+       accel_cal_across = accel_across_total >> ACCEL_CALIBRATE_SHIFT;
+       accel_cal_through = accel_through_total >> ACCEL_CALIBRATE_SHIFT;
+#endif
        return accel_total >> ACCEL_CALIBRATE_SHIFT;
 }
 
        return accel_total >> ACCEL_CALIBRATE_SHIFT;
 }
 
@@ -310,12 +350,28 @@ void
 ao_config_accel_calibrate_set(void) __reentrant
 {
        int16_t up, down;
 ao_config_accel_calibrate_set(void) __reentrant
 {
        int16_t up, down;
+#if HAS_GYRO
+       int16_t accel_along_up, accel_along_down;
+       int16_t accel_across_up, accel_across_down;
+       int16_t accel_through_up, accel_through_down;
+#endif
+       
        ao_cmd_decimal();
        if (ao_cmd_status != ao_cmd_success)
                return;
        if (ao_cmd_lex_i == 0) {
                up = ao_config_accel_calibrate_auto("up");
        ao_cmd_decimal();
        if (ao_cmd_status != ao_cmd_success)
                return;
        if (ao_cmd_lex_i == 0) {
                up = ao_config_accel_calibrate_auto("up");
+#if HAS_GYRO
+               accel_along_up = accel_cal_along;
+               accel_across_up = accel_cal_across;
+               accel_through_up = accel_cal_through;
+#endif
                down = ao_config_accel_calibrate_auto("down");
                down = ao_config_accel_calibrate_auto("down");
+#if HAS_GYRO
+               accel_along_down = accel_cal_along;
+               accel_across_down = accel_cal_across;
+               accel_through_down = accel_cal_through;
+#endif
        } else {
                up = ao_cmd_lex_i;
                ao_cmd_decimal();
        } else {
                up = ao_cmd_lex_i;
                ao_cmd_decimal();
@@ -331,6 +387,11 @@ ao_config_accel_calibrate_set(void) __reentrant
        _ao_config_edit_start();
        ao_config.accel_plus_g = up;
        ao_config.accel_minus_g = down;
        _ao_config_edit_start();
        ao_config.accel_plus_g = up;
        ao_config.accel_minus_g = down;
+#if HAS_GYRO
+       ao_config.accel_zero_along = (accel_along_up + accel_along_down) / 2;
+       ao_config.accel_zero_across = (accel_across_up + accel_across_down) / 2;
+       ao_config.accel_zero_through = (accel_through_up + accel_through_down) / 2;
+#endif
        _ao_config_edit_finish();
 }
 #endif /* HAS_ACCEL */
        _ao_config_edit_finish();
 }
 #endif /* HAS_ACCEL */
index 676e0ffdc74775a4e5201a4c38202be4bb331edd..a9d50cb2535c528947d4cc3fb0f8b8aa06df5828 100644 (file)
@@ -139,19 +139,16 @@ ao_sample_preflight_set(void)
        /* No rotation yet */
        ao_quaternion_init_zero_rotation(&ao_rotation);
 
        /* No rotation yet */
        ao_quaternion_init_zero_rotation(&ao_rotation);
 
-       /* XXX Assume we're pointing straight up for now */
+       /* Take the pad IMU acceleration values and compute our current direction
+        */
        ao_quaternion_init_vector(&ao_pad_orientation,
        ao_quaternion_init_vector(&ao_pad_orientation,
-                                 ao_ground_accel_across,
-                                 ao_ground_accel_through,
-                                 -ao_ground_accel_along);
+                                 ao_ground_accel_across - ao_config.accel_zero_across,
+                                 ao_ground_accel_through - ao_config.accel_zero_through,
+                                 -ao_ground_accel_along - ao_config.accel_zero_along);
+
        ao_quaternion_normalize(&ao_pad_orientation,
                                &ao_pad_orientation);
                                  
        ao_quaternion_normalize(&ao_pad_orientation,
                                &ao_pad_orientation);
                                  
-       printf ("pad r%8.5f x%8.5f y%8.5f z%8.5f\n",
-               ao_pad_orientation.r,
-               ao_pad_orientation.x,
-               ao_pad_orientation.y,
-               ao_pad_orientation.z);
 #endif 
        nsamples = 0;
 }
 #endif 
        nsamples = 0;
 }
index e2f63e3498278614e36edec310b33ea720f6afbd..7f18c80e7d3db0551fbedf219073c85b81a7006e 100644 (file)
@@ -234,6 +234,9 @@ struct ao_config {
        uint16_t        apogee_lockout;
 #if TELEMEGA
        struct ao_pyro  pyro[AO_PYRO_NUM];      /* minor version 12 */
        uint16_t        apogee_lockout;
 #if TELEMEGA
        struct ao_pyro  pyro[AO_PYRO_NUM];      /* minor version 12 */
+       int16_t         accel_zero_along;
+       int16_t         accel_zero_across;
+       int16_t         accel_zero_through;
 #endif
 };
 
 #endif
 };
 
@@ -719,6 +722,13 @@ ao_sleep(void *wchan)
                        } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
                                ao_config.accel_plus_g = atoi(words[3]);
                                ao_config.accel_minus_g = atoi(words[5]);
                        } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
                                ao_config.accel_plus_g = atoi(words[3]);
                                ao_config.accel_minus_g = atoi(words[5]);
+#ifdef TELEMEGA
+                       } else if (nword >= 8 && strcmp(words[0], "IMU") == 0) {
+                               ao_config.accel_zero_along = atoi(words[3]);
+                               ao_config.accel_zero_across = atoi(words[5]);
+                               ao_config.accel_zero_through = atoi(words[7]);
+                               printf ("%d %d %d\n", ao_config.accel_zero_along, ao_config.accel_zero_across, ao_config.accel_zero_through);
+#endif
                        } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
                                ao_config.main_deploy = atoi(words[2]);
                        } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
                        } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
                                ao_config.main_deploy = atoi(words[2]);
                        } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&