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; version 2 of the License.
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.
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.
18 exception non_hexchar(int c);
19 exception file_ended();
20 exception invalid_crc();
30 if (!Ctype::isspace((c = File::getc(f))))
38 int c = get_nonwhite(f);
40 if ('0' <= c && c <= '9')
42 if ('a' <= c && c <= 'f')
44 if ('A' <= c && c <= 'F')
52 log_crc(int crc, int byte)
56 for (i = 0; i < 8; i++) {
57 if (((crc & 0x0001) ^ (byte & 0x0001)) != 0)
58 crc = (crc >> 1) ^ POLY;
77 file_crc = log_crc(file_crc, h);
84 while (!File::end(f)) {
85 if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
95 for (int i = 0; i < 4; i++) {
96 v += get_hex(f) << (i * 8);
105 for (int i = 0; i < 2; i++) {
106 v += get_hex(f) << (i * 8);
113 return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
128 log.ground_baro = get_32(f);
129 log.min_baro = get_32(f);
130 int nsamples = get_16(f);
131 log.samples = (int[nsamples]) { [i] = get_16(f) };
133 int current_crc = swap16(~file_crc & 0xffff);
136 if (crc != current_crc)
142 * Pressure Sensor Model, version 1.1
144 * written by Holly Grimes
146 * Uses the International Standard Atmosphere as described in
147 * "A Quick Derivation relating altitude to air pressure" (version 1.03)
148 * from the Portland State Aerospace Society, except that the atmosphere
149 * is divided into layers with each layer having a different lapse rate.
151 * Lapse rate data for each layer was obtained from Wikipedia on Sept. 1, 2007
152 * at site <http://en.wikipedia.org/wiki/International_Standard_Atmosphere
154 * Height measurements use the local tangent plane. The postive z-direction is up.
156 * All measurements are given in SI units (Kelvin, Pascal, meter, meters/second^2).
157 * The lapse rate is given in Kelvin/meter, the gas constant for air is given
158 * in Joules/(kilogram-Kelvin).
161 const real GRAVITATIONAL_ACCELERATION = -9.80665;
162 const real AIR_GAS_CONSTANT = 287.053;
163 const int NUMBER_OF_LAYERS = 7;
164 const real MAXIMUM_ALTITUDE = 84852;
165 const real MINIMUM_PRESSURE = 0.3734;
166 const real LAYER0_BASE_TEMPERATURE = 288.15;
167 const real LAYER0_BASE_PRESSURE = 101325;
169 /* lapse rate and base altitude for each layer in the atmosphere */
170 const real[NUMBER_OF_LAYERS] lapse_rate = {
171 -0.0065, 0.0, 0.001, 0.0028, 0.0, -0.0028, -0.002
173 const int[NUMBER_OF_LAYERS] base_altitude = {
174 0, 11000, 20000, 32000, 47000, 51000, 71000
178 /* outputs atmospheric pressure associated with the given altitude. altitudes
179 are measured with respect to the mean sea level */
180 real altitude_to_pressure(real altitude) {
182 real base_temperature = LAYER0_BASE_TEMPERATURE;
183 real base_pressure = LAYER0_BASE_PRESSURE;
186 real base; /* base for function to determine pressure */
187 real exponent; /* exponent for function to determine pressure */
188 int layer_number; /* identifies layer in the atmosphere */
189 int delta_z; /* difference between two altitudes */
191 if (altitude > MAXIMUM_ALTITUDE) /* FIX ME: use sensor data to improve model */
194 /* calculate the base temperature and pressure for the atmospheric layer
195 associated with the inputted altitude */
196 for(layer_number = 0; layer_number < NUMBER_OF_LAYERS - 1 && altitude > base_altitude[layer_number + 1]; layer_number++) {
197 delta_z = base_altitude[layer_number + 1] - base_altitude[layer_number];
198 if (lapse_rate[layer_number] == 0.0) {
199 exponent = GRAVITATIONAL_ACCELERATION * delta_z
200 / AIR_GAS_CONSTANT / base_temperature;
201 base_pressure *= exp(exponent);
204 base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
205 exponent = GRAVITATIONAL_ACCELERATION /
206 (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
207 base_pressure *= pow(base, exponent);
209 base_temperature += delta_z * lapse_rate[layer_number];
212 /* calculate the pressure at the inputted altitude */
213 delta_z = altitude - base_altitude[layer_number];
214 if (lapse_rate[layer_number] == 0.0) {
215 exponent = GRAVITATIONAL_ACCELERATION * delta_z
216 / AIR_GAS_CONSTANT / base_temperature;
217 pressure = base_pressure * exp(exponent);
220 base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
221 exponent = GRAVITATIONAL_ACCELERATION /
222 (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
223 pressure = base_pressure * pow(base, exponent);
230 /* outputs the altitude associated with the given pressure. the altitude
231 returned is measured with respect to the mean sea level */
232 real pressure_to_altitude(real pressure) {
234 real next_base_temperature = LAYER0_BASE_TEMPERATURE;
235 real next_base_pressure = LAYER0_BASE_PRESSURE;
239 real base_temperature;
240 real base; /* base for function to determine base pressure of next layer */
241 real exponent; /* exponent for function to determine base pressure
244 int layer_number; /* identifies layer in the atmosphere */
245 int delta_z; /* difference between two altitudes */
247 if (pressure < 0) /* illegal pressure */
249 if (pressure < MINIMUM_PRESSURE) /* FIX ME: use sensor data to improve model */
250 return MAXIMUM_ALTITUDE;
252 /* calculate the base temperature and pressure for the atmospheric layer
253 associated with the inputted pressure. */
257 base_pressure = next_base_pressure;
258 base_temperature = next_base_temperature;
259 delta_z = base_altitude[layer_number + 1] - base_altitude[layer_number];
260 if (lapse_rate[layer_number] == 0.0) {
261 exponent = GRAVITATIONAL_ACCELERATION * delta_z
262 / AIR_GAS_CONSTANT / base_temperature;
263 next_base_pressure *= exp(exponent);
266 base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
267 exponent = GRAVITATIONAL_ACCELERATION /
268 (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
269 next_base_pressure *= pow(base, exponent);
271 next_base_temperature += delta_z * lapse_rate[layer_number];
273 while(layer_number < NUMBER_OF_LAYERS - 1 && pressure < next_base_pressure);
275 /* calculate the altitude associated with the inputted pressure */
276 if (lapse_rate[layer_number] == 0.0) {
277 coefficient = (AIR_GAS_CONSTANT / GRAVITATIONAL_ACCELERATION)
279 altitude = base_altitude[layer_number]
280 + coefficient * log(pressure / base_pressure);
283 base = pressure / base_pressure;
284 exponent = AIR_GAS_CONSTANT * lapse_rate[layer_number]
285 / GRAVITATIONAL_ACCELERATION;
286 coefficient = base_temperature / lapse_rate[layer_number];
287 altitude = base_altitude[layer_number]
288 + coefficient * (pow(base, exponent) - 1);
294 real feet_to_meters(real feet)
296 return feet * (12 * 2.54 / 100);
299 real meters_to_feet(real meters)
301 return meters / (12 * 2.54 / 100);
307 real interval = 0.192;
312 printf ("%9.2f %9.1f %d\n", time, pressure_to_altitude(pa) - ground_alt, pa);
317 int mix_in (int high, int low)
319 return high - (high & 0xffff) + low;
322 bool closer (int target, int a, int b)
324 return abs (target - a) < abs(target - b);
328 dump_log(log_t log) {
329 int cur = log.ground_baro;
331 ground_alt = pressure_to_altitude(cur);
333 for (int l = 0; l < dim(log.samples); l++) {
334 int k = log.samples[l];
335 int same = mix_in(cur, k);
336 int up = mix_in(cur + 0x10000, k);
337 int down = mix_in(cur - 0x10000, k);
339 if (closer (cur, same, up)) {
340 if (closer (cur, same, down))
345 if (closer (cur, up, down))
355 log_t log = get_log(stdin);