doc: Micropeak doc updates for 1.3.2
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.micropeak;
19
20 import java.lang.*;
21 import java.io.*;
22 import java.util.*;
23 import org.altusmetrum.altoslib_3.*;
24 import org.altusmetrum.altosuilib_1.*;
25
26 class MicroIterator implements Iterator<MicroDataPoint> {
27         int             i;
28         MicroData       data;
29
30         public boolean hasNext() {
31                 return i < data.pressures.length;
32         }
33
34         public MicroDataPoint next() {
35                 return new MicroDataPoint(data, i++);
36         }
37
38         public MicroIterator (MicroData data) {
39                 this.data = data;
40                 i = 0;
41         }
42
43         public void remove() {
44         }
45 }
46
47 class MicroIterable implements Iterable<MicroDataPoint> {
48
49         MicroData       data;
50
51         public Iterator<MicroDataPoint> iterator() {
52                 return new MicroIterator(data);
53         }
54
55         public MicroIterable(MicroData data) {
56                 this.data = data;
57         }
58 }
59
60 class MicroUIIterator implements Iterator<AltosUIDataPoint> {
61         int             i;
62         MicroData       data;
63
64         public boolean hasNext() {
65                 return i < data.pressures.length;
66         }
67
68         public AltosUIDataPoint next() {
69                 return new MicroDataPoint(data, i++);
70         }
71
72         public MicroUIIterator (MicroData data) {
73                 this.data = data;
74                 i = 0;
75         }
76
77         public void remove() {
78         }
79 }
80
81 class MicroUIIterable implements Iterable<AltosUIDataPoint> {
82         MicroData       data;
83
84         public Iterator<AltosUIDataPoint> iterator() {
85                 return new MicroUIIterator(data);
86         }
87
88         public MicroUIIterable(MicroData data) {
89                 this.data = data;
90         }
91 }
92
93 public class MicroData implements AltosUIDataSet {
94         public int              ground_pressure;
95         public int              min_pressure;
96         public int[]            pressures;
97         private double          time_step;
98         private double          ground_altitude;
99         private ArrayList<Integer>      bytes;
100         String                  name;
101         MicroStats              stats;
102         
103         public class FileEndedException extends Exception {
104         }
105
106         public class NonHexcharException extends Exception {
107         }
108
109         public class InvalidCrcException extends Exception {
110         }
111
112         private int getc(InputStream f) throws IOException, FileEndedException {
113                 int     c = f.read();
114
115                 if (c == -1)
116                         throw new FileEndedException();
117                 bytes.add(c);
118                 return c;
119         }
120
121         private int get_nonwhite(InputStream f) throws IOException, FileEndedException {
122                 int     c;
123
124                 for (;;) {
125                         c = getc(f);
126                         if (!Character.isWhitespace(c))
127                                 return c;
128                 }
129         }
130
131         private int get_hexc(InputStream f) throws IOException, FileEndedException, NonHexcharException {
132                 int     c = get_nonwhite(f);
133
134                 if ('0' <= c && c <= '9')
135                         return c - '0';
136                 if ('a' <= c && c <= 'f')
137                         return c - 'a' + 10;
138                 if ('A' <= c && c <= 'F')
139                         return c - 'A' + 10;
140                 throw new NonHexcharException();
141         }
142
143         private static final int POLY = 0x8408;
144
145         private int log_crc(int crc, int b) {
146                 int     i;
147
148                 for (i = 0; i < 8; i++) {
149                         if (((crc & 0x0001) ^ (b & 0x0001)) != 0)
150                                 crc = (crc >> 1) ^ POLY;
151                         else
152                                 crc = crc >> 1;
153                         b >>= 1;
154                 }
155                 return crc & 0xffff;
156         }
157
158         int     file_crc;
159
160         private int get_hex(InputStream f) throws IOException, FileEndedException, NonHexcharException {
161                 int     a = get_hexc(f);
162                 int     b = get_hexc(f);
163
164                 int h = (a << 4) + b;
165
166                 file_crc = log_crc(file_crc, h);
167                 return h;
168         }
169
170         private boolean find_header(InputStream f) throws IOException, FileEndedException {
171                 for (;;) {
172                         if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
173                                 return true;
174                 }
175         } 
176
177         private int get_32(InputStream f)  throws IOException, FileEndedException, NonHexcharException {
178                 int     v = 0;
179                 for (int i = 0; i < 4; i++) {
180                         v += get_hex(f) << (i * 8);
181                 }
182                 return v;
183         }
184
185         private int get_16(InputStream f) throws IOException, FileEndedException, NonHexcharException {
186                 int     v = 0;
187                 for (int i = 0; i < 2; i++) {
188                         v += get_hex(f) << (i * 8);
189                 }
190                 return v;
191         }
192
193         private int swap16(int i) {
194                 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
195         }
196
197         public boolean  crc_valid;
198
199         int mix_in (int high, int low) {
200                 return  high - (high & 0xffff) + low;
201         }
202
203         boolean closer (int target, int a, int b) {
204                 return Math.abs (target - a) < Math.abs(target - b);
205         }
206
207         public double altitude(int i) {
208                 return AltosConvert.pressure_to_altitude(pressures[i]);
209         }
210
211         public String name() {
212                 return name;
213         }
214
215         public Iterable<AltosUIDataPoint> dataPoints() {
216                 return new MicroUIIterable(this);
217         }
218
219         public Iterable<MicroDataPoint> points() {
220                 return new MicroIterable(this);
221         }
222
223         int fact(int n) {
224                 if (n == 0)
225                         return 1;
226                 return n * fact(n-1);
227         }
228
229         int choose(int n, int k) {
230                 return fact(n) / (fact(k) * fact(n-k));
231         }
232
233
234         public double avg_altitude(int center, int dist) {
235                 int     start = center - dist;
236                 int     stop = center + dist;
237
238                 if (start < 0)
239                         start = 0;
240                 if (stop >= pressures.length)
241                         stop = pressures.length - 1;
242
243                 double  sum = 0;
244                 double  div = 0;
245
246                 int     n = dist * 2;
247
248                 for (int i = start; i <= stop; i++) {
249                         int     k = i - (center - dist);
250                         int     c = choose (n, k);
251
252                         sum += c * pressures[i];
253                         div += c;
254                 }
255
256                 double pres = sum / div;
257
258                 double alt = AltosConvert.pressure_to_altitude(pres);
259                 return alt;
260         }
261
262         public double pressure(int i) {
263                 return pressures[i];
264         }
265
266         public double height(int i) {
267                 return altitude(i) - ground_altitude;
268         }
269
270         public double apogee_pressure() {
271                 return min_pressure;
272         }
273
274         public double apogee_altitude() {
275                 return AltosConvert.pressure_to_altitude(apogee_pressure());
276         }
277
278         public double apogee_height() {
279                 return apogee_altitude() - ground_altitude;
280         }
281
282         static final int speed_avg = 3;
283         static final int accel_avg = 5;
284
285         private double avg_speed(int center, int dist) {
286                 if (center == 0)
287                         return 0;
288
289                 double ai = avg_altitude(center, dist);
290                 double aj = avg_altitude(center - 1, dist);
291                 double s = (ai - aj) / time_step;
292
293                 return s;
294         }
295
296         public double speed(int i) {
297                 return avg_speed(i, speed_avg);
298         }
299
300         public double acceleration(int i) {
301                 if (i == 0)
302                         return 0;
303                 return (avg_speed(i, accel_avg) - avg_speed(i-1, accel_avg)) / time_step;
304         }
305
306         public double time(int i) {
307                 return i * time_step;
308         }
309
310         public void save (OutputStream f) throws IOException {
311                 for (int c : bytes)
312                         f.write(c);
313                 f.write('\n');
314         }
315
316         public void export (Writer f) throws IOException {
317                 PrintWriter     pw = new PrintWriter(f);
318                 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");
319                 for (MicroDataPoint point : points()) {
320                         pw.printf("%6.3f,%10.0f,%10.1f,%10.1f,%11.2f,%11.2f,%12.4f,%12.2f,%13.2f,%10.4f\n",
321                                   point.time,
322                                   point.pressure,
323                                   point.height,
324                                   AltosConvert.meters_to_feet(point.height),
325                                   point.speed,
326                                   AltosConvert.meters_to_mph(point.speed),
327                                   AltosConvert.meters_to_mach(point.speed),
328                                   point.accel,
329                                   AltosConvert.meters_to_feet(point.accel),
330                                   AltosConvert.meters_to_g(point.accel));
331                 }
332         }
333
334         public void set_name(String name) {
335                 this.name = name;
336         }
337
338         public MicroData (InputStream f, String name) throws IOException, InterruptedException, NonHexcharException, FileEndedException {
339                 this.name = name;
340                 bytes = new ArrayList<Integer>();
341                 if (!find_header(f))
342                         throw new IOException("No MicroPeak data header found");
343                 try {
344                         file_crc = 0xffff;
345                         ground_pressure = get_32(f);
346                         min_pressure = get_32(f);
347                         int nsamples = get_16(f);
348                         pressures = new int[nsamples + 1];
349
350                         ground_altitude = AltosConvert.pressure_to_altitude(ground_pressure);
351                         int cur = ground_pressure;
352                         pressures[0] = cur;
353                         for (int i = 0; i < nsamples; i++) {
354                                 int     k = get_16(f);
355                                 int     same = mix_in(cur, k);
356                                 int     up = mix_in(cur + 0x10000, k);
357                                 int     down = mix_in(cur - 0x10000, k);
358
359                                 if (closer (cur, same, up)) {
360                                         if (closer (cur, same, down))
361                                                 cur = same;
362                                         else
363                                                 cur = down;
364                                 } else {
365                                         if (closer (cur, up, down))
366                                                 cur = up;
367                                         else
368                                                 cur = down;
369                                 }
370                                 
371                                 pressures[i+1] = cur;
372                         }
373
374                         int current_crc = swap16(~file_crc & 0xffff);
375                         int crc = get_16(f);
376
377                         crc_valid = crc == current_crc;
378
379                         time_step = 0.192;
380                         stats = new MicroStats(this);
381                 } catch (FileEndedException fe) {
382                         throw new IOException("File Ended Unexpectedly");
383                 }
384         }
385
386         public MicroData() {
387                 ground_pressure = 101000;
388                 min_pressure = 101000;
389                 pressures = new int[1];
390                 pressures[0] = 101000;
391         }
392         
393 }