altos: Try IMU self-test 10 times before giving up
[fw/altos] / src / core / ao_flight.c
index a0affc4868f028dd61ccea1b86c0ac863e404623..463ff4a2974b38e045fb2214ca9bc163cd490d28 100644 (file)
 #include <ao_log.h>
 #endif
 
+#if HAS_MPU6000
+#include <ao_quaternion.h>
+#endif
+
 #ifndef HAS_ACCEL
 #error Please define HAS_ACCEL
 #endif
 
 __pdata enum ao_flight_state   ao_flight_state;        /* current flight state */
 __pdata uint16_t               ao_boost_tick;          /* time of launch detect */
+__pdata uint16_t               ao_motor_number;        /* number of motors burned so far */
+
+#if HAS_IMU
+/* Any sensor can set this to mark the flight computer as 'broken' */
+__xdata uint8_t                        ao_sensor_errors;
+#endif
 
 /*
  * track min/max data over a long interval to detect
@@ -48,7 +58,9 @@ __pdata uint16_t              ao_boost_tick;          /* time of launch detect */
 static __data uint16_t         ao_interval_end;
 static __data int16_t          ao_interval_min_height;
 static __data int16_t          ao_interval_max_height;
+#if HAS_ACCEL
 static __data int16_t          ao_coast_avg_accel;
+#endif
 
 __pdata uint8_t                        ao_flight_force_idle;
 
@@ -91,7 +103,12 @@ ao_flight(void)
                        if (ao_config.accel_plus_g == 0 ||
                            ao_config.accel_minus_g == 0 ||
                            ao_ground_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP ||
-                           ao_ground_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP)
+                           ao_ground_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP ||
+#if HAS_IMU
+                           ao_sensor_errors ||
+#endif
+                           ao_ground_height < -1000 ||
+                           ao_ground_height > 7000)
                        {
                                /* Detected an accel value outside -1.5g to 1.5g
                                 * (or uncalibrated values), so we go into invalid mode
@@ -112,14 +129,14 @@ ao_flight(void)
                        {
                                /* Set pad mode - we can fly! */
                                ao_flight_state = ao_flight_pad;
-#if HAS_USB && HAS_RADIO
+#if HAS_USB && !HAS_FLIGHT_DEBUG && !HAS_SAMPLE_PROFILE
                                /* Disable the USB controller in flight mode
                                 * to save power
                                 */
                                ao_usb_disable();
 #endif
 
-#if !HAS_ACCEL
+#if !HAS_ACCEL && PACKET_HAS_SLAVE
                                /* Disable packet mode in pad state on TeleMini */
                                ao_packet_slave_stop();
 #endif
@@ -129,8 +146,10 @@ ao_flight(void)
                                ao_rdf_set(1);
                                ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
 #endif
+#if HAS_LED
                                /* signal successful initialization by turning off the LED */
                                ao_led_off(AO_LED_RED);
+#endif
                        } else {
                                /* Set idle mode */
                                ao_flight_state = ao_flight_idle;
@@ -140,8 +159,10 @@ ao_flight(void)
                                ao_packet_slave_start();
 #endif
 
+#if HAS_LED
                                /* signal successful initialization by turning off the LED */
                                ao_led_off(AO_LED_RED);
+#endif
                        }
                        /* wakeup threads due to state change */
                        ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
@@ -185,8 +206,8 @@ ao_flight(void)
 
 #if HAS_GPS
                                /* Record current GPS position by waking up GPS log tasks */
-                               ao_wakeup(&ao_gps_data);
-                               ao_wakeup(&ao_gps_tracking_data);
+                               ao_gps_new = AO_GPS_NEW_DATA | AO_GPS_NEW_TRACKING;
+                               ao_wakeup(&ao_gps_new);
 #endif
 
                                ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
@@ -213,6 +234,7 @@ ao_flight(void)
 #else
                                ao_flight_state = ao_flight_coast;
 #endif
+                               ++ao_motor_number;
                                ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
                        }
                        break;
@@ -350,13 +372,25 @@ ao_flight(void)
                                ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
                        }
                        break;
-               case ao_flight_landed:
+#if HAS_FLIGHT_DEBUG
+               case ao_flight_test:
+#if HAS_GYRO
+                       printf ("angle %4d pitch %7d yaw %7d roll %7d\n",
+                               ao_sample_orient,
+                               ((ao_sample_pitch << 9) - ao_ground_pitch) >> 9,
+                               ((ao_sample_yaw << 9) - ao_ground_yaw) >> 9,
+                               ((ao_sample_roll << 9) - ao_ground_roll) >> 9);
+#endif
+                       flush();
+                       break;
+#endif /* HAS_FLIGHT_DEBUG */
+               default:
                        break;
                }
        }
 }
 
-#if !HAS_RADIO
+#if HAS_FLIGHT_DEBUG
 static inline int int_part(int16_t i)  { return i >> 4; }
 static inline int frac_part(int16_t i) { return ((i & 0xf) * 100 + 8) / 16; }
 
@@ -376,6 +410,7 @@ ao_flight_dump(void)
        printf ("  raw accel   %d\n", ao_sample_accel);
 #endif
        printf ("  ground pres %d\n", ao_ground_pres);
+       printf ("  ground alt  %d\n", ao_ground_height);
 #if HAS_ACCEL
        printf ("  raw accel   %d\n", ao_sample_accel);
        printf ("  groundaccel %d\n", ao_ground_accel);
@@ -399,8 +434,17 @@ ao_flight_dump(void)
        printf ("  error_avg   %d\n", ao_error_h_sq_avg);
 }
 
+static void
+ao_gyro_test(void)
+{
+       ao_flight_state = ao_flight_test;
+       ao_getchar();
+       ao_flight_state = ao_flight_idle;
+}
+
 __code struct ao_cmds ao_flight_cmds[] = {
        { ao_flight_dump,       "F\0Dump flight status" },
+       { ao_gyro_test,         "G\0Test gyro code" },
        { 0, NULL },
 };
 #endif
@@ -411,7 +455,7 @@ void
 ao_flight_init(void)
 {
        ao_flight_state = ao_flight_startup;
-#if !HAS_RADIO
+#if HAS_FLIGHT_DEBUG
        ao_cmd_register(&ao_flight_cmds[0]);
 #endif
        ao_add_task(&flight_task, ao_flight, "flight");