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 * Track flight state from telemetry or eeprom data stream
23 package org.altusmetrum.altoslib_12;
25 public class AltosState extends AltosDataListener {
27 public static final int set_position = 1;
28 public static final int set_gps = 2;
29 public static final int set_data = 4;
33 static final double filter_len = 2.0;
34 static final double ascent_filter_len = 0.5;
35 static final double descent_filter_len = 5.0;
39 public long received_time;
47 private double max_value;
48 private double set_time;
49 private double prev_set_time;
51 boolean can_max() { return true; }
53 void set(double new_value, double time) {
54 if (new_value != AltosLib.MISSING) {
56 if (can_max() && (max_value == AltosLib.MISSING || value > max_value))
62 void set_filtered(double new_value, double time) {
63 if (prev_value != AltosLib.MISSING) {
64 double f = 1/Math.exp((time - prev_set_time) / filter_len);
65 new_value = f * new_value + (1-f) * prev_value;
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;
128 final double max = 100000;
131 if (Math.abs(ddv) > max) {
141 filter_len = ascent_filter_len;
143 filter_len = descent_filter_len;
145 double f = 1/Math.exp(ddt/ filter_len);
146 n = p * f + n * (1-f);
151 void set_integral(AltosValue in) {
152 double change = in.integrate();
154 if (change != AltosLib.MISSING) {
155 double prev = prev_value;
156 if (prev == AltosLib.MISSING)
158 set(prev + change, in.time());
162 void copy(AltosValue old) {
164 set_time = old.set_time;
165 prev_value = old.value;
166 prev_set_time = old.set_time;
167 max_value = old.max_value;
170 void finish_update() {
172 prev_set_time = set_time;
176 value = AltosLib.MISSING;
177 prev_value = AltosLib.MISSING;
178 max_value = AltosLib.MISSING;
185 class AltosIValue extends AltosValue {
195 public AltosIValue measured;
196 public AltosIValue computed;
198 boolean can_max() { return true; }
200 boolean c_can_max() { return can_max(); }
203 double v = measured.value();
204 if (v != AltosLib.MISSING)
206 return computed.value();
209 boolean is_measured() {
210 return measured.value() != AltosLib.MISSING;
214 double m = measured.max();
216 if (m != AltosLib.MISSING)
218 return computed.max();
221 double prev_value() {
222 if (measured.value != AltosLib.MISSING && measured.prev_value != AltosLib.MISSING)
223 return measured.prev_value;
224 return computed.prev_value;
227 AltosValue altos_value() {
228 if (measured.value() != AltosLib.MISSING)
234 double c = measured.change();
235 if (c == AltosLib.MISSING)
236 c = computed.change();
241 double r = measured.rate();
242 if (r == AltosLib.MISSING)
247 void set_measured(double new_value, double time) {
248 measured.set(new_value, time);
251 void set_computed(double new_value, double time) {
252 computed.set(new_value, time);
255 void set_derivative(AltosValue in) {
256 computed.set_derivative(in);
259 void set_derivative(AltosCValue in) {
260 set_derivative(in.altos_value());
263 void set_integral(AltosValue in) {
264 computed.set_integral(in);
267 void set_integral(AltosCValue in) {
268 set_integral(in.altos_value());
271 void copy(AltosCValue old) {
272 measured.copy(old.measured);
273 computed.copy(old.computed);
276 void finish_update() {
277 measured.finish_update();
278 computed.finish_update();
281 public AltosCValue() {
282 measured = new AltosIValue();
283 computed = new AltosIValue();
287 public boolean landed;
288 public boolean ascent; /* going up? */
289 public boolean boost; /* under power */
291 private double pressure_to_altitude(double p) {
292 if (p == AltosLib.MISSING)
293 return AltosLib.MISSING;
294 return AltosConvert.pressure_to_altitude(p);
297 private AltosCValue ground_altitude;
299 public double ground_altitude() {
300 return ground_altitude.value();
303 public void set_ground_altitude(double a) {
304 ground_altitude.set_measured(a, time);
307 class AltosGpsGroundAltitude extends AltosValue {
308 void set(double a, double t) {
311 gps_altitude.set_gps_height();
314 void set_filtered(double a, double t) {
315 super.set_filtered(a, t);
317 gps_altitude.set_gps_height();
320 AltosGpsGroundAltitude() {
325 private AltosGpsGroundAltitude gps_ground_altitude;
327 public double gps_ground_altitude() {
328 return gps_ground_altitude.value();
331 public void set_gps_ground_altitude(double a) {
332 gps_ground_altitude.set(a, time);
335 class AltosGroundPressure extends AltosCValue {
336 void set_filtered(double p, double time) {
337 computed.set_filtered(p, time);
339 ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
342 void set_measured(double p, double time) {
343 super.set_measured(p, time);
344 ground_altitude.set_computed(pressure_to_altitude(p), time);
347 AltosGroundPressure () {
352 private AltosGroundPressure ground_pressure;
354 public double ground_pressure() {
355 return ground_pressure.value();
358 public void set_ground_pressure (double pressure) {
359 ground_pressure.set_measured(pressure, time);
362 class AltosAltitude extends AltosCValue {
364 private void set_speed(AltosValue v) {
365 if (!acceleration.is_measured() || !ascent)
366 speed.set_derivative(this);
369 void set_computed(double a, double time) {
370 super.set_computed(a,time);
375 void set_measured(double a, double time) {
376 super.set_measured(a,time);
386 private AltosAltitude altitude;
388 class AltosGpsAltitude extends AltosValue {
390 private void set_gps_height() {
392 double g = gps_ground_altitude.value();
394 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
397 gps_height = AltosLib.MISSING;
400 void set(double a, double t) {
410 private AltosGpsAltitude gps_altitude;
412 private AltosValue gps_ground_speed;
413 private AltosValue gps_ascent_rate;
414 private AltosValue gps_course;
415 private AltosValue gps_speed;
417 public double altitude() {
418 double a = altitude.value();
419 if (a != AltosLib.MISSING)
421 return gps_altitude.value();
424 public double max_altitude() {
425 double a = altitude.max();
426 if (a != AltosLib.MISSING)
428 return gps_altitude.max();
431 public void set_altitude(double new_altitude) {
432 double old_altitude = altitude.value();
433 if (old_altitude != AltosLib.MISSING) {
434 while (old_altitude - new_altitude > 32000)
435 new_altitude += 65536.0;
437 altitude.set_measured(new_altitude, time);
440 public double gps_altitude() {
441 return gps_altitude.value();
444 public double max_gps_altitude() {
445 return gps_altitude.max();
448 public void set_gps_altitude(double new_gps_altitude) {
449 gps_altitude.set(new_gps_altitude, time);
452 public double gps_ground_speed() {
453 return gps_ground_speed.value();
456 public double max_gps_ground_speed() {
457 return gps_ground_speed.max();
460 public double gps_ascent_rate() {
461 return gps_ascent_rate.value();
464 public double max_gps_ascent_rate() {
465 return gps_ascent_rate.max();
468 public double gps_course() {
469 return gps_course.value();
472 public double gps_speed() {
473 return gps_speed.value();
476 public double max_gps_speed() {
477 return gps_speed.max();
480 class AltosPressure extends AltosValue {
481 void set(double p, double time) {
483 if (state() == AltosLib.ao_flight_pad)
484 ground_pressure.set_filtered(p, time);
485 double a = pressure_to_altitude(p);
486 altitude.set_computed(a, time);
494 private AltosPressure pressure;
496 public double pressure() {
497 return pressure.value();
500 public void set_pressure(double p) {
501 pressure.set(p, time);
504 public void set_thrust(double N) {
507 public double baro_height() {
508 double a = altitude();
509 double g = ground_altitude();
510 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
512 return AltosLib.MISSING;
515 public double height() {
516 double k = kalman_height.value();
517 if (k != AltosLib.MISSING)
520 double b = baro_height();
521 if (b != AltosLib.MISSING)
527 public double max_height() {
528 double k = kalman_height.max();
529 if (k != AltosLib.MISSING)
532 double a = altitude.max();
533 double g = ground_altitude();
534 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
536 return max_gps_height();
539 public double gps_height() {
540 double a = gps_altitude();
541 double g = gps_ground_altitude();
543 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
545 return AltosLib.MISSING;
548 public double max_gps_height() {
549 double a = gps_altitude.max();
550 double g = gps_ground_altitude();
552 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
554 return AltosLib.MISSING;
557 class AltosSpeed extends AltosCValue {
560 return state() < AltosLib.ao_flight_fast || state() == AltosLib.ao_flight_stateless;
564 acceleration.set_derivative(this);
567 void set_derivative(AltosCValue in) {
568 super.set_derivative(in);
572 void set_computed(double new_value, double time) {
573 super.set_computed(new_value, time);
577 void set_measured(double new_value, double time) {
578 super.set_measured(new_value, time);
587 private AltosSpeed speed;
589 public double speed() {
590 double v = kalman_speed.value();
591 if (v != AltosLib.MISSING)
594 if (v != AltosLib.MISSING)
597 if (v != AltosLib.MISSING)
599 return AltosLib.MISSING;
602 public double max_speed() {
603 double v = kalman_speed.max();
604 if (v != AltosLib.MISSING)
607 if (v != AltosLib.MISSING)
610 if (v != AltosLib.MISSING)
612 return AltosLib.MISSING;
615 class AltosAccel extends AltosCValue {
618 return state() < AltosLib.ao_flight_fast || state() == AltosLib.ao_flight_stateless;
621 void set_measured(double a, double time) {
622 super.set_measured(a, time);
624 speed.set_integral(this.measured);
632 AltosAccel acceleration;
634 public double acceleration() {
635 return acceleration.value();
638 public double max_acceleration() {
639 return acceleration.max();
642 public AltosCValue orient;
644 public void set_orient(double new_orient) {
645 orient.set_measured(new_orient, time);
648 public double orient() {
649 return orient.value();
652 public double max_orient() {
656 public AltosValue kalman_height, kalman_speed, kalman_acceleration;
658 public void set_kalman(double height, double speed, double acceleration) {
659 double old_height = kalman_height.value();
660 if (old_height != AltosLib.MISSING) {
661 while (old_height - height > 32000)
664 kalman_height.set(height, time);
665 kalman_speed.set(speed, time);
666 kalman_acceleration.set(acceleration, time);
669 public double battery_voltage;
670 public double pyro_voltage;
671 public double temperature;
672 public double apogee_voltage;
673 public double main_voltage;
675 public double igniter_voltage[];
678 public boolean gps_pending;
680 public static final int MIN_PAD_SAMPLES = 10;
683 public int gps_waiting;
684 public boolean gps_ready;
688 public AltosGreatCircle from_pad;
689 public double elevation; /* from pad */
690 public double distance; /* distance along ground */
691 public double range; /* total distance */
693 public double gps_height;
695 public double pad_lat, pad_lon, pad_alt;
697 public int speak_tick;
698 public double speak_altitude;
700 public double ground_accel;
702 public AltosCompanion companion;
704 public int pyro_fired;
706 public void set_npad(int npad) {
708 gps_waiting = MIN_PAD_SAMPLES - npad;
709 if (this.gps_waiting < 0)
711 gps_ready = gps_waiting == 0;
719 received_time = System.currentTimeMillis();
722 rssi = AltosLib.MISSING;
725 ground_altitude = new AltosCValue();
726 ground_pressure = new AltosGroundPressure();
727 altitude = new AltosAltitude();
728 pressure = new AltosPressure();
729 speed = new AltosSpeed();
730 acceleration = new AltosAccel();
731 orient = new AltosCValue();
733 temperature = AltosLib.MISSING;
734 battery_voltage = AltosLib.MISSING;
735 pyro_voltage = AltosLib.MISSING;
736 apogee_voltage = AltosLib.MISSING;
737 main_voltage = AltosLib.MISSING;
738 igniter_voltage = null;
740 kalman_height = new AltosValue();
741 kalman_speed = new AltosValue();
742 kalman_acceleration = new AltosValue();
747 last_imu_time = AltosLib.MISSING;
750 accel_ground_along = AltosLib.MISSING;
751 accel_ground_across = AltosLib.MISSING;
752 accel_ground_through = AltosLib.MISSING;
754 accel_along = AltosLib.MISSING;
755 accel_across = AltosLib.MISSING;
756 accel_through = AltosLib.MISSING;
758 gyro_roll = AltosLib.MISSING;
759 gyro_pitch = AltosLib.MISSING;
760 gyro_yaw = AltosLib.MISSING;
762 mag_along = AltosLib.MISSING;
763 mag_across = AltosLib.MISSING;
764 mag_through = AltosLib.MISSING;
770 elevation = AltosLib.MISSING;
771 distance = AltosLib.MISSING;
772 range = AltosLib.MISSING;
773 gps_height = AltosLib.MISSING;
775 pad_lat = AltosLib.MISSING;
776 pad_lon = AltosLib.MISSING;
777 pad_alt = AltosLib.MISSING;
779 gps_altitude = new AltosGpsAltitude();
780 gps_ground_altitude = new AltosGpsGroundAltitude();
781 gps_ground_speed = new AltosValue();
782 gps_speed = new AltosValue();
783 gps_ascent_rate = new AltosValue();
784 gps_course = new AltosValue();
786 speak_tick = AltosLib.MISSING;
787 speak_altitude = AltosLib.MISSING;
789 ground_accel = AltosLib.MISSING;
796 void finish_update() {
797 ground_altitude.finish_update();
798 altitude.finish_update();
799 pressure.finish_update();
800 speed.finish_update();
801 acceleration.finish_update();
802 orient.finish_update();
804 kalman_height.finish_update();
805 kalman_speed.finish_update();
806 kalman_acceleration.finish_update();
813 elevation = AltosLib.MISSING;
814 distance = AltosLib.MISSING;
815 range = AltosLib.MISSING;
820 if (gps.locked && gps.nsat >= 4) {
821 /* Track consecutive 'good' gps reports, waiting for 10 of them */
822 if (state() == AltosLib.ao_flight_pad || state() == AltosLib.ao_flight_stateless) {
824 if (pad_lat != AltosLib.MISSING && (npad < 10 || state() == AltosLib.ao_flight_pad)) {
825 pad_lat = (pad_lat * 31 + gps.lat) / 32;
826 pad_lon = (pad_lon * 31 + gps.lon) / 32;
827 gps_ground_altitude.set_filtered(gps.alt, time);
830 if (pad_lat == AltosLib.MISSING) {
833 gps_ground_altitude.set(gps.alt, time);
835 gps_altitude.set(gps.alt, time);
836 if (gps.climb_rate != AltosLib.MISSING)
837 gps_ascent_rate.set(gps.climb_rate, time);
838 if (gps.ground_speed != AltosLib.MISSING)
839 gps_ground_speed.set(gps.ground_speed, time);
840 if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
841 gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
842 gps.climb_rate * gps.climb_rate), time);
843 if (gps.course != AltosLib.MISSING)
844 gps_course.set(gps.course, time);
846 if (gps.lat != 0 && gps.lon != 0 &&
847 pad_lat != AltosLib.MISSING &&
848 pad_lon != AltosLib.MISSING)
852 if (h == AltosLib.MISSING)
854 from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
855 elevation = from_pad.elevation;
856 distance = from_pad.distance;
857 range = from_pad.range;
861 public String state_name() {
862 return AltosLib.state_name(state());
865 public void set_state(int state) {
866 super.set_state(state);
867 ascent = (AltosLib.ao_flight_boost <= state() &&
868 state() <= AltosLib.ao_flight_coast);
869 boost = (AltosLib.ao_flight_boost == state());
873 if (rssi == AltosLib.MISSING)
878 public void set_rssi(int rssi, int status) {
879 if (rssi != AltosLib.MISSING) {
881 this.status = status;
885 public void set_received_time(long ms) {
889 public void set_gps(AltosGPS gps) {
898 public AltosRotation rotation;
900 public double accel_ground_along, accel_ground_across, accel_ground_through;
902 void update_pad_rotation() {
903 if (cal_data().pad_orientation != AltosLib.MISSING && accel_ground_along != AltosLib.MISSING) {
904 rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - cal_data().accel_zero_across),
905 AltosIMU.convert_accel(accel_ground_through - cal_data().accel_zero_through),
906 AltosIMU.convert_accel(accel_ground_along - cal_data().accel_zero_along),
907 cal_data().pad_orientation);
908 orient.set_computed(rotation.tilt(), time);
912 public void set_accel_ground(double ground_along, double ground_across, double ground_through) {
913 accel_ground_along = ground_along;
914 accel_ground_across = ground_across;
915 accel_ground_through = ground_through;
916 update_pad_rotation();
919 public double last_imu_time;
921 private void update_orient() {
922 if (last_imu_time != AltosLib.MISSING) {
923 double t = time - last_imu_time;
925 if (t > 0 && gyro_pitch != AltosLib.MISSING && rotation != null) {
926 double pitch = AltosConvert.degrees_to_radians(gyro_pitch) * t;
927 double yaw = AltosConvert.degrees_to_radians(gyro_yaw) * t;
928 double roll = AltosConvert.degrees_to_radians(gyro_roll) * t;
930 rotation.rotate(pitch, yaw, roll);
931 orient.set_computed(rotation.tilt(), time);
934 last_imu_time = time;
937 private double gyro_roll, gyro_pitch, gyro_yaw;
939 public void set_gyro(double roll, double pitch, double yaw) {
946 private double accel_along, accel_across, accel_through;
948 public void set_accel(double along, double across, double through) {
950 accel_across = across;
951 accel_through = through;
955 public double accel_along() {
959 public double accel_across() {
963 public double accel_through() {
964 return accel_through;
967 public double gyro_roll() {
971 public double gyro_pitch() {
975 public double gyro_yaw() {
979 private double mag_along, mag_across, mag_through;
981 public void set_mag(double along, double across, double through) {
984 mag_through = through;
987 public double mag_along() {
991 public double mag_across() {
995 public double mag_through() {
999 public void set_companion(AltosCompanion companion) {
1000 this.companion = companion;
1003 public void set_acceleration(double acceleration) {
1004 if (acceleration != AltosLib.MISSING) {
1005 this.acceleration.set_measured(acceleration, time);
1010 public void set_temperature(double temperature) {
1011 if (temperature != AltosLib.MISSING) {
1012 this.temperature = temperature;
1017 public void set_battery_voltage(double battery_voltage) {
1018 if (battery_voltage != AltosLib.MISSING) {
1019 this.battery_voltage = battery_voltage;
1024 public void set_pyro_voltage(double pyro_voltage) {
1025 if (pyro_voltage != AltosLib.MISSING) {
1026 this.pyro_voltage = pyro_voltage;
1031 public void set_apogee_voltage(double apogee_voltage) {
1032 if (apogee_voltage != AltosLib.MISSING) {
1033 this.apogee_voltage = apogee_voltage;
1038 public void set_main_voltage(double main_voltage) {
1039 if (main_voltage != AltosLib.MISSING) {
1040 this.main_voltage = main_voltage;
1045 public void set_igniter_voltage(double[] voltage) {
1046 this.igniter_voltage = voltage;
1049 public void set_pyro_fired(int fired) {
1050 this.pyro_fired = fired;
1053 public AltosState() {
1057 public AltosState (AltosCalData cal_data) {