summaryrefslogtreecommitdiff
path: root/micropeak/MicroData.java
blob: 70492a07abbebf5e95eaf4ac128ba1f03227590c (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * Copyright © 2012 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.
 */

package org.altusmetrum.micropeak;

import java.lang.*;
import java.io.*;
import java.util.*;
import org.altusmetrum.altoslib_12.*;
import org.altusmetrum.altosuilib_12.*;

public class MicroData {
	public int		ground_pressure;
	public int		min_pressure;

	AltosUIFlightSeries	flight_series;
	AltosFlightStats	flight_stats;
	AltosCalData		cal_data;

	private double		time_step;
	private ArrayList<Integer>	bytes;
	public int		log_id;
	String			name;

	public static final int LOG_ID_MICROPEAK = 0;
	public static final int LOG_ID_MICROKITE = 1;
	public static final int LOG_ID_MICROPEAK2 = 2;

	public static final double CLOCK_MP1 = 0.096;
	public static final double CLOCK_MP2 = 0.1;

	public class FileEndedException extends Exception {
	}

	public class NonHexcharException extends Exception {
	}

	public class InvalidCrcException extends Exception {
	}

	private int getc(InputStream f) throws IOException, FileEndedException {
		int	c = f.read();

		if (c == -1)
			throw new FileEndedException();
		bytes.add(c);
		return c;
	}

	private int get_nonwhite(InputStream f) throws IOException, FileEndedException {
		int	c;

		for (;;) {
			c = getc(f);
			if (!Character.isWhitespace(c))
				return c;
		}
	}

	private int get_hexc(InputStream f) throws IOException, FileEndedException, NonHexcharException {
		int	c = get_nonwhite(f);

		if ('0' <= c && c <= '9')
			return c - '0';
		if ('a' <= c && c <= 'f')
			return c - 'a' + 10;
		if ('A' <= c && c <= 'F')
			return c - 'A' + 10;
		throw new NonHexcharException();
	}

	private static final int POLY = 0x8408;

	private int log_crc(int crc, int b) {
		int	i;

		for (i = 0; i < 8; i++) {
			if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
				crc = (crc >> 1) ^ POLY;
			else
				crc = crc >> 1;
			b >>= 1;
		}
		return crc & 0xffff;
	}

	int	file_crc;

	private int get_hex(InputStream f) throws IOException, FileEndedException, NonHexcharException {
		int	a = get_hexc(f);
		int	b = get_hexc(f);

		int h = (a << 4) + b;

		file_crc = log_crc(file_crc, h);
		return h;
	}

	private boolean find_header(InputStream f) throws IOException, FileEndedException {
		for (;;) {
			if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
				return true;
		}
	}

	private int get_32(InputStream f)  throws IOException, FileEndedException, NonHexcharException {
		int	v = 0;
		for (int i = 0; i < 4; i++) {
			v += get_hex(f) << (i * 8);
		}
		return v;
	}

	private int get_16(InputStream f) throws IOException, FileEndedException, NonHexcharException {
		int	v = 0;
		for (int i = 0; i < 2; i++) {
			v += get_hex(f) << (i * 8);
		}
		return v;
	}

	private int swap16(int i) {
		return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
	}

	public boolean	crc_valid;

	int mix_in (int high, int low) {
		return  high - (high & 0xffff) + low;
	}

	boolean closer (int target, int a, int b) {
		return Math.abs (target - a) < Math.abs(target - b);
	}

	public double altitude(double time) {
		if (flight_series.altitude_series == null)
			return 0.0;
		return flight_series.altitude_series.value(time);
	}

	public double altitude(int i) {
		return altitude(time(i));
	}

	public String name() {
		return name;
	}

	public double pressure(int i) {
		if (flight_series.pressure_series == null)
			return 0.0;

		return flight_series.pressure_series.value(time(i));
	}

	public double height(double time) {
		if (flight_series.height_series == null)
			return 0.0;

		return flight_series.height_series.value(time);
	}

	public double height(int i) {
		return height(time(i));
	}

	public int length() {
		if (flight_series.pressure_series == null)
			return 0;
		return flight_series.pressure_series.size();
	}

	/* Use the recorded apogee pressure for stats so that it agrees with the device */
	public double apogee_pressure() {
		return min_pressure;
	}

	public double apogee_altitude() {
		return AltosConvert.pressure_to_altitude(apogee_pressure());
	}

	public double apogee_height() {
		return apogee_altitude() - cal_data.ground_altitude;
	}

	public double speed(double time) {
		if (flight_series.speed_series == null)
			return 0.0;
		return flight_series.speed_series.value(time);
	}

	public double speed(int i) {
		return speed(time(i));
	}

	public double acceleration(double time) {
		if (flight_series.accel_series == null)
			return 0.0;
		return flight_series.accel_series.value(time);
	}

	public double acceleration(int i) {
		return acceleration(time(i));
	}

	public double time(int i) {
		return i * time_step;
	}

	public void save (OutputStream f) throws IOException {
		for (int c : bytes)
			f.write(c);
		f.write('\n');
	}

	public void export (Writer f) throws IOException {
		PrintWriter	pw = new PrintWriter(f);
		pw.printf("  Time, Press(Pa), Height(m), Height(f), Speed(m/s), Speed(mph), Speed(mach), Accel(m/s²), Accel(ft/s²),  Accel(g)\n");

		for (AltosTimeValue ptv : flight_series.pressure_series) {

			double height = height(ptv.time);
			double speed = speed(ptv.time);
			double accel = acceleration(ptv.time);

			pw.printf("%6.3f,%10.0f,%10.1f,%10.1f,%11.2f,%11.2f,%12.4f,%12.2f,%13.2f,%10.4f\n",
				  ptv.time,
				  ptv.value,
				  height,
				  AltosConvert.meters_to_feet(height),
				  speed,
				  AltosConvert.meters_to_mph(speed),
				  AltosConvert.meters_to_mach(speed),
				  accel,
				  AltosConvert.meters_to_feet(accel),
				  AltosConvert.meters_to_g(accel));
		}
	}

	public void set_name(String name) {
		this.name = name;
	}

	public MicroData() {
		ground_pressure = 101000;
		min_pressure = 101000;
		cal_data = new AltosCalData();
		flight_series = new AltosUIFlightSeries(cal_data);
	}

	public MicroData (InputStream f, String name) throws IOException, InterruptedException, NonHexcharException, FileEndedException {
		this.name = name;
		bytes = new ArrayList<Integer>();

		cal_data = new AltosCalData();
		flight_series = new AltosUIFlightSeries(cal_data);

		if (!find_header(f))
			throw new IOException("No MicroPeak data header found");
		try {
			file_crc = 0xffff;
			ground_pressure = get_32(f);
			min_pressure = get_32(f);
			int nsamples = get_16(f);

			log_id = nsamples >> 12;
			nsamples &= 0xfff;

			cal_data.set_ground_pressure(ground_pressure);

			switch (log_id) {
			case LOG_ID_MICROPEAK:
				time_step = 2 * CLOCK_MP1;
				break;
			case LOG_ID_MICROKITE:
				time_step = 200 * CLOCK_MP1;
				break;
			case LOG_ID_MICROPEAK2:
				time_step = CLOCK_MP2;
				break;
			default:
				throw new IOException(String.format("Unknown device type: %d", log_id));
			}
			cal_data.set_ticks_per_sec(1/time_step);
			cal_data.set_tick(0);
			cal_data.set_boost_tick();

			int cur = ground_pressure;
			cal_data.set_tick(0);
			flight_series.set_time(cal_data.time());
			flight_series.set_pressure(cur);
			for (int i = 0; i < nsamples; i++) {
				int	k = get_16(f);
				int	same = mix_in(cur, k);
				int	up = mix_in(cur + 0x10000, k);
				int	down = mix_in(cur - 0x10000, k);

				if (closer (cur, same, up)) {
					if (closer (cur, same, down))
						cur = same;
					else
						cur = down;
				} else {
					if (closer (cur, up, down))
						cur = up;
					else
						cur = down;
				}

				cal_data.set_tick(i+1);
				flight_series.set_time(cal_data.time());
				flight_series.set_pressure(cur);
			}

			flight_series.finish();

			/* Build states */

			flight_series.set_time(0);
			flight_series.set_state(AltosLib.ao_flight_boost);

			flight_series.set_time(flight_series.speed_series.max().time);
			flight_series.set_state(AltosLib.ao_flight_coast);

			flight_series.set_time(flight_series.height_series.max().time);
			flight_series.set_state(AltosLib.ao_flight_drogue);

			cal_data.set_tick(nsamples);
			flight_series.set_time(cal_data.time());
			flight_series.set_state(AltosLib.ao_flight_landed);

			flight_series.finish();

			flight_stats = new AltosFlightStats(flight_series);

			int current_crc = swap16(~file_crc & 0xffff);
			int crc = get_16(f);

			crc_valid = crc == current_crc;

		} catch (FileEndedException fe) {
			throw new IOException("File Ended Unexpectedly");
		}
	}

}