altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / micropeak / micro-log-parse.5c
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 exception non_hexchar(int c);
20 exception file_ended();
21 exception invalid_crc();
22
23 int
24 get_nonwhite(file f)
25 {
26         int     c;
27
28         for (;;) {
29                 if (File::end(f))
30                         raise file_ended();
31                 if (!Ctype::isspace((c = File::getc(f))))
32                         return c;
33         }
34 }
35
36 int
37 get_hexc(file f)
38 {
39         int     c = get_nonwhite(f);
40
41         if ('0' <= c && c <= '9')
42                 return c - '0';
43         if ('a' <= c && c <= 'f')
44                 return c - 'a' + 10;
45         if ('A' <= c && c <= 'F')
46                 return c - 'A' + 10;
47         raise non_hexchar(c);
48 }
49
50 int POLY = 0x8408;
51
52 int
53 log_crc(int crc, int byte)
54 {
55         int     i;
56
57         for (i = 0; i < 8; i++) {
58                 if (((crc & 0x0001) ^ (byte & 0x0001)) != 0)
59                         crc = (crc >> 1) ^ POLY;
60                 else
61                         crc = crc >> 1;
62                 byte >>= 1;
63         }
64         return crc & 0xffff;
65 }
66
67 int     file_crc;
68
69
70 int
71 get_hex(file f)
72 {
73         int     a = get_hexc(f);
74         int     b = get_hexc(f);
75
76         int h = (a << 4) + b;
77
78         file_crc = log_crc(file_crc, h);
79         return h;
80 }
81
82 bool
83 find_header(file f)
84 {
85         while (!File::end(f)) {
86                 if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
87                         return true;
88         }
89         return false;
90 }
91
92 int
93 get_32(file f)
94 {
95         int     v = 0;
96         for (int i = 0; i < 4; i++) {
97                 v += get_hex(f) << (i * 8);
98         }
99         return v;
100 }
101
102 int
103 get_16(file f)
104 {
105         int     v = 0;
106         for (int i = 0; i < 2; i++) {
107                 v += get_hex(f) << (i * 8);
108         }
109         return v;
110 }
111
112 int
113 swap16(int i) {
114         return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
115 }
116 typedef struct {
117         int     ground_baro;
118         int     min_baro;
119         int[*]  samples;
120 } log_t;
121
122 log_t
123 get_log(file f) {
124         log_t   log;
125
126         if (!find_header(f))
127                 raise file_ended();
128         file_crc = 0xffff;
129         log.ground_baro = get_32(f);
130         log.min_baro = get_32(f);
131         int nsamples = get_16(f);
132         log.samples = (int[nsamples]) { [i] = get_16(f) };
133
134         int current_crc = swap16(~file_crc & 0xffff);
135         int crc = get_16(f);
136
137         if (crc != current_crc)
138                 raise invalid_crc();
139         return log;
140 }
141
142 /*
143  * Pressure Sensor Model, version 1.1
144  *
145  * written by Holly Grimes
146  *
147  * Uses the International Standard Atmosphere as described in
148  *   "A Quick Derivation relating altitude to air pressure" (version 1.03)
149  *    from the Portland State Aerospace Society, except that the atmosphere
150  *    is divided into layers with each layer having a different lapse rate.
151  *
152  * Lapse rate data for each layer was obtained from Wikipedia on Sept. 1, 2007
153  *    at site <http://en.wikipedia.org/wiki/International_Standard_Atmosphere
154  *
155  * Height measurements use the local tangent plane.  The postive z-direction is up.
156  *
157  * All measurements are given in SI units (Kelvin, Pascal, meter, meters/second^2).
158  *   The lapse rate is given in Kelvin/meter, the gas constant for air is given
159  *   in Joules/(kilogram-Kelvin).
160  */
161
162 const real GRAVITATIONAL_ACCELERATION = -9.80665;
163 const real AIR_GAS_CONSTANT = 287.053;
164 const int NUMBER_OF_LAYERS = 7;
165 const real MAXIMUM_ALTITUDE = 84852;
166 const real MINIMUM_PRESSURE = 0.3734;
167 const real LAYER0_BASE_TEMPERATURE = 288.15;
168 const real LAYER0_BASE_PRESSURE = 101325;
169
170 /* lapse rate and base altitude for each layer in the atmosphere */
171 const real[NUMBER_OF_LAYERS] lapse_rate = {
172         -0.0065, 0.0, 0.001, 0.0028, 0.0, -0.0028, -0.002
173 };
174 const int[NUMBER_OF_LAYERS] base_altitude = {
175         0, 11000, 20000, 32000, 47000, 51000, 71000
176 };
177
178
179 /* outputs atmospheric pressure associated with the given altitude. altitudes
180    are measured with respect to the mean sea level */
181 real altitude_to_pressure(real altitude) {
182
183    real base_temperature = LAYER0_BASE_TEMPERATURE;
184    real base_pressure = LAYER0_BASE_PRESSURE;
185
186    real pressure;
187    real base; /* base for function to determine pressure */
188    real exponent; /* exponent for function to determine pressure */
189    int layer_number; /* identifies layer in the atmosphere */
190    int delta_z; /* difference between two altitudes */
191
192    if (altitude > MAXIMUM_ALTITUDE) /* FIX ME: use sensor data to improve model */
193       return 0;
194
195    /* calculate the base temperature and pressure for the atmospheric layer
196       associated with the inputted altitude */
197    for(layer_number = 0; layer_number < NUMBER_OF_LAYERS - 1 && altitude > base_altitude[layer_number + 1]; layer_number++) {
198       delta_z = base_altitude[layer_number + 1] - base_altitude[layer_number];
199       if (lapse_rate[layer_number] == 0.0) {
200          exponent = GRAVITATIONAL_ACCELERATION * delta_z
201               / AIR_GAS_CONSTANT / base_temperature;
202          base_pressure *= exp(exponent);
203       }
204       else {
205          base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
206          exponent = GRAVITATIONAL_ACCELERATION /
207               (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
208          base_pressure *= pow(base, exponent);
209       }
210       base_temperature += delta_z * lapse_rate[layer_number];
211    }
212
213    /* calculate the pressure at the inputted altitude */
214    delta_z = altitude - base_altitude[layer_number];
215    if (lapse_rate[layer_number] == 0.0) {
216       exponent = GRAVITATIONAL_ACCELERATION * delta_z
217            / AIR_GAS_CONSTANT / base_temperature;
218       pressure = base_pressure * exp(exponent);
219    }
220    else {
221       base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
222       exponent = GRAVITATIONAL_ACCELERATION /
223            (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
224       pressure = base_pressure * pow(base, exponent);
225    }
226
227    return pressure;
228 }
229
230
231 /* outputs the altitude associated with the given pressure. the altitude
232    returned is measured with respect to the mean sea level */
233 real pressure_to_altitude(real pressure) {
234
235    real next_base_temperature = LAYER0_BASE_TEMPERATURE;
236    real next_base_pressure = LAYER0_BASE_PRESSURE;
237
238    real altitude;
239    real base_pressure;
240    real base_temperature;
241    real base; /* base for function to determine base pressure of next layer */
242    real exponent; /* exponent for function to determine base pressure
243                              of next layer */
244    real coefficient;
245    int layer_number; /* identifies layer in the atmosphere */
246    int delta_z; /* difference between two altitudes */
247
248    if (pressure < 0)  /* illegal pressure */
249       return -1;
250    if (pressure < MINIMUM_PRESSURE) /* FIX ME: use sensor data to improve model */
251       return MAXIMUM_ALTITUDE;
252
253    /* calculate the base temperature and pressure for the atmospheric layer
254       associated with the inputted pressure. */
255    layer_number = -1;
256    do {
257       layer_number++;
258       base_pressure = next_base_pressure;
259       base_temperature = next_base_temperature;
260       delta_z = base_altitude[layer_number + 1] - base_altitude[layer_number];
261       if (lapse_rate[layer_number] == 0.0) {
262          exponent = GRAVITATIONAL_ACCELERATION * delta_z
263               / AIR_GAS_CONSTANT / base_temperature;
264          next_base_pressure *= exp(exponent);
265       }
266       else {
267          base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
268          exponent = GRAVITATIONAL_ACCELERATION /
269               (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
270          next_base_pressure *= pow(base, exponent);
271       }
272       next_base_temperature += delta_z * lapse_rate[layer_number];
273    }
274    while(layer_number < NUMBER_OF_LAYERS - 1 && pressure < next_base_pressure);
275
276    /* calculate the altitude associated with the inputted pressure */
277    if (lapse_rate[layer_number] == 0.0) {
278       coefficient = (AIR_GAS_CONSTANT / GRAVITATIONAL_ACCELERATION)
279                                                     * base_temperature;
280       altitude = base_altitude[layer_number]
281                     + coefficient * log(pressure / base_pressure);
282    }
283    else {
284       base = pressure / base_pressure;
285       exponent = AIR_GAS_CONSTANT * lapse_rate[layer_number]
286                                        / GRAVITATIONAL_ACCELERATION;
287       coefficient = base_temperature / lapse_rate[layer_number];
288       altitude = base_altitude[layer_number]
289                       + coefficient * (pow(base, exponent) - 1);
290    }
291
292    return altitude;
293 }
294
295 real feet_to_meters(real feet)
296 {
297     return feet * (12 * 2.54 / 100);
298 }
299
300 real meters_to_feet(real meters)
301 {
302     return meters / (12 * 2.54 / 100);
303 }
304
305
306 real    time = 0;
307 int     sample = 0;
308 real    interval = 0.192;
309 real    ground_alt = 0;
310
311 void show(int pa)
312 {
313         printf ("%9.2f %9.1f %d\n", time, pressure_to_altitude(pa) - ground_alt, pa);
314         sample++;
315         time += interval;
316 }
317
318 int mix_in (int high, int low)
319 {
320         return  high - (high & 0xffff) + low;
321 }
322
323 bool closer (int target, int a, int b)
324 {
325         return abs (target - a) < abs(target - b);
326 }
327
328 void
329 dump_log(log_t log) {
330         int cur = log.ground_baro;
331
332         ground_alt = pressure_to_altitude(cur);
333         show(cur);
334         for (int l = 0; l < dim(log.samples); l++) {
335                 int     k = log.samples[l];
336                 int     same = mix_in(cur, k);
337                 int     up = mix_in(cur + 0x10000, k);
338                 int     down = mix_in(cur - 0x10000, k);
339
340                 if (closer (cur, same, up)) {
341                         if (closer (cur, same, down))
342                                 cur = same;
343                         else
344                                 cur = down;
345                 } else {
346                         if (closer (cur, up, down))
347                                 cur = up;
348                         else
349                                 cur = down;
350                 }
351                 show(cur);
352         }
353 }
354
355
356 log_t log = get_log(stdin);
357 dump_log(log);