2 * Copyright © 2010 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; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
20 * Sensor data conversion functions
22 package org.altusmetrum.altoslib_11;
24 public class AltosConvert {
26 * Pressure Sensor Model, version 1.1
28 * written by Holly Grimes
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.
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
38 * Height measurements use the local tangent plane. The postive z-direction is up.
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).
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;
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
58 public static final int[] base_altitude = {
59 0, 11000, 20000, 32000, 47000, 51000, 71000
62 /* outputs atmospheric pressure associated with the given altitude.
63 * altitudes are measured with respect to the mean sea level
66 altitude_to_pressure(double altitude)
68 double base_temperature = LAYER0_BASE_TEMPERATURE;
69 double base_pressure = LAYER0_BASE_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 */
77 if (altitude > MAXIMUM_ALTITUDE) /* FIX ME: use sensor data to improve model */
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);
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);
95 base_temperature += delta_z * lapse_rate[layer_number];
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);
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);
116 /* outputs the altitude associated with the given pressure. the altitude
117 returned is measured with respect to the mean sea level */
119 pressure_to_altitude(double pressure)
122 double next_base_temperature = LAYER0_BASE_TEMPERATURE;
123 double next_base_pressure = LAYER0_BASE_PRESSURE;
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
132 int layer_number; /* identifies layer in the atmosphere */
133 int delta_z; /* difference between two altitudes */
135 if (pressure < 0) /* illegal pressure */
137 if (pressure < MINIMUM_PRESSURE) /* FIX ME: use sensor data to improve model */
138 return MAXIMUM_ALTITUDE;
140 /* calculate the base temperature and pressure for the atmospheric layer
141 associated with the inputted pressure. */
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);
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);
159 next_base_temperature += delta_z * lapse_rate[layer_number];
161 while(layer_number < NUMBER_OF_LAYERS - 1 && pressure < next_base_pressure);
163 /* calculate the altitude associated with the inputted pressure */
164 if (lapse_rate[layer_number] == 0.0) {
165 coefficient = (AIR_GAS_CONSTANT / GRAVITATIONAL_ACCELERATION)
167 altitude = base_altitude[layer_number]
168 + coefficient * Math.log(pressure / base_pressure);
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);
183 cc_battery_to_voltage(double battery)
185 return battery / 32767.0 * 5.0;
189 cc_ignitor_to_voltage(double ignite)
191 return ignite / 32767 * 15.0;
195 barometer_to_pressure(double count)
197 return ((count / 16.0) / 2047.0 + 0.095) / 0.009 * 1000.0;
201 thermometer_to_temperature(double thermo)
203 return (thermo - 19791.268) / 32728.0 * 1.25 / 0.00247;
206 static double mega_adc(int raw) {
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;
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;
222 static double tele_mini_3_adc(int raw) {
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;
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;
238 static double tele_mini_2_voltage(int sensor) {
241 return sensor / 32767.0 * supply * 127/27;
244 static double tele_gps_voltage(int sensor) {
247 return sensor / 32767.0 * supply * (5.6 + 10.0) / 10.0;
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;
256 static double easy_mini_voltage(int sensor, int serial) {
258 double diode_offset = 0.0;
260 /* early prototypes had a 3.0V regulator */
264 /* Purple v1.0 boards had the sensor after the
265 * blocking diode, which drops about 150mV
268 diode_offset = 0.150;
270 return sensor / 32767.0 * supply * 127/27 + diode_offset;
273 public static double radio_to_frequency(int freq, int setting, int cal, int channel) {
281 f = 434.550 * setting / cal;
282 /* Round to nearest 50KHz */
283 f = Math.floor (20.0 * f + 0.5) / 20.0;
285 return f + channel * 0.100;
288 public static int radio_frequency_to_setting(double frequency, int cal) {
289 double set = frequency / 434.550 * cal;
291 return (int) Math.floor (set + 0.5);
294 public static int radio_frequency_to_channel(double frequency) {
295 int channel = (int) Math.floor ((frequency - 434.550) / 0.100 + 0.5);
304 public static double radio_channel_to_frequency(int channel) {
305 return 434.550 + channel * 0.100;
308 public static int[] ParseHex(String line) {
309 String[] tokens = line.split("\\s+");
310 int[] array = new int[tokens.length];
312 for (int i = 0; i < tokens.length; i++)
314 array[i] = Integer.parseInt(tokens[i], 16);
315 } catch (NumberFormatException ne) {
321 public static double meters_to_feet(double meters) {
322 return meters * (100 / (2.54 * 12));
325 public static double feet_to_meters(double feet) {
326 return feet * 12 * 2.54 / 100.0;
329 public static double meters_to_miles(double meters) {
330 return meters_to_feet(meters) / 5280;
333 public static double miles_to_meters(double miles) {
334 return feet_to_meters(miles * 5280);
337 public static double meters_to_mph(double mps) {
338 return meters_to_miles(mps) * 3600;
341 public static double mph_to_meters(double mps) {
342 return miles_to_meters(mps) / 3600;
345 public static double mps_to_fps(double mps) {
346 return meters_to_miles(mps) * 5280;
349 public static double fps_to_mps(double mps) {
350 return miles_to_meters(mps) / 5280;
353 public static double meters_to_mach(double meters) {
354 return meters / 343; /* something close to mach at usual rocket sites */
357 public static double meters_to_g(double meters) {
358 return meters / 9.80665;
361 public static double c_to_f(double c) {
365 public static double f_to_c(double c) {
366 return (c - 32) * 5/9;
369 public static double psi_to_pa(double psi) {
370 return psi * 6894.76;
373 public static boolean imperial_units = false;
375 public static AltosDistance distance = new AltosDistance();
377 public static AltosHeight height = new AltosHeight();
379 public static AltosSpeed speed = new AltosSpeed();
381 public static AltosAccel accel = new AltosAccel();
383 public static AltosTemperature temperature = new AltosTemperature();
385 public static AltosOrient orient = new AltosOrient();
387 public static AltosVoltage voltage = new AltosVoltage();
389 public static AltosLatitude latitude = new AltosLatitude();
391 public static AltosLongitude longitude = new AltosLongitude();
393 public static String show_gs(String format, double a) {
395 format = format.concat(" g");
396 return String.format(format, a);
399 public static String say_gs(double a) {
400 return String.format("%6.0 gees", meters_to_g(a));
403 public static int checksum(int[] data, int start, int length) {
405 for (int i = 0; i < length; i++)
406 csum += data[i + start];
410 public static double beep_value_to_freq(int value) {
413 return 1.0/2.0 * (24.0e6/32.0) / (double) value;
416 public static int beep_freq_to_value(double freq) {
419 return (int) Math.floor (1.0/2.0 * (24.0e6/32.0) / freq + 0.5);
422 public static final int BEARING_LONG = 0;
423 public static final int BEARING_SHORT = 1;
424 public static final int BEARING_VOICE = 2;
426 public static String bearing_to_words(int length, double bearing) {
427 String [][] bearing_string = {
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"
434 "N", "NNE", "NE", "ENE",
435 "E", "ESE", "SE", "SSE",
436 "S", "SSW", "SW", "WSW",
437 "W", "WNW", "NW", "NNW"
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 "
445 return bearing_string[length][(int)((bearing / 90 * 8 + 1) / 2)%16];