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'
140 #define AO_LOG_POS_NONE (~0UL)
148 read_eeprom(const char *line, struct cc_flightraw *f, double *ground_pres, int *ground_pres_count)
153 static struct cc_gpselt gps;
154 static int gps_valid;
155 struct cc_gpssat sat;
158 if (sscanf(line, "serial-number %u", &serial) == 1) {
162 if (sscanf(line, "%c %x %x %x", &type, &tick, &a, &b) != 4)
170 *ground_pres_count = 0;
173 timedata_add(&f->accel, tick, a);
174 timedata_add(&f->pres, tick, b);
175 if (*ground_pres_count < 20) {
177 (*ground_pres_count)++;
178 if (*ground_pres_count >= 20)
179 f->ground_pres = *ground_pres / *ground_pres_count;
182 case AO_LOG_TEMP_VOLT:
183 timedata_add(&f->temp, tick, a);
184 timedata_add(&f->volt, tick, b);
187 timedata_add(&f->drogue, tick, a);
188 timedata_add(&f->main, tick, b);
191 timedata_add(&f->state, tick, a);
193 case AO_LOG_GPS_TIME:
194 /* the flight computer writes TIME first, so reset
195 * any stale data before adding this record
198 gps_valid = GPS_TIME;
201 gps.lat = ((int32_t) (a + (b << 16))) / 10000000.0;
202 gps_valid |= GPS_LAT;
205 gps.lon = ((int32_t) (a + (b << 16))) / 10000000.0;
206 gps_valid |= GPS_LON;
209 gps.alt = (int16_t) a;
210 gps_valid |= GPS_ALT;
215 sat.state = (b & 0xff);
216 sat.c_n = (b >> 8) & 0xff;
217 gpssat_add(&f->gps, &sat);
222 if (gps_valid == 0xf) {
224 gpsdata_add(&f->gps, &gps);
229 static const char *state_names[] = {
242 static enum ao_flight_state
243 state_name_to_state(char *state_name)
245 enum ao_flight_state state;
246 for (state = ao_flight_startup; state < ao_flight_invalid; state++)
247 if (!strcmp(state_names[state], state_name))
249 return ao_flight_invalid;
253 read_telem(const char *line, struct cc_flightraw *f)
255 struct cc_telem telem;
256 struct cc_gpselt gps;
257 if (!cc_telem_parse(line, &telem))
259 f->ground_accel = telem.ground_accel;
260 f->ground_pres = telem.ground_pres;
262 timedata_add(&f->accel, telem.tick, telem.flight_accel);
263 timedata_add(&f->pres, telem.tick, telem.flight_pres);
264 timedata_add(&f->temp, telem.tick, telem.temp);
265 timedata_add(&f->volt, telem.tick, telem.batt);
266 timedata_add(&f->drogue, telem.tick, telem.drogue);
267 timedata_add(&f->main, telem.tick, telem.main);
268 timedata_add(&f->state, telem.tick, state_name_to_state(telem.state));
269 if (telem.gps.gps_locked) {
270 gps.time = telem.tick;
271 gps.lat = telem.gps.lat;
272 gps.lon = telem.gps.lon;
273 gps.alt = telem.gps.alt;
274 gpsdata_add(&f->gps, &gps);
279 struct cc_flightraw *
280 cc_log_read(FILE *file)
282 struct cc_flightraw *f;
285 int ground_pres_count;
287 f = calloc(1, sizeof (struct cc_flightraw));
290 while (fgets(line, sizeof (line), file)) {
291 if (read_eeprom(line, f, &ground_pres, &ground_pres_count))
293 if (read_telem(line, f))
295 fprintf (stderr, "invalid line: %s", line);
301 cc_flightraw_free(struct cc_flightraw *raw)
303 timedata_free(&raw->accel);
304 timedata_free(&raw->pres);
305 timedata_free(&raw->temp);
306 timedata_free(&raw->volt);
307 timedata_free(&raw->main);
308 timedata_free(&raw->drogue);
309 timedata_free(&raw->state);
310 gpsdata_free(&raw->gps);