2 * Copyright © 2012 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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.micropeak;
24 import org.altusmetrum.altoslib_14.*;
25 import org.altusmetrum.altosuilib_14.*;
27 public class MicroData {
28 public int ground_pressure;
29 public int min_pressure;
31 AltosUIFlightSeries flight_series;
32 AltosFlightStats flight_stats;
33 AltosCalData cal_data;
35 private double time_step;
36 private ArrayList<Integer> bytes;
42 public static final int LOG_ID_MICROPEAK = 0;
43 public static final int LOG_ID_MICROKITE = 1;
44 public static final int LOG_ID_MICROPEAK2 = 2;
46 public static final double CLOCK_MP1 = 0.096;
47 public static final double CLOCK_MP2 = 0.1;
49 public class FileEndedException extends Exception {
52 public class NonHexcharException extends Exception {
55 public class InvalidCrcException extends Exception {
58 private int getc(InputStream f) throws IOException, FileEndedException {
62 throw new FileEndedException();
67 private int get_nonwhite(InputStream f) throws IOException, FileEndedException {
72 if (!Character.isWhitespace(c))
77 private int get_hexc(InputStream f) throws IOException, FileEndedException, NonHexcharException {
78 int c = get_nonwhite(f);
80 if ('0' <= c && c <= '9')
82 if ('a' <= c && c <= 'f')
84 if ('A' <= c && c <= 'F')
86 throw new NonHexcharException();
89 private static final int POLY = 0x8408;
91 private int log_crc(int crc, int b) {
94 for (i = 0; i < 8; i++) {
95 if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
96 crc = (crc >> 1) ^ POLY;
106 private int get_hex(InputStream f) throws IOException, FileEndedException, NonHexcharException {
110 int h = (a << 4) + b;
112 file_crc = log_crc(file_crc, h);
116 private boolean find_header(InputStream f) throws IOException, FileEndedException {
118 if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
123 private int get_32(InputStream f) throws IOException, FileEndedException, NonHexcharException {
125 for (int i = 0; i < 4; i++) {
126 v += get_hex(f) << (i * 8);
131 private int get_16(InputStream f) throws IOException, FileEndedException, NonHexcharException {
133 for (int i = 0; i < 2; i++) {
134 v += get_hex(f) << (i * 8);
139 private String get_line(InputStream f) throws IOException, FileEndedException, NonHexcharException {
141 StringBuffer line = new StringBuffer();
145 } while (Character.isWhitespace(c));
148 line.append((char) c);
150 } while (!Character.isWhitespace(c));
151 return new String(line);
154 private int swap16(int i) {
155 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
158 public boolean crc_valid;
160 int mix_in (int high, int low) {
161 return high - (high & 0xffff) + low;
164 boolean closer (int target, int a, int b) {
165 return Math.abs (target - a) < Math.abs(target - b);
168 public double altitude(double time) {
169 if (flight_series.altitude_series == null)
171 return flight_series.altitude_series.value(time);
174 public double altitude(int i) {
175 return altitude(time(i));
178 public String name() {
182 public double pressure(int i) {
183 if (flight_series.pressure_series == null)
186 return flight_series.pressure_series.value(time(i));
189 public double height(double time) {
190 if (flight_series.height_series == null)
193 return flight_series.height_series.value(time);
196 public double height(int i) {
197 return height(time(i));
200 public int length() {
201 if (flight_series.pressure_series == null)
203 return flight_series.pressure_series.size();
206 /* Use the recorded apogee pressure for stats so that it agrees with the device */
207 public double apogee_pressure() {
211 public double apogee_altitude() {
212 return AltosConvert.pressure_to_altitude(apogee_pressure());
215 public double apogee_height() {
216 return apogee_altitude() - cal_data.ground_altitude;
219 public double speed(double time) {
220 if (flight_series.speed_series == null)
222 return flight_series.speed_series.value(time);
225 public double speed(int i) {
226 return speed(time(i));
229 public double acceleration(double time) {
230 if (flight_series.accel_series == null)
232 return flight_series.accel_series.value(time);
235 public double acceleration(int i) {
236 return acceleration(time(i));
239 public double time(int i) {
240 return i * time_step;
243 public void save (OutputStream f) throws IOException {
249 public void export (Writer f) throws IOException {
250 PrintWriter pw = new PrintWriter(f);
251 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");
253 for (AltosTimeValue ptv : flight_series.pressure_series) {
255 double height = height(ptv.time);
256 double speed = speed(ptv.time);
257 double accel = acceleration(ptv.time);
259 pw.printf("%6.3f,%10.0f,%10.1f,%10.1f,%11.2f,%11.2f,%12.4f,%12.2f,%13.2f,%10.4f\n",
263 AltosConvert.meters_to_feet(height),
265 AltosConvert.meters_to_mph(speed),
266 AltosConvert.meters_to_mach(speed),
268 AltosConvert.meters_to_feet(accel),
269 AltosConvert.meters_to_g(accel));
273 public void set_name(String name) {
278 ground_pressure = 101000;
279 min_pressure = 101000;
280 cal_data = new AltosCalData();
281 flight_series = new AltosUIFlightSeries(cal_data);
284 public MicroData (InputStream f, String name) throws IOException, InterruptedException, NonHexcharException, FileEndedException {
286 bytes = new ArrayList<Integer>();
288 cal_data = new AltosCalData();
289 flight_series = new AltosUIFlightSeries(cal_data);
292 throw new IOException("No MicroPeak data header found");
295 ground_pressure = get_32(f);
296 min_pressure = get_32(f);
297 nsamples = get_16(f);
299 log_id = nsamples >> 12;
301 if (log_id == LOG_ID_MICROPEAK2) {
302 int nsamples_high = get_16(f);
303 nsamples |= (nsamples_high << 12);
306 cal_data.set_ground_pressure(ground_pressure);
309 case LOG_ID_MICROPEAK:
310 time_step = 2 * CLOCK_MP1;
312 case LOG_ID_MICROKITE:
313 time_step = 200 * CLOCK_MP1;
315 case LOG_ID_MICROPEAK2:
316 time_step = CLOCK_MP2;
319 throw new IOException(String.format("Unknown device type: %d", log_id));
321 cal_data.set_ticks_per_sec(1/time_step);
322 cal_data.set_tick(0);
323 cal_data.set_boost_tick();
325 int cur = ground_pressure;
326 cal_data.set_tick(0);
327 flight_series.set_time(cal_data.time());
328 flight_series.set_pressure(cur);
329 for (int i = 0; i < nsamples; i++) {
331 int same = mix_in(cur, k);
332 int up = mix_in(cur + 0x10000, k);
333 int down = mix_in(cur - 0x10000, k);
335 if (closer (cur, same, up)) {
336 if (closer (cur, same, down))
341 if (closer (cur, up, down))
347 cal_data.set_tick(i+1);
348 flight_series.set_time(cal_data.time());
349 flight_series.set_pressure(cur);
352 int current_crc = swap16(~file_crc & 0xffff);
355 crc_valid = crc == current_crc;
357 if (log_id == LOG_ID_MICROPEAK2) {
358 unique_id = get_line(f);
361 flight_series.finish();
365 flight_series.set_time(0);
366 flight_series.set_state(AltosLib.ao_flight_boost);
368 if (flight_series.speed_series != null && flight_series.speed_series.max() != null) {
369 flight_series.set_time(flight_series.speed_series.max().time);
370 flight_series.set_state(AltosLib.ao_flight_coast);
373 flight_series.set_time(flight_series.height_series.max().time);
374 flight_series.set_state(AltosLib.ao_flight_drogue);
376 cal_data.set_tick(nsamples);
377 flight_series.set_time(cal_data.time());
378 flight_series.set_state(AltosLib.ao_flight_landed);
380 flight_series.set_min_pressure(min_pressure);
382 flight_series.finish();
384 flight_stats = new AltosFlightStats(flight_series);
387 } catch (FileEndedException fe) {
388 throw new IOException("File Ended Unexpectedly");