Merge remote branch 'origin/master' into new-packet-format
[fw/altos] / ao-tools / lib / cc-convert.c
1 /*
2  * Copyright © 2009 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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 #include "cc.h"
19 #include <math.h>
20
21 /*
22  * Pressure Sensor Model, version 1.1
23  *
24  * written by Holly Grimes
25  *
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.
30  *
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
33  *
34  * Height measurements use the local tangent plane.  The postive z-direction is up.
35  *
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).
39  */
40
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
48
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
52 };
53
54 static const int base_altitude[NUMBER_OF_LAYERS] = {
55         0, 11000, 20000, 32000, 47000, 51000, 71000
56 };
57
58 /* outputs atmospheric pressure associated with the given altitude. altitudes
59    are measured with respect to the mean sea level */
60 double
61 cc_altitude_to_pressure(double altitude)
62 {
63
64    double base_temperature = LAYER0_BASE_TEMPERATURE;
65    double base_pressure = LAYER0_BASE_PRESSURE;
66
67    double 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 */
72
73    if (altitude > MAXIMUM_ALTITUDE) /* FIX ME: use sensor data to improve model */
74       return 0;
75
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);
84       }
85       else {
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);
90       }
91       base_temperature += delta_z * lapse_rate[layer_number];
92    }
93
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);
100    }
101    else {
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);
106    }
107
108    return pressure;
109 }
110
111
112 /* outputs the altitude associated with the given pressure. the altitude
113    returned is measured with respect to the mean sea level */
114 double
115 cc_pressure_to_altitude(double pressure)
116 {
117
118    double next_base_temperature = LAYER0_BASE_TEMPERATURE;
119    double next_base_pressure = LAYER0_BASE_PRESSURE;
120
121    double altitude;
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
126                              of next layer */
127    double coefficient;
128    int layer_number; /* identifies layer in the atmosphere */
129    int delta_z; /* difference between two altitudes */
130
131    if (pressure < 0)  /* illegal pressure */
132       return -1;
133    if (pressure < MINIMUM_PRESSURE) /* FIX ME: use sensor data to improve model */
134       return MAXIMUM_ALTITUDE;
135
136    /* calculate the base temperature and pressure for the atmospheric layer
137       associated with the inputted pressure. */
138    layer_number = -1;
139    do {
140       layer_number++;
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);
148       }
149       else {
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);
154       }
155       next_base_temperature += delta_z * lapse_rate[layer_number];
156    }
157    while(layer_number < NUMBER_OF_LAYERS - 1 && pressure < next_base_pressure);
158
159    /* calculate the altitude associated with the inputted pressure */
160    if (lapse_rate[layer_number] == 0.0) {
161       coefficient = (AIR_GAS_CONSTANT / GRAVITATIONAL_ACCELERATION)
162                                                     * base_temperature;
163       altitude = base_altitude[layer_number]
164                     + coefficient * log(pressure / base_pressure);
165    }
166    else {
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);
173    }
174
175    return altitude;
176 }
177
178 /*
179  * Values for our MP3H6115A pressure sensor
180  *
181  * From the data sheet:
182  *
183  * Pressure range: 15-115 kPa
184  * Voltage at 115kPa: 2.82
185  * Output scale: 27mV/kPa
186  *
187  *
188  * 27 mV/kPa * 2047 / 3300 counts/mV = 16.75 counts/kPa
189  * 2.82V * 2047 / 3.3 counts/V = 1749 counts/115 kPa
190  */
191
192 static const double counts_per_kPa = 27 * 2047 / 3300;
193 static const double counts_at_101_3kPa = 1674.0;
194
195 double
196 cc_barometer_to_pressure(double count)
197 {
198         return ((count / 16.0) / 2047.0 + 0.095) / 0.009 * 1000.0;
199 }
200
201 double
202 cc_barometer_to_altitude(double baro)
203 {
204         double Pa = cc_barometer_to_pressure(baro);
205         return cc_pressure_to_altitude(Pa);
206 }
207
208 static const double count_per_mss = 27.0;
209
210 double
211 cc_accelerometer_to_acceleration(double accel, double ground_accel)
212 {
213         return (ground_accel - accel) / count_per_mss;
214 }
215
216 /* Value for the CC1111 built-in temperature sensor
217  * Output voltage at 0°C = 0.755V
218  * Coefficient = 0.00247V/°C
219  * Reference voltage = 1.25V
220  *
221  * temp = ((value / 32767) * 1.25 - 0.755) / 0.00247
222  *      = (value - 19791.268) / 32768 * 1.25 / 0.00247
223  */
224
225 double
226 cc_thermometer_to_temperature(double thermo)
227 {
228         return (thermo - 19791.268) / 32728.0 * 1.25 / 0.00247;
229 }
230
231 double
232 cc_battery_to_voltage(double battery)
233 {
234         return battery / 32767.0 * 5.0;
235 }
236
237 double
238 cc_ignitor_to_voltage(double ignite)
239 {
240         return ignite / 32767 * 15.0;
241 }
242
243 static inline double sqr(double a) { return a * a; }
244
245 void
246 cc_great_circle (double start_lat, double start_lon,
247                  double end_lat, double end_lon,
248                  double *dist, double *bearing)
249 {
250         const double rad = M_PI / 180;
251         const double earth_radius = 6371.2 * 1000;      /* in meters */
252         double lat1 = rad * start_lat;
253         double lon1 = rad * -start_lon;
254         double lat2 = rad * end_lat;
255         double lon2 = rad * -end_lon;
256
257 //      double d_lat = lat2 - lat1;
258         double d_lon = lon2 - lon1;
259
260         /* From http://en.wikipedia.org/wiki/Great-circle_distance */
261         double vdn = sqrt(sqr(cos(lat2) * sin(d_lon)) +
262                           sqr(cos(lat1) * sin(lat2) -
263                               sin(lat1) * cos(lat2) * cos(d_lon)));
264         double vdd = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(d_lon);
265         double d = atan2(vdn,vdd);
266         double course;
267
268         if (cos(lat1) < 1e-20) {
269                 if (lat1 > 0)
270                         course = M_PI;
271                 else
272                         course = -M_PI;
273         } else {
274                 if (d < 1e-10)
275                         course = 0;
276                 else
277                         course = acos((sin(lat2)-sin(lat1)*cos(d)) /
278                                       (sin(d)*cos(lat1)));
279                 if (sin(lon2-lon1) > 0)
280                         course = 2 * M_PI-course;
281         }
282         *dist = d * earth_radius;
283         *bearing = course * 180/M_PI;
284 }