summaryrefslogtreecommitdiff
path: root/ao-tools/ao-edit-telem/ao-edit-telem.c
blob: e2795ede180908ac11acb1b2ff93c4e24ba80660 (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
/*
 * Copyright © 2013 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.
 */

#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include "cc.h"

static const struct option options[] = {
	{ .name = "lat", .has_arg = 1, .val = 'L' },
	{ .name = "lon", .has_arg = 1, .val = 'l' },
	{ 0, 0, 0, 0},
};

static void usage(char *program)
{
	fprintf(stderr, "usage: %s [--lat <pad-lat>] [--lon <pad-lon>]\n"
		"\t{flight-log} ...\n", program);
	exit(1);
}

#define bool(b)	((b) ? "true" : "false")

struct telem_ent {
	struct telem_ent	*next;
	union ao_telemetry_all	telem;
};

static struct telem_ent *pad, **last = &pad;

static void
save_telem(union ao_telemetry_all *telem)
{
	struct telem_ent *t = malloc (sizeof *t);
	t->telem = *telem;
	t->next = NULL;
	*last = t;
	last = &t->next;
}

static void
dump_telem(union ao_telemetry_all *telem)
{
	char s[CC_TELEMETRY_BUFSIZE];

	cc_telemetry_unparse(telem, s);
	printf("%s\n", s);
}

double	pad_lat = 0, pad_lon = 0;
double	target_pad_lat = 0, target_pad_lon = 0;
double	lat_off = 0, lon_off = 0;
int pending = 1;

static void
dump_saved(void);

void
doit(union ao_telemetry_all *telem)
{
	double lat, lon;

	switch (telem->generic.type) {
	case AO_TELEMETRY_SENSOR_TELEMETRUM:
	case AO_TELEMETRY_SENSOR_TELEMINI:
	case AO_TELEMETRY_SENSOR_TELENANO:
		if (telem->sensor.state > ao_flight_pad && pad) {
			pending = 0;
			if (target_pad_lat)
				lat_off = target_pad_lat - pad_lat;
			if (target_pad_lon)
				lon_off = target_pad_lon - pad_lon;
			dump_saved();
		}
		break;
	case AO_TELEMETRY_LOCATION: {
		lat = telem->location.latitude / 1.0e7;
		lon = telem->location.longitude / 1.0e7;
		if (pending) {
			if (telem->location.flags & (1 << 4)) {
				if (pad_lat) {
					pad_lat = pad_lat - pad_lat / 32 + lat / 32.0;
					pad_lon = pad_lon - pad_lon / 32 + lon / 32.0;
				} else {
					pad_lat = lat;
					pad_lon = lon;
				}
			}
		} else {
			lat += lat_off;
			lon += lon_off;
			if (lat > 90)
				lat = 90;
			if (lat < -90)
				lat = -90;
			while (lon > 180)
				lon -= 360;
			while (lon < -180)
				lon += 360;
			telem->location.latitude = lat * 1.0e7;
			telem->location.longitude = lon * 1.0e7;
		}
		break;
	}
	}
}

static void
dump_saved(void)
{
	struct telem_ent	*t, *n;

	for (t = pad; t; t = n) {
		n = t->next;
		doit(&t->telem);
		dump_telem(&t->telem);
		free(t);
	}
	pad = NULL;
	last = &pad;
}

int
main (int argc, char **argv)
{
	char	line[80];
	int c, i, ret;
	char *s;
	FILE *file;
	int serial;
	while ((c = getopt_long(argc, argv, "l:L:", options, NULL)) != -1) {
		switch (c) {
		case 'L':
			target_pad_lat = strtod(optarg, NULL);
			break;
		case 'l':
			target_pad_lon = strtod(optarg, NULL);
			break;
		default:
			usage(argv[0]);
			break;
		}
	}
	for (i = optind; i < argc; i++) {
		file = fopen(argv[i], "r");
		if (!file) {
			perror(argv[i]);
			ret++;
			continue;
		}
		s = strstr(argv[i], "-serial-");
		if (s)
			serial = atoi(s + 8);
		else
			serial = 0;
		while (fgets(line, sizeof (line), file)) {
			union ao_telemetry_all telem;

			if (cc_telemetry_parse(line, &telem)) {
				if ((telem.generic.status & (1 << 7)) == 0) {
					dump_telem(&telem);
					continue;
				}
				doit (&telem);
				if (pending)
					save_telem(&telem);
				else
					dump_telem(&telem);
			}
		}
		fclose (file);

	}
	return ret;
}