Switch from GPLv2 to GPLv2+
[fw/altos] / altoslib / AltosConvert.java
1 /*
2  * Copyright © 2010 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 /*
20  * Sensor data conversion functions
21  */
22 package org.altusmetrum.altoslib_11;
23
24 public class AltosConvert {
25         /*
26          * Pressure Sensor Model, version 1.1
27          *
28          * written by Holly Grimes
29          *
30          * Uses the International Standard Atmosphere as described in
31          *   "A Quick Derivation relating altitude to air pressure" (version 1.03)
32          *    from the Portland State Aerospace Society, except that the atmosphere
33          *    is divided into layers with each layer having a different lapse rate.
34          *
35          * Lapse rate data for each layer was obtained from Wikipedia on Sept. 1, 2007
36          *    at site <http://en.wikipedia.org/wiki/International_Standard_Atmosphere
37          *
38          * Height measurements use the local tangent plane.  The postive z-direction is up.
39          *
40          * All measurements are given in SI units (Kelvin, Pascal, meter, meters/second^2).
41          *   The lapse rate is given in Kelvin/meter, the gas constant for air is given
42          *   in Joules/(kilogram-Kelvin).
43          */
44
45         public static final double GRAVITATIONAL_ACCELERATION = -9.80665;
46         public static final double AIR_GAS_CONSTANT             = 287.053;
47         public static final double NUMBER_OF_LAYERS             = 7;
48         public static final double MAXIMUM_ALTITUDE             = 84852.0;
49         public static final double MINIMUM_PRESSURE             = 0.3734;
50         public static final double LAYER0_BASE_TEMPERATURE      = 288.15;
51         public static final double LAYER0_BASE_PRESSURE = 101325;
52
53         /* lapse rate and base altitude for each layer in the atmosphere */
54         public static final double[] lapse_rate = {
55                 -0.0065, 0.0, 0.001, 0.0028, 0.0, -0.0028, -0.002
56         };
57
58         public static final int[] base_altitude = {
59                 0, 11000, 20000, 32000, 47000, 51000, 71000
60         };
61
62         /* outputs atmospheric pressure associated with the given altitude.
63          * altitudes are measured with respect to the mean sea level
64          */
65         public static double
66         altitude_to_pressure(double altitude)
67         {
68                 double base_temperature = LAYER0_BASE_TEMPERATURE;
69                 double base_pressure = LAYER0_BASE_PRESSURE;
70
71                 double pressure;
72                 double base; /* base for function to determine pressure */
73                 double exponent; /* exponent for function to determine pressure */
74                 int layer_number; /* identifies layer in the atmosphere */
75                 double delta_z; /* difference between two altitudes */
76
77                 if (altitude > MAXIMUM_ALTITUDE) /* FIX ME: use sensor data to improve model */
78                         return 0;
79
80                 /* calculate the base temperature and pressure for the atmospheric layer
81                    associated with the inputted altitude */
82                 for(layer_number = 0; layer_number < NUMBER_OF_LAYERS - 1 && altitude > base_altitude[layer_number + 1]; layer_number++) {
83                         delta_z = base_altitude[layer_number + 1] - base_altitude[layer_number];
84                         if (lapse_rate[layer_number] == 0.0) {
85                                 exponent = GRAVITATIONAL_ACCELERATION * delta_z
86                                         / AIR_GAS_CONSTANT / base_temperature;
87                                 base_pressure *= Math.exp(exponent);
88                         }
89                         else {
90                                 base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
91                                 exponent = GRAVITATIONAL_ACCELERATION /
92                                         (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
93                                 base_pressure *= Math.pow(base, exponent);
94                         }
95                         base_temperature += delta_z * lapse_rate[layer_number];
96                 }
97
98                 /* calculate the pressure at the inputted altitude */
99                 delta_z = altitude - base_altitude[layer_number];
100                 if (lapse_rate[layer_number] == 0.0) {
101                         exponent = GRAVITATIONAL_ACCELERATION * delta_z
102                                 / AIR_GAS_CONSTANT / base_temperature;
103                         pressure = base_pressure * Math.exp(exponent);
104                 }
105                 else {
106                         base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
107                         exponent = GRAVITATIONAL_ACCELERATION /
108                                 (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
109                         pressure = base_pressure * Math.pow(base, exponent);
110                 }
111
112                 return pressure;
113         }
114
115
116 /* outputs the altitude associated with the given pressure. the altitude
117    returned is measured with respect to the mean sea level */
118         public static double
119         pressure_to_altitude(double pressure)
120         {
121
122                 double next_base_temperature = LAYER0_BASE_TEMPERATURE;
123                 double next_base_pressure = LAYER0_BASE_PRESSURE;
124
125                 double altitude;
126                 double base_pressure;
127                 double base_temperature;
128                 double base; /* base for function to determine base pressure of next layer */
129                 double exponent; /* exponent for function to determine base pressure
130                                     of next layer */
131                 double coefficient;
132                 int layer_number; /* identifies layer in the atmosphere */
133                 int delta_z; /* difference between two altitudes */
134
135                 if (pressure < 0)  /* illegal pressure */
136                         return -1;
137                 if (pressure < MINIMUM_PRESSURE) /* FIX ME: use sensor data to improve model */
138                         return MAXIMUM_ALTITUDE;
139
140                 /* calculate the base temperature and pressure for the atmospheric layer
141                    associated with the inputted pressure. */
142                 layer_number = -1;
143                 do {
144                         layer_number++;
145                         base_pressure = next_base_pressure;
146                         base_temperature = next_base_temperature;
147                         delta_z = base_altitude[layer_number + 1] - base_altitude[layer_number];
148                         if (lapse_rate[layer_number] == 0.0) {
149                                 exponent = GRAVITATIONAL_ACCELERATION * delta_z
150                                         / AIR_GAS_CONSTANT / base_temperature;
151                                 next_base_pressure *= Math.exp(exponent);
152                         }
153                         else {
154                                 base = (lapse_rate[layer_number] * delta_z / base_temperature) + 1.0;
155                                 exponent = GRAVITATIONAL_ACCELERATION /
156                                         (AIR_GAS_CONSTANT * lapse_rate[layer_number]);
157                                 next_base_pressure *= Math.pow(base, exponent);
158                         }
159                         next_base_temperature += delta_z * lapse_rate[layer_number];
160                 }
161                 while(layer_number < NUMBER_OF_LAYERS - 1 && pressure < next_base_pressure);
162
163                 /* calculate the altitude associated with the inputted pressure */
164                 if (lapse_rate[layer_number] == 0.0) {
165                         coefficient = (AIR_GAS_CONSTANT / GRAVITATIONAL_ACCELERATION)
166                                 * base_temperature;
167                         altitude = base_altitude[layer_number]
168                                 + coefficient * Math.log(pressure / base_pressure);
169                 }
170                 else {
171                         base = pressure / base_pressure;
172                         exponent = AIR_GAS_CONSTANT * lapse_rate[layer_number]
173                                 / GRAVITATIONAL_ACCELERATION;
174                         coefficient = base_temperature / lapse_rate[layer_number];
175                         altitude = base_altitude[layer_number]
176                                 + coefficient * (Math.pow(base, exponent) - 1);
177                 }
178
179                 return altitude;
180         }
181
182         public static double
183         cc_battery_to_voltage(double battery)
184         {
185                 return battery / 32767.0 * 5.0;
186         }
187
188         public static double
189         cc_ignitor_to_voltage(double ignite)
190         {
191                 return ignite / 32767 * 15.0;
192         }
193
194         public static double
195         barometer_to_pressure(double count)
196         {
197                 return ((count / 16.0) / 2047.0 + 0.095) / 0.009 * 1000.0;
198         }
199
200         static double
201         thermometer_to_temperature(double thermo)
202         {
203                 return (thermo - 19791.268) / 32728.0 * 1.25 / 0.00247;
204         }
205
206         static double mega_adc(int raw) {
207                 return raw / 4095.0;
208         }
209
210         static public double mega_battery_voltage(int v_batt) {
211                 if (v_batt != AltosLib.MISSING)
212                         return 3.3 * mega_adc(v_batt) * (5.6 + 10.0) / 10.0;
213                 return AltosLib.MISSING;
214         }
215
216         static double mega_pyro_voltage(int raw) {
217                 if (raw != AltosLib.MISSING)
218                         return 3.3 * mega_adc(raw) * (100.0 + 27.0) / 27.0;
219                 return AltosLib.MISSING;
220         }
221
222         static double tele_mini_voltage(int sensor) {
223                 double  supply = 3.3;
224
225                 return sensor / 32767.0 * supply * 127/27;
226         }
227
228         static double tele_gps_voltage(int sensor) {
229                 double  supply = 3.3;
230
231                 return sensor / 32767.0 * supply * (5.6 + 10.0) / 10.0;
232         }
233
234         static double tele_bt_3_battery(int raw) {
235                 if (raw == AltosLib.MISSING)
236                         return AltosLib.MISSING;
237                 return 3.3 * mega_adc(raw) * (5.1 + 10.0) / 10.0;
238         }
239
240         static double easy_mini_voltage(int sensor, int serial) {
241                 double  supply = 3.3;
242                 double  diode_offset = 0.0;
243
244                 /* early prototypes had a 3.0V regulator */
245                 if (serial < 1000)
246                         supply = 3.0;
247
248                 /* Purple v1.0 boards had the sensor after the
249                  * blocking diode, which drops about 150mV
250                  */
251                 if (serial < 1665)
252                         diode_offset = 0.150;
253
254                 return sensor / 32767.0 * supply * 127/27 + diode_offset;
255         }
256
257         public static double radio_to_frequency(int freq, int setting, int cal, int channel) {
258                 double  f;
259
260                 if (freq > 0)
261                         f = freq / 1000.0;
262                 else {
263                         if (setting <= 0)
264                                 setting = cal;
265                         f = 434.550 * setting / cal;
266                         /* Round to nearest 50KHz */
267                         f = Math.floor (20.0 * f + 0.5) / 20.0;
268                 }
269                 return f + channel * 0.100;
270         }
271
272         public static int radio_frequency_to_setting(double frequency, int cal) {
273                 double  set = frequency / 434.550 * cal;
274
275                 return (int) Math.floor (set + 0.5);
276         }
277
278         public static int radio_frequency_to_channel(double frequency) {
279                 int     channel = (int) Math.floor ((frequency - 434.550) / 0.100 + 0.5);
280
281                 if (channel < 0)
282                         channel = 0;
283                 if (channel > 9)
284                         channel = 9;
285                 return channel;
286         }
287
288         public static double radio_channel_to_frequency(int channel) {
289                 return 434.550 + channel * 0.100;
290         }
291
292         public static int[] ParseHex(String line) {
293                 String[] tokens = line.split("\\s+");
294                 int[] array = new int[tokens.length];
295
296                 for (int i = 0; i < tokens.length; i++)
297                         try {
298                                 array[i] = Integer.parseInt(tokens[i], 16);
299                         } catch (NumberFormatException ne) {
300                                 return null;
301                         }
302                 return array;
303         }
304
305         public static double meters_to_feet(double meters) {
306                 return meters * (100 / (2.54 * 12));
307         }
308
309         public static double feet_to_meters(double feet) {
310                 return feet * 12 * 2.54 / 100.0;
311         }
312
313         public static double meters_to_miles(double meters) {
314                 return meters_to_feet(meters) / 5280;
315         }
316
317         public static double miles_to_meters(double miles) {
318                 return feet_to_meters(miles * 5280);
319         }
320
321         public static double meters_to_mph(double mps) {
322                 return meters_to_miles(mps) * 3600;
323         }
324
325         public static double mph_to_meters(double mps) {
326                 return miles_to_meters(mps) / 3600;
327         }
328
329         public static double mps_to_fps(double mps) {
330                 return meters_to_miles(mps) * 5280;
331         }
332
333         public static double fps_to_mps(double mps) {
334                 return miles_to_meters(mps) / 5280;
335         }
336
337         public static double meters_to_mach(double meters) {
338                 return meters / 343;            /* something close to mach at usual rocket sites */
339         }
340
341         public static double meters_to_g(double meters) {
342                 return meters / 9.80665;
343         }
344
345         public static double c_to_f(double c) {
346                 return c * 9/5 + 32;
347         }
348
349         public static double f_to_c(double c) {
350                 return (c - 32) * 5/9;
351         }
352
353         public static boolean imperial_units = false;
354
355         public static AltosDistance distance = new AltosDistance();
356
357         public static AltosHeight height = new AltosHeight();
358
359         public static AltosSpeed speed = new AltosSpeed();
360
361         public static AltosAccel accel = new AltosAccel();
362
363         public static AltosTemperature temperature = new AltosTemperature();
364
365         public static AltosOrient orient = new AltosOrient();
366
367         public static AltosVoltage voltage = new AltosVoltage();
368
369         public static AltosLatitude latitude = new AltosLatitude();
370
371         public static AltosLongitude longitude = new AltosLongitude();
372
373         public static String show_gs(String format, double a) {
374                 a = meters_to_g(a);
375                 format = format.concat(" g");
376                 return String.format(format, a);
377         }
378
379         public static String say_gs(double a) {
380                 return String.format("%6.0 gees", meters_to_g(a));
381         }
382
383         public static int checksum(int[] data, int start, int length) {
384                 int     csum = 0x5a;
385                 for (int i = 0; i < length; i++)
386                         csum += data[i + start];
387                 return csum & 0xff;
388         }
389
390         public static double beep_value_to_freq(int value) {
391                 if (value == 0)
392                         return 4000;
393                 return 1.0/2.0 * (24.0e6/32.0) / (double) value;
394         }
395
396         public static int beep_freq_to_value(double freq) {
397                 if (freq == 0)
398                         return 94;
399                 return (int) Math.floor (1.0/2.0 * (24.0e6/32.0) / freq + 0.5);
400         }
401
402         public static final int BEARING_LONG = 0;
403         public static final int BEARING_SHORT = 1;
404         public static final int BEARING_VOICE = 2;
405
406         public static String bearing_to_words(int length, double bearing) {
407                 String [][] bearing_string = {
408                         {
409                                 "North", "North North East", "North East", "East North East",
410                                 "East", "East South East", "South East", "South South East",
411                                 "South", "South South West", "South West", "West South West",
412                                 "West", "West North West", "North West", "North North West"
413                         }, {
414                                 "N", "NNE", "NE", "ENE",
415                                 "E", "ESE", "SE", "SSE",
416                                 "S", "SSW", "SW", "WSW",
417                                 "W", "WNW", "NW", "NNW"
418                         }, {
419                                 "north", "nor nor east", "north east", "east nor east",
420                                 "east", "east sow east", "south east", "sow sow east",
421                                 "south", "sow sow west", "south west", "west sow west",
422                                 "west", "west nor west", "north west", "nor nor west "
423                         }
424                 };
425                 return bearing_string[length][(int)((bearing / 90 * 8 + 1) / 2)%16];
426         }
427 }