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 exception non_hexchar(int c);
20 exception file_ended();
21 exception invalid_crc();
31 if (!Ctype::isspace((c = File::getc(f))))
39 int c = get_nonwhite(f);
41 if ('0' <= c && c <= '9')
43 if ('a' <= c && c <= 'f')
45 if ('A' <= c && c <= 'F')
53 log_crc(int crc, int byte)
57 for (i = 0; i < 8; i++) {
58 if (((crc & 0x0001) ^ (byte & 0x0001)) != 0)
59 crc = (crc >> 1) ^ POLY;
78 file_crc = log_crc(file_crc, h);
85 while (!File::end(f)) {
86 if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
96 for (int i = 0; i < 4; i++) {
97 v += get_hex(f) << (i * 8);
106 for (int i = 0; i < 2; i++) {
107 v += get_hex(f) << (i * 8);
114 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
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) };
134 int current_crc = swap16(~file_crc & 0xffff);
137 if (crc != current_crc)
143 * Pressure Sensor Model, version 1.1
145 * written by Holly Grimes
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.
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
155 * Height measurements use the local tangent plane. The postive z-direction is up.
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).
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;
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
174 const int[NUMBER_OF_LAYERS] base_altitude = {
175 0, 11000, 20000, 32000, 47000, 51000, 71000
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) {
183 real base_temperature = LAYER0_BASE_TEMPERATURE;
184 real base_pressure = LAYER0_BASE_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 */
192 if (altitude > MAXIMUM_ALTITUDE) /* FIX ME: use sensor data to improve model */
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);
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);
210 base_temperature += delta_z * lapse_rate[layer_number];
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);
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);
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) {
235 real next_base_temperature = LAYER0_BASE_TEMPERATURE;
236 real next_base_pressure = LAYER0_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
245 int layer_number; /* identifies layer in the atmosphere */
246 int delta_z; /* difference between two altitudes */
248 if (pressure < 0) /* illegal pressure */
250 if (pressure < MINIMUM_PRESSURE) /* FIX ME: use sensor data to improve model */
251 return MAXIMUM_ALTITUDE;
253 /* calculate the base temperature and pressure for the atmospheric layer
254 associated with the inputted pressure. */
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);
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);
272 next_base_temperature += delta_z * lapse_rate[layer_number];
274 while(layer_number < NUMBER_OF_LAYERS - 1 && pressure < next_base_pressure);
276 /* calculate the altitude associated with the inputted pressure */
277 if (lapse_rate[layer_number] == 0.0) {
278 coefficient = (AIR_GAS_CONSTANT / GRAVITATIONAL_ACCELERATION)
280 altitude = base_altitude[layer_number]
281 + coefficient * log(pressure / base_pressure);
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);
295 real feet_to_meters(real feet)
297 return feet * (12 * 2.54 / 100);
300 real meters_to_feet(real meters)
302 return meters / (12 * 2.54 / 100);
308 real interval = 0.192;
313 printf ("%9.2f %9.1f %d\n", time, pressure_to_altitude(pa) - ground_alt, pa);
318 int mix_in (int high, int low)
320 return high - (high & 0xffff) + low;
323 bool closer (int target, int a, int b)
325 return abs (target - a) < abs(target - b);
329 dump_log(log_t log) {
330 int cur = log.ground_baro;
332 ground_alt = pressure_to_altitude(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);
340 if (closer (cur, same, up)) {
341 if (closer (cur, same, down))
346 if (closer (cur, up, down))
356 log_t log = get_log(stdin);