3 * Pressure Sensor Model, version 1.1
5 * written by Holly Grimes
7 * Uses the International Standard Atmosphere as described in
8 * "A Quick Derivation relating altitude to air pressure" (version 1.03)
9 * from the Portland State Aerospace Society, except that the atmosphere
10 * is divided into layers with each layer having a different lapse rate.
12 * Lapse rate data for each layer was obtained from Wikipedia on Sept. 1, 2007
13 * at site <http://en.wikipedia.org/wiki/International_Standard_Atmosphere
15 * Height measurements use the local tangent plane. The postive z-direction is up.
17 * All measurements are given in SI units (Kelvin, Pascal, meter, meters/second^2).
18 * The lapse rate is given in Kelvin/meter, the gas constant for air is given
19 * in Joules/(kilogram-Kelvin).
22 const real GRAVITATIONAL_ACCELERATION = -9.80665;
23 const real AIR_GAS_CONSTANT = 287.053;
24 const int NUMBER_OF_LAYERS = 7;
25 const real MAXIMUM_ALTITUDE = 84852;
26 const real MINIMUM_PRESSURE = 0.3734;
27 const real LAYER0_BASE_TEMPERATURE = 288.15;
28 const real LAYER0_BASE_PRESSURE = 101325;
30 /* lapse rate and base altitude for each layer in the atmosphere */
31 const real[NUMBER_OF_LAYERS] lapse_rate = {
32 -0.0065, 0.0, 0.001, 0.0028, 0.0, -0.0028, -0.002
34 const int[NUMBER_OF_LAYERS] base_altitude = {
35 0, 11000, 20000, 32000, 47000, 51000, 71000
39 /* outputs atmospheric pressure associated with the given altitude. altitudes
40 are measured with respect to the mean sea level */
41 real altitude_to_pressure(real altitude) {
43 real base_temperature = LAYER0_BASE_TEMPERATURE;
44 real base_pressure = LAYER0_BASE_PRESSURE;
47 real base; /* base for function to determine pressure */
48 real exponent; /* exponent for function to determine pressure */
49 int layer_number; /* identifies layer in the atmosphere */
50 int delta_z; /* difference between two altitudes */
52 if (altitude > MAXIMUM_ALTITUDE) /* FIX ME: use sensor data to improve model */
55 /* calculate the base temperature and pressure for the atmospheric layer
56 associated with the inputted altitude */
57 for(layer_number = 0; layer_number < NUMBER_OF_LAYERS - 1 && altitude > base_altitude[layer_number + 1]; layer_number++) {
58 delta_z = base_altitude[layer_number + 1] - base_altitude[layer_number];
59 if (lapse_rate[layer_number] == 0.0) {
60 exponent = GRAVITATIONAL_ACCELERATION * delta_z
61 / AIR_GAS_CONSTANT / base_temperature;
62 base_pressure *= exp(exponent);
65 base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
66 exponent = GRAVITATIONAL_ACCELERATION /
67 (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
68 base_pressure *= pow(base, exponent);
70 base_temperature += delta_z * lapse_rate[layer_number];
73 /* calculate the pressure at the inputted altitude */
74 delta_z = altitude - base_altitude[layer_number];
75 if (lapse_rate[layer_number] == 0.0) {
76 exponent = GRAVITATIONAL_ACCELERATION * delta_z
77 / AIR_GAS_CONSTANT / base_temperature;
78 pressure = base_pressure * exp(exponent);
81 base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
82 exponent = GRAVITATIONAL_ACCELERATION /
83 (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
84 pressure = base_pressure * pow(base, exponent);
91 /* outputs the altitude associated with the given pressure. the altitude
92 returned is measured with respect to the mean sea level */
93 real pressure_to_altitude(real pressure) {
95 real next_base_temperature = LAYER0_BASE_TEMPERATURE;
96 real next_base_pressure = LAYER0_BASE_PRESSURE;
100 real base_temperature;
101 real base; /* base for function to determine base pressure of next layer */
102 real exponent; /* exponent for function to determine base pressure
105 int layer_number; /* identifies layer in the atmosphere */
106 int delta_z; /* difference between two altitudes */
108 if (pressure < 0) /* illegal pressure */
110 if (pressure < MINIMUM_PRESSURE) /* FIX ME: use sensor data to improve model */
111 return MAXIMUM_ALTITUDE;
113 /* calculate the base temperature and pressure for the atmospheric layer
114 associated with the inputted pressure. */
118 base_pressure = next_base_pressure;
119 base_temperature = next_base_temperature;
120 delta_z = base_altitude[layer_number + 1] - base_altitude[layer_number];
121 if (lapse_rate[layer_number] == 0.0) {
122 exponent = GRAVITATIONAL_ACCELERATION * delta_z
123 / AIR_GAS_CONSTANT / base_temperature;
124 next_base_pressure *= exp(exponent);
127 base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
128 exponent = GRAVITATIONAL_ACCELERATION /
129 (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
130 next_base_pressure *= pow(base, exponent);
132 next_base_temperature += delta_z * lapse_rate[layer_number];
134 while(layer_number < NUMBER_OF_LAYERS - 1 && pressure < next_base_pressure);
136 /* calculate the altitude associated with the inputted pressure */
137 if (lapse_rate[layer_number] == 0.0) {
138 coefficient = (AIR_GAS_CONSTANT / GRAVITATIONAL_ACCELERATION)
140 altitude = base_altitude[layer_number]
141 + coefficient * log(pressure / base_pressure);
144 base = pressure / base_pressure;
145 exponent = AIR_GAS_CONSTANT * lapse_rate[layer_number]
146 / GRAVITATIONAL_ACCELERATION;
147 coefficient = base_temperature / lapse_rate[layer_number];
148 altitude = base_altitude[layer_number]
149 + coefficient * (pow(base, exponent) - 1);