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_voltage(int sensor) {
225 return sensor / 32767.0 * supply * 127/27;
228 static double tele_gps_voltage(int sensor) {
231 return sensor / 32767.0 * supply * (5.6 + 10.0) / 10.0;
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;
240 static double easy_mini_voltage(int sensor, int serial) {
242 double diode_offset = 0.0;
244 /* early prototypes had a 3.0V regulator */
248 /* Purple v1.0 boards had the sensor after the
249 * blocking diode, which drops about 150mV
252 diode_offset = 0.150;
254 return sensor / 32767.0 * supply * 127/27 + diode_offset;
257 public static double radio_to_frequency(int freq, int setting, int cal, int channel) {
265 f = 434.550 * setting / cal;
266 /* Round to nearest 50KHz */
267 f = Math.floor (20.0 * f + 0.5) / 20.0;
269 return f + channel * 0.100;
272 public static int radio_frequency_to_setting(double frequency, int cal) {
273 double set = frequency / 434.550 * cal;
275 return (int) Math.floor (set + 0.5);
278 public static int radio_frequency_to_channel(double frequency) {
279 int channel = (int) Math.floor ((frequency - 434.550) / 0.100 + 0.5);
288 public static double radio_channel_to_frequency(int channel) {
289 return 434.550 + channel * 0.100;
292 public static int[] ParseHex(String line) {
293 String[] tokens = line.split("\\s+");
294 int[] array = new int[tokens.length];
296 for (int i = 0; i < tokens.length; i++)
298 array[i] = Integer.parseInt(tokens[i], 16);
299 } catch (NumberFormatException ne) {
305 public static double meters_to_feet(double meters) {
306 return meters * (100 / (2.54 * 12));
309 public static double feet_to_meters(double feet) {
310 return feet * 12 * 2.54 / 100.0;
313 public static double meters_to_miles(double meters) {
314 return meters_to_feet(meters) / 5280;
317 public static double miles_to_meters(double miles) {
318 return feet_to_meters(miles * 5280);
321 public static double meters_to_mph(double mps) {
322 return meters_to_miles(mps) * 3600;
325 public static double mph_to_meters(double mps) {
326 return miles_to_meters(mps) / 3600;
329 public static double mps_to_fps(double mps) {
330 return meters_to_miles(mps) * 5280;
333 public static double fps_to_mps(double mps) {
334 return miles_to_meters(mps) / 5280;
337 public static double meters_to_mach(double meters) {
338 return meters / 343; /* something close to mach at usual rocket sites */
341 public static double meters_to_g(double meters) {
342 return meters / 9.80665;
345 public static double c_to_f(double c) {
349 public static double f_to_c(double c) {
350 return (c - 32) * 5/9;
353 public static boolean imperial_units = false;
355 public static AltosDistance distance = new AltosDistance();
357 public static AltosHeight height = new AltosHeight();
359 public static AltosSpeed speed = new AltosSpeed();
361 public static AltosAccel accel = new AltosAccel();
363 public static AltosTemperature temperature = new AltosTemperature();
365 public static AltosOrient orient = new AltosOrient();
367 public static AltosVoltage voltage = new AltosVoltage();
369 public static AltosLatitude latitude = new AltosLatitude();
371 public static AltosLongitude longitude = new AltosLongitude();
373 public static String show_gs(String format, double a) {
375 format = format.concat(" g");
376 return String.format(format, a);
379 public static String say_gs(double a) {
380 return String.format("%6.0 gees", meters_to_g(a));
383 public static int checksum(int[] data, int start, int length) {
385 for (int i = 0; i < length; i++)
386 csum += data[i + start];
390 public static double beep_value_to_freq(int value) {
393 return 1.0/2.0 * (24.0e6/32.0) / (double) value;
396 public static int beep_freq_to_value(double freq) {
399 return (int) Math.floor (1.0/2.0 * (24.0e6/32.0) / freq + 0.5);
402 public static final int BEARING_LONG = 0;
403 public static final int BEARING_SHORT = 1;
404 public static final int BEARING_VOICE = 2;
406 public static String bearing_to_words(int length, double bearing) {
407 String [][] bearing_string = {
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"
414 "N", "NNE", "NE", "ENE",
415 "E", "ESE", "SE", "SSE",
416 "S", "SSW", "SW", "WSW",
417 "W", "WNW", "NW", "NNW"
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 "
425 return bearing_string[length][(int)((bearing / 90 * 8 + 1) / 2)%16];