Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / src / kernel / ao_tracker.c
index b207c56204d866fae30eb33e53b5c263223f46ab..9b007af82109defdc752e56cfcd375a374c5b1e6 100644 (file)
 
 #include <ao.h>
 #include <ao_flight.h>
+#include <ao_log.h>
+#include <ao_log_gps.h>
 #include <ao_distance.h>
+#include <ao_tracker.h>
 #include <ao_exti.h>
 
-enum ao_flight_state   ao_flight_state;
-
-/* Speeds for the various modes, 2m/s seems reasonable for 'not moving' */
-#define AO_TRACKER_NOT_MOVING  200
-
-static uint8_t ao_tracker_force_telem;
-static uint8_t ao_tracker_force_launch;
+static uint8_t         ao_tracker_force_telem;
 
 #if HAS_USB_CONNECT
 static inline uint8_t
@@ -38,47 +35,35 @@ ao_usb_connected(void)
 #define ao_usb_connected()     1
 #endif
 
-#define STARTUP_AVERAGE        5
+struct gps_position {
+       int32_t         latitude;
+       int32_t         longitude;
+       gps_alt_t       altitude;
+};
 
-static void
-ao_tracker_start_flight(void)
-{
-       struct ao_log_mega log;
-       ao_log_start();
-       log.type = AO_LOG_FLIGHT;
-       log.tick = ao_time();
-#if HAS_ACCEL
-       log.u.flight.ground_accel = ao_ground_accel;
-#endif
-#if HAS_GYRO
-       log.u.flight.ground_accel_along = ao_ground_accel_along;
-       log.u.flight.ground_accel_across = ao_ground_accel_across;
-       log.u.flight.ground_accel_through = ao_ground_accel_through;
-       log.u.flight.ground_roll = ao_ground_roll;
-       log.u.flight.ground_pitch = ao_ground_pitch;
-       log.u.flight.ground_yaw = ao_ground_yaw;
-#endif
-#if HAS_FLIGHT
-       log.u.flight.ground_pres = ao_ground_pres;
-#endif
-       log.u.flight.flight = ao_flight_number;
-       ao_log_mega(&log);
-}
+#define GPS_RING       16
+
+struct gps_position    gps_position[GPS_RING];
+
+#define ao_gps_ring_next(n)    (((n) + 1) & (GPS_RING - 1))
+#define ao_gps_ring_prev(n)    (((n) - 1) & (GPS_RING - 1))
+
+static uint8_t gps_head;
+
+static uint8_t tracker_mutex;
+static uint8_t log_started;
+static struct ao_telemetry_location gps_data;
+static uint8_t tracker_running;
+static uint16_t tracker_interval;
 
 static void
 ao_tracker(void)
 {
-       uint16_t        telem_rate = AO_SEC_TO_TICKS(1), new_telem_rate;
-       uint8_t         gps_rate = 1, new_gps_rate;
-       uint8_t         telem_enabled = 0, new_telem_enabled;
-       int32_t         start_latitude = 0, start_longitude = 0;
-       int16_t         start_altitude = 0;
-       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;
+       uint8_t new;
+       int32_t ground_distance;
+       int16_t height;
+       uint16_t gps_tick;
+       uint8_t new_tracker_running;
 
 #if HAS_ADC
        ao_timer_set_adc_interval(100);
@@ -87,98 +72,117 @@ ao_tracker(void)
 #if !HAS_USB_CONNECT
        ao_tracker_force_telem = 1;
 #endif
-
-       ao_log_scan();
+       log_started = ao_log_scan();
 
        ao_rdf_set(1);
 
-       ao_telemetry_set_interval(0);
+       tracker_interval = ao_config.tracker_interval;
+       ao_gps_set_rate(tracker_interval);
 
-       ao_flight_state = ao_flight_startup;
        for (;;) {
-               ao_sleep(&ao_gps_new);
 
-               new_gps_rate = gps_rate;
-               new_telem_rate = telem_rate;
+               /** Wait for new GPS data
+                */
+               while (!(new = ao_gps_new))
+                       ao_sleep(&ao_gps_new);
+               ao_mutex_get(&ao_gps_mutex);
+               gps_data = ao_gps_data;
+               gps_tick = ao_gps_tick;
+               ao_gps_new = 0;
+               ao_mutex_put(&ao_gps_mutex);
+
+               new_tracker_running = ao_tracker_force_telem || !ao_usb_connected();
 
-               new_telem_enabled = ao_tracker_force_telem || !ao_usb_connected();
+               if (ao_config.tracker_interval != tracker_interval) {
+                       tracker_interval = ao_config.tracker_interval;
+                       ao_gps_set_rate(tracker_interval);
 
-               ao_mutex_get(&ao_gps_mutex);
+                       /* force telemetry interval to be reset */
+                       tracker_running = 0;
+               }
 
-               /* Don't change anything if GPS isn't locked */
-               if ((ao_gps_data.flags & (AO_GPS_VALID|AO_GPS_COURSE_VALID)) ==
-                   (AO_GPS_VALID|AO_GPS_COURSE_VALID))
-               {
-                       switch (ao_flight_state) {
-                       case ao_flight_startup:
-                               /* startup to pad when GPS locks */
-
-                               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;
+               if (new_tracker_running && !tracker_running)
+                       ao_telemetry_set_interval(AO_SEC_TO_TICKS(tracker_interval));
+               else if (!new_tracker_running && tracker_running)
+                       ao_telemetry_set_interval(0);
+
+               tracker_running = new_tracker_running;
+
+               if (new_tracker_running && !ao_log_running)
+                       ao_log_start();
+               else if (!new_tracker_running && ao_log_running)
+                       ao_log_stop();
+
+               if (!ao_log_running)
+                       continue;
+
+               if (new & AO_GPS_NEW_DATA) {
+                       if ((gps_data.flags & (AO_GPS_VALID|AO_GPS_COURSE_VALID)) ==
+                           (AO_GPS_VALID|AO_GPS_COURSE_VALID))
+                       {
+                               uint8_t ring;
+                               uint8_t moving = 0;
+                               gps_alt_t altitude = AO_TELEMETRY_LOCATION_ALTITUDE(&gps_data);
+
+                               for (ring = ao_gps_ring_next(gps_head); ring != gps_head; ring = ao_gps_ring_next(ring)) {
+                                       ground_distance = ao_distance(gps_data.latitude, gps_data.longitude,
+                                                                     gps_position[ring].latitude,
+                                                                     gps_position[ring].longitude);
+                                       height = gps_position[ring].altitude - altitude;
+                                       if (height < 0)
+                                               height = -height;
+
+                                       if (ao_tracker_force_telem)
+                                               printf("head %d ring %d ground_distance %d height %d\n", gps_head, ring, ground_distance, height);
+                                       if (ground_distance > ao_config.tracker_motion ||
+                                           height > (ao_config.tracker_motion << 1))
+                                       {
+                                               moving = 1;
+                                               break;
+                                       }
                                }
-                               break;
-                       case ao_flight_pad:
-                               ground_distance = ao_distance(ao_gps_data.latitude,
-                                                             ao_gps_data.longitude,
-                                                             start_latitude,
-                                                             start_longitude);
-                               height = ao_gps_data.altitude - start_altitude;
-                               if (height < 0)
-                                       height = -height;
-
-                               if (ground_distance >= ao_config.tracker_start_horiz ||
-                                   height >= ao_config.tracker_start_vert ||
-                                   ao_tracker_force_launch)
-                               {
-                                       ao_flight_state = ao_flight_drogue;
-                                       ao_wakeup(&ao_flight_state);
-                                       ao_log_start();
-                                       ao_tracker_start_flight();
+                               if (ao_tracker_force_telem) {
+                                       printf ("moving %d started %d\n", moving, log_started);
+                                       flush();
                                }
-                               break;
-                       case ao_flight_drogue:
-                               /* Modulate data rates based on speed (in cm/s) */
-                               if (ao_gps_data.climb_rate < 0)
-                                       speed = -ao_gps_data.climb_rate;
-                               else
-                                       speed = ao_gps_data.climb_rate;
-                               speed += ao_gps_data.ground_speed;
-
-                               if (speed < AO_TRACKER_NOT_MOVING) {
-                                       new_telem_rate = AO_SEC_TO_TICKS(10);
-                                       new_gps_rate = 10;
-                               } else {
-                                       new_telem_rate = AO_SEC_TO_TICKS(1);
-                                       new_gps_rate = 1;
+                               if (moving) {
+                                       ao_mutex_get(&tracker_mutex);
+                                       if (!log_started) {
+                                               ao_log_gps_flight();
+                                               log_started = 1;
+                                       }
+                                       ao_log_gps_data(gps_tick, &gps_data);
+                                       gps_position[gps_head].latitude = gps_data.latitude;
+                                       gps_position[gps_head].longitude = gps_data.longitude;
+                                       gps_position[gps_head].altitude = altitude;
+                                       gps_head = ao_gps_ring_next(gps_head);
+                                       ao_mutex_put(&tracker_mutex);
                                }
-                               break;
-                       default:
-                               break;
                        }
                }
-               ao_mutex_put(&ao_gps_mutex);
+       }
+}
 
-               if (new_telem_rate != telem_rate || new_telem_enabled != telem_enabled) {
-                       if (new_telem_enabled)
-                               ao_telemetry_set_interval(new_telem_rate);
-                       else
-                               ao_telemetry_set_interval(0);
-                       telem_rate = new_telem_rate;
-                       telem_enabled = new_telem_enabled;
-               }
+static uint8_t erasing_current;
 
-               if (new_gps_rate != gps_rate) {
-                       ao_gps_set_rate(new_gps_rate);
-                       gps_rate = new_gps_rate;
-               }
+void
+ao_tracker_erase_start(uint16_t flight)
+{
+       erasing_current = flight == ao_flight_number;
+       if (erasing_current) {
+               ao_mutex_get(&tracker_mutex);
+               ao_log_stop();
+               if (++ao_flight_number == 0)
+                       ao_flight_number = 1;
+       }
+}
+
+void
+ao_tracker_erase_end(void)
+{
+       if (erasing_current) {
+               log_started = ao_log_scan();
+               ao_mutex_put(&tracker_mutex);
        }
 }
 
@@ -187,13 +191,23 @@ static struct ao_task ao_tracker_task;
 static void
 ao_tracker_set_telem(void)
 {
+       uint8_t telem;
        ao_cmd_hex();
-       if (ao_cmd_status == ao_cmd_success) {
-               ao_tracker_force_telem = (ao_cmd_lex_i & 1) != 0;
-               ao_tracker_force_launch = (ao_cmd_lex_i & 2) != 0;
-       }
-       printf ("flight %d force telem %d force launch %d\n",
-               ao_flight_number, ao_tracker_force_telem, ao_tracker_force_launch);
+       telem = ao_cmd_lex_i;
+       if (ao_cmd_status == ao_cmd_success)
+               ao_tracker_force_telem = telem;
+       ao_cmd_status = ao_cmd_success;
+       printf ("flight: %d\n", ao_flight_number);
+       printf ("force_telem: %d\n", ao_tracker_force_telem);
+       printf ("tracker_running: %d\n", tracker_running);
+       printf ("log_started: %d\n", log_started);
+       printf ("latitude: %ld\n", (long) gps_data.latitude);
+       printf ("longitude: %ld\n", (long) gps_data.longitude);
+       printf ("altitude: %ld\n", AO_TELEMETRY_LOCATION_ALTITUDE(&gps_data));
+       printf ("log_running: %d\n", ao_log_running);
+       printf ("log_start_pos: %ld\n", (long) ao_log_start_pos);
+       printf ("log_cur_pos: %ld\n", (long) ao_log_current_pos);
+       printf ("log_end_pos: %ld\n", (long) ao_log_end_pos);
 }
 
 static const struct ao_cmds ao_tracker_cmds[] = {