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->data, (newsizesats = data->sizesats * 2)
105 * sizeof (struct cc_gpssats));
108 data->sats = newsats;
111 data->sats[i].nsat = 0;
113 j = data->sats[i].nsat;
115 data->sats[i].sat[j] = *sat;
116 data->sats[i].nsat = j + 1;
122 gpsdata_free(struct cc_gpsdata *data)
128 #define AO_LOG_FLIGHT 'F'
129 #define AO_LOG_SENSOR 'A'
130 #define AO_LOG_TEMP_VOLT 'T'
131 #define AO_LOG_DEPLOY 'D'
132 #define AO_LOG_STATE 'S'
133 #define AO_LOG_GPS_TIME 'G'
134 #define AO_LOG_GPS_LAT 'N'
135 #define AO_LOG_GPS_LON 'W'
136 #define AO_LOG_GPS_ALT 'H'
137 #define AO_LOG_GPS_SAT 'V'
139 #define AO_LOG_POS_NONE (~0UL)
147 read_eeprom(const char *line, struct cc_flightraw *f, double *ground_pres, int *ground_pres_count)
152 static struct cc_gpselt gps;
153 static int gps_valid;
154 struct cc_gpssat sat;
157 if (sscanf(line, "serial-number %u", &serial) == 1) {
161 if (sscanf(line, "%c %x %x %x", &type, &tick, &a, &b) != 4)
169 *ground_pres_count = 0;
172 timedata_add(&f->accel, tick, a);
173 timedata_add(&f->pres, tick, b);
174 if (*ground_pres_count < 20) {
176 (*ground_pres_count)++;
177 if (*ground_pres_count >= 20)
178 f->ground_pres = *ground_pres / *ground_pres_count;
181 case AO_LOG_TEMP_VOLT:
182 timedata_add(&f->temp, tick, a);
183 timedata_add(&f->volt, tick, b);
186 timedata_add(&f->drogue, tick, a);
187 timedata_add(&f->main, tick, b);
190 timedata_add(&f->state, tick, a);
192 case AO_LOG_GPS_TIME:
193 /* the flight computer writes TIME first, so reset
194 * any stale data before adding this record
197 gps_valid = GPS_TIME;
200 gps.lat = ((int32_t) (a + (b << 16))) / 10000000.0;
201 gps_valid |= GPS_LAT;
204 gps.lon = ((int32_t) (a + (b << 16))) / 10000000.0;
205 gps_valid |= GPS_LON;
208 gps.alt = (int16_t) a;
209 gps_valid |= GPS_ALT;
214 sat.state = (b & 0xff);
215 sat.c_n = (b >> 8) & 0xff;
216 gpssat_add(&f->gps, &sat);
221 if (gps_valid == 0xf) {
223 gpsdata_add(&f->gps, &gps);
228 static const char *state_names[] = {
241 static enum ao_flight_state
242 state_name_to_state(char *state_name)
244 enum ao_flight_state state;
245 for (state = ao_flight_startup; state < ao_flight_invalid; state++)
246 if (!strcmp(state_names[state], state_name))
248 return ao_flight_invalid;
252 read_telem(const char *line, struct cc_flightraw *f)
254 struct cc_telem telem;
255 struct cc_gpselt gps;
256 if (!cc_telem_parse(line, &telem))
258 f->ground_accel = telem.ground_accel;
259 f->ground_pres = telem.ground_pres;
261 timedata_add(&f->accel, telem.tick, telem.flight_accel);
262 timedata_add(&f->pres, telem.tick, telem.flight_pres);
263 timedata_add(&f->temp, telem.tick, telem.temp);
264 timedata_add(&f->volt, telem.tick, telem.batt);
265 timedata_add(&f->drogue, telem.tick, telem.drogue);
266 timedata_add(&f->main, telem.tick, telem.main);
267 timedata_add(&f->state, telem.tick, state_name_to_state(telem.state));
268 if (telem.gps.gps_locked) {
269 gps.time = telem.tick;
270 gps.lat = telem.gps.lat;
271 gps.lon = telem.gps.lon;
272 gps.alt = telem.gps.alt;
273 gpsdata_add(&f->gps, &gps);
278 struct cc_flightraw *
279 cc_log_read(FILE *file)
281 struct cc_flightraw *f;
284 int ground_pres_count;
286 f = calloc(1, sizeof (struct cc_flightraw));
289 while (fgets(line, sizeof (line), file)) {
290 if (read_eeprom(line, f, &ground_pres, &ground_pres_count))
292 if (read_telem(line, f))
294 fprintf (stderr, "invalid line: %s", line);
300 cc_flightraw_free(struct cc_flightraw *raw)
302 timedata_free(&raw->accel);
303 timedata_free(&raw->pres);
304 timedata_free(&raw->temp);
305 timedata_free(&raw->volt);
306 timedata_free(&raw->main);
307 timedata_free(&raw->drogue);
308 timedata_free(&raw->state);
309 gpsdata_free(&raw->gps);