2 * Copyright © 2014 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include <ao_log_gps.h>
22 #include <ao_flight.h>
23 #include <ao_distance.h>
24 #include <ao_tracker.h>
26 static __xdata uint8_t ao_log_mutex;
27 static __xdata struct ao_log_gps log;
29 __code uint8_t ao_log_format = AO_LOG_FORMAT_TELEGPS;
32 ao_log_csum(__xdata uint8_t *b) __reentrant
37 for (i = 0; i < sizeof (struct ao_log_gps); i++)
43 ao_log_gps(__xdata struct ao_log_gps *log) __reentrant
48 log->csum = ao_log_csum((__xdata uint8_t *) log);
49 ao_mutex_get(&ao_log_mutex); {
50 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
54 ao_storage_write(ao_log_current_pos,
56 sizeof (struct ao_log_gps));
57 ao_log_current_pos += sizeof (struct ao_log_gps);
59 } ao_mutex_put(&ao_log_mutex);
64 static int32_t prev_lat, prev_lon;
65 static int16_t prev_alt;
66 static uint8_t has_prev, unmoving;
68 #define GPS_SPARSE_UNMOVING_REPORTS 10
69 #define GPS_SPARSE_UNMOVING_GROUND 10
70 #define GPS_SPARSE_UNMOVING_AIR 10
73 ao_log_gps_should_log(int32_t lat, int32_t lon, int16_t alt)
75 if (has_prev && ao_log_running) {
76 uint32_t h = ao_distance(prev_lat, prev_lon, lat, lon);
77 uint16_t v = alt > prev_alt ? (alt - prev_alt) : (prev_alt - alt);
79 if (h < GPS_SPARSE_UNMOVING_GROUND && v < GPS_SPARSE_UNMOVING_AIR) {
80 if (unmoving < GPS_SPARSE_UNMOVING_REPORTS)
91 return unmoving >= GPS_SPARSE_UNMOVING_REPORTS;
95 ao_log_gps_flight(void)
97 log.type = AO_LOG_FLIGHT;
99 log.u.flight.flight = ao_flight_number;
100 log.u.flight.start_altitude = ao_tracker_start_altitude;
101 log.u.flight.start_latitude = ao_tracker_start_latitude;
102 log.u.flight.start_longitude = ao_tracker_start_longitude;
107 ao_log_gps_data(uint16_t tick, uint8_t state, struct ao_telemetry_location *gps_data)
110 log.type = AO_LOG_GPS_TIME;
111 log.u.gps.latitude = gps_data->latitude;
112 log.u.gps.longitude = gps_data->longitude;
113 log.u.gps.altitude = gps_data->altitude;
115 log.u.gps.hour = gps_data->hour;
116 log.u.gps.minute = gps_data->minute;
117 log.u.gps.second = gps_data->second;
118 log.u.gps.flags = gps_data->flags;
119 log.u.gps.year = gps_data->year;
120 log.u.gps.month = gps_data->month;
121 log.u.gps.day = gps_data->day;
122 log.u.gps.course = gps_data->course;
123 log.u.gps.ground_speed = gps_data->ground_speed;
124 log.u.gps.climb_rate = gps_data->climb_rate;
125 log.u.gps.pdop = gps_data->pdop;
126 log.u.gps.hdop = gps_data->hdop;
127 log.u.gps.vdop = gps_data->vdop;
128 log.u.gps.mode = gps_data->mode;
129 log.u.gps.state = state;
134 ao_log_gps_tracking(uint16_t tick, struct ao_telemetry_satellite *gps_tracking_data)
139 log.type = AO_LOG_GPS_SAT;
141 n = gps_tracking_data->channels;
142 for (c = 0; c < n; c++)
143 if ((log.u.gps_sat.sats[i].svid = gps_tracking_data->sats[c].svid))
145 log.u.gps_sat.sats[i].c_n = gps_tracking_data->sats[c].c_n_1;
150 log.u.gps_sat.channels = i;
155 ao_log_dump_check_data(void)
157 if (ao_log_csum((uint8_t *) &log) != 0)
163 ao_log_flight(uint8_t slot)
165 if (!ao_storage_read(ao_log_pos(slot),
167 sizeof (struct ao_log_gps)))
170 if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT)
171 return log.u.flight.flight;