2 * Copyright © 2009 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.
24 timedata_add(struct cc_timedata *data, double time, double value)
26 struct cc_timedataelt *newdata;
28 if (data->size == data->num) {
30 newdata = malloc((newsize = 256) * sizeof (struct cc_timedataelt));
32 newdata = realloc (data->data, (newsize = data->size * 2)
33 * sizeof (struct cc_timedataelt));
39 time += data->time_offset;
40 if (data->num && data->data[data->num-1].time > time) {
41 data->time_offset += 65536;
44 data->data[data->num].time = time;
45 data->data[data->num].value = value;
51 timedata_free(struct cc_timedata *data)
58 gpsdata_add(struct cc_gpsdata *data, struct cc_gpselt *elt)
60 struct cc_gpselt *newdata;
62 if (data->size == data->num) {
64 newdata = malloc((newsize = 256) * sizeof (struct cc_gpselt));
66 newdata = realloc (data->data, (newsize = data->size * 2)
67 * sizeof (struct cc_gpselt));
73 elt->time += data->time_offset;
74 if (data->num && data->data[data->num-1].time > elt->time) {
75 data->time_offset += 65536;
78 data->data[data->num] = *elt;
84 gpssat_add(struct cc_gpsdata *data, struct cc_gpssat *sat)
89 struct cc_gpssats *newsats;
91 for (i = data->numsats; --i >= 0;) {
92 if (data->sats[i].sat[0].time == sat->time) {
96 if (data->sats[i].sat[0].time < sat->time)
100 if (data->numsats == data->sizesats) {
101 if (data->sizesats == 0)
102 newsats = malloc((newsizesats = 256) * sizeof (struct cc_gpssats));
104 newsats = realloc (data->sats, (newsizesats = data->sizesats * 2)
105 * sizeof (struct cc_gpssats));
108 data->sats = newsats;
109 data->sizesats = newsizesats;
112 data->sats[i].nsat = 0;
114 j = data->sats[i].nsat;
116 data->sats[i].sat[j] = *sat;
117 data->sats[i].nsat = j + 1;
123 gpsdata_free(struct cc_gpsdata *data)
129 #define AO_LOG_FLIGHT 'F'
130 #define AO_LOG_SENSOR 'A'
131 #define AO_LOG_TEMP_VOLT 'T'
132 #define AO_LOG_DEPLOY 'D'
133 #define AO_LOG_STATE 'S'
134 #define AO_LOG_GPS_TIME 'G'
135 #define AO_LOG_GPS_LAT 'N'
136 #define AO_LOG_GPS_LON 'W'
137 #define AO_LOG_GPS_ALT 'H'
138 #define AO_LOG_GPS_SAT 'V'
139 #define AO_LOG_GPS_DATE 'Y'
141 #define AO_LOG_POS_NONE (~0UL)
149 read_eeprom(const char *line, struct cc_flightraw *f, double *ground_pres, int *ground_pres_count)
154 static struct cc_gpselt gps;
155 static int gps_valid;
156 struct cc_gpssat sat;
159 if (sscanf(line, "serial-number %u", &serial) == 1) {
163 if (sscanf(line, "%c %x %x %x", &type, &tick, &a, &b) != 4)
171 *ground_pres_count = 0;
174 timedata_add(&f->accel, tick, a);
175 timedata_add(&f->pres, tick, b);
176 if (*ground_pres_count < 20) {
178 (*ground_pres_count)++;
179 if (*ground_pres_count >= 20)
180 f->ground_pres = *ground_pres / *ground_pres_count;
183 case AO_LOG_TEMP_VOLT:
184 timedata_add(&f->temp, tick, a);
185 timedata_add(&f->volt, tick, b);
188 timedata_add(&f->drogue, tick, a);
189 timedata_add(&f->main, tick, b);
192 timedata_add(&f->state, tick, a);
194 case AO_LOG_GPS_TIME:
195 /* the flight computer writes TIME first, so reset
196 * any stale data before adding this record
199 gps.hour = (a & 0xff);
200 gps.minute = (a >> 8) & 0xff;
201 gps.second = (b & 0xff);
202 gps.flags = (b >> 8) & 0xff;
203 gps_valid = GPS_TIME;
206 gps.lat = ((int32_t) (a + (b << 16))) / 10000000.0;
207 gps_valid |= GPS_LAT;
210 gps.lon = ((int32_t) (a + (b << 16))) / 10000000.0;
211 gps_valid |= GPS_LON;
214 gps.alt = (int16_t) a;
215 gps_valid |= GPS_ALT;
220 sat.c_n = (b >> 8) & 0xff;
221 gpssat_add(&f->gps, &sat);
223 case AO_LOG_GPS_DATE:
224 f->year = 2000 + (a & 0xff);
225 f->month = (a >> 8) & 0xff;
231 if (gps_valid == 0xf) {
233 gpsdata_add(&f->gps, &gps);
238 static const char *state_names[] = {
251 static enum ao_flight_state
252 state_name_to_state(char *state_name)
254 enum ao_flight_state state;
255 for (state = ao_flight_startup; state < ao_flight_invalid; state++)
256 if (!strcmp(state_names[state], state_name))
258 return ao_flight_invalid;
262 read_telem(const char *line, struct cc_flightraw *f)
264 struct cc_telem telem;
265 struct cc_gpselt gps;
266 struct cc_gpssat sat;
269 if (!cc_telem_parse(line, &telem))
271 f->ground_accel = telem.ground_accel;
272 f->ground_pres = telem.ground_pres;
274 timedata_add(&f->accel, telem.tick, telem.flight_accel);
275 timedata_add(&f->pres, telem.tick, telem.flight_pres);
276 timedata_add(&f->temp, telem.tick, telem.temp);
277 timedata_add(&f->volt, telem.tick, telem.batt);
278 timedata_add(&f->drogue, telem.tick, telem.drogue);
279 timedata_add(&f->main, telem.tick, telem.main);
280 timedata_add(&f->state, telem.tick, state_name_to_state(telem.state));
281 if (telem.gps.gps_locked) {
282 f->year = telem.gps.gps_time.year;
283 f->month = telem.gps.gps_time.month;
284 f->day = telem.gps.gps_time.day;
285 gps.time = telem.tick;
286 gps.lat = telem.gps.lat;
287 gps.lon = telem.gps.lon;
288 gps.alt = telem.gps.alt;
289 gps.hour = telem.gps.gps_time.hour;
290 gps.minute = telem.gps.gps_time.minute;
291 gps.second = telem.gps.gps_time.second;
292 gpsdata_add(&f->gps, &gps);
294 for (s = 0; s < telem.gps_tracking.channels; s++) {
295 sat.time = telem.tick;
296 sat.svid = telem.gps_tracking.sats[s].svid;
297 sat.c_n = telem.gps_tracking.sats[s].c_n0;
298 gpssat_add(&f->gps, &sat);
303 struct cc_flightraw *
304 cc_log_read(FILE *file)
306 struct cc_flightraw *f;
309 int ground_pres_count;
311 f = calloc(1, sizeof (struct cc_flightraw));
314 while (fgets(line, sizeof (line), file)) {
315 if (read_eeprom(line, f, &ground_pres, &ground_pres_count))
317 if (read_telem(line, f))
319 fprintf (stderr, "invalid line: %s", line);
325 cc_flightraw_free(struct cc_flightraw *raw)
327 timedata_free(&raw->accel);
328 timedata_free(&raw->pres);
329 timedata_free(&raw->temp);
330 timedata_free(&raw->volt);
331 timedata_free(&raw->main);
332 timedata_free(&raw->drogue);
333 timedata_free(&raw->state);
334 gpsdata_free(&raw->gps);