Display last known GPS coord while unlocked
[fw/altos] / aoview / aoview_state.c
index d5e978b68833a856651fd0a999f9c7c9506d4997..9852cabd257633687bbe691ce2db0a64fcca86ab 100644 (file)
@@ -62,7 +62,7 @@ aoview_great_circle (double start_lat, double start_lon,
 }
 
 static void
-aoview_state_add_deg(char *label, double deg, char pos, char neg)
+aoview_state_add_deg(int column, char *label, double deg, char pos, char neg)
 {
        double  int_part;
        double  min;
@@ -74,7 +74,7 @@ aoview_state_add_deg(char *label, double deg, char pos, char neg)
        }
        int_part = floor (deg);
        min = (deg - int_part) * 60.0;
-       aoview_table_add_row(label, "%d°%lf'%c",
+       aoview_table_add_row(column, label, "%d°%lf'%c",
                             (int) int_part, min, sign);
 
 }
@@ -110,6 +110,7 @@ aoview_state_derive(struct aodata *data, struct aostate *state)
        state->report_time = aoview_time();
 
        state->prev_data = state->data;
+       state->prev_npad = state->npad;
        state->data = *data;
        tick_count = data->tick;
        if (tick_count < state->prev_data.tick)
@@ -129,14 +130,20 @@ aoview_state_derive(struct aodata *data, struct aostate *state)
        state->main_sense = data->main / 32767.0 * 15.0;
        state->battery = data->batt / 32767.0 * 5.0;
        if (!strcmp(data->state, "pad")) {
-               if (data->locked && data->nsat > 4) {
+               if (data->gps.gps_locked && data->gps.nsat >= 4) {
                        state->npad++;
-                       state->pad_lat_total += data->lat;
-                       state->pad_lon_total += data->lon;
-                       state->pad_alt_total += data->alt;
-                       state->pad_lat = state->pad_lat_total / state->npad;
-                       state->pad_lon = state->pad_lon_total / state->npad;
-                       state->pad_alt = state->pad_alt_total / state->npad;
+                       state->pad_lat_total += data->gps.lat;
+                       state->pad_lon_total += data->gps.lon;
+                       state->pad_alt_total += data->gps.alt;
+                       if (state->npad > 1) {
+                               state->pad_lat = (state->pad_lat * 31 + data->gps.lat) / 32.0;
+                               state->pad_lon = (state->pad_lon * 31 + data->gps.lon) / 32.0;
+                               state->pad_alt = (state->pad_alt * 31 + data->gps.alt) / 32.0;
+                       } else {
+                               state->pad_lat = data->gps.lat;
+                               state->pad_lon = data->gps.lon;
+                               state->pad_alt = data->gps.alt;
+                       }
                }
        }
        state->ascent = FALSE;
@@ -152,15 +159,17 @@ aoview_state_derive(struct aodata *data, struct aostate *state)
 
        if (state->height > state->max_height)
                state->max_height = state->height;
-       if (data->locked) {
-               state->lat = data->lat;
-               state->lon = data->lon;
-               aoview_great_circle(state->pad_lat, state->pad_lon, data->lat, data->lon,
-                                   &state->distance, &state->bearing);
+       state->gps.gps_locked = data->gps.gps_locked;
+       state->gps.gps_connected = data->gps.gps_connected;
+       if (data->gps.gps_locked) {
+               state->gps = data->gps;
                state->gps_valid = 1;
+               if (state->npad)
+                       aoview_great_circle(state->pad_lat, state->pad_lon, state->gps.lat, state->gps.lon,
+                                           &state->distance, &state->bearing);
        }
        if (state->npad) {
-               state->gps_height = data->alt - state->pad_alt;
+               state->gps_height = state->gps.alt - state->pad_alt;
        } else {
                state->gps_height = 0;
        }
@@ -178,6 +187,8 @@ aoview_speak_state(struct aostate *state)
                        aoview_voice_speak("max speed %d meters per second\n",
                                           (int) state->max_speed);
        }
+       if (state->prev_npad < MIN_PAD_SAMPLES && state->npad >= MIN_PAD_SAMPLES)
+               aoview_voice_speak("g p s ready\n");
 }
 
 void
@@ -240,7 +251,7 @@ aoview_state_timeout(gpointer data)
                                aoview_voice_speak("rocket landed safely\n");
                        else
                                aoview_voice_speak("rocket may have crashed\n");
-                       if (aostate.gps_valid) {
+                       if (aostate.gps.gps_connected) {
                                aoview_voice_speak("rocket reported %s of pad distance %d meters\n",
                                                   aoview_compass_point(aostate.bearing),
                                                   (int) aostate.distance);
@@ -266,51 +277,58 @@ aoview_state_notify(struct aodata *data)
        aoview_table_start();
 
        if (state->npad >= MIN_PAD_SAMPLES)
-               aoview_table_add_row("Ground state", "ready");
+               aoview_table_add_row(0, "Ground state", "ready");
        else
-               aoview_table_add_row("Ground state", "waiting for gps (%d)",
+               aoview_table_add_row(0, "Ground state", "waiting for gps (%d)",
                                     MIN_PAD_SAMPLES - state->npad);
-       aoview_table_add_row("Rocket state", "%s", state->data.state);
-       aoview_table_add_row("Callsign", "%s", state->data.callsign);
-       aoview_table_add_row("Rocket serial", "%d", state->data.serial);
-
-       aoview_table_add_row("RSSI", "%6ddBm", state->data.rssi);
-       aoview_table_add_row("Height", "%6dm", state->height);
-       aoview_table_add_row("Max height", "%6dm", state->max_height);
-       aoview_table_add_row("Acceleration", "%7.1fm/s²", state->acceleration);
-       aoview_table_add_row("Max acceleration", "%7.1fm/s²", state->max_acceleration);
-       aoview_table_add_row("Speed", "%7.1fm/s", state->ascent ? state->speed : state->baro_speed);
-       aoview_table_add_row("Max Speed", "%7.1fm/s", state->max_speed);
-       aoview_table_add_row("Temperature", "%6.2f°C", state->temperature);
-       aoview_table_add_row("Battery", "%5.2fV", state->battery);
-       aoview_table_add_row("Drogue", "%5.2fV", state->drogue_sense);
-       aoview_table_add_row("Main", "%5.2fV", state->main_sense);
-       aoview_table_add_row("Pad altitude", "%dm", state->ground_altitude);
-       aoview_table_add_row("Satellites", "%d", state->data.nsat);
-       if (state->data.locked) {
-               aoview_state_add_deg("Latitude", state->data.lat, 'N', 'S');
-               aoview_state_add_deg("Longitude", state->data.lon, 'E', 'W');
-               aoview_table_add_row("GPS height", "%d", state->gps_height);
-               aoview_table_add_row("GPS time", "%02d:%02d:%02d",
-                                    state->data.gps_time.hour,
-                                    state->data.gps_time.minute,
-                                    state->data.gps_time.second);
-               aoview_table_add_row("GPS ground speed", "%7.1fm/s %d°",
-                                    state->data.ground_speed,
-                                    state->data.course);
-               aoview_table_add_row("GPS climb rate", "%7.1fm/s",
-                                    state->data.climb_rate);
-               aoview_table_add_row("GPS precision", "%f(hdop) %dm(h) %dm(v)\n",
-                                    state->data.hdop, state->data.h_error, state->data.v_error);
-               aoview_table_add_row("Distance from pad", "%5.0fm", state->distance);
-               aoview_table_add_row("Direction from pad", "%4.0f°", state->bearing);
+       aoview_table_add_row(0, "Rocket state", "%s", state->data.state);
+       aoview_table_add_row(0, "Callsign", "%s", state->data.callsign);
+       aoview_table_add_row(0, "Rocket serial", "%d", state->data.serial);
+
+       aoview_table_add_row(0, "RSSI", "%6ddBm", state->data.rssi);
+       aoview_table_add_row(0, "Height", "%6dm", state->height);
+       aoview_table_add_row(0, "Max height", "%6dm", state->max_height);
+       aoview_table_add_row(0, "Acceleration", "%7.1fm/s²", state->acceleration);
+       aoview_table_add_row(0, "Max acceleration", "%7.1fm/s²", state->max_acceleration);
+       aoview_table_add_row(0, "Speed", "%7.1fm/s", state->ascent ? state->speed : state->baro_speed);
+       aoview_table_add_row(0, "Max Speed", "%7.1fm/s", state->max_speed);
+       aoview_table_add_row(0, "Temperature", "%6.2f°C", state->temperature);
+       aoview_table_add_row(0, "Battery", "%5.2fV", state->battery);
+       aoview_table_add_row(0, "Drogue", "%5.2fV", state->drogue_sense);
+       aoview_table_add_row(0, "Main", "%5.2fV", state->main_sense);
+       aoview_table_add_row(0, "Pad altitude", "%dm", state->ground_altitude);
+       aoview_table_add_row(1, "Satellites", "%d", state->gps.nsat);
+       if (state->gps.gps_locked) {
+               aoview_table_add_row(1, "GPS", "locked");
+       } else if (state->gps.gps_connected) {
+               aoview_table_add_row(1, "GPS", "unlocked");
        } else {
-               aoview_table_add_row("GPS", "unlocked");
+               aoview_table_add_row(1, "GPS", "not available");
+       }
+       if (state->gps_valid) {
+               aoview_state_add_deg(1, "Latitude", state->gps.lat, 'N', 'S');
+               aoview_state_add_deg(1, "Longitude", state->gps.lon, 'E', 'W');
+               aoview_table_add_row(1, "GPS height", "%d", state->gps_height);
+               aoview_table_add_row(1, "GPS time", "%02d:%02d:%02d",
+                                    state->gps.gps_time.hour,
+                                    state->gps.gps_time.minute,
+                                    state->gps.gps_time.second);
+       }
+       if (state->gps.gps_extended) {
+               aoview_table_add_row(1, "GPS ground speed", "%7.1fm/s %d°",
+                                    state->gps.ground_speed,
+                                    state->gps.course);
+               aoview_table_add_row(1, "GPS climb rate", "%7.1fm/s",
+                                    state->gps.climb_rate);
+               aoview_table_add_row(1, "GPS precision", "%4.1f(hdop) %3dm(h) %3dm(v)",
+                                    state->gps.hdop, state->gps.h_error, state->gps.v_error);
        }
        if (state->npad) {
-               aoview_state_add_deg("Pad latitude", state->pad_lat, 'N', 'S');
-               aoview_state_add_deg("Pad longitude", state->pad_lon, 'E', 'W');
-               aoview_table_add_row("Pad GPS alt", "%gm", state->pad_alt);
+               aoview_table_add_row(1, "Distance from pad", "%5.0fm", state->distance);
+               aoview_table_add_row(1, "Direction from pad", "%4.0f°", state->bearing);
+               aoview_state_add_deg(1, "Pad latitude", state->pad_lat, 'N', 'S');
+               aoview_state_add_deg(1, "Pad longitude", state->pad_lon, 'E', 'W');
+               aoview_table_add_row(1, "Pad GPS alt", "%gm", state->pad_alt);
        }
        aoview_table_finish();
        aoview_label_show(state);