altos: Fix tracker start state
[fw/altos] / src / product / ao_tracker.c
index 5c2bb73ffa29475bd2a2f71dccb2734d23e5ed43..d4960d258081a0b0e8223baab5df493c2cfe83af 100644 (file)
@@ -37,6 +37,8 @@ ao_usb_connected(void)
 #define ao_usb_connected()     1
 #endif
 
+#define STARTUP_AVERAGE        5
+
 static void
 ao_tracker(void)
 {
@@ -48,8 +50,19 @@ ao_tracker(void)
        uint32_t        ground_distance;
        int16_t         height;
        uint16_t        speed;
+       int64_t         lat_sum = 0, lon_sum = 0;
+       int32_t         alt_sum = 0;
+       int             nsamples = 0;
 
+#if HAS_ADC
        ao_timer_set_adc_interval(100);
+#endif
+
+       ao_log_scan();
+
+       ao_rdf_set(1);
+
+       ao_telemetry_set_interval(0);
 
        ao_flight_state = ao_flight_startup;
        for (;;) {
@@ -69,10 +82,18 @@ ao_tracker(void)
                        switch (ao_flight_state) {
                        case ao_flight_startup:
                                /* startup to pad when GPS locks */
-                               ao_flight_state = ao_flight_pad;
-                               start_latitude = ao_gps_data.longitude;
-                               start_longitude = ao_gps_data.latitude;
-                               start_altitude = ao_gps_data.altitude;
+
+                               lat_sum += ao_gps_data.latitude;
+                               lon_sum += ao_gps_data.longitude;
+                               alt_sum += ao_gps_data.altitude;
+
+                               if (++nsamples >= STARTUP_AVERAGE) {
+                                       ao_flight_state = ao_flight_pad;
+                                       ao_wakeup(&ao_flight_state);
+                                       start_latitude = lat_sum / nsamples;
+                                       start_longitude = lon_sum / nsamples;
+                                       start_altitude = alt_sum / nsamples;
+                               }
                                break;
                        case ao_flight_pad:
                                ground_distance = ao_distance(ao_gps_data.latitude,