altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / micropeak / MicroData.java
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.micropeak;
20
21 import java.lang.*;
22 import java.io.*;
23 import java.util.*;
24 import org.altusmetrum.altoslib_14.*;
25 import org.altusmetrum.altosuilib_14.*;
26
27 public class MicroData {
28         public int              ground_pressure;
29         public int              min_pressure;
30
31         AltosUIFlightSeries     flight_series;
32         AltosFlightStats        flight_stats;
33         AltosCalData            cal_data;
34
35         private double          time_step;
36         private ArrayList<Integer>      bytes;
37         public int              nsamples;
38         public int              log_id;
39         String                  name;
40         String                  unique_id;
41
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;
45
46         public static final double CLOCK_MP1 = 0.096;
47         public static final double CLOCK_MP2 = 0.1;
48
49         public class FileEndedException extends Exception {
50         }
51
52         public class NonHexcharException extends Exception {
53         }
54
55         public class InvalidCrcException extends Exception {
56         }
57
58         private int getc(InputStream f) throws IOException, FileEndedException {
59                 int     c = f.read();
60
61                 if (c == -1)
62                         throw new FileEndedException();
63                 bytes.add(c);
64                 return c;
65         }
66
67         private int get_nonwhite(InputStream f) throws IOException, FileEndedException {
68                 int     c;
69
70                 for (;;) {
71                         c = getc(f);
72                         if (!Character.isWhitespace(c))
73                                 return c;
74                 }
75         }
76
77         private int get_hexc(InputStream f) throws IOException, FileEndedException, NonHexcharException {
78                 int     c = get_nonwhite(f);
79
80                 if ('0' <= c && c <= '9')
81                         return c - '0';
82                 if ('a' <= c && c <= 'f')
83                         return c - 'a' + 10;
84                 if ('A' <= c && c <= 'F')
85                         return c - 'A' + 10;
86                 throw new NonHexcharException();
87         }
88
89         private static final int POLY = 0x8408;
90
91         private int log_crc(int crc, int b) {
92                 int     i;
93
94                 for (i = 0; i < 8; i++) {
95                         if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
96                                 crc = (crc >> 1) ^ POLY;
97                         else
98                                 crc = crc >> 1;
99                         b >>= 1;
100                 }
101                 return crc & 0xffff;
102         }
103
104         int     file_crc;
105
106         private int get_hex(InputStream f) throws IOException, FileEndedException, NonHexcharException {
107                 int     a = get_hexc(f);
108                 int     b = get_hexc(f);
109
110                 int h = (a << 4) + b;
111
112                 file_crc = log_crc(file_crc, h);
113                 return h;
114         }
115
116         private boolean find_header(InputStream f) throws IOException, FileEndedException {
117                 for (;;) {
118                         if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
119                                 return true;
120                 }
121         }
122
123         private int get_32(InputStream f)  throws IOException, FileEndedException, NonHexcharException {
124                 int     v = 0;
125                 for (int i = 0; i < 4; i++) {
126                         v += get_hex(f) << (i * 8);
127                 }
128                 return v;
129         }
130
131         private int get_16(InputStream f) throws IOException, FileEndedException, NonHexcharException {
132                 int     v = 0;
133                 for (int i = 0; i < 2; i++) {
134                         v += get_hex(f) << (i * 8);
135                 }
136                 return v;
137         }
138
139         private String get_line(InputStream f) throws IOException, FileEndedException, NonHexcharException {
140                 int c;
141                 StringBuffer line = new StringBuffer();
142
143                 do {
144                         c = f.read();
145                 } while (Character.isWhitespace(c));
146
147                 do {
148                         line.append((char) c);
149                         c = f.read();
150                 } while (!Character.isWhitespace(c));
151                 return new String(line);
152         }
153
154         private int swap16(int i) {
155                 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
156         }
157
158         public boolean  crc_valid;
159
160         int mix_in (int high, int low) {
161                 return  high - (high & 0xffff) + low;
162         }
163
164         boolean closer (int target, int a, int b) {
165                 return Math.abs (target - a) < Math.abs(target - b);
166         }
167
168         public double altitude(double time) {
169                 if (flight_series.altitude_series == null)
170                         return 0.0;
171                 return flight_series.altitude_series.value(time);
172         }
173
174         public double altitude(int i) {
175                 return altitude(time(i));
176         }
177
178         public String name() {
179                 return name;
180         }
181
182         public double pressure(int i) {
183                 if (flight_series.pressure_series == null)
184                         return 0.0;
185
186                 return flight_series.pressure_series.value(time(i));
187         }
188
189         public double height(double time) {
190                 if (flight_series.height_series == null)
191                         return 0.0;
192
193                 return flight_series.height_series.value(time);
194         }
195
196         public double height(int i) {
197                 return height(time(i));
198         }
199
200         public int length() {
201                 if (flight_series.pressure_series == null)
202                         return 0;
203                 return flight_series.pressure_series.size();
204         }
205
206         /* Use the recorded apogee pressure for stats so that it agrees with the device */
207         public double apogee_pressure() {
208                 return min_pressure;
209         }
210
211         public double apogee_altitude() {
212                 return AltosConvert.pressure_to_altitude(apogee_pressure());
213         }
214
215         public double apogee_height() {
216                 return apogee_altitude() - cal_data.ground_altitude;
217         }
218
219         public double speed(double time) {
220                 if (flight_series.speed_series == null)
221                         return 0.0;
222                 return flight_series.speed_series.value(time);
223         }
224
225         public double speed(int i) {
226                 return speed(time(i));
227         }
228
229         public double acceleration(double time) {
230                 if (flight_series.accel_series == null)
231                         return 0.0;
232                 return flight_series.accel_series.value(time);
233         }
234
235         public double acceleration(int i) {
236                 return acceleration(time(i));
237         }
238
239         public double time(int i) {
240                 return i * time_step;
241         }
242
243         public void save (OutputStream f) throws IOException {
244                 for (int c : bytes)
245                         f.write(c);
246                 f.write('\n');
247         }
248
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");
252
253                 for (AltosTimeValue ptv : flight_series.pressure_series) {
254
255                         double height = height(ptv.time);
256                         double speed = speed(ptv.time);
257                         double accel = acceleration(ptv.time);
258
259                         pw.printf("%6.3f,%10.0f,%10.1f,%10.1f,%11.2f,%11.2f,%12.4f,%12.2f,%13.2f,%10.4f\n",
260                                   ptv.time,
261                                   ptv.value,
262                                   height,
263                                   AltosConvert.meters_to_feet(height),
264                                   speed,
265                                   AltosConvert.meters_to_mph(speed),
266                                   AltosConvert.meters_to_mach(speed),
267                                   accel,
268                                   AltosConvert.meters_to_feet(accel),
269                                   AltosConvert.meters_to_g(accel));
270                 }
271         }
272
273         public void set_name(String name) {
274                 this.name = name;
275         }
276
277         public MicroData() {
278                 ground_pressure = 101000;
279                 min_pressure = 101000;
280                 cal_data = new AltosCalData();
281                 flight_series = new AltosUIFlightSeries(cal_data);
282         }
283
284         public MicroData (InputStream f, String name) throws IOException, InterruptedException, NonHexcharException, FileEndedException {
285                 this.name = name;
286                 bytes = new ArrayList<Integer>();
287
288                 cal_data = new AltosCalData();
289                 flight_series = new AltosUIFlightSeries(cal_data);
290
291                 if (!find_header(f))
292                         throw new IOException("No MicroPeak data header found");
293                 try {
294                         file_crc = 0xffff;
295                         ground_pressure = get_32(f);
296                         min_pressure = get_32(f);
297                         nsamples = get_16(f);
298
299                         log_id = nsamples >> 12;
300                         nsamples &= 0xfff;
301                         if (log_id == LOG_ID_MICROPEAK2) {
302                                 int nsamples_high = get_16(f);
303                                 nsamples |= (nsamples_high << 12);
304                         }
305
306                         cal_data.set_ground_pressure(ground_pressure);
307
308                         switch (log_id) {
309                         case LOG_ID_MICROPEAK:
310                                 time_step = 2 * CLOCK_MP1;
311                                 break;
312                         case LOG_ID_MICROKITE:
313                                 time_step = 200 * CLOCK_MP1;
314                                 break;
315                         case LOG_ID_MICROPEAK2:
316                                 time_step = CLOCK_MP2;
317                                 break;
318                         default:
319                                 throw new IOException(String.format("Unknown device type: %d", log_id));
320                         }
321                         cal_data.set_ticks_per_sec(1/time_step);
322                         cal_data.set_tick(0);
323                         cal_data.set_boost_tick();
324
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++) {
330                                 int     k = get_16(f);
331                                 int     same = mix_in(cur, k);
332                                 int     up = mix_in(cur + 0x10000, k);
333                                 int     down = mix_in(cur - 0x10000, k);
334
335                                 if (closer (cur, same, up)) {
336                                         if (closer (cur, same, down))
337                                                 cur = same;
338                                         else
339                                                 cur = down;
340                                 } else {
341                                         if (closer (cur, up, down))
342                                                 cur = up;
343                                         else
344                                                 cur = down;
345                                 }
346
347                                 cal_data.set_tick(i+1);
348                                 flight_series.set_time(cal_data.time());
349                                 flight_series.set_pressure(cur);
350                         }
351
352                         int current_crc = swap16(~file_crc & 0xffff);
353                         int crc = get_16(f);
354
355                         crc_valid = crc == current_crc;
356
357                         if (log_id == LOG_ID_MICROPEAK2) {
358                                 unique_id = get_line(f);
359                         }
360
361                         flight_series.finish();
362
363                         /* Build states */
364
365                         flight_series.set_time(0);
366                         flight_series.set_state(AltosLib.ao_flight_boost);
367
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);
371                         }
372
373                         flight_series.set_time(flight_series.height_series.max().time);
374                         flight_series.set_state(AltosLib.ao_flight_drogue);
375
376                         cal_data.set_tick(nsamples);
377                         flight_series.set_time(cal_data.time());
378                         flight_series.set_state(AltosLib.ao_flight_landed);
379
380                         flight_series.set_min_pressure(min_pressure);
381
382                         flight_series.finish();
383
384                         flight_stats = new AltosFlightStats(flight_series);
385
386
387                 } catch (FileEndedException fe) {
388                         throw new IOException("File Ended Unexpectedly");
389                 }
390         }
391
392 }