summaryrefslogtreecommitdiff
path: root/ao-tools/altosui/AltosCSV.java
blob: 4ce8e30e77dece9e86edbc1cc37f3fca1bf9f3de (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
/*
 * Copyright © 2010 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; version 2 of the License.
 *
 * 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.
 */

package altosui;

import java.lang.*;
import java.io.*;
import java.text.*;
import java.util.*;

import altosui.AltosRecord;
import altosui.AltosReader;

public class AltosCSV {
	File			name;
	PrintStream		out;
	boolean			header_written;
	boolean			seen_boost;
	int			boost_tick;
	LinkedList<AltosRecord>	pad_records;
	AltosState		state;

	static final int ALTOS_CSV_VERSION = 1;

	/* Version 1 format:
	 *
	 * General info
	 *	version number
	 *	serial number
	 *	flight number
	 *	callsign
	 *	time (seconds since boost)
	 *	rssi
	 *
	 * Flight status
	 *	state
	 *	state name
	 *
	 * Basic sensors
	 *	acceleration (m/s²)
	 *	pressure (mBar)
	 *	altitude (m)
	 *	height (m)
	 *	accelerometer speed (m/s)
	 *	barometer speed (m/s)
	 *	temp (°C)
	 *	battery (V)
	 *	drogue (V)
	 *	main (V)
	 *
	 * GPS data
	 *	connected (1/0)
	 *	locked (1/0)
	 *	nsat (used for solution)
	 *	latitude (°)
	 *	longitude (°)
	 *	altitude (m)
	 *	year (e.g. 2010)
	 *	month (1-12)
	 *	day (1-31)
	 *	hour (0-23)
	 *	minute (0-59)
	 *	second (0-59)
	 *	from_pad_dist (m)
	 *	from_pad_azimuth (deg true)
	 *	from_pad_range (m)
	 *	from_pad_elevation (deg from horizon)
	 *
	 * GPS Sat data
	 *	hdop
	 *	C/N0 data for all 32 valid SDIDs
	 */

	void write_general_header() {
		out.printf("version serial flight call time rssi");
	}

	void write_general(AltosRecord record) {
		out.printf("%s,%d,%d,%s,%8.2f,%4d",
			   record.version, record.serial, record.flight, record.callsign,
			   (double) record.time,
			   record.rssi);
	}

	void write_flight_header() {
		out.printf("state state_name");
	}

	void write_flight(AltosRecord record) {
		out.printf("%d,%8s", record.state, record.state());
	}

	void write_basic_header() {
		out.printf("acceleration pressure altitude height accel_speed baro_speed temperature battery_voltage drogue_voltage main_voltage");
	}

	void write_basic(AltosRecord record) {
		out.printf("%8.2f,%10.2f,%8.2f,%8.2f,%8.2f,%8.2f,%5.1f,%5.2f,%5.2f,%5.2f",
			   record.acceleration(),
			   record.pressure(),
			   record.altitude(),
			   record.height(),
			   record.accel_speed(),
			   state.baro_speed,
			   record.temperature(),
			   record.battery_voltage(),
			   record.drogue_voltage(),
			   record.main_voltage());
	}

	void write_gps_header() {
		out.printf("connected locked nsat latitude longitude altitude year month day hour minute second pad_dist pad_range pad_az pad_el");
	}

	void write_gps(AltosRecord record) {
		AltosGPS	gps = record.gps;
		if (gps == null)
			gps = new AltosGPS();

		AltosGreatCircle from_pad = state.from_pad;
		if (from_pad == null)
			from_pad = new AltosGreatCircle();

		out.printf("%2d,%2d,%3d,%12.7f,%12.7f,%6d,%5d,%3d,%3d,%3d,%3d,%3d,%9.0f,%9.0f,%4.0f,%4.0f",
			   gps.connected?1:0,
			   gps.locked?1:0,
			   gps.nsat,
			   gps.lat,
			   gps.lon,
			   gps.alt,
			   gps.year,
			   gps.month,
			   gps.day,
			   gps.hour,
			   gps.minute,
			   gps.second,
			   from_pad.distance,
			   state.range,
			   from_pad.bearing,
			   state.elevation);
	}

	void write_header() {
		out.printf("# "); write_general_header();
		out.printf(" "); write_flight_header();
		out.printf(" "); write_basic_header();
		out.printf(" "); write_gps_header();
		out.printf ("\n");
	}

	void write_one(AltosRecord record) {
		state = new AltosState(record, state);
		write_general(record); out.printf(",");
		write_flight(record); out.printf(",");
		write_basic(record); out.printf(",");
		write_gps(record);
		out.printf ("\n");
	}

	void flush_pad() {
		while (!pad_records.isEmpty()) {
			write_one (pad_records.remove());
		}
	}

	public void write(AltosRecord record) {
		if (!header_written) {
			write_header();
			header_written = true;
		}
		if (!seen_boost) {
			if (record.state >= Altos.ao_flight_boost) {
				seen_boost = true;
				boost_tick = record.tick;
				flush_pad();
			}
		}
		if (seen_boost)
			write_one(record);
		else
			pad_records.add(record);
	}

	public PrintStream out() {
		return out;
	}

	public void close() {
		if (!pad_records.isEmpty()) {
			boost_tick = pad_records.element().tick;
			flush_pad();
		}
		out.close();
	}

	public void write(AltosReader reader) {
		AltosRecord	record;

		reader.write_comments(out());
		try {
			for (;;) {
				record = reader.read();
				if (record == null)
					break;
				write(record);
			}
		} catch (IOException ie) {
			System.out.printf("IOException\n");
		} catch (ParseException pe) {
			System.out.printf("ParseException %s\n", pe.getMessage());
		}
	}

	public AltosCSV(File in_name) throws FileNotFoundException {
		name = in_name;
		out = new PrintStream(name);
		pad_records = new LinkedList<AltosRecord>();
	}

	public AltosCSV(String in_string) throws FileNotFoundException {
		this(new File(in_string));
	}
}