altos: Add TeleGPS logging format
[fw/altos] / src / kernel / ao_log_gps.c
1 /*
2  * Copyright © 2014 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19 #include <ao_log.h>
20 #include <ao_log_gps.h>
21 #include <ao_data.h>
22 #include <ao_flight.h>
23 #include <ao_distance.h>
24 #include <ao_tracker.h>
25
26 static __xdata uint8_t  ao_log_mutex;
27 static __xdata struct ao_log_gps log;
28
29 __code uint8_t ao_log_format = AO_LOG_FORMAT_TELEGPS;
30
31 static uint8_t
32 ao_log_csum(__xdata uint8_t *b) __reentrant
33 {
34         uint8_t sum = 0x5a;
35         uint8_t i;
36
37         for (i = 0; i < sizeof (struct ao_log_gps); i++)
38                 sum += *b++;
39         return -sum;
40 }
41
42 uint8_t
43 ao_log_gps(__xdata struct ao_log_gps *log) __reentrant
44 {
45         uint8_t wrote = 0;
46         /* set checksum */
47         log->csum = 0;
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)
51                         ao_log_stop();
52                 if (ao_log_running) {
53                         wrote = 1;
54                         ao_storage_write(ao_log_current_pos,
55                                          log,
56                                          sizeof (struct ao_log_gps));
57                         ao_log_current_pos += sizeof (struct ao_log_gps);
58                 }
59         } ao_mutex_put(&ao_log_mutex);
60         return wrote;
61 }
62
63
64 static int32_t  prev_lat, prev_lon;
65 static int16_t  prev_alt;
66 static uint8_t  has_prev, unmoving;
67
68 #define GPS_SPARSE_UNMOVING_REPORTS     10
69 #define GPS_SPARSE_UNMOVING_GROUND      10
70 #define GPS_SPARSE_UNMOVING_AIR         10
71
72 uint8_t
73 ao_log_gps_should_log(int32_t lat, int32_t lon, int16_t alt)
74 {
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);
78
79                 if (h < GPS_SPARSE_UNMOVING_GROUND && v < GPS_SPARSE_UNMOVING_AIR) {
80                         if (unmoving < GPS_SPARSE_UNMOVING_REPORTS)
81                                 ++unmoving;
82                 } else
83                         unmoving = 0;
84         } else
85                 unmoving = 0;
86
87         prev_lat = lat;
88         prev_lon = lon;
89         prev_alt = alt;
90         has_prev = 1;
91         return unmoving >= GPS_SPARSE_UNMOVING_REPORTS;
92 }
93
94 void
95 ao_log_gps_flight(void)
96 {
97         log.type = AO_LOG_FLIGHT;
98         log.tick = ao_time();
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;
103         ao_log_gps(&log);
104 }
105
106 void
107 ao_log_gps_data(uint16_t tick, uint8_t state, struct ao_telemetry_location *gps_data)
108 {
109         log.tick = tick;
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;
114
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;
130         ao_log_gps(&log);
131 }
132
133 void
134 ao_log_gps_tracking(uint16_t tick, struct ao_telemetry_satellite *gps_tracking_data)
135 {
136         uint8_t c, n, i;
137
138         log.tick = tick;
139         log.type = AO_LOG_GPS_SAT;
140         i = 0;
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))
144                 {
145                         log.u.gps_sat.sats[i].c_n = gps_tracking_data->sats[c].c_n_1;
146                         i++;
147                         if (i >= 12)
148                                 break;
149                 }
150         log.u.gps_sat.channels = i;
151         ao_log_gps(&log);
152 }
153
154 static uint8_t
155 ao_log_dump_check_data(void)
156 {
157         if (ao_log_csum((uint8_t *) &log) != 0)
158                 return 0;
159         return 1;
160 }
161
162 uint16_t
163 ao_log_flight(uint8_t slot)
164 {
165         if (!ao_storage_read(ao_log_pos(slot),
166                              &log,
167                              sizeof (struct ao_log_gps)))
168                 return 0;
169
170         if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT)
171                 return log.u.flight.flight;
172         return 0;
173 }