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.
23 timedata_add(struct cc_timedata *data, double time, double value)
25 struct cc_timedataelt *newdata;
27 if (data->size == data->num) {
29 newdata = malloc((newsize = 256) * sizeof (struct cc_timedataelt));
31 newdata = realloc (data->data, (newsize = data->size * 2)
32 * sizeof (struct cc_timedataelt));
38 if (data->num && data->data[data->num-1].time > time)
40 data->data[data->num].time = time;
41 data->data[data->num].value = value;
47 timedata_free(struct cc_timedata *data)
54 gpsdata_add(struct cc_gpsdata *data, struct cc_gpselt *elt)
56 struct cc_gpselt *newdata;
58 if (data->size == data->num) {
60 newdata = malloc((newsize = 256) * sizeof (struct cc_gpselt));
62 newdata = realloc (data->data, (newsize = data->size * 2)
63 * sizeof (struct cc_gpselt));
69 data->data[data->num] = *elt;
75 gpsdata_free(struct cc_gpsdata *data)
81 #define AO_LOG_FLIGHT 'F'
82 #define AO_LOG_SENSOR 'A'
83 #define AO_LOG_TEMP_VOLT 'T'
84 #define AO_LOG_DEPLOY 'D'
85 #define AO_LOG_STATE 'S'
86 #define AO_LOG_GPS_TIME 'G'
87 #define AO_LOG_GPS_LAT 'N'
88 #define AO_LOG_GPS_LON 'W'
89 #define AO_LOG_GPS_ALT 'H'
90 #define AO_LOG_GPS_SAT 'V'
92 #define AO_LOG_POS_NONE (~0UL)
95 read_eeprom(const char *line, struct cc_flightraw *f, double *ground_pres, int *ground_pres_count)
100 struct cc_gpselt gps;
103 if (sscanf(line, "serial-number %u", &serial) == 1) {
107 if (sscanf(line, "%c %x %x %x", &type, &tick, &a, &b) != 4)
115 *ground_pres_count = 0;
118 timedata_add(&f->accel, tick, a);
119 timedata_add(&f->pres, tick, b);
120 if (*ground_pres_count < 20) {
122 (*ground_pres_count)++;
123 if (*ground_pres_count >= 20)
124 f->ground_pres = *ground_pres / *ground_pres_count;
127 case AO_LOG_TEMP_VOLT:
128 timedata_add(&f->temp, tick, a);
129 timedata_add(&f->volt, tick, b);
132 timedata_add(&f->drogue, tick, a);
133 timedata_add(&f->main, tick, b);
136 timedata_add(&f->state, tick, a);
138 case AO_LOG_GPS_TIME:
142 gps.lat = ((int32_t) (a + (b << 16))) / 10000000.0;
145 gps.lon = ((int32_t) (a + (b << 16))) / 10000000.0;
148 gps.alt = ((int32_t) (a + (b << 16)));
149 gpsdata_add(&f->gps, &gps);
160 read_telem(const char *line, struct cc_flightraw *f)
162 struct cc_telem telem;
163 struct cc_gpselt gps;
164 if (!cc_telem_parse(line, &telem))
166 f->ground_accel = telem.ground_accel;
167 f->ground_pres = telem.ground_pres;
169 timedata_add(&f->accel, telem.tick, telem.flight_accel);
170 timedata_add(&f->pres, telem.tick, telem.flight_pres);
171 timedata_add(&f->temp, telem.tick, telem.temp);
172 timedata_add(&f->volt, telem.tick, telem.batt);
173 timedata_add(&f->drogue, telem.tick, telem.drogue);
174 timedata_add(&f->main, telem.tick, telem.main);
175 if (telem.gps.gps_locked) {
176 gps.time = telem.tick;
177 gps.lat = telem.gps.lat;
178 gps.lon = telem.gps.lon;
179 gps.alt = telem.gps.alt;
180 gpsdata_add(&f->gps, &gps);
185 struct cc_flightraw *
186 cc_log_read(FILE *file)
188 struct cc_flightraw *f;
191 int ground_pres_count;
193 f = calloc(1, sizeof (struct cc_flightraw));
196 while (fgets(line, sizeof (line), file)) {
197 if (read_eeprom(line, f, &ground_pres, &ground_pres_count))
199 if (read_telem(line, f))
201 fprintf (stderr, "invalid line: %s", line);
207 cc_flightraw_free(struct cc_flightraw *raw)
209 timedata_free(&raw->accel);
210 timedata_free(&raw->pres);
211 timedata_free(&raw->temp);
212 timedata_free(&raw->volt);
213 timedata_free(&raw->main);
214 timedata_free(&raw->drogue);
215 timedata_free(&raw->state);
216 gpsdata_free(&raw->gps);