altos/test: Adjust CRC error rate after FEC fix
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 #include "cc.h"
20 #include <math.h>
21
22 /*
23  * Pressure Sensor Model, version 1.1
24  *
25  * written by Holly Grimes
26  *
27  * Uses the International Standard Atmosphere as described in
28  *   "A Quick Derivation relating altitude to air pressure" (version 1.03)
29  *    from the Portland State Aerospace Society, except that the atmosphere
30  *    is divided into layers with each layer having a different lapse rate.
31  *
32  * Lapse rate data for each layer was obtained from Wikipedia on Sept. 1, 2007
33  *    at site <http://en.wikipedia.org/wiki/International_Standard_Atmosphere
34  *
35  * Height measurements use the local tangent plane.  The postive z-direction is up.
36  *
37  * All measurements are given in SI units (Kelvin, Pascal, meter, meters/second^2).
38  *   The lapse rate is given in Kelvin/meter, the gas constant for air is given
39  *   in Joules/(kilogram-Kelvin).
40  */
41
42 #define GRAVITATIONAL_ACCELERATION -9.80665
43 #define AIR_GAS_CONSTANT        287.053
44 #define NUMBER_OF_LAYERS        7
45 #define MAXIMUM_ALTITUDE        84852.0
46 #define MINIMUM_PRESSURE        0.3734
47 #define LAYER0_BASE_TEMPERATURE 288.15
48 #define LAYER0_BASE_PRESSURE    101325
49
50 /* lapse rate and base altitude for each layer in the atmosphere */
51 static const double lapse_rate[NUMBER_OF_LAYERS] = {
52         -0.0065, 0.0, 0.001, 0.0028, 0.0, -0.0028, -0.002
53 };
54
55 static const int base_altitude[NUMBER_OF_LAYERS] = {
56         0, 11000, 20000, 32000, 47000, 51000, 71000
57 };
58
59 /* outputs atmospheric pressure associated with the given altitude. altitudes
60    are measured with respect to the mean sea level */
61 double
62 cc_altitude_to_pressure(double altitude)
63 {
64
65    double base_temperature = LAYER0_BASE_TEMPERATURE;
66    double base_pressure = LAYER0_BASE_PRESSURE;
67
68    double pressure;
69    double base; /* base for function to determine pressure */
70    double exponent; /* exponent for function to determine pressure */
71    int layer_number; /* identifies layer in the atmosphere */
72    int delta_z; /* difference between two altitudes */
73
74    if (altitude > MAXIMUM_ALTITUDE) /* FIX ME: use sensor data to improve model */
75       return 0;
76
77    /* calculate the base temperature and pressure for the atmospheric layer
78       associated with the inputted altitude */
79    for(layer_number = 0; layer_number < NUMBER_OF_LAYERS - 1 && altitude > base_altitude[layer_number + 1]; layer_number++) {
80       delta_z = base_altitude[layer_number + 1] - base_altitude[layer_number];
81       if (lapse_rate[layer_number] == 0.0) {
82          exponent = GRAVITATIONAL_ACCELERATION * delta_z
83               / AIR_GAS_CONSTANT / base_temperature;
84          base_pressure *= exp(exponent);
85       }
86       else {
87          base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
88          exponent = GRAVITATIONAL_ACCELERATION /
89               (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
90          base_pressure *= pow(base, exponent);
91       }
92       base_temperature += delta_z * lapse_rate[layer_number];
93    }
94
95    /* calculate the pressure at the inputted altitude */
96    delta_z = altitude - base_altitude[layer_number];
97    if (lapse_rate[layer_number] == 0.0) {
98       exponent = GRAVITATIONAL_ACCELERATION * delta_z
99            / AIR_GAS_CONSTANT / base_temperature;
100       pressure = base_pressure * exp(exponent);
101    }
102    else {
103       base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
104       exponent = GRAVITATIONAL_ACCELERATION /
105            (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
106       pressure = base_pressure * pow(base, exponent);
107    }
108
109    return pressure;
110 }
111
112 double
113 cc_altitude_to_temperature(double altitude)
114 {
115
116    double base_temperature = LAYER0_BASE_TEMPERATURE;
117    double temperature;
118
119    int layer_number; /* identifies layer in the atmosphere */
120    double delta_z; /* difference between two altitudes */
121
122    /* calculate the base temperature for the atmospheric layer
123       associated with the inputted altitude */
124    for(layer_number = 0; layer_number < NUMBER_OF_LAYERS - 1 && altitude > base_altitude[layer_number + 1]; layer_number++) {
125       delta_z = base_altitude[layer_number + 1] - base_altitude[layer_number];
126       base_temperature += delta_z * lapse_rate[layer_number];
127    }
128
129    /* calculate the pressure at the inputted altitude */
130    delta_z = altitude - base_altitude[layer_number];
131    temperature = base_temperature + lapse_rate[layer_number] * delta_z;
132
133    return temperature - 273.15;
134 }
135
136 /* outputs the altitude associated with the given pressure. the altitude
137    returned is measured with respect to the mean sea level */
138 double
139 cc_pressure_to_altitude(double pressure)
140 {
141
142    double next_base_temperature = LAYER0_BASE_TEMPERATURE;
143    double next_base_pressure = LAYER0_BASE_PRESSURE;
144
145    double altitude;
146    double base_pressure;
147    double base_temperature;
148    double base; /* base for function to determine base pressure of next layer */
149    double exponent; /* exponent for function to determine base pressure
150                              of next layer */
151    double coefficient;
152    int layer_number; /* identifies layer in the atmosphere */
153    int delta_z; /* difference between two altitudes */
154
155    if (pressure < 0)  /* illegal pressure */
156       return -1;
157    if (pressure < MINIMUM_PRESSURE) /* FIX ME: use sensor data to improve model */
158       return MAXIMUM_ALTITUDE;
159
160    /* calculate the base temperature and pressure for the atmospheric layer
161       associated with the inputted pressure. */
162    layer_number = -1;
163    do {
164       layer_number++;
165       base_pressure = next_base_pressure;
166       base_temperature = next_base_temperature;
167       delta_z = base_altitude[layer_number + 1] - base_altitude[layer_number];
168       if (lapse_rate[layer_number] == 0.0) {
169          exponent = GRAVITATIONAL_ACCELERATION * delta_z
170               / AIR_GAS_CONSTANT / base_temperature;
171          next_base_pressure *= exp(exponent);
172       }
173       else {
174          base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
175          exponent = GRAVITATIONAL_ACCELERATION /
176               (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
177          next_base_pressure *= pow(base, exponent);
178       }
179       next_base_temperature += delta_z * lapse_rate[layer_number];
180    }
181    while(layer_number < NUMBER_OF_LAYERS - 1 && pressure < next_base_pressure);
182
183    /* calculate the altitude associated with the inputted pressure */
184    if (lapse_rate[layer_number] == 0.0) {
185       coefficient = (AIR_GAS_CONSTANT / GRAVITATIONAL_ACCELERATION)
186                                                     * base_temperature;
187       altitude = base_altitude[layer_number]
188                     + coefficient * log(pressure / base_pressure);
189    }
190    else {
191       base = pressure / base_pressure;
192       exponent = AIR_GAS_CONSTANT * lapse_rate[layer_number]
193                                        / GRAVITATIONAL_ACCELERATION;
194       coefficient = base_temperature / lapse_rate[layer_number];
195       altitude = base_altitude[layer_number]
196                       + coefficient * (pow(base, exponent) - 1);
197    }
198
199    return altitude;
200 }
201
202 /*
203  * Values for our MP3H6115A pressure sensor
204  *
205  * From the data sheet:
206  *
207  * Pressure range: 15-115 kPa
208  * Voltage at 115kPa: 2.82
209  * Output scale: 27mV/kPa
210  *
211  *
212  * 27 mV/kPa * 2047 / 3300 counts/mV = 16.75 counts/kPa
213  * 2.82V * 2047 / 3.3 counts/V = 1749 counts/115 kPa
214  */
215
216 double
217 cc_barometer_to_pressure(double count)
218 {
219         return ((count / 16.0) / 2047.0 + 0.095) / 0.009 * 1000.0;
220 }
221
222 double
223 cc_barometer_to_altitude(double baro)
224 {
225         double Pa = cc_barometer_to_pressure(baro);
226         return cc_pressure_to_altitude(Pa);
227 }
228
229 static const double count_per_mss = 27.0;
230
231 double
232 cc_accelerometer_to_acceleration(double accel, double ground_accel)
233 {
234         return (ground_accel - accel) / count_per_mss;
235 }
236
237 /* Value for the CC1111 built-in temperature sensor
238  * Output voltage at 0°C = 0.755V
239  * Coefficient = 0.00247V/°C
240  * Reference voltage = 1.25V
241  *
242  * temp = ((value / 32767) * 1.25 - 0.755) / 0.00247
243  *      = (value - 19791.268) / 32768 * 1.25 / 0.00247
244  */
245
246 double
247 cc_thermometer_to_temperature(double thermo)
248 {
249         return (thermo - 19791.268) / 32728.0 * 1.25 / 0.00247;
250 }
251
252 double
253 cc_battery_to_voltage(double battery)
254 {
255         return battery / 32767.0 * 5.0;
256 }
257
258 double
259 cc_ignitor_to_voltage(double ignite)
260 {
261         return ignite / 32767 * 15.0;
262 }
263
264 static inline double sqr(double a) { return a * a; }
265
266 void
267 cc_great_circle (double start_lat, double start_lon,
268                  double end_lat, double end_lon,
269                  double *dist, double *bearing)
270 {
271         const double rad = M_PI / 180;
272         const double earth_radius = 6371.2 * 1000;      /* in meters */
273         double lat1 = rad * start_lat;
274         double lon1 = rad * -start_lon;
275         double lat2 = rad * end_lat;
276         double lon2 = rad * -end_lon;
277
278 //      double d_lat = lat2 - lat1;
279         double d_lon = lon2 - lon1;
280
281         /* From http://en.wikipedia.org/wiki/Great-circle_distance */
282         double vdn = sqrt(sqr(cos(lat2) * sin(d_lon)) +
283                           sqr(cos(lat1) * sin(lat2) -
284                               sin(lat1) * cos(lat2) * cos(d_lon)));
285         double vdd = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(d_lon);
286         double d = atan2(vdn,vdd);
287         double course;
288
289         if (cos(lat1) < 1e-20) {
290                 if (lat1 > 0)
291                         course = M_PI;
292                 else
293                         course = -M_PI;
294         } else {
295                 if (d < 1e-10)
296                         course = 0;
297                 else
298                         course = acos((sin(lat2)-sin(lat1)*cos(d)) /
299                                       (sin(d)*cos(lat1)));
300                 if (sin(lon2-lon1) > 0)
301                         course = 2 * M_PI-course;
302         }
303         *dist = d * earth_radius;
304         *bearing = course * 180/M_PI;
305 }