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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 * Track flight state from telemetry or eeprom data stream
22 package org.altusmetrum.altoslib_3;
24 public class AltosState implements Cloneable {
26 public static final int set_position = 1;
27 public static final int set_gps = 2;
28 public static final int set_data = 4;
32 static final double ascent_filter_len = 0.5;
33 static final double descent_filter_len = 0.5;
37 public long received_time;
40 public double prev_time;
41 public double time_change;
43 private int prev_tick;
44 public int boost_tick;
48 private double prev_value;
49 private double max_value;
50 private double set_time;
51 private double prev_set_time;
53 void set(double new_value, double time) {
54 if (new_value != AltosLib.MISSING) {
56 if (max_value == AltosLib.MISSING || value > max_value) {
63 void set_filtered(double new_value, double time) {
64 if (prev_value != AltosLib.MISSING)
65 new_value = (prev_value * 15.0 + new_value) / 16.0;
82 if (value != AltosLib.MISSING && prev_value != AltosLib.MISSING)
83 return value - prev_value;
84 return AltosLib.MISSING;
89 double t = set_time - prev_set_time;
91 if (c != AltosLib.MISSING && t != 0)
93 return AltosLib.MISSING;
97 if (value == AltosLib.MISSING)
98 return AltosLib.MISSING;
99 if (prev_value == AltosLib.MISSING)
100 return AltosLib.MISSING;
102 return (value + prev_value) / 2 * (set_time - prev_set_time);
109 void set_derivative(AltosValue in) {
110 double n = in.rate();
112 if (n == AltosLib.MISSING)
115 double p = prev_value;
116 double pt = prev_set_time;
118 if (p == AltosLib.MISSING) {
120 pt = in.time() - 0.01;
123 /* Clip changes to reduce noise */
124 double ddt = in.time() - pt;
125 double ddv = (n - p) / ddt;
127 final double max = 100000;
130 if (Math.abs(ddv) > max) {
140 filter_len = ascent_filter_len;
142 filter_len = descent_filter_len;
144 double f = 1/Math.exp(ddt/ filter_len);
145 n = p * f + n * (1-f);
150 void set_integral(AltosValue in) {
151 double change = in.integrate();
153 if (change != AltosLib.MISSING) {
154 double prev = prev_value;
155 if (prev == AltosLib.MISSING)
157 set(prev + change, in.time());
161 void copy(AltosValue old) {
163 set_time = old.set_time;
164 prev_value = old.value;
165 prev_set_time = old.set_time;
166 max_value = old.max_value;
169 void finish_update() {
171 prev_set_time = set_time;
175 value = AltosLib.MISSING;
176 prev_value = AltosLib.MISSING;
177 max_value = AltosLib.MISSING;
186 double v = measured.value();
187 if (v != AltosLib.MISSING)
189 return computed.value();
192 boolean is_measured() {
193 return measured.value() != AltosLib.MISSING;
197 double m = measured.max();
199 if (m != AltosLib.MISSING)
201 return computed.max();
204 double prev_value() {
205 if (measured.value != AltosLib.MISSING && measured.prev_value != AltosLib.MISSING)
206 return measured.prev_value;
207 return computed.prev_value;
210 AltosValue altos_value() {
211 if (measured.value() != AltosLib.MISSING)
217 double c = measured.change();
218 if (c == AltosLib.MISSING)
219 c = computed.change();
224 double r = measured.rate();
225 if (r == AltosLib.MISSING)
230 void set_measured(double new_value, double time) {
231 measured.set(new_value, time);
234 void set_computed(double new_value, double time) {
235 computed.set(new_value, time);
238 void set_derivative(AltosValue in) {
239 computed.set_derivative(in);
242 void set_derivative(AltosCValue in) {
243 set_derivative(in.altos_value());
246 void set_integral(AltosValue in) {
247 computed.set_integral(in);
250 void set_integral(AltosCValue in) {
251 set_integral(in.altos_value());
254 void copy(AltosCValue old) {
255 measured.copy(old.measured);
256 computed.copy(old.computed);
259 void finish_update() {
260 measured.finish_update();
261 computed.finish_update();
265 measured = new AltosValue();
266 computed = new AltosValue();
273 public int receiver_serial;
274 public boolean landed;
275 public boolean ascent; /* going up? */
276 public boolean boost; /* under power */
279 public int device_type;
280 public int config_major;
281 public int config_minor;
282 public int apogee_delay;
283 public int main_deploy;
284 public int flight_log_max;
286 private double pressure_to_altitude(double p) {
287 if (p == AltosLib.MISSING)
288 return AltosLib.MISSING;
289 return AltosConvert.pressure_to_altitude(p);
292 private AltosCValue ground_altitude;
294 public double ground_altitude() {
295 return ground_altitude.value();
298 public void set_ground_altitude(double a) {
299 ground_altitude.set_measured(a, time);
302 class AltosGroundPressure extends AltosCValue {
303 void set_filtered(double p, double time) {
304 computed.set_filtered(p, time);
306 ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
309 void set_measured(double p, double time) {
310 super.set_measured(p, time);
311 ground_altitude.set_computed(pressure_to_altitude(p), time);
315 private AltosGroundPressure ground_pressure;
317 public double ground_pressure() {
318 return ground_pressure.value();
321 public void set_ground_pressure (double pressure) {
322 ground_pressure.set_measured(pressure, time);
325 class AltosAltitude extends AltosCValue {
327 private void set_speed(AltosValue v) {
328 if (!acceleration.is_measured() || !ascent)
329 speed.set_derivative(this);
332 void set_computed(double a, double time) {
333 super.set_computed(a,time);
338 void set_measured(double a, double time) {
339 super.set_measured(a,time);
345 private AltosAltitude altitude;
347 public double altitude() {
348 double a = altitude.value();
349 if (a != AltosLib.MISSING)
353 return AltosLib.MISSING;
356 public double max_altitude() {
357 double a = altitude.max();
358 if (a != AltosLib.MISSING)
360 return AltosLib.MISSING;
363 public void set_altitude(double new_altitude) {
364 altitude.set_measured(new_altitude, time);
367 class AltosPressure extends AltosValue {
368 void set(double p, double time) {
370 if (state == AltosLib.ao_flight_pad)
371 ground_pressure.set_filtered(p, time);
372 double a = pressure_to_altitude(p);
373 altitude.set_computed(a, time);
377 private AltosPressure pressure;
379 public double pressure() {
380 return pressure.value();
383 public void set_pressure(double p) {
384 pressure.set(p, time);
387 public double height() {
388 double k = kalman_height.value();
389 if (k != AltosLib.MISSING)
392 double a = altitude();
393 double g = ground_altitude();
394 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
396 return AltosLib.MISSING;
399 public double max_height() {
400 double k = kalman_height.max();
401 if (k != AltosLib.MISSING)
404 double a = altitude.max();
405 double g = ground_altitude();
406 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
408 return AltosLib.MISSING;
411 class AltosSpeed extends AltosCValue {
414 acceleration.set_derivative(this);
417 void set_derivative(AltosCValue in) {
418 super.set_derivative(in);
422 void set_computed(double new_value, double time) {
423 super.set_computed(new_value, time);
427 void set_measured(double new_value, double time) {
428 super.set_measured(new_value, time);
433 private AltosSpeed speed;
435 public double speed() {
436 double v = kalman_speed.value();
437 if (v != AltosLib.MISSING)
439 return speed.value();
442 public double max_speed() {
443 double v = kalman_speed.max();
444 if (v != AltosLib.MISSING)
449 class AltosAccel extends AltosCValue {
450 void set_measured(double a, double time) {
451 super.set_measured(a, time);
453 speed.set_integral(this.measured);
457 AltosAccel acceleration;
459 public double acceleration() {
460 return acceleration.value();
463 public double max_acceleration() {
464 return acceleration.max();
467 public AltosValue orient;
469 public void set_orient(double new_orient) {
470 orient.set(new_orient, time);
473 public double orient() {
474 return orient.value();
477 public double max_orient() {
481 public AltosValue kalman_height, kalman_speed, kalman_acceleration;
483 public void set_kalman(double height, double speed, double acceleration) {
484 kalman_height.set(height, time);
485 kalman_speed.set(speed, time);
486 kalman_acceleration.set(acceleration, time);
489 public double battery_voltage;
490 public double pyro_voltage;
491 public double temperature;
492 public double apogee_voltage;
493 public double main_voltage;
495 public double ignitor_voltage[];
498 public AltosGPS temp_gps;
499 public int temp_gps_sat_tick;
500 public boolean gps_pending;
501 public int gps_sequence;
506 public static final int MIN_PAD_SAMPLES = 10;
509 public int gps_waiting;
510 public boolean gps_ready;
514 public AltosGreatCircle from_pad;
515 public double elevation; /* from pad */
516 public double range; /* total distance */
518 public double gps_height;
520 public double pad_lat, pad_lon, pad_alt;
522 public int speak_tick;
523 public double speak_altitude;
525 public String callsign;
526 public String firmware_version;
528 public double accel_plus_g;
529 public double accel_minus_g;
531 public double ground_accel;
532 public double ground_accel_avg;
534 public int log_format;
536 public AltosMs5607 baro;
538 public AltosCompanion companion;
540 public void set_npad(int npad) {
542 gps_waiting = MIN_PAD_SAMPLES - npad;
543 if (this.gps_waiting < 0)
545 gps_ready = gps_waiting == 0;
551 received_time = System.currentTimeMillis();
552 time = AltosLib.MISSING;
553 time_change = AltosLib.MISSING;
554 prev_time = AltosLib.MISSING;
555 tick = AltosLib.MISSING;
556 prev_tick = AltosLib.MISSING;
557 boost_tick = AltosLib.MISSING;
558 state = AltosLib.ao_flight_invalid;
559 flight = AltosLib.MISSING;
562 rssi = AltosLib.MISSING;
564 device_type = AltosLib.MISSING;
565 config_major = AltosLib.MISSING;
566 config_minor = AltosLib.MISSING;
567 apogee_delay = AltosLib.MISSING;
568 main_deploy = AltosLib.MISSING;
569 flight_log_max = AltosLib.MISSING;
571 ground_altitude = new AltosCValue();
572 ground_pressure = new AltosGroundPressure();
573 altitude = new AltosAltitude();
574 pressure = new AltosPressure();
575 speed = new AltosSpeed();
576 acceleration = new AltosAccel();
577 orient = new AltosValue();
579 temperature = AltosLib.MISSING;
580 battery_voltage = AltosLib.MISSING;
581 pyro_voltage = AltosLib.MISSING;
582 apogee_voltage = AltosLib.MISSING;
583 main_voltage = AltosLib.MISSING;
584 ignitor_voltage = null;
586 kalman_height = new AltosValue();
587 kalman_speed = new AltosValue();
588 kalman_acceleration = new AltosValue();
592 temp_gps_sat_tick = 0;
603 elevation = AltosLib.MISSING;
604 range = AltosLib.MISSING;
605 gps_height = AltosLib.MISSING;
607 pad_lat = AltosLib.MISSING;
608 pad_lon = AltosLib.MISSING;
609 pad_alt = AltosLib.MISSING;
611 speak_tick = AltosLib.MISSING;
612 speak_altitude = AltosLib.MISSING;
616 accel_plus_g = AltosLib.MISSING;
617 accel_minus_g = AltosLib.MISSING;
618 accel = AltosLib.MISSING;
620 ground_accel = AltosLib.MISSING;
621 ground_accel_avg = AltosLib.MISSING;
623 log_format = AltosLib.MISSING;
624 serial = AltosLib.MISSING;
625 receiver_serial = AltosLib.MISSING;
631 void finish_update() {
634 ground_altitude.finish_update();
635 altitude.finish_update();
636 pressure.finish_update();
637 speed.finish_update();
638 acceleration.finish_update();
639 orient.finish_update();
641 kalman_height.finish_update();
642 kalman_speed.finish_update();
643 kalman_acceleration.finish_update();
646 void copy(AltosState old) {
653 received_time = old.received_time;
655 time_change = old.time_change;
656 prev_time = old.time;
659 prev_tick = old.tick;
660 boost_tick = old.boost_tick;
669 device_type = old.device_type;
670 config_major = old.config_major;
671 config_minor = old.config_minor;
672 apogee_delay = old.apogee_delay;
673 main_deploy = old.main_deploy;
674 flight_log_max = old.flight_log_max;
678 ground_pressure.copy(old.ground_pressure);
679 ground_altitude.copy(old.ground_altitude);
680 altitude.copy(old.altitude);
681 pressure.copy(old.pressure);
682 speed.copy(old.speed);
683 acceleration.copy(old.acceleration);
684 orient.copy(old.orient);
686 battery_voltage = old.battery_voltage;
687 pyro_voltage = old.pyro_voltage;
688 temperature = old.temperature;
689 apogee_voltage = old.apogee_voltage;
690 main_voltage = old.main_voltage;
691 ignitor_voltage = old.ignitor_voltage;
693 kalman_height.copy(old.kalman_height);
694 kalman_speed.copy(old.kalman_speed);
695 kalman_acceleration.copy(old.kalman_acceleration);
698 gps = old.gps.clone();
701 if (old.temp_gps != null)
702 temp_gps = old.temp_gps.clone();
705 temp_gps_sat_tick = old.temp_gps_sat_tick;
706 gps_sequence = old.gps_sequence;
707 gps_pending = old.gps_pending;
710 imu = old.imu.clone();
715 mag = old.mag.clone();
720 gps_waiting = old.gps_waiting;
721 gps_ready = old.gps_ready;
724 if (old.from_pad != null)
725 from_pad = old.from_pad.clone();
729 elevation = old.elevation;
732 gps_height = old.gps_height;
733 pad_lat = old.pad_lat;
734 pad_lon = old.pad_lon;
735 pad_alt = old.pad_alt;
737 speak_tick = old.speak_tick;
738 speak_altitude = old.speak_altitude;
740 callsign = old.callsign;
742 accel_plus_g = old.accel_plus_g;
743 accel_minus_g = old.accel_minus_g;
745 ground_accel = old.ground_accel;
746 ground_accel_avg = old.ground_accel_avg;
748 log_format = old.log_format;
750 receiver_serial = old.receiver_serial;
753 companion = old.companion;
767 if (gps.locked && gps.nsat >= 4) {
768 /* Track consecutive 'good' gps reports, waiting for 10 of them */
769 if (state == AltosLib.ao_flight_pad) {
771 if (pad_lat != AltosLib.MISSING) {
772 pad_lat = (pad_lat * 31 + gps.lat) / 32;
773 pad_lon = (pad_lon * 31 + gps.lon) / 32;
774 pad_alt = (pad_alt * 31 + gps.alt) / 32;
777 if (pad_lat == AltosLib.MISSING) {
783 if (gps.lat != 0 && gps.lon != 0 &&
784 pad_lat != AltosLib.MISSING &&
785 pad_lon != AltosLib.MISSING)
789 if (h == AltosLib.MISSING)
791 from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
792 elevation = from_pad.elevation;
793 range = from_pad.range;
794 gps_height = gps.alt - pad_alt;
798 public void set_tick(int new_tick) {
799 if (new_tick != AltosLib.MISSING) {
800 if (prev_tick != AltosLib.MISSING) {
801 while (new_tick < prev_tick - 1000) {
807 time_change = time - prev_time;
811 public void set_boost_tick(int boost_tick) {
812 if (boost_tick != AltosLib.MISSING)
813 this.boost_tick = boost_tick;
816 public String state_name() {
817 return AltosLib.state_name(state);
820 public void set_state(int state) {
821 if (state != AltosLib.ao_flight_invalid) {
823 ascent = (AltosLib.ao_flight_boost <= state &&
824 state <= AltosLib.ao_flight_coast);
825 boost = (AltosLib.ao_flight_boost == state);
830 public void set_device_type(int device_type) {
831 this.device_type = device_type;
834 public void set_config(int major, int minor, int apogee_delay, int main_deploy, int flight_log_max) {
835 config_major = major;
836 config_minor = minor;
837 this.apogee_delay = apogee_delay;
838 this.main_deploy = main_deploy;
839 this.flight_log_max = flight_log_max;
842 public void set_callsign(String callsign) {
843 this.callsign = callsign;
846 public void set_firmware_version(String version) {
847 firmware_version = version;
850 public void set_flight(int flight) {
852 /* When the flight changes, reset the state */
853 if (flight != AltosLib.MISSING && flight != 0) {
854 if (this.flight != AltosLib.MISSING &&
855 this.flight != flight) {
860 this.flight = flight;
864 public void set_serial(int serial) {
865 /* When the serial changes, reset the state */
866 if (serial != AltosLib.MISSING) {
867 if (this.serial != AltosLib.MISSING &&
868 this.serial != serial) {
873 this.serial = serial;
877 public void set_receiver_serial(int serial) {
878 if (serial != AltosLib.MISSING)
879 receiver_serial = serial;
883 if (rssi == AltosLib.MISSING)
888 public void set_rssi(int rssi, int status) {
889 if (rssi != AltosLib.MISSING) {
891 this.status = status;
895 public void set_received_time(long ms) {
899 public void set_gps(AltosGPS gps, int sequence) {
901 this.gps = gps.clone();
902 gps_sequence = sequence;
908 public void set_imu(AltosIMU imu) {
914 public void set_mag(AltosMag mag) {
915 this.mag = mag.clone();
918 public AltosMs5607 make_baro() {
920 baro = new AltosMs5607();
924 public void set_ms5607(AltosMs5607 ms5607) {
928 set_pressure(baro.pa);
929 set_temperature(baro.cc / 100.0);
933 public void set_ms5607(int pres, int temp) {
935 baro.set(pres, temp);
937 set_pressure(baro.pa);
938 set_temperature(baro.cc / 100.0);
942 public void make_companion (int nchannels) {
943 if (companion == null)
944 companion = new AltosCompanion(nchannels);
947 public void set_companion(AltosCompanion companion) {
948 this.companion = companion;
951 void update_accel() {
952 double ground = ground_accel;
954 if (ground == AltosLib.MISSING)
955 ground = ground_accel_avg;
956 if (accel == AltosLib.MISSING)
958 if (ground == AltosLib.MISSING)
960 if (accel_plus_g == AltosLib.MISSING)
962 if (accel_minus_g == AltosLib.MISSING)
965 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
966 double counts_per_mss = counts_per_g / 9.80665;
967 acceleration.set_measured((ground - accel) / counts_per_mss, time);
970 public void set_accel_g(double accel_plus_g, double accel_minus_g) {
971 if (accel_plus_g != AltosLib.MISSING) {
972 this.accel_plus_g = accel_plus_g;
973 this.accel_minus_g = accel_minus_g;
978 public void set_ground_accel(double ground_accel) {
979 if (ground_accel != AltosLib.MISSING) {
980 this.ground_accel = ground_accel;
985 public void set_accel(double accel) {
986 if (accel != AltosLib.MISSING) {
988 if (state == AltosLib.ao_flight_pad) {
989 if (ground_accel_avg == AltosLib.MISSING)
990 ground_accel_avg = accel;
992 ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
998 public void set_temperature(double temperature) {
999 if (temperature != AltosLib.MISSING) {
1000 this.temperature = temperature;
1005 public void set_battery_voltage(double battery_voltage) {
1006 if (battery_voltage != AltosLib.MISSING) {
1007 this.battery_voltage = battery_voltage;
1012 public void set_pyro_voltage(double pyro_voltage) {
1013 if (pyro_voltage != AltosLib.MISSING) {
1014 this.pyro_voltage = pyro_voltage;
1019 public void set_apogee_voltage(double apogee_voltage) {
1020 if (apogee_voltage != AltosLib.MISSING) {
1021 this.apogee_voltage = apogee_voltage;
1026 public void set_main_voltage(double main_voltage) {
1027 if (main_voltage != AltosLib.MISSING) {
1028 this.main_voltage = main_voltage;
1033 public void set_ignitor_voltage(double[] voltage) {
1034 this.ignitor_voltage = voltage;
1037 public double time_since_boost() {
1038 if (tick == AltosLib.MISSING)
1041 if (boost_tick == AltosLib.MISSING)
1042 return tick / 100.0;
1043 return (tick - boost_tick) / 100.0;
1046 public boolean valid() {
1047 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1050 public AltosGPS make_temp_gps(boolean sats) {
1051 if (temp_gps == null) {
1052 temp_gps = new AltosGPS(gps);
1056 if (tick != temp_gps_sat_tick)
1057 temp_gps.cc_gps_sat = null;
1058 temp_gps_sat_tick = tick;
1063 public void set_temp_gps() {
1064 set_gps(temp_gps, gps_sequence + 1);
1065 gps_pending = false;
1069 public AltosState clone() {
1070 AltosState s = new AltosState();
1075 public AltosState () {