-static void
-ao_tracker_state_update(struct ao_tracker_data *tracker)
-{
- uint16_t new_telem_rate;
- uint8_t new_gps_rate;
- uint8_t new_telem_enabled;
- uint32_t ground_distance;
- int16_t height;
- uint16_t speed;
-
- new_gps_rate = gps_rate;
- new_telem_rate = telem_rate;
-
- new_telem_enabled = ao_tracker_force_telem || !ao_usb_connected();
-
- /* Don't change anything if GPS isn't locked */
- if ((tracker->new & AO_GPS_NEW_DATA) &&
- (tracker->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 += tracker->gps_data.latitude;
- lon_sum += tracker->gps_data.longitude;
- alt_sum += tracker->gps_data.altitude;
-
- if (++nsamples >= STARTUP_AVERAGE) {
- ao_flight_state = ao_flight_pad;
- ao_wakeup(&ao_flight_state);
- ao_tracker_start_latitude = lat_sum / nsamples;
- ao_tracker_start_longitude = lon_sum / nsamples;
- ao_tracker_start_altitude = alt_sum / nsamples;
- }
- break;
- case ao_flight_pad:
- ground_distance = ao_distance(tracker->gps_data.latitude,
- tracker->gps_data.longitude,
- ao_tracker_start_latitude,
- ao_tracker_start_longitude);
- height = tracker->gps_data.altitude - ao_tracker_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_tracker_log_pos = ao_tracker_ring_next(ao_tracker_head);
- ao_log_start();
- ao_log_gps_flight();
- }
- break;
- case ao_flight_drogue:
- /* Modulate data rates based on speed (in cm/s) */
- if (tracker->gps_data.climb_rate < 0)
- speed = -tracker->gps_data.climb_rate;
- else
- speed = tracker->gps_data.climb_rate;
- speed += tracker->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;
- }
- break;
- default:
- break;
- }
- }
-
- 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;
- }
-
- if (new_gps_rate != gps_rate) {
- ao_gps_set_rate(new_gps_rate);
- gps_rate = new_gps_rate;
- }
-}