2 * Copyright © 2009 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.
22 * Pressure Sensor Model, version 1.1
24 * written by Holly Grimes
26 * Uses the International Standard Atmosphere as described in
27 * "A Quick Derivation relating altitude to air pressure" (version 1.03)
28 * from the Portland State Aerospace Society, except that the atmosphere
29 * is divided into layers with each layer having a different lapse rate.
31 * Lapse rate data for each layer was obtained from Wikipedia on Sept. 1, 2007
32 * at site <http://en.wikipedia.org/wiki/International_Standard_Atmosphere
34 * Height measurements use the local tangent plane. The postive z-direction is up.
36 * All measurements are given in SI units (Kelvin, Pascal, meter, meters/second^2).
37 * The lapse rate is given in Kelvin/meter, the gas constant for air is given
38 * in Joules/(kilogram-Kelvin).
41 #define GRAVITATIONAL_ACCELERATION -9.80665
42 #define AIR_GAS_CONSTANT 287.053
43 #define NUMBER_OF_LAYERS 7
44 #define MAXIMUM_ALTITUDE 84852.0
45 #define MINIMUM_PRESSURE 0.3734
46 #define LAYER0_BASE_TEMPERATURE 288.15
47 #define LAYER0_BASE_PRESSURE 101325
49 /* lapse rate and base altitude for each layer in the atmosphere */
50 static const double lapse_rate[NUMBER_OF_LAYERS] = {
51 -0.0065, 0.0, 0.001, 0.0028, 0.0, -0.0028, -0.002
54 static const int base_altitude[NUMBER_OF_LAYERS] = {
55 0, 11000, 20000, 32000, 47000, 51000, 71000
58 /* outputs atmospheric pressure associated with the given altitude. altitudes
59 are measured with respect to the mean sea level */
61 cc_altitude_to_pressure(double altitude)
64 double base_temperature = LAYER0_BASE_TEMPERATURE;
65 double base_pressure = LAYER0_BASE_PRESSURE;
68 double base; /* base for function to determine pressure */
69 double exponent; /* exponent for function to determine pressure */
70 int layer_number; /* identifies layer in the atmosphere */
71 int delta_z; /* difference between two altitudes */
73 if (altitude > MAXIMUM_ALTITUDE) /* FIX ME: use sensor data to improve model */
76 /* calculate the base temperature and pressure for the atmospheric layer
77 associated with the inputted altitude */
78 for(layer_number = 0; layer_number < NUMBER_OF_LAYERS - 1 && altitude > base_altitude[layer_number + 1]; layer_number++) {
79 delta_z = base_altitude[layer_number + 1] - base_altitude[layer_number];
80 if (lapse_rate[layer_number] == 0.0) {
81 exponent = GRAVITATIONAL_ACCELERATION * delta_z
82 / AIR_GAS_CONSTANT / base_temperature;
83 base_pressure *= exp(exponent);
86 base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
87 exponent = GRAVITATIONAL_ACCELERATION /
88 (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
89 base_pressure *= pow(base, exponent);
91 base_temperature += delta_z * lapse_rate[layer_number];
94 /* calculate the pressure at the inputted altitude */
95 delta_z = altitude - base_altitude[layer_number];
96 if (lapse_rate[layer_number] == 0.0) {
97 exponent = GRAVITATIONAL_ACCELERATION * delta_z
98 / AIR_GAS_CONSTANT / base_temperature;
99 pressure = base_pressure * exp(exponent);
102 base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
103 exponent = GRAVITATIONAL_ACCELERATION /
104 (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
105 pressure = base_pressure * pow(base, exponent);
112 /* outputs the altitude associated with the given pressure. the altitude
113 returned is measured with respect to the mean sea level */
115 cc_pressure_to_altitude(double pressure)
118 double next_base_temperature = LAYER0_BASE_TEMPERATURE;
119 double next_base_pressure = LAYER0_BASE_PRESSURE;
122 double base_pressure;
123 double base_temperature;
124 double base; /* base for function to determine base pressure of next layer */
125 double exponent; /* exponent for function to determine base pressure
128 int layer_number; /* identifies layer in the atmosphere */
129 int delta_z; /* difference between two altitudes */
131 if (pressure < 0) /* illegal pressure */
133 if (pressure < MINIMUM_PRESSURE) /* FIX ME: use sensor data to improve model */
134 return MAXIMUM_ALTITUDE;
136 /* calculate the base temperature and pressure for the atmospheric layer
137 associated with the inputted pressure. */
141 base_pressure = next_base_pressure;
142 base_temperature = next_base_temperature;
143 delta_z = base_altitude[layer_number + 1] - base_altitude[layer_number];
144 if (lapse_rate[layer_number] == 0.0) {
145 exponent = GRAVITATIONAL_ACCELERATION * delta_z
146 / AIR_GAS_CONSTANT / base_temperature;
147 next_base_pressure *= exp(exponent);
150 base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
151 exponent = GRAVITATIONAL_ACCELERATION /
152 (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
153 next_base_pressure *= pow(base, exponent);
155 next_base_temperature += delta_z * lapse_rate[layer_number];
157 while(layer_number < NUMBER_OF_LAYERS - 1 && pressure < next_base_pressure);
159 /* calculate the altitude associated with the inputted pressure */
160 if (lapse_rate[layer_number] == 0.0) {
161 coefficient = (AIR_GAS_CONSTANT / GRAVITATIONAL_ACCELERATION)
163 altitude = base_altitude[layer_number]
164 + coefficient * log(pressure / base_pressure);
167 base = pressure / base_pressure;
168 exponent = AIR_GAS_CONSTANT * lapse_rate[layer_number]
169 / GRAVITATIONAL_ACCELERATION;
170 coefficient = base_temperature / lapse_rate[layer_number];
171 altitude = base_altitude[layer_number]
172 + coefficient * (pow(base, exponent) - 1);
179 * Values for our MP3H6115A pressure sensor
181 * From the data sheet:
183 * Pressure range: 15-115 kPa
184 * Voltage at 115kPa: 2.82
185 * Output scale: 27mV/kPa
188 * 27 mV/kPa * 2047 / 3300 counts/mV = 16.75 counts/kPa
189 * 2.82V * 2047 / 3.3 counts/V = 1749 counts/115 kPa
192 static const double counts_per_kPa = 27 * 2047 / 3300;
193 static const double counts_at_101_3kPa = 1674.0;
196 cc_barometer_to_pressure(double count)
198 return ((count / 16.0) / 2047.0 + 0.095) / 0.009 * 1000.0;
202 cc_barometer_to_altitude(double baro)
204 double Pa = cc_barometer_to_pressure(baro);
205 return cc_pressure_to_altitude(Pa);
208 static const double count_per_mss = 27.0;
211 cc_accelerometer_to_acceleration(double accel, double ground_accel)
213 return (ground_accel - accel) / count_per_mss;
217 cc_thermometer_to_temperature(double thermo)
219 return ((thermo / 32767 * 3.3) - 0.5) / 0.01;
223 cc_battery_to_voltage(double battery)
225 return battery / 32767.0 * 5.0;
229 cc_ignitor_to_voltage(double ignite)
231 return ignite / 32767 * 15.0;
234 static inline double sqr(double a) { return a * a; }
237 cc_great_circle (double start_lat, double start_lon,
238 double end_lat, double end_lon,
239 double *dist, double *bearing)
241 const double rad = M_PI / 180;
242 const double earth_radius = 6371.2 * 1000; /* in meters */
243 double lat1 = rad * start_lat;
244 double lon1 = rad * -start_lon;
245 double lat2 = rad * end_lat;
246 double lon2 = rad * -end_lon;
248 // double d_lat = lat2 - lat1;
249 double d_lon = lon2 - lon1;
251 /* From http://en.wikipedia.org/wiki/Great-circle_distance */
252 double vdn = sqrt(sqr(cos(lat2) * sin(d_lon)) +
253 sqr(cos(lat1) * sin(lat2) -
254 sin(lat1) * cos(lat2) * cos(d_lon)));
255 double vdd = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(d_lon);
256 double d = atan2(vdn,vdd);
259 if (cos(lat1) < 1e-20) {
268 course = acos((sin(lat2)-sin(lat1)*cos(d)) /
270 if (sin(lon2-lon1) > 0)
271 course = 2 * M_PI-course;
273 *dist = d * earth_radius;
274 *bearing = course * 180/M_PI;