altoslib: Add TeleFireTwo eeprom support
[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_3_adc(int raw) {
223                 return raw / 4095.0;
224         }
225
226         static public double tele_mini_3_battery_voltage(int v_batt) {
227                 if (v_batt != AltosLib.MISSING)
228                         return 3.3 * tele_mini_3_adc(v_batt) * (5.6 + 10.0) / 10.0;
229                 return AltosLib.MISSING;
230         }
231
232         static double tele_mini_3_pyro_voltage(int raw) {
233                 if (raw != AltosLib.MISSING)
234                         return 3.3 * tele_mini_3_adc(raw) * (100.0 + 27.0) / 27.0;
235                 return AltosLib.MISSING;
236         }
237
238         static double tele_mini_2_voltage(int sensor) {
239                 double  supply = 3.3;
240
241                 return sensor / 32767.0 * supply * 127/27;
242         }
243
244         static double tele_gps_voltage(int sensor) {
245                 double  supply = 3.3;
246
247                 return sensor / 32767.0 * supply * (5.6 + 10.0) / 10.0;
248         }
249
250         static double tele_bt_3_battery(int raw) {
251                 if (raw == AltosLib.MISSING)
252                         return AltosLib.MISSING;
253                 return 3.3 * mega_adc(raw) * (5.1 + 10.0) / 10.0;
254         }
255
256         static double easy_mini_voltage(int sensor, int serial) {
257                 double  supply = 3.3;
258                 double  diode_offset = 0.0;
259
260                 /* early prototypes had a 3.0V regulator */
261                 if (serial < 1000)
262                         supply = 3.0;
263
264                 /* Purple v1.0 boards had the sensor after the
265                  * blocking diode, which drops about 150mV
266                  */
267                 if (serial < 1665)
268                         diode_offset = 0.150;
269
270                 return sensor / 32767.0 * supply * 127/27 + diode_offset;
271         }
272
273         public static double radio_to_frequency(int freq, int setting, int cal, int channel) {
274                 double  f;
275
276                 if (freq > 0)
277                         f = freq / 1000.0;
278                 else {
279                         if (setting <= 0)
280                                 setting = cal;
281                         f = 434.550 * setting / cal;
282                         /* Round to nearest 50KHz */
283                         f = Math.floor (20.0 * f + 0.5) / 20.0;
284                 }
285                 return f + channel * 0.100;
286         }
287
288         public static int radio_frequency_to_setting(double frequency, int cal) {
289                 double  set = frequency / 434.550 * cal;
290
291                 return (int) Math.floor (set + 0.5);
292         }
293
294         public static int radio_frequency_to_channel(double frequency) {
295                 int     channel = (int) Math.floor ((frequency - 434.550) / 0.100 + 0.5);
296
297                 if (channel < 0)
298                         channel = 0;
299                 if (channel > 9)
300                         channel = 9;
301                 return channel;
302         }
303
304         public static double radio_channel_to_frequency(int channel) {
305                 return 434.550 + channel * 0.100;
306         }
307
308         public static int[] ParseHex(String line) {
309                 String[] tokens = line.split("\\s+");
310                 int[] array = new int[tokens.length];
311
312                 for (int i = 0; i < tokens.length; i++)
313                         try {
314                                 array[i] = Integer.parseInt(tokens[i], 16);
315                         } catch (NumberFormatException ne) {
316                                 return null;
317                         }
318                 return array;
319         }
320
321         public static double meters_to_feet(double meters) {
322                 return meters * (100 / (2.54 * 12));
323         }
324
325         public static double feet_to_meters(double feet) {
326                 return feet * 12 * 2.54 / 100.0;
327         }
328
329         public static double meters_to_miles(double meters) {
330                 return meters_to_feet(meters) / 5280;
331         }
332
333         public static double miles_to_meters(double miles) {
334                 return feet_to_meters(miles * 5280);
335         }
336
337         public static double meters_to_mph(double mps) {
338                 return meters_to_miles(mps) * 3600;
339         }
340
341         public static double mph_to_meters(double mps) {
342                 return miles_to_meters(mps) / 3600;
343         }
344
345         public static double mps_to_fps(double mps) {
346                 return meters_to_miles(mps) * 5280;
347         }
348
349         public static double fps_to_mps(double mps) {
350                 return miles_to_meters(mps) / 5280;
351         }
352
353         public static double meters_to_mach(double meters) {
354                 return meters / 343;            /* something close to mach at usual rocket sites */
355         }
356
357         public static double meters_to_g(double meters) {
358                 return meters / 9.80665;
359         }
360
361         public static double c_to_f(double c) {
362                 return c * 9/5 + 32;
363         }
364
365         public static double f_to_c(double c) {
366                 return (c - 32) * 5/9;
367         }
368
369         public static double psi_to_pa(double psi) {
370                 return psi * 6894.76;
371         }
372
373         public static boolean imperial_units = false;
374
375         public static AltosDistance distance = new AltosDistance();
376
377         public static AltosHeight height = new AltosHeight();
378
379         public static AltosSpeed speed = new AltosSpeed();
380
381         public static AltosAccel accel = new AltosAccel();
382
383         public static AltosTemperature temperature = new AltosTemperature();
384
385         public static AltosOrient orient = new AltosOrient();
386
387         public static AltosVoltage voltage = new AltosVoltage();
388
389         public static AltosLatitude latitude = new AltosLatitude();
390
391         public static AltosLongitude longitude = new AltosLongitude();
392
393         public static String show_gs(String format, double a) {
394                 a = meters_to_g(a);
395                 format = format.concat(" g");
396                 return String.format(format, a);
397         }
398
399         public static String say_gs(double a) {
400                 return String.format("%6.0 gees", meters_to_g(a));
401         }
402
403         public static int checksum(int[] data, int start, int length) {
404                 int     csum = 0x5a;
405                 for (int i = 0; i < length; i++)
406                         csum += data[i + start];
407                 return csum & 0xff;
408         }
409
410         public static double beep_value_to_freq(int value) {
411                 if (value == 0)
412                         return 4000;
413                 return 1.0/2.0 * (24.0e6/32.0) / (double) value;
414         }
415
416         public static int beep_freq_to_value(double freq) {
417                 if (freq == 0)
418                         return 94;
419                 return (int) Math.floor (1.0/2.0 * (24.0e6/32.0) / freq + 0.5);
420         }
421
422         public static final int BEARING_LONG = 0;
423         public static final int BEARING_SHORT = 1;
424         public static final int BEARING_VOICE = 2;
425
426         public static String bearing_to_words(int length, double bearing) {
427                 String [][] bearing_string = {
428                         {
429                                 "North", "North North East", "North East", "East North East",
430                                 "East", "East South East", "South East", "South South East",
431                                 "South", "South South West", "South West", "West South West",
432                                 "West", "West North West", "North West", "North North West"
433                         }, {
434                                 "N", "NNE", "NE", "ENE",
435                                 "E", "ESE", "SE", "SSE",
436                                 "S", "SSW", "SW", "WSW",
437                                 "W", "WNW", "NW", "NNW"
438                         }, {
439                                 "north", "nor nor east", "north east", "east nor east",
440                                 "east", "east sow east", "south east", "sow sow east",
441                                 "south", "sow sow west", "south west", "west sow west",
442                                 "west", "west nor west", "north west", "nor nor west "
443                         }
444                 };
445                 return bearing_string[length][(int)((bearing / 90 * 8 + 1) / 2)%16];
446         }
447 }