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_11;
27 public class AltosState implements Cloneable {
29 public static final int set_position = 1;
30 public static final int set_gps = 2;
31 public static final int set_data = 4;
35 static final double filter_len = 2.0;
36 static final double ascent_filter_len = 0.5;
37 static final double descent_filter_len = 5.0;
41 public long received_time;
44 public double prev_time;
45 public double time_change;
47 private int prev_tick;
48 public int boost_tick;
53 private double max_value;
54 private double set_time;
55 private double prev_set_time;
57 boolean can_max() { return true; }
59 void set(double new_value, double time) {
60 if (new_value != AltosLib.MISSING) {
62 if (can_max() && (max_value == AltosLib.MISSING || value > max_value))
68 void set_filtered(double new_value, double time) {
69 if (prev_value != AltosLib.MISSING) {
70 double f = 1/Math.exp((time - prev_set_time) / filter_len);
71 new_value = f * new_value + (1-f) * prev_value;
89 if (value != AltosLib.MISSING && prev_value != AltosLib.MISSING)
90 return value - prev_value;
91 return AltosLib.MISSING;
96 double t = set_time - prev_set_time;
98 if (c != AltosLib.MISSING && t != 0)
100 return AltosLib.MISSING;
104 if (value == AltosLib.MISSING)
105 return AltosLib.MISSING;
106 if (prev_value == AltosLib.MISSING)
107 return AltosLib.MISSING;
109 return (value + prev_value) / 2 * (set_time - prev_set_time);
116 void set_derivative(AltosValue in) {
117 double n = in.rate();
119 if (n == AltosLib.MISSING)
122 double p = prev_value;
123 double pt = prev_set_time;
125 if (p == AltosLib.MISSING) {
127 pt = in.time() - 0.01;
130 /* Clip changes to reduce noise */
131 double ddt = in.time() - pt;
132 double ddv = (n - p) / ddt;
134 final double max = 100000;
137 if (Math.abs(ddv) > max) {
147 filter_len = ascent_filter_len;
149 filter_len = descent_filter_len;
151 double f = 1/Math.exp(ddt/ filter_len);
152 n = p * f + n * (1-f);
157 void set_integral(AltosValue in) {
158 double change = in.integrate();
160 if (change != AltosLib.MISSING) {
161 double prev = prev_value;
162 if (prev == AltosLib.MISSING)
164 set(prev + change, in.time());
168 void copy(AltosValue old) {
170 set_time = old.set_time;
171 prev_value = old.value;
172 prev_set_time = old.set_time;
173 max_value = old.max_value;
176 void finish_update() {
178 prev_set_time = set_time;
182 value = AltosLib.MISSING;
183 prev_value = AltosLib.MISSING;
184 max_value = AltosLib.MISSING;
191 class AltosIValue extends AltosValue {
201 public AltosIValue measured;
202 public AltosIValue computed;
204 boolean can_max() { return true; }
206 boolean c_can_max() { return can_max(); }
209 double v = measured.value();
210 if (v != AltosLib.MISSING)
212 return computed.value();
215 boolean is_measured() {
216 return measured.value() != AltosLib.MISSING;
220 double m = measured.max();
222 if (m != AltosLib.MISSING)
224 return computed.max();
227 double prev_value() {
228 if (measured.value != AltosLib.MISSING && measured.prev_value != AltosLib.MISSING)
229 return measured.prev_value;
230 return computed.prev_value;
233 AltosValue altos_value() {
234 if (measured.value() != AltosLib.MISSING)
240 double c = measured.change();
241 if (c == AltosLib.MISSING)
242 c = computed.change();
247 double r = measured.rate();
248 if (r == AltosLib.MISSING)
253 void set_measured(double new_value, double time) {
254 measured.set(new_value, time);
257 void set_computed(double new_value, double time) {
258 computed.set(new_value, time);
261 void set_derivative(AltosValue in) {
262 computed.set_derivative(in);
265 void set_derivative(AltosCValue in) {
266 set_derivative(in.altos_value());
269 void set_integral(AltosValue in) {
270 computed.set_integral(in);
273 void set_integral(AltosCValue in) {
274 set_integral(in.altos_value());
277 void copy(AltosCValue old) {
278 measured.copy(old.measured);
279 computed.copy(old.computed);
282 void finish_update() {
283 measured.finish_update();
284 computed.finish_update();
287 public AltosCValue() {
288 measured = new AltosIValue();
289 computed = new AltosIValue();
296 public int altitude_32;
297 public int receiver_serial;
298 public boolean landed;
299 public boolean ascent; /* going up? */
300 public boolean boost; /* under power */
303 public int device_type;
304 public int config_major;
305 public int config_minor;
306 public int apogee_delay;
307 public int main_deploy;
308 public int flight_log_max;
310 private double pressure_to_altitude(double p) {
311 if (p == AltosLib.MISSING)
312 return AltosLib.MISSING;
313 return AltosConvert.pressure_to_altitude(p);
316 private AltosCValue ground_altitude;
318 public double ground_altitude() {
319 return ground_altitude.value();
322 public void set_ground_altitude(double a) {
323 ground_altitude.set_measured(a, time);
326 class AltosGpsGroundAltitude extends AltosValue {
327 void set(double a, double t) {
330 gps_altitude.set_gps_height();
333 void set_filtered(double a, double t) {
334 super.set_filtered(a, t);
336 gps_altitude.set_gps_height();
339 AltosGpsGroundAltitude() {
344 private AltosGpsGroundAltitude gps_ground_altitude;
346 public double gps_ground_altitude() {
347 return gps_ground_altitude.value();
350 public void set_gps_ground_altitude(double a) {
351 gps_ground_altitude.set(a, time);
354 class AltosGroundPressure extends AltosCValue {
355 void set_filtered(double p, double time) {
356 computed.set_filtered(p, time);
358 ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
361 void set_measured(double p, double time) {
362 super.set_measured(p, time);
363 ground_altitude.set_computed(pressure_to_altitude(p), time);
366 AltosGroundPressure () {
371 private AltosGroundPressure ground_pressure;
373 public double ground_pressure() {
374 return ground_pressure.value();
377 public void set_ground_pressure (double pressure) {
378 ground_pressure.set_measured(pressure, time);
381 class AltosAltitude extends AltosCValue {
383 private void set_speed(AltosValue v) {
384 if (!acceleration.is_measured() || !ascent)
385 speed.set_derivative(this);
388 void set_computed(double a, double time) {
389 super.set_computed(a,time);
394 void set_measured(double a, double time) {
395 super.set_measured(a,time);
405 private AltosAltitude altitude;
407 class AltosGpsAltitude extends AltosValue {
409 private void set_gps_height() {
411 double g = gps_ground_altitude.value();
413 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
416 gps_height = AltosLib.MISSING;
419 void set(double a, double t) {
429 private AltosGpsAltitude gps_altitude;
431 private AltosValue gps_ground_speed;
432 private AltosValue gps_ascent_rate;
433 private AltosValue gps_course;
434 private AltosValue gps_speed;
436 public double altitude() {
437 double a = altitude.value();
438 if (a != AltosLib.MISSING)
440 return gps_altitude.value();
443 public double max_altitude() {
444 double a = altitude.max();
445 if (a != AltosLib.MISSING)
447 return gps_altitude.max();
450 public void set_altitude(double new_altitude) {
451 altitude.set_measured(new_altitude, time);
454 public double gps_altitude() {
455 return gps_altitude.value();
458 public double max_gps_altitude() {
459 return gps_altitude.max();
462 public void set_gps_altitude(double new_gps_altitude) {
463 gps_altitude.set(new_gps_altitude, time);
466 public double gps_ground_speed() {
467 return gps_ground_speed.value();
470 public double max_gps_ground_speed() {
471 return gps_ground_speed.max();
474 public double gps_ascent_rate() {
475 return gps_ascent_rate.value();
478 public double max_gps_ascent_rate() {
479 return gps_ascent_rate.max();
482 public double gps_course() {
483 return gps_course.value();
486 public double gps_speed() {
487 return gps_speed.value();
490 public double max_gps_speed() {
491 return gps_speed.max();
494 class AltosPressure extends AltosValue {
495 void set(double p, double time) {
497 if (state == AltosLib.ao_flight_pad)
498 ground_pressure.set_filtered(p, time);
499 double a = pressure_to_altitude(p);
500 altitude.set_computed(a, time);
508 private AltosPressure pressure;
510 public double pressure() {
511 return pressure.value();
514 public void set_pressure(double p) {
515 pressure.set(p, time);
518 public double baro_height() {
519 double a = altitude();
520 double g = ground_altitude();
521 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
523 return AltosLib.MISSING;
526 public double height() {
527 double k = kalman_height.value();
528 if (k != AltosLib.MISSING)
531 double b = baro_height();
532 if (b != AltosLib.MISSING)
538 public double max_height() {
539 double k = kalman_height.max();
540 if (k != AltosLib.MISSING)
543 double a = altitude.max();
544 double g = ground_altitude();
545 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
547 return max_gps_height();
550 public double gps_height() {
551 double a = gps_altitude();
552 double g = gps_ground_altitude();
554 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
556 return AltosLib.MISSING;
559 public double max_gps_height() {
560 double a = gps_altitude.max();
561 double g = gps_ground_altitude();
563 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
565 return AltosLib.MISSING;
568 class AltosSpeed extends AltosCValue {
571 return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
575 acceleration.set_derivative(this);
578 void set_derivative(AltosCValue in) {
579 super.set_derivative(in);
583 void set_computed(double new_value, double time) {
584 super.set_computed(new_value, time);
588 void set_measured(double new_value, double time) {
589 super.set_measured(new_value, time);
598 private AltosSpeed speed;
600 public double speed() {
601 double v = kalman_speed.value();
602 if (v != AltosLib.MISSING)
605 if (v != AltosLib.MISSING)
608 if (v != AltosLib.MISSING)
610 return AltosLib.MISSING;
613 public double max_speed() {
614 double v = kalman_speed.max();
615 if (v != AltosLib.MISSING)
618 if (v != AltosLib.MISSING)
621 if (v != AltosLib.MISSING)
623 return AltosLib.MISSING;
626 class AltosAccel extends AltosCValue {
629 return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
632 void set_measured(double a, double time) {
633 super.set_measured(a, time);
635 speed.set_integral(this.measured);
643 AltosAccel acceleration;
645 public double acceleration() {
646 return acceleration.value();
649 public double max_acceleration() {
650 return acceleration.max();
653 public AltosCValue orient;
655 public void set_orient(double new_orient) {
656 orient.set_measured(new_orient, time);
659 public double orient() {
660 return orient.value();
663 public double max_orient() {
667 public AltosValue kalman_height, kalman_speed, kalman_acceleration;
669 public void set_kalman(double height, double speed, double acceleration) {
670 kalman_height.set(height, time);
671 kalman_speed.set(speed, time);
672 kalman_acceleration.set(acceleration, time);
675 public double battery_voltage;
676 public double pyro_voltage;
677 public double temperature;
678 public double apogee_voltage;
679 public double main_voltage;
681 public double ignitor_voltage[];
684 public AltosGPS temp_gps;
685 public int temp_gps_sat_tick;
686 public boolean gps_pending;
687 public int gps_sequence;
692 public static final int MIN_PAD_SAMPLES = 10;
695 public int gps_waiting;
696 public boolean gps_ready;
700 public AltosGreatCircle from_pad;
701 public double elevation; /* from pad */
702 public double range; /* total distance */
704 public double gps_height;
706 public double pad_lat, pad_lon, pad_alt;
708 public int speak_tick;
709 public double speak_altitude;
711 public String callsign;
712 public String firmware_version;
714 public double accel_plus_g;
715 public double accel_minus_g;
717 public double ground_accel;
718 public double ground_accel_avg;
720 public int log_format;
721 public int log_space;
722 public String product;
724 public AltosMs5607 baro;
726 public AltosCompanion companion;
728 public int pyro_fired;
730 public void set_npad(int npad) {
732 gps_waiting = MIN_PAD_SAMPLES - npad;
733 if (this.gps_waiting < 0)
735 gps_ready = gps_waiting == 0;
741 received_time = System.currentTimeMillis();
742 time = AltosLib.MISSING;
743 time_change = AltosLib.MISSING;
744 prev_time = AltosLib.MISSING;
745 tick = AltosLib.MISSING;
746 prev_tick = AltosLib.MISSING;
747 boost_tick = AltosLib.MISSING;
748 state = AltosLib.ao_flight_invalid;
749 flight = AltosLib.MISSING;
752 rssi = AltosLib.MISSING;
754 device_type = AltosLib.MISSING;
755 config_major = AltosLib.MISSING;
756 config_minor = AltosLib.MISSING;
757 apogee_delay = AltosLib.MISSING;
758 main_deploy = AltosLib.MISSING;
759 flight_log_max = AltosLib.MISSING;
761 ground_altitude = new AltosCValue();
762 ground_pressure = new AltosGroundPressure();
763 altitude = new AltosAltitude();
764 pressure = new AltosPressure();
765 speed = new AltosSpeed();
766 acceleration = new AltosAccel();
767 orient = new AltosCValue();
769 temperature = AltosLib.MISSING;
770 battery_voltage = AltosLib.MISSING;
771 pyro_voltage = AltosLib.MISSING;
772 apogee_voltage = AltosLib.MISSING;
773 main_voltage = AltosLib.MISSING;
774 ignitor_voltage = null;
776 kalman_height = new AltosValue();
777 kalman_speed = new AltosValue();
778 kalman_acceleration = new AltosValue();
782 temp_gps_sat_tick = 0;
787 last_imu_time = AltosLib.MISSING;
789 ground_rotation = null;
792 accel_zero_along = AltosLib.MISSING;
793 accel_zero_across = AltosLib.MISSING;
794 accel_zero_through = AltosLib.MISSING;
796 accel_ground_along = AltosLib.MISSING;
797 accel_ground_across = AltosLib.MISSING;
798 accel_ground_through = AltosLib.MISSING;
800 pad_orientation = AltosLib.MISSING;
802 gyro_zero_roll = AltosLib.MISSING;
803 gyro_zero_pitch = AltosLib.MISSING;
804 gyro_zero_yaw = AltosLib.MISSING;
810 elevation = AltosLib.MISSING;
811 range = AltosLib.MISSING;
812 gps_height = AltosLib.MISSING;
814 pad_lat = AltosLib.MISSING;
815 pad_lon = AltosLib.MISSING;
816 pad_alt = AltosLib.MISSING;
818 gps_altitude = new AltosGpsAltitude();
819 gps_ground_altitude = new AltosGpsGroundAltitude();
820 gps_ground_speed = new AltosValue();
821 gps_speed = new AltosValue();
822 gps_ascent_rate = new AltosValue();
823 gps_course = new AltosValue();
825 speak_tick = AltosLib.MISSING;
826 speak_altitude = AltosLib.MISSING;
829 firmware_version = null;
831 accel_plus_g = AltosLib.MISSING;
832 accel_minus_g = AltosLib.MISSING;
833 accel = AltosLib.MISSING;
835 ground_accel = AltosLib.MISSING;
836 ground_accel_avg = AltosLib.MISSING;
838 log_format = AltosLib.MISSING;
839 log_space = AltosLib.MISSING;
841 serial = AltosLib.MISSING;
842 receiver_serial = AltosLib.MISSING;
843 altitude_32 = AltosLib.MISSING;
851 void finish_update() {
854 ground_altitude.finish_update();
855 altitude.finish_update();
856 pressure.finish_update();
857 speed.finish_update();
858 acceleration.finish_update();
859 orient.finish_update();
861 kalman_height.finish_update();
862 kalman_speed.finish_update();
863 kalman_acceleration.finish_update();
866 void copy(AltosState old) {
873 received_time = old.received_time;
875 time_change = old.time_change;
876 prev_time = old.time;
879 prev_tick = old.tick;
880 boost_tick = old.boost_tick;
889 device_type = old.device_type;
890 config_major = old.config_major;
891 config_minor = old.config_minor;
892 apogee_delay = old.apogee_delay;
893 main_deploy = old.main_deploy;
894 flight_log_max = old.flight_log_max;
898 ground_pressure.copy(old.ground_pressure);
899 ground_altitude.copy(old.ground_altitude);
900 altitude.copy(old.altitude);
901 pressure.copy(old.pressure);
902 speed.copy(old.speed);
903 acceleration.copy(old.acceleration);
904 orient.copy(old.orient);
906 battery_voltage = old.battery_voltage;
907 pyro_voltage = old.pyro_voltage;
908 temperature = old.temperature;
909 apogee_voltage = old.apogee_voltage;
910 main_voltage = old.main_voltage;
911 ignitor_voltage = old.ignitor_voltage;
913 kalman_height.copy(old.kalman_height);
914 kalman_speed.copy(old.kalman_speed);
915 kalman_acceleration.copy(old.kalman_acceleration);
918 gps = old.gps.clone();
921 if (old.temp_gps != null)
922 temp_gps = old.temp_gps.clone();
925 temp_gps_sat_tick = old.temp_gps_sat_tick;
926 gps_sequence = old.gps_sequence;
927 gps_pending = old.gps_pending;
930 imu = old.imu.clone();
933 last_imu_time = old.last_imu_time;
935 if (old.rotation != null)
936 rotation = new AltosRotation (old.rotation);
938 if (old.ground_rotation != null) {
939 ground_rotation = new AltosRotation(old.ground_rotation);
942 accel_zero_along = old.accel_zero_along;
943 accel_zero_across = old.accel_zero_across;
944 accel_zero_through = old.accel_zero_through;
946 accel_ground_along = old.accel_ground_along;
947 accel_ground_across = old.accel_ground_across;
948 accel_ground_through = old.accel_ground_through;
949 pad_orientation = old.pad_orientation;
951 gyro_zero_roll = old.gyro_zero_roll;
952 gyro_zero_pitch = old.gyro_zero_pitch;
953 gyro_zero_yaw = old.gyro_zero_yaw;
956 mag = old.mag.clone();
961 gps_waiting = old.gps_waiting;
962 gps_ready = old.gps_ready;
965 if (old.from_pad != null)
966 from_pad = old.from_pad.clone();
970 elevation = old.elevation;
973 gps_height = old.gps_height;
975 gps_altitude.copy(old.gps_altitude);
976 gps_ground_altitude.copy(old.gps_ground_altitude);
977 gps_ground_speed.copy(old.gps_ground_speed);
978 gps_ascent_rate.copy(old.gps_ascent_rate);
979 gps_course.copy(old.gps_course);
980 gps_speed.copy(old.gps_speed);
982 pad_lat = old.pad_lat;
983 pad_lon = old.pad_lon;
984 pad_alt = old.pad_alt;
986 speak_tick = old.speak_tick;
987 speak_altitude = old.speak_altitude;
989 callsign = old.callsign;
990 firmware_version = old.firmware_version;
992 accel_plus_g = old.accel_plus_g;
993 accel_minus_g = old.accel_minus_g;
995 ground_accel = old.ground_accel;
996 ground_accel_avg = old.ground_accel_avg;
998 log_format = old.log_format;
999 log_space = old.log_space;
1000 product = old.product;
1001 serial = old.serial;
1002 receiver_serial = old.receiver_serial;
1003 altitude_32 = old.altitude_32;
1006 companion = old.companion;
1008 pyro_fired = old.pyro_fired;
1011 void update_time() {
1015 elevation = AltosLib.MISSING;
1016 range = AltosLib.MISSING;
1021 if (gps.locked && gps.nsat >= 4) {
1022 /* Track consecutive 'good' gps reports, waiting for 10 of them */
1023 if (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_stateless) {
1025 if (pad_lat != AltosLib.MISSING && (npad < 10 || state == AltosLib.ao_flight_pad)) {
1026 pad_lat = (pad_lat * 31 + gps.lat) / 32;
1027 pad_lon = (pad_lon * 31 + gps.lon) / 32;
1028 gps_ground_altitude.set_filtered(gps.alt, time);
1031 if (pad_lat == AltosLib.MISSING) {
1034 gps_ground_altitude.set(gps.alt, time);
1036 gps_altitude.set(gps.alt, time);
1037 if (gps.climb_rate != AltosLib.MISSING)
1038 gps_ascent_rate.set(gps.climb_rate, time);
1039 if (gps.ground_speed != AltosLib.MISSING)
1040 gps_ground_speed.set(gps.ground_speed, time);
1041 if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
1042 gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
1043 gps.climb_rate * gps.climb_rate), time);
1044 if (gps.course != AltosLib.MISSING)
1045 gps_course.set(gps.course, time);
1047 if (gps.lat != 0 && gps.lon != 0 &&
1048 pad_lat != AltosLib.MISSING &&
1049 pad_lon != AltosLib.MISSING)
1051 double h = height();
1053 if (h == AltosLib.MISSING)
1055 from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
1056 elevation = from_pad.elevation;
1057 range = from_pad.range;
1061 public void set_tick(int new_tick) {
1062 if (new_tick != AltosLib.MISSING) {
1063 if (prev_tick != AltosLib.MISSING) {
1064 while (new_tick < prev_tick - 1000) {
1069 time = tick / 100.0;
1070 time_change = time - prev_time;
1074 public void set_boost_tick(int boost_tick) {
1075 if (boost_tick != AltosLib.MISSING)
1076 this.boost_tick = boost_tick;
1079 public String state_name() {
1080 return AltosLib.state_name(state);
1083 public void set_product(String product) {
1084 this.product = product;
1087 public void set_state(int state) {
1088 if (state != AltosLib.ao_flight_invalid) {
1090 ascent = (AltosLib.ao_flight_boost <= state &&
1091 state <= AltosLib.ao_flight_coast);
1092 boost = (AltosLib.ao_flight_boost == state);
1096 public int state() {
1100 public void set_device_type(int device_type) {
1101 this.device_type = device_type;
1102 switch (device_type) {
1103 case AltosLib.product_telegps:
1104 this.state = AltosLib.ao_flight_stateless;
1109 public void set_log_format(int log_format) {
1110 this.log_format = log_format;
1111 switch (log_format) {
1112 case AltosLib.AO_LOG_FORMAT_TELEGPS:
1113 this.state = AltosLib.ao_flight_stateless;
1118 public void set_log_space(int log_space) {
1119 this.log_space = log_space;
1122 public void set_flight_params(int apogee_delay, int main_deploy) {
1123 this.apogee_delay = apogee_delay;
1124 this.main_deploy = main_deploy;
1127 public void set_config(int major, int minor, int flight_log_max) {
1128 config_major = major;
1129 config_minor = minor;
1130 this.flight_log_max = flight_log_max;
1133 public void set_callsign(String callsign) {
1134 this.callsign = callsign;
1137 public void set_firmware_version(String version) {
1138 firmware_version = version;
1141 public int compare_version(String other_version) {
1142 if (firmware_version == null)
1143 return AltosLib.MISSING;
1144 return AltosLib.compare_version(firmware_version, other_version);
1147 private void re_init() {
1148 int bt = boost_tick;
1149 int rs = receiver_serial;
1152 receiver_serial = rs;
1155 public void set_flight(int flight) {
1157 /* When the flight changes, reset the state */
1158 if (flight != AltosLib.MISSING) {
1159 if (this.flight != AltosLib.MISSING &&
1160 this.flight != flight) {
1163 this.flight = flight;
1167 public void set_serial(int serial) {
1168 /* When the serial changes, reset the state */
1169 if (serial != AltosLib.MISSING) {
1170 if (this.serial != AltosLib.MISSING &&
1171 this.serial != serial) {
1174 this.serial = serial;
1178 public void set_receiver_serial(int serial) {
1179 if (serial != AltosLib.MISSING)
1180 receiver_serial = serial;
1183 public boolean altitude_32() {
1184 return altitude_32 == 1;
1187 public void set_altitude_32(int altitude_32) {
1188 if (altitude_32 != AltosLib.MISSING)
1189 this.altitude_32 = altitude_32;
1193 if (rssi == AltosLib.MISSING)
1198 public void set_rssi(int rssi, int status) {
1199 if (rssi != AltosLib.MISSING) {
1201 this.status = status;
1205 public void set_received_time(long ms) {
1209 public void set_gps(AltosGPS gps, int sequence) {
1211 this.gps = gps.clone();
1212 gps_sequence = sequence;
1219 public double accel_zero_along;
1220 public double accel_zero_across;
1221 public double accel_zero_through;
1223 public AltosRotation rotation;
1224 public AltosRotation ground_rotation;
1226 public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
1227 if (zero_along != AltosLib.MISSING) {
1228 accel_zero_along = zero_along;
1229 accel_zero_across = zero_across;
1230 accel_zero_through = zero_through;
1234 public int pad_orientation;
1236 public double accel_ground_along, accel_ground_across, accel_ground_through;
1238 void update_pad_rotation() {
1239 if (pad_orientation != AltosLib.MISSING && accel_ground_along != AltosLib.MISSING) {
1240 rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - accel_zero_across),
1241 AltosIMU.convert_accel(accel_ground_through - accel_zero_through),
1242 AltosIMU.convert_accel(accel_ground_along - accel_zero_along),
1244 ground_rotation = rotation;
1245 orient.set_computed(rotation.tilt(), time);
1249 public void set_accel_ground(double ground_along, double ground_across, double ground_through) {
1250 accel_ground_along = ground_along;
1251 accel_ground_across = ground_across;
1252 accel_ground_through = ground_through;
1253 update_pad_rotation();
1256 public void set_pad_orientation(int pad_orientation) {
1257 this.pad_orientation = pad_orientation;
1258 update_pad_rotation();
1261 public double gyro_zero_roll;
1262 public double gyro_zero_pitch;
1263 public double gyro_zero_yaw;
1265 public void set_gyro_zero(double roll, double pitch, double yaw) {
1266 if (roll != AltosLib.MISSING) {
1267 gyro_zero_roll = roll;
1268 gyro_zero_pitch = pitch;
1269 gyro_zero_yaw = yaw;
1273 public double last_imu_time;
1275 private double radians(double degrees) {
1276 if (degrees == AltosLib.MISSING)
1277 return AltosLib.MISSING;
1278 return degrees * Math.PI / 180.0;
1281 private void update_orient() {
1282 if (last_imu_time != AltosLib.MISSING) {
1283 double t = time - last_imu_time;
1285 double pitch = radians(gyro_pitch());
1286 double yaw = radians(gyro_yaw());
1287 double roll = radians(gyro_roll());
1289 if (t > 0 & pitch != AltosLib.MISSING && rotation != null) {
1290 rotation.rotate(t, pitch, yaw, roll);
1291 orient.set_computed(rotation.tilt(), time);
1294 last_imu_time = time;
1297 public void set_imu(AltosIMU imu) {
1304 private double gyro_zero_overflow(double first) {
1305 double v = first / 128.0;
1313 public void check_imu_wrap(AltosIMU imu) {
1314 if (this.imu == null) {
1315 gyro_zero_roll += gyro_zero_overflow(imu.gyro_roll);
1316 gyro_zero_pitch += gyro_zero_overflow(imu.gyro_pitch);
1317 gyro_zero_yaw += gyro_zero_overflow(imu.gyro_yaw);
1321 public double accel_along() {
1322 if (imu != null && accel_zero_along != AltosLib.MISSING)
1323 return AltosIMU.convert_accel(imu.accel_along - accel_zero_along);
1324 return AltosLib.MISSING;
1327 public double accel_across() {
1328 if (imu != null && accel_zero_across != AltosLib.MISSING)
1329 return AltosIMU.convert_accel(imu.accel_across - accel_zero_across);
1330 return AltosLib.MISSING;
1333 public double accel_through() {
1334 if (imu != null && accel_zero_through != AltosLib.MISSING)
1335 return AltosIMU.convert_accel(imu.accel_through - accel_zero_through);
1336 return AltosLib.MISSING;
1339 public double gyro_roll() {
1340 if (imu != null && gyro_zero_roll != AltosLib.MISSING) {
1341 return AltosIMU.convert_gyro(imu.gyro_roll - gyro_zero_roll);
1343 return AltosLib.MISSING;
1346 public double gyro_pitch() {
1347 if (imu != null && gyro_zero_pitch != AltosLib.MISSING) {
1348 return AltosIMU.convert_gyro(imu.gyro_pitch - gyro_zero_pitch);
1350 return AltosLib.MISSING;
1353 public double gyro_yaw() {
1354 if (imu != null && gyro_zero_yaw != AltosLib.MISSING) {
1355 return AltosIMU.convert_gyro(imu.gyro_yaw - gyro_zero_yaw);
1357 return AltosLib.MISSING;
1360 public void set_mag(AltosMag mag) {
1361 this.mag = mag.clone();
1364 public double mag_along() {
1366 return AltosMag.convert_gauss(mag.along);
1367 return AltosLib.MISSING;
1370 public double mag_across() {
1372 return AltosMag.convert_gauss(mag.across);
1373 return AltosLib.MISSING;
1376 public double mag_through() {
1378 return AltosMag.convert_gauss(mag.through);
1379 return AltosLib.MISSING;
1382 public AltosMs5607 make_baro() {
1384 baro = new AltosMs5607();
1388 public void set_ms5607(AltosMs5607 ms5607) {
1392 set_pressure(baro.pa);
1393 set_temperature(baro.cc / 100.0);
1397 public void set_ms5607(int pres, int temp) {
1399 baro.set(pres, temp);
1401 set_pressure(baro.pa);
1402 set_temperature(baro.cc / 100.0);
1406 public void set_companion(AltosCompanion companion) {
1407 this.companion = companion;
1410 void update_accel() {
1411 if (accel == AltosLib.MISSING)
1413 if (accel_plus_g == AltosLib.MISSING)
1415 if (accel_minus_g == AltosLib.MISSING)
1418 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
1419 double counts_per_mss = counts_per_g / 9.80665;
1420 acceleration.set_measured((accel_plus_g - accel) / counts_per_mss, time);
1423 public void set_accel_g(double accel_plus_g, double accel_minus_g) {
1424 if (accel_plus_g != AltosLib.MISSING) {
1425 this.accel_plus_g = accel_plus_g;
1426 this.accel_minus_g = accel_minus_g;
1431 public void set_ground_accel(double ground_accel) {
1432 if (ground_accel != AltosLib.MISSING)
1433 this.ground_accel = ground_accel;
1436 public void set_accel(double accel) {
1437 if (accel != AltosLib.MISSING) {
1439 if (state == AltosLib.ao_flight_pad) {
1440 if (ground_accel_avg == AltosLib.MISSING)
1441 ground_accel_avg = accel;
1443 ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
1449 public void set_temperature(double temperature) {
1450 if (temperature != AltosLib.MISSING) {
1451 this.temperature = temperature;
1456 public void set_battery_voltage(double battery_voltage) {
1457 if (battery_voltage != AltosLib.MISSING) {
1458 this.battery_voltage = battery_voltage;
1463 public void set_pyro_voltage(double pyro_voltage) {
1464 if (pyro_voltage != AltosLib.MISSING) {
1465 this.pyro_voltage = pyro_voltage;
1470 public void set_apogee_voltage(double apogee_voltage) {
1471 if (apogee_voltage != AltosLib.MISSING) {
1472 this.apogee_voltage = apogee_voltage;
1477 public void set_main_voltage(double main_voltage) {
1478 if (main_voltage != AltosLib.MISSING) {
1479 this.main_voltage = main_voltage;
1484 public void set_ignitor_voltage(double[] voltage) {
1485 this.ignitor_voltage = voltage;
1488 public void set_pyro_fired(int fired) {
1489 this.pyro_fired = fired;
1492 public double time_since_boost() {
1493 if (tick == AltosLib.MISSING)
1496 if (boost_tick == AltosLib.MISSING)
1497 return tick / 100.0;
1498 return (tick - boost_tick) / 100.0;
1501 public boolean valid() {
1502 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1505 public AltosGPS make_temp_gps(boolean sats) {
1506 if (temp_gps == null) {
1507 temp_gps = new AltosGPS(gps);
1511 if (tick != temp_gps_sat_tick)
1512 temp_gps.cc_gps_sat = null;
1513 temp_gps_sat_tick = tick;
1518 public void set_temp_gps() {
1519 set_gps(temp_gps, gps_sequence + 1);
1520 gps_pending = false;
1524 public AltosState clone() {
1525 AltosState s = new AltosState();
1528 /* Code to test state save/restore. Enable only for that purpose
1531 AltosJson json = new AltosJson(this);
1532 String onetrip = json.toPrettyString();
1533 AltosJson back = AltosJson.fromString(onetrip);
1534 AltosState tripstate = (AltosState) back.make(this.getClass());
1535 AltosJson tripjson = new AltosJson(tripstate);
1536 String twotrip = tripjson.toPrettyString();
1538 if (!onetrip.equals(twotrip)) {
1540 FileWriter one_file = new FileWriter("one.json", true);
1541 one_file.write(onetrip);
1543 FileWriter two_file = new FileWriter("two.json", true);
1544 two_file.write(twotrip);
1546 } catch (Exception e) {
1548 System.out.printf("json error\n");
1555 public AltosState () {