summaryrefslogtreecommitdiff
path: root/ao-tools/lib/cc-logfile.c
blob: 88ddda58822ee487da4a7b07a6620bc3b914bb9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * Copyright © 2009 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include "cc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int
timedata_add(struct cc_timedata *data, double time, double value)
{
	struct cc_timedataelt	*newdata;
	int			newsize;
	if (data->size == data->num) {
		if (data->size == 0)
			newdata = malloc((newsize = 256) * sizeof (struct cc_timedataelt));
		else
			newdata = realloc (data->data, (newsize = data->size * 2)
					   * sizeof (struct cc_timedataelt));
		if (!newdata)
			return 0;
		data->size = newsize;
		data->data = newdata;
	}
	time += data->time_offset;
	if (data->num && data->data[data->num-1].time > time) {
		data->time_offset += 65536;
		time += 65536;
	}
	data->data[data->num].time = time;
	data->data[data->num].value = value;
	data->num++;
	return 1;
}

static void
timedata_free(struct cc_timedata *data)
{
	if (data->data)
		free(data->data);
}

static int
gpsdata_add(struct cc_gpsdata *data, struct cc_gpselt *elt)
{
	struct cc_gpselt	*newdata;
	int			newsize;
	if (data->size == data->num) {
		if (data->size == 0)
			newdata = malloc((newsize = 256) * sizeof (struct cc_gpselt));
		else
			newdata = realloc (data->data, (newsize = data->size * 2)
					   * sizeof (struct cc_gpselt));
		if (!newdata)
			return 0;
		data->size = newsize;
		data->data = newdata;
	}
	elt->time += data->time_offset;
	if (data->num && data->data[data->num-1].time > elt->time) {
		data->time_offset += 65536;
		elt->time += 65536;
	}
	data->data[data->num] = *elt;
	data->num++;
	return 1;
}

static int
gpssat_add(struct cc_gpsdata *data, struct cc_gpssat *sat)
{
	int	i, j;
	int	reuse = 0;
	int	newsizesats;
	struct cc_gpssats	*newsats;

	for (i = data->numsats; --i >= 0;) {
		if (data->sats[i].sat[0].time == sat->time) {
			reuse = 1;
			break;
		}
		if (data->sats[i].sat[0].time < sat->time)
			break;
	}
	if (!reuse) {
		if (data->numsats == data->sizesats) {
			if (data->sizesats == 0)
				newsats = malloc((newsizesats = 256) * sizeof (struct cc_gpssats));
			else
				newsats = realloc (data->sats, (newsizesats = data->sizesats * 2)
						   * sizeof (struct cc_gpssats));
			if (!newsats)
				return 0;
			data->sats = newsats;
			data->sizesats = newsizesats;
		}
		i = data->numsats++;
		data->sats[i].nsat = 0;
	}
	j = data->sats[i].nsat;
	if (j < 12) {
		data->sats[i].sat[j] = *sat;
		data->sats[i].nsat = j + 1;
	}
	return 1;
}

static void
gpsdata_free(struct cc_gpsdata *data)
{
	if (data->data)
		free(data->data);
}

#define AO_LOG_FLIGHT		'F'
#define AO_LOG_SENSOR		'A'
#define AO_LOG_TEMP_VOLT	'T'
#define AO_LOG_DEPLOY		'D'
#define AO_LOG_STATE		'S'
#define AO_LOG_GPS_TIME		'G'
#define AO_LOG_GPS_LAT		'N'
#define AO_LOG_GPS_LON		'W'
#define AO_LOG_GPS_ALT		'H'
#define AO_LOG_GPS_SAT		'V'
#define AO_LOG_GPS_DATE		'Y'

#define AO_LOG_POS_NONE		(~0UL)

#define GPS_TIME	1
#define GPS_LAT		2
#define GPS_LON		4
#define GPS_ALT		8

static int
read_eeprom(const char *line, struct cc_flightraw *f, double *ground_pres, int *ground_pres_count)
{
	char	type;
	int	tick;
	int	a, b;
	static struct cc_gpselt	gps;
	static int		gps_valid;
	struct cc_gpssat	sat;
	int	serial;

	if (sscanf(line, "serial-number %u", &serial) == 1) {
		f->serial = serial;
		return 1;
	}
	if (sscanf(line, "%c %x %x %x", &type, &tick, &a, &b) != 4)
		return 0;
	switch (type) {
	case AO_LOG_FLIGHT:
		f->ground_accel = a;
		f->ground_pres = 0;
		f->flight = b;
		*ground_pres = 0;
		*ground_pres_count = 0;
		break;
	case AO_LOG_SENSOR:
		timedata_add(&f->accel, tick, a);
		timedata_add(&f->pres, tick, b);
		if (*ground_pres_count < 20) {
			*ground_pres += b;
			(*ground_pres_count)++;
			if (*ground_pres_count >= 20)
				f->ground_pres = *ground_pres / *ground_pres_count;
		}
		break;
	case AO_LOG_TEMP_VOLT:
		timedata_add(&f->temp, tick, a);
		timedata_add(&f->volt, tick, b);
		break;
	case AO_LOG_DEPLOY:
		timedata_add(&f->drogue, tick, a);
		timedata_add(&f->main, tick, b);
		break;
	case AO_LOG_STATE:
		timedata_add(&f->state, tick, a);
		break;
	case AO_LOG_GPS_TIME:
		/* the flight computer writes TIME first, so reset
		 * any stale data before adding this record
		 */
		gps.time = tick;
		gps.hour = (a & 0xff);
		gps.minute = (a >> 8) & 0xff;
		gps.second = (b & 0xff);
		gps.flags = (b >> 8) & 0xff;
		gps_valid = GPS_TIME;
		break;
	case AO_LOG_GPS_LAT:
		gps.lat = ((int32_t) (a + (b << 16))) / 10000000.0;
		gps_valid |= GPS_LAT;
		break;
	case AO_LOG_GPS_LON:
		gps.lon = ((int32_t) (a + (b << 16))) / 10000000.0;
		gps_valid |= GPS_LON;
		break;
	case AO_LOG_GPS_ALT:
		gps.alt = (int16_t) a;
		gps_valid |= GPS_ALT;
		break;
	case AO_LOG_GPS_SAT:
		sat.time = tick;
		sat.svid = a;
		sat.c_n = (b >> 8) & 0xff;
		gpssat_add(&f->gps, &sat);
		break;
	case AO_LOG_GPS_DATE:
		f->year = 2000 + (a & 0xff);
		f->month = (a >> 8) & 0xff;
		f->day = (b & 0xff);
		break;
	default:
		return 0;
	}
	if (gps_valid == 0xf) {
		gps_valid = 0;
		gpsdata_add(&f->gps, &gps);
	}
	return 1;
}

static const char *state_names[] = {
	"startup",
	"idle",
	"pad",
	"boost",
	"fast",
	"coast",
	"drogue",
	"main",
	"landed",
	"invalid"
};

static enum ao_flight_state
state_name_to_state(char *state_name)
{
	enum ao_flight_state	state;
	for (state = ao_flight_startup; state < ao_flight_invalid; state++)
		if (!strcmp(state_names[state], state_name))
			return state;
	return ao_flight_invalid;
}

static int
read_telem(const char *line, struct cc_flightraw *f)
{
	struct cc_telem		telem;
	struct cc_gpselt	gps;
	struct cc_gpssat	sat;
	int			s;

	if (!cc_telem_parse(line, &telem))
		return 0;
	f->ground_accel = telem.ground_accel;
	f->ground_pres = telem.ground_pres;
	f->flight = 0;
	timedata_add(&f->accel, telem.tick, telem.flight_accel);
	timedata_add(&f->pres, telem.tick, telem.flight_pres);
	timedata_add(&f->temp, telem.tick, telem.temp);
	timedata_add(&f->volt, telem.tick, telem.batt);
	timedata_add(&f->drogue, telem.tick, telem.drogue);
	timedata_add(&f->main, telem.tick, telem.main);
	timedata_add(&f->state, telem.tick, state_name_to_state(telem.state));
	if (telem.gps.gps_locked) {
		f->year = telem.gps.gps_time.year;
		f->month = telem.gps.gps_time.month;
		f->day = telem.gps.gps_time.day;
		gps.time = telem.tick;
		gps.lat = telem.gps.lat;
		gps.lon = telem.gps.lon;
		gps.alt = telem.gps.alt;
		gps.hour = telem.gps.gps_time.hour;
		gps.minute = telem.gps.gps_time.minute;
		gps.second = telem.gps.gps_time.second;
		gpsdata_add(&f->gps, &gps);
	}
	for (s = 0; s < telem.gps_tracking.channels; s++) {
		sat.time = telem.tick;
		sat.svid = telem.gps_tracking.sats[s].svid;
		sat.c_n = telem.gps_tracking.sats[s].c_n0;
		gpssat_add(&f->gps, &sat);
	}
	return 1;
}

struct cc_flightraw *
cc_log_read(FILE *file)
{
	struct cc_flightraw	*f;
	char			line[8192];
	double			ground_pres;
	int			ground_pres_count;

	f = calloc(1, sizeof (struct cc_flightraw));
	if (!f)
		return NULL;
	while (fgets(line, sizeof (line), file)) {
		if (read_eeprom(line, f, &ground_pres, &ground_pres_count))
			continue;
		if (read_telem(line, f))
			continue;
		fprintf (stderr, "invalid line: %s", line);
	}
	return f;
}

void
cc_flightraw_free(struct cc_flightraw *raw)
{
	timedata_free(&raw->accel);
	timedata_free(&raw->pres);
	timedata_free(&raw->temp);
	timedata_free(&raw->volt);
	timedata_free(&raw->main);
	timedata_free(&raw->drogue);
	timedata_free(&raw->state);
	gpsdata_free(&raw->gps);
	free(raw);
}