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_2;
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.1;
33 static final double descent_filter_len = 2.0;
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;
52 private double max_rate = 1000.0;
54 void set(double new_value, double time) {
55 if (new_value != AltosLib.MISSING) {
57 if (max_value == AltosLib.MISSING || value > max_value) {
64 void set_filtered(double new_value, double time) {
65 if (prev_value != AltosLib.MISSING)
66 new_value = (prev_value * 15.0 + new_value) / 16.0;
83 if (value != AltosLib.MISSING && prev_value != AltosLib.MISSING)
84 return value - prev_value;
85 return AltosLib.MISSING;
90 double t = set_time - prev_set_time;
92 if (c != AltosLib.MISSING && t != 0)
94 return AltosLib.MISSING;
98 if (value == AltosLib.MISSING)
99 return AltosLib.MISSING;
100 if (prev_value == AltosLib.MISSING)
101 return AltosLib.MISSING;
103 return (value + prev_value) / 2 * (set_time - prev_set_time);
110 void set_derivative(AltosValue in) {
111 double n = in.rate();
113 if (n == AltosLib.MISSING)
116 double p = prev_value;
117 double pt = prev_set_time;
119 if (p == AltosLib.MISSING) {
121 pt = in.time() - 0.01;
124 /* Clip changes to reduce noise */
125 double ddt = in.time() - pt;
126 double ddv = (n - p) / ddt;
129 if (Math.abs(ddv) > 1000) {
139 filter_len = ascent_filter_len;
141 filter_len = descent_filter_len;
143 double f = 1/Math.exp(ddt/ filter_len);
144 n = p * f + n * (1-f);
149 void set_integral(AltosValue in) {
150 double change = in.integrate();
152 if (change != AltosLib.MISSING) {
153 double prev = prev_value;
154 if (prev == AltosLib.MISSING)
156 set(prev + change, in.time());
160 void copy(AltosValue old) {
162 set_time = old.set_time;
163 prev_value = old.value;
164 prev_set_time = old.set_time;
165 max_value = old.max_value;
168 void finish_update() {
170 prev_set_time = set_time;
174 value = AltosLib.MISSING;
175 prev_value = AltosLib.MISSING;
176 max_value = AltosLib.MISSING;
185 double v = measured.value();
186 if (v != AltosLib.MISSING)
188 return computed.value();
191 boolean is_measured() {
192 return measured.value() != AltosLib.MISSING;
196 double m = measured.max();
198 if (m != AltosLib.MISSING)
200 return computed.max();
203 double prev_value() {
204 if (measured.value != AltosLib.MISSING && measured.prev_value != AltosLib.MISSING)
205 return measured.prev_value;
206 return computed.prev_value;
209 AltosValue altos_value() {
210 if (measured.value() != AltosLib.MISSING)
216 double c = measured.change();
217 if (c == AltosLib.MISSING)
218 c = computed.change();
223 double r = measured.rate();
224 if (r == AltosLib.MISSING)
229 void set_measured(double new_value, double time) {
230 measured.set(new_value, time);
233 void set_computed(double new_value, double time) {
234 computed.set(new_value, time);
237 void set_derivative(AltosValue in) {
238 computed.set_derivative(in);
241 void set_derivative(AltosCValue in) {
242 set_derivative(in.altos_value());
245 void set_integral(AltosValue in) {
246 computed.set_integral(in);
249 void set_integral(AltosCValue in) {
250 set_integral(in.altos_value());
253 void copy(AltosCValue old) {
254 measured.copy(old.measured);
255 computed.copy(old.computed);
258 void finish_update() {
259 measured.finish_update();
260 computed.finish_update();
264 measured = new AltosValue();
265 computed = new AltosValue();
272 public boolean landed;
273 public boolean ascent; /* going up? */
274 public boolean boost; /* under power */
277 public int device_type;
278 public int config_major;
279 public int config_minor;
280 public int apogee_delay;
281 public int main_deploy;
282 public int flight_log_max;
284 private double pressure_to_altitude(double p) {
285 if (p == AltosLib.MISSING)
286 return AltosLib.MISSING;
287 return AltosConvert.pressure_to_altitude(p);
290 private AltosCValue ground_altitude;
292 public double ground_altitude() {
293 return ground_altitude.value();
296 public void set_ground_altitude(double a) {
297 ground_altitude.set_measured(a, time);
300 class AltosGroundPressure extends AltosCValue {
301 void set_filtered(double p, double time) {
302 computed.set_filtered(p, time);
303 ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
306 void set_measured(double p, double time) {
307 super.set_measured(p, time);
308 ground_altitude.set_computed(pressure_to_altitude(p), time);
312 private AltosGroundPressure ground_pressure;
314 public double ground_pressure() {
315 return ground_pressure.value();
318 public void set_ground_pressure (double pressure) {
319 ground_pressure.set_measured(pressure, time);
322 class AltosAltitude extends AltosCValue {
324 private void set_speed(AltosValue v) {
325 if (!acceleration.is_measured() || !ascent)
326 speed.set_derivative(this);
329 void set_computed(double a, double time) {
330 super.set_computed(a,time);
335 void set_measured(double a, double time) {
336 super.set_measured(a,time);
342 private AltosAltitude altitude;
344 public double altitude() {
345 double a = altitude.value();
346 if (a != AltosLib.MISSING)
350 return AltosLib.MISSING;
353 public double max_altitude() {
354 double a = altitude.max();
355 if (a != AltosLib.MISSING)
357 return AltosLib.MISSING;
360 public void set_altitude(double new_altitude) {
361 altitude.set_measured(new_altitude, time);
364 class AltosPressure extends AltosValue {
365 void set(double p, double time) {
367 if (state == AltosLib.ao_flight_pad)
368 ground_pressure.set_filtered(p, time);
369 double a = pressure_to_altitude(p);
370 altitude.set_computed(a, time);
374 private AltosPressure pressure;
376 public double pressure() {
377 return pressure.value();
380 public void set_pressure(double p) {
381 pressure.set(p, time);
384 public double height() {
385 double k = kalman_height.value();
386 if (k != AltosLib.MISSING)
389 double a = altitude();
390 double g = ground_altitude();
391 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
393 return AltosLib.MISSING;
396 public double max_height() {
397 double k = kalman_height.max();
398 if (k != AltosLib.MISSING)
401 double a = altitude.max();
402 double g = ground_altitude();
403 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
405 return AltosLib.MISSING;
408 class AltosSpeed extends AltosCValue {
411 acceleration.set_derivative(this);
414 void set_derivative(AltosCValue in) {
415 super.set_derivative(in);
419 void set_computed(double new_value, double time) {
420 super.set_computed(new_value, time);
424 void set_measured(double new_value, double time) {
425 super.set_measured(new_value, time);
430 private AltosSpeed speed;
432 public double speed() {
433 double v = kalman_speed.value();
434 if (v != AltosLib.MISSING)
436 return speed.value();
439 public double max_speed() {
440 double v = kalman_speed.max();
441 if (v != AltosLib.MISSING)
446 class AltosAccel extends AltosCValue {
447 void set_measured(double a, double time) {
448 super.set_measured(a, time);
450 speed.set_integral(this.measured);
454 AltosAccel acceleration;
456 public double acceleration() {
457 return acceleration.value();
460 public double max_acceleration() {
461 return acceleration.max();
464 public AltosValue kalman_height, kalman_speed, kalman_acceleration;
466 public void set_kalman(double height, double speed, double acceleration) {
467 kalman_height.set(height, time);
468 kalman_speed.set(speed, time);
469 kalman_acceleration.set(acceleration, time);
472 public double battery_voltage;
473 public double pyro_voltage;
474 public double temperature;
475 public double apogee_voltage;
476 public double main_voltage;
478 public double ignitor_voltage[];
481 public AltosGPS temp_gps;
482 public int temp_gps_sat_tick;
483 public boolean gps_pending;
484 public int gps_sequence;
489 public static final int MIN_PAD_SAMPLES = 10;
492 public int gps_waiting;
493 public boolean gps_ready;
497 public AltosGreatCircle from_pad;
498 public double elevation; /* from pad */
499 public double range; /* total distance */
501 public double gps_height;
503 public double pad_lat, pad_lon, pad_alt;
505 public int speak_tick;
506 public double speak_altitude;
508 public String callsign;
509 public String firmware_version;
511 public double accel_plus_g;
512 public double accel_minus_g;
514 public double ground_accel;
515 public double ground_accel_avg;
517 public int log_format;
519 public AltosMs5607 baro;
521 public AltosCompanion companion;
523 public void set_npad(int npad) {
525 gps_waiting = MIN_PAD_SAMPLES - npad;
526 if (this.gps_waiting < 0)
528 gps_ready = gps_waiting == 0;
534 received_time = System.currentTimeMillis();
535 time = AltosLib.MISSING;
536 time_change = AltosLib.MISSING;
537 prev_time = AltosLib.MISSING;
538 tick = AltosLib.MISSING;
539 prev_tick = AltosLib.MISSING;
540 boost_tick = AltosLib.MISSING;
541 state = AltosLib.ao_flight_invalid;
542 flight = AltosLib.MISSING;
545 rssi = AltosLib.MISSING;
547 device_type = AltosLib.MISSING;
548 config_major = AltosLib.MISSING;
549 config_minor = AltosLib.MISSING;
550 apogee_delay = AltosLib.MISSING;
551 main_deploy = AltosLib.MISSING;
552 flight_log_max = AltosLib.MISSING;
554 ground_altitude = new AltosCValue();
555 ground_pressure = new AltosGroundPressure();
556 altitude = new AltosAltitude();
557 pressure = new AltosPressure();
558 speed = new AltosSpeed();
559 acceleration = new AltosAccel();
561 temperature = AltosLib.MISSING;
562 battery_voltage = AltosLib.MISSING;
563 pyro_voltage = AltosLib.MISSING;
564 apogee_voltage = AltosLib.MISSING;
565 main_voltage = AltosLib.MISSING;
566 ignitor_voltage = null;
568 kalman_height = new AltosValue();
569 kalman_speed = new AltosValue();
570 kalman_acceleration = new AltosValue();
574 temp_gps_sat_tick = 0;
585 elevation = AltosLib.MISSING;
586 range = AltosLib.MISSING;
587 gps_height = AltosLib.MISSING;
589 pad_lat = AltosLib.MISSING;
590 pad_lon = AltosLib.MISSING;
591 pad_alt = AltosLib.MISSING;
593 speak_tick = AltosLib.MISSING;
594 speak_altitude = AltosLib.MISSING;
598 accel_plus_g = AltosLib.MISSING;
599 accel_minus_g = AltosLib.MISSING;
600 accel = AltosLib.MISSING;
602 ground_accel = AltosLib.MISSING;
603 ground_accel_avg = AltosLib.MISSING;
605 log_format = AltosLib.MISSING;
606 serial = AltosLib.MISSING;
612 void finish_update() {
615 ground_altitude.finish_update();
616 altitude.finish_update();
617 pressure.finish_update();
618 speed.finish_update();
619 acceleration.finish_update();
621 kalman_height.finish_update();
622 kalman_speed.finish_update();
623 kalman_acceleration.finish_update();
626 void copy(AltosState old) {
633 received_time = old.received_time;
637 prev_tick = old.tick;
638 boost_tick = old.boost_tick;
647 device_type = old.device_type;
648 config_major = old.config_major;
649 config_minor = old.config_minor;
650 apogee_delay = old.apogee_delay;
651 main_deploy = old.main_deploy;
652 flight_log_max = old.flight_log_max;
656 ground_altitude.copy(old.ground_altitude);
657 altitude.copy(old.altitude);
658 pressure.copy(old.pressure);
659 speed.copy(old.speed);
660 acceleration.copy(old.acceleration);
662 battery_voltage = old.battery_voltage;
663 pyro_voltage = old.pyro_voltage;
664 temperature = old.temperature;
665 apogee_voltage = old.apogee_voltage;
666 main_voltage = old.main_voltage;
667 ignitor_voltage = old.ignitor_voltage;
669 kalman_height.copy(old.kalman_height);
670 kalman_speed.copy(old.kalman_speed);
671 kalman_acceleration.copy(old.kalman_acceleration);
674 gps = old.gps.clone();
677 if (old.temp_gps != null)
678 temp_gps = old.temp_gps.clone();
681 temp_gps_sat_tick = old.temp_gps_sat_tick;
682 gps_sequence = old.gps_sequence;
683 gps_pending = old.gps_pending;
686 imu = old.imu.clone();
691 mag = old.mag.clone();
696 gps_waiting = old.gps_waiting;
697 gps_ready = old.gps_ready;
700 if (old.from_pad != null)
701 from_pad = old.from_pad.clone();
705 elevation = old.elevation;
708 gps_height = old.gps_height;
709 pad_lat = old.pad_lat;
710 pad_lon = old.pad_lon;
711 pad_alt = old.pad_alt;
713 speak_tick = old.speak_tick;
714 speak_altitude = old.speak_altitude;
716 callsign = old.callsign;
718 accel_plus_g = old.accel_plus_g;
719 accel_minus_g = old.accel_minus_g;
721 ground_accel = old.ground_accel;
722 ground_accel_avg = old.ground_accel_avg;
724 log_format = old.log_format;
728 companion = old.companion;
742 if (gps.locked && gps.nsat >= 4) {
743 /* Track consecutive 'good' gps reports, waiting for 10 of them */
744 if (state == AltosLib.ao_flight_pad) {
746 if (pad_lat != AltosLib.MISSING) {
747 pad_lat = (pad_lat * 31 + gps.lat) / 32;
748 pad_lon = (pad_lon * 31 + gps.lon) / 32;
749 pad_alt = (pad_alt * 31 + gps.alt) / 32;
752 if (pad_lat == AltosLib.MISSING) {
758 if (gps.lat != 0 && gps.lon != 0 &&
759 pad_lat != AltosLib.MISSING &&
760 pad_lon != AltosLib.MISSING)
764 if (h == AltosLib.MISSING)
766 from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
767 elevation = from_pad.elevation;
768 range = from_pad.range;
769 gps_height = gps.alt - pad_alt;
773 public void set_tick(int new_tick) {
774 if (new_tick != AltosLib.MISSING) {
775 if (prev_tick != AltosLib.MISSING) {
776 while (new_tick < prev_tick - 1000) {
785 public void set_boost_tick(int boost_tick) {
786 if (boost_tick != AltosLib.MISSING)
787 this.boost_tick = boost_tick;
790 public String state_name() {
791 return AltosLib.state_name(state);
794 public void set_state(int state) {
795 if (state != AltosLib.ao_flight_invalid) {
797 ascent = (AltosLib.ao_flight_boost <= state &&
798 state <= AltosLib.ao_flight_coast);
799 boost = (AltosLib.ao_flight_boost == state);
804 public void set_device_type(int device_type) {
805 this.device_type = device_type;
808 public void set_config(int major, int minor, int apogee_delay, int main_deploy, int flight_log_max) {
809 config_major = major;
810 config_minor = minor;
811 this.apogee_delay = apogee_delay;
812 this.main_deploy = main_deploy;
813 this.flight_log_max = flight_log_max;
816 public void set_callsign(String callsign) {
817 this.callsign = callsign;
820 public void set_firmware_version(String version) {
821 firmware_version = version;
824 public void set_flight(int flight) {
826 /* When the flight changes, reset the state */
827 if (flight != AltosLib.MISSING && flight != 0) {
828 if (this.flight != AltosLib.MISSING &&
829 this.flight != flight) {
832 this.flight = flight;
836 public void set_serial(int serial) {
837 /* When the serial changes, reset the state */
838 if (serial != AltosLib.MISSING) {
839 if (this.serial != AltosLib.MISSING &&
840 this.serial != serial) {
843 this.serial = serial;
848 if (rssi == AltosLib.MISSING)
853 public void set_rssi(int rssi, int status) {
854 if (rssi != AltosLib.MISSING) {
856 this.status = status;
860 public void set_received_time(long ms) {
864 public void set_gps(AltosGPS gps, int sequence) {
866 this.gps = gps.clone();
867 gps_sequence = sequence;
873 public void set_imu(AltosIMU imu) {
879 public void set_mag(AltosMag mag) {
880 this.mag = mag.clone();
883 public AltosMs5607 make_baro() {
885 baro = new AltosMs5607();
889 public void set_ms5607(AltosMs5607 ms5607) {
893 set_pressure(baro.pa);
894 set_temperature(baro.cc / 100.0);
898 public void set_ms5607(int pres, int temp) {
900 baro.set(pres, temp);
902 set_pressure(baro.pa);
903 set_temperature(baro.cc / 100.0);
907 public void make_companion (int nchannels) {
908 if (companion == null)
909 companion = new AltosCompanion(nchannels);
912 public void set_companion(AltosCompanion companion) {
913 this.companion = companion;
916 void update_accel() {
917 double ground = ground_accel;
919 if (ground == AltosLib.MISSING)
920 ground = ground_accel_avg;
921 if (accel == AltosLib.MISSING)
923 if (ground == AltosLib.MISSING)
925 if (accel_plus_g == AltosLib.MISSING)
927 if (accel_minus_g == AltosLib.MISSING)
930 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
931 double counts_per_mss = counts_per_g / 9.80665;
932 acceleration.set_measured((ground - accel) / counts_per_mss, time);
935 public void set_accel_g(double accel_plus_g, double accel_minus_g) {
936 if (accel_plus_g != AltosLib.MISSING) {
937 this.accel_plus_g = accel_plus_g;
938 this.accel_minus_g = accel_minus_g;
943 public void set_ground_accel(double ground_accel) {
944 if (ground_accel != AltosLib.MISSING) {
945 this.ground_accel = ground_accel;
950 public void set_accel(double accel) {
951 if (accel != AltosLib.MISSING) {
953 if (state == AltosLib.ao_flight_pad) {
954 if (ground_accel_avg == AltosLib.MISSING)
955 ground_accel_avg = accel;
957 ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
963 public void set_temperature(double temperature) {
964 if (temperature != AltosLib.MISSING) {
965 this.temperature = temperature;
970 public void set_battery_voltage(double battery_voltage) {
971 if (battery_voltage != AltosLib.MISSING) {
972 this.battery_voltage = battery_voltage;
977 public void set_pyro_voltage(double pyro_voltage) {
978 if (pyro_voltage != AltosLib.MISSING) {
979 this.pyro_voltage = pyro_voltage;
984 public void set_apogee_voltage(double apogee_voltage) {
985 if (apogee_voltage != AltosLib.MISSING) {
986 this.apogee_voltage = apogee_voltage;
991 public void set_main_voltage(double main_voltage) {
992 if (main_voltage != AltosLib.MISSING) {
993 this.main_voltage = main_voltage;
998 public void set_ignitor_voltage(double[] voltage) {
999 this.ignitor_voltage = voltage;
1002 public double time_since_boost() {
1003 if (tick == AltosLib.MISSING)
1006 if (boost_tick != AltosLib.MISSING) {
1007 return (tick - boost_tick) / 100.0;
1009 return tick / 100.0;
1012 public boolean valid() {
1013 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1016 public AltosGPS make_temp_gps(boolean sats) {
1017 if (temp_gps == null) {
1018 temp_gps = new AltosGPS(gps);
1022 if (tick != temp_gps_sat_tick)
1023 temp_gps.cc_gps_sat = null;
1024 temp_gps_sat_tick = tick;
1029 public void set_temp_gps() {
1030 set_gps(temp_gps, gps_sequence + 1);
1031 gps_pending = false;
1035 public AltosState clone() {
1036 AltosState s = new AltosState();
1041 public AltosState () {