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_11;
26 public class AltosState implements Cloneable, AltosHashable {
28 public static final int set_position = 1;
29 public static final int set_gps = 2;
30 public static final int set_data = 4;
34 static final double filter_len = 2.0;
35 static final double ascent_filter_len = 0.5;
36 static final double descent_filter_len = 5.0;
40 public long received_time;
43 public double prev_time;
44 public double time_change;
46 private int prev_tick;
47 public int boost_tick;
49 class AltosValue implements AltosHashable, AltosJsonable {
52 private double max_value;
53 private double set_time;
54 private double prev_set_time;
56 boolean can_max() { return true; }
58 void set(double new_value, double time) {
59 if (new_value != AltosLib.MISSING) {
61 if (can_max() && (max_value == AltosLib.MISSING || value > max_value))
67 void set_filtered(double new_value, double time) {
68 if (prev_value != AltosLib.MISSING) {
69 double f = 1/Math.exp((time - prev_set_time) / filter_len);
70 new_value = f * new_value + (1-f) * prev_value;
88 if (value != AltosLib.MISSING && prev_value != AltosLib.MISSING)
89 return value - prev_value;
90 return AltosLib.MISSING;
95 double t = set_time - prev_set_time;
97 if (c != AltosLib.MISSING && t != 0)
99 return AltosLib.MISSING;
103 if (value == AltosLib.MISSING)
104 return AltosLib.MISSING;
105 if (prev_value == AltosLib.MISSING)
106 return AltosLib.MISSING;
108 return (value + prev_value) / 2 * (set_time - prev_set_time);
115 void set_derivative(AltosValue in) {
116 double n = in.rate();
118 if (n == AltosLib.MISSING)
121 double p = prev_value;
122 double pt = prev_set_time;
124 if (p == AltosLib.MISSING) {
126 pt = in.time() - 0.01;
129 /* Clip changes to reduce noise */
130 double ddt = in.time() - pt;
131 double ddv = (n - p) / ddt;
133 final double max = 100000;
136 if (Math.abs(ddv) > max) {
146 filter_len = ascent_filter_len;
148 filter_len = descent_filter_len;
150 double f = 1/Math.exp(ddt/ filter_len);
151 n = p * f + n * (1-f);
156 void set_integral(AltosValue in) {
157 double change = in.integrate();
159 if (change != AltosLib.MISSING) {
160 double prev = prev_value;
161 if (prev == AltosLib.MISSING)
163 set(prev + change, in.time());
167 void copy(AltosValue old) {
169 set_time = old.set_time;
170 prev_value = old.value;
171 prev_set_time = old.set_time;
172 max_value = old.max_value;
175 void finish_update() {
177 prev_set_time = set_time;
180 public AltosHashSet hashSet() {
181 AltosHashSet h = new AltosHashSet();
183 h.putDouble("value", value);
184 h.putDouble("prev_value", prev_value);
185 h.putDouble("max_value", max_value);
186 h.putDouble("set_time", set_time);
187 h.putDouble("prev_set_time", prev_set_time);
191 public AltosJson json() {
192 AltosJson j = new AltosJson();
194 j.put("value", value);
195 j.put("prev_value", prev_value);
196 j.put("max_value", max_value);
197 j.put("set_time", set_time);
198 j.put("prev_set_time", prev_set_time);
202 AltosValue(AltosHashSet h) {
205 value = h.getDouble("value", value);
206 prev_value = h.getDouble("prev_value", prev_value);
207 max_value = h.getDouble("max_value", max_value);
208 set_time = h.getDouble("set_time", 0);
209 prev_set_time = h.getDouble("prev_set_time", 0);
213 AltosValue(AltosJson j) {
216 value = j.get_double("value", value);
217 prev_value = j.get_double("prev_value", prev_value);
218 max_value = j.get_double("max_value", max_value);
219 set_time = j.get_double("set_time", 0);
220 prev_set_time = j.get_double("prev_set_time", 0);
225 value = AltosLib.MISSING;
226 prev_value = AltosLib.MISSING;
227 max_value = AltosLib.MISSING;
232 AltosValue AltosValue_fromHashSet(AltosHashSet h, AltosValue def) {
235 return new AltosValue(h);
238 AltosValue AltosValue_fromJson(AltosJson j, AltosValue def) {
241 return new AltosValue(j);
244 class AltosCValue implements AltosHashable, AltosJsonable {
246 class AltosIValue extends AltosValue implements AltosHashable, AltosJsonable {
255 AltosIValue(AltosHashSet h) {
259 AltosIValue(AltosJson j) {
264 public AltosIValue measured;
265 public AltosIValue computed;
267 boolean can_max() { return true; }
269 boolean c_can_max() { return can_max(); }
272 double v = measured.value();
273 if (v != AltosLib.MISSING)
275 return computed.value();
278 boolean is_measured() {
279 return measured.value() != AltosLib.MISSING;
283 double m = measured.max();
285 if (m != AltosLib.MISSING)
287 return computed.max();
290 double prev_value() {
291 if (measured.value != AltosLib.MISSING && measured.prev_value != AltosLib.MISSING)
292 return measured.prev_value;
293 return computed.prev_value;
296 AltosValue altos_value() {
297 if (measured.value() != AltosLib.MISSING)
303 double c = measured.change();
304 if (c == AltosLib.MISSING)
305 c = computed.change();
310 double r = measured.rate();
311 if (r == AltosLib.MISSING)
316 void set_measured(double new_value, double time) {
317 measured.set(new_value, time);
320 void set_computed(double new_value, double time) {
321 computed.set(new_value, time);
324 void set_derivative(AltosValue in) {
325 computed.set_derivative(in);
328 void set_derivative(AltosCValue in) {
329 set_derivative(in.altos_value());
332 void set_integral(AltosValue in) {
333 computed.set_integral(in);
336 void set_integral(AltosCValue in) {
337 set_integral(in.altos_value());
340 void copy(AltosCValue old) {
341 measured.copy(old.measured);
342 computed.copy(old.computed);
345 void finish_update() {
346 measured.finish_update();
347 computed.finish_update();
351 measured = new AltosIValue();
352 computed = new AltosIValue();
355 public AltosHashSet hashSet() {
356 AltosHashSet h = new AltosHashSet();
358 h.putHashable("measured", measured);
359 h.putHashable("computed", computed);
363 public AltosJson json() {
364 AltosJson j = new AltosJson();
366 j.put("measured", measured.json());
367 j.put("computed", computed.json());
371 AltosCValue(AltosHashSet h) {
372 measured = new AltosIValue(h.getHash("measured"));
373 computed = new AltosIValue(h.getHash("computed"));
376 AltosCValue(AltosJson j) {
377 measured = new AltosIValue(j.get("measured"));
378 computed = new AltosIValue(j.get("computed"));
382 AltosCValue AltosCValue_fromHashSet(AltosHashSet h, AltosCValue def) {
385 return new AltosCValue(h);
388 AltosCValue AltosCValue_fromJson(AltosJson j, AltosCValue def) {
391 return new AltosCValue(j);
397 public int altitude_32;
398 public int receiver_serial;
399 public boolean landed;
400 public boolean ascent; /* going up? */
401 public boolean boost; /* under power */
404 public int device_type;
405 public int config_major;
406 public int config_minor;
407 public int apogee_delay;
408 public int main_deploy;
409 public int flight_log_max;
411 private double pressure_to_altitude(double p) {
412 if (p == AltosLib.MISSING)
413 return AltosLib.MISSING;
414 return AltosConvert.pressure_to_altitude(p);
417 private AltosCValue ground_altitude;
419 public double ground_altitude() {
420 return ground_altitude.value();
423 public void set_ground_altitude(double a) {
424 ground_altitude.set_measured(a, time);
427 class AltosGpsGroundAltitude extends AltosValue {
428 void set(double a, double t) {
431 gps_altitude.set_gps_height();
434 void set_filtered(double a, double t) {
435 super.set_filtered(a, t);
437 gps_altitude.set_gps_height();
440 AltosGpsGroundAltitude() {
444 AltosGpsGroundAltitude (AltosHashSet h) {
448 AltosGpsGroundAltitude (AltosJson j) {
453 AltosGpsGroundAltitude AltosGpsGroundAltitude_fromHashSet(AltosHashSet h, AltosGpsGroundAltitude def) {
454 if (h == null) return def;
455 return new AltosGpsGroundAltitude(h);
458 AltosGpsGroundAltitude AltosGpsGroundAltitude_fromJson(AltosJson j, AltosGpsGroundAltitude def) {
459 if (j == null) return def;
460 return new AltosGpsGroundAltitude(j);
463 private AltosGpsGroundAltitude gps_ground_altitude;
465 public double gps_ground_altitude() {
466 return gps_ground_altitude.value();
469 public void set_gps_ground_altitude(double a) {
470 gps_ground_altitude.set(a, time);
473 class AltosGroundPressure extends AltosCValue {
474 void set_filtered(double p, double time) {
475 computed.set_filtered(p, time);
477 ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
480 void set_measured(double p, double time) {
481 super.set_measured(p, time);
482 ground_altitude.set_computed(pressure_to_altitude(p), time);
485 AltosGroundPressure () {
489 AltosGroundPressure (AltosHashSet h) {
493 AltosGroundPressure (AltosJson j) {
498 AltosGroundPressure AltosGroundPressure_fromHashSet(AltosHashSet h, AltosGroundPressure def) {
499 if (h == null) return def;
500 return new AltosGroundPressure(h);
503 AltosGroundPressure AltosGroundPressure_fromJson(AltosJson j, AltosGroundPressure def) {
504 if (j == null) return def;
505 return new AltosGroundPressure(j);
508 private AltosGroundPressure ground_pressure;
510 public double ground_pressure() {
511 return ground_pressure.value();
514 public void set_ground_pressure (double pressure) {
515 ground_pressure.set_measured(pressure, time);
518 class AltosAltitude extends AltosCValue implements AltosHashable {
520 private void set_speed(AltosValue v) {
521 if (!acceleration.is_measured() || !ascent)
522 speed.set_derivative(this);
525 void set_computed(double a, double time) {
526 super.set_computed(a,time);
531 void set_measured(double a, double time) {
532 super.set_measured(a,time);
541 AltosAltitude (AltosHashSet h) {
545 AltosAltitude (AltosJson j) {
550 AltosAltitude AltosAltitude_fromHashSet(AltosHashSet h, AltosAltitude def) {
551 if (h == null) return def;
552 return new AltosAltitude(h);
555 AltosAltitude AltosAltitude_fromJson(AltosJson j, AltosAltitude def) {
556 if (j == null) return def;
557 return new AltosAltitude(j);
560 private AltosAltitude altitude;
562 class AltosGpsAltitude extends AltosValue implements AltosHashable {
564 private void set_gps_height() {
566 double g = gps_ground_altitude.value();
568 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
571 gps_height = AltosLib.MISSING;
574 void set(double a, double t) {
583 AltosGpsAltitude (AltosHashSet h) {
587 AltosGpsAltitude (AltosJson j) {
592 AltosGpsAltitude AltosGpsAltitude_fromHashSet(AltosHashSet h, AltosGpsAltitude def) {
593 if (h == null) return def;
594 return new AltosGpsAltitude(h);
597 AltosGpsAltitude AltosGpsAltitude_fromJson(AltosJson j, AltosGpsAltitude def) {
598 if (j == null) return def;
599 return new AltosGpsAltitude(j);
602 private AltosGpsAltitude gps_altitude;
604 private AltosValue gps_ground_speed;
605 private AltosValue gps_ascent_rate;
606 private AltosValue gps_course;
607 private AltosValue gps_speed;
609 public double altitude() {
610 double a = altitude.value();
611 if (a != AltosLib.MISSING)
613 return gps_altitude.value();
616 public double max_altitude() {
617 double a = altitude.max();
618 if (a != AltosLib.MISSING)
620 return gps_altitude.max();
623 public void set_altitude(double new_altitude) {
624 altitude.set_measured(new_altitude, time);
627 public double gps_altitude() {
628 return gps_altitude.value();
631 public double max_gps_altitude() {
632 return gps_altitude.max();
635 public void set_gps_altitude(double new_gps_altitude) {
636 gps_altitude.set(new_gps_altitude, time);
639 public double gps_ground_speed() {
640 return gps_ground_speed.value();
643 public double max_gps_ground_speed() {
644 return gps_ground_speed.max();
647 public double gps_ascent_rate() {
648 return gps_ascent_rate.value();
651 public double max_gps_ascent_rate() {
652 return gps_ascent_rate.max();
655 public double gps_course() {
656 return gps_course.value();
659 public double gps_speed() {
660 return gps_speed.value();
663 public double max_gps_speed() {
664 return gps_speed.max();
667 class AltosPressure extends AltosValue {
668 void set(double p, double time) {
670 if (state == AltosLib.ao_flight_pad)
671 ground_pressure.set_filtered(p, time);
672 double a = pressure_to_altitude(p);
673 altitude.set_computed(a, time);
680 AltosPressure (AltosHashSet h) {
684 AltosPressure (AltosJson j) {
689 AltosPressure AltosPressure_fromHashSet(AltosHashSet h, AltosPressure def) {
690 if (h == null) return def;
691 return new AltosPressure(h);
694 AltosPressure AltosPressure_fromJson(AltosJson j, AltosPressure def) {
695 if (j == null) return def;
696 return new AltosPressure(j);
699 private AltosPressure pressure;
701 public double pressure() {
702 return pressure.value();
705 public void set_pressure(double p) {
706 pressure.set(p, time);
709 public double baro_height() {
710 double a = altitude();
711 double g = ground_altitude();
712 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
714 return AltosLib.MISSING;
717 public double height() {
718 double k = kalman_height.value();
719 if (k != AltosLib.MISSING)
722 double b = baro_height();
723 if (b != AltosLib.MISSING)
729 public double max_height() {
730 double k = kalman_height.max();
731 if (k != AltosLib.MISSING)
734 double a = altitude.max();
735 double g = ground_altitude();
736 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
738 return max_gps_height();
741 public double gps_height() {
742 double a = gps_altitude();
743 double g = gps_ground_altitude();
745 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
747 return AltosLib.MISSING;
750 public double max_gps_height() {
751 double a = gps_altitude.max();
752 double g = gps_ground_altitude();
754 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
756 return AltosLib.MISSING;
759 class AltosSpeed extends AltosCValue implements AltosHashable {
762 return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
766 acceleration.set_derivative(this);
769 void set_derivative(AltosCValue in) {
770 super.set_derivative(in);
774 void set_computed(double new_value, double time) {
775 super.set_computed(new_value, time);
779 void set_measured(double new_value, double time) {
780 super.set_measured(new_value, time);
788 AltosSpeed (AltosHashSet h) {
792 AltosSpeed (AltosJson j) {
797 AltosSpeed AltosSpeed_fromHashSet(AltosHashSet h, AltosSpeed def) {
798 if (h == null) return def;
799 return new AltosSpeed(h);
802 AltosSpeed AltosSpeed_fromJson(AltosJson j, AltosSpeed def) {
803 if (j == null) return def;
804 return new AltosSpeed(j);
807 private AltosSpeed speed;
809 public double speed() {
810 double v = kalman_speed.value();
811 if (v != AltosLib.MISSING)
814 if (v != AltosLib.MISSING)
817 if (v != AltosLib.MISSING)
819 return AltosLib.MISSING;
822 public double max_speed() {
823 double v = kalman_speed.max();
824 if (v != AltosLib.MISSING)
827 if (v != AltosLib.MISSING)
830 if (v != AltosLib.MISSING)
832 return AltosLib.MISSING;
835 class AltosAccel extends AltosCValue implements AltosHashable {
838 return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
841 void set_measured(double a, double time) {
842 super.set_measured(a, time);
844 speed.set_integral(this.measured);
851 AltosAccel (AltosHashSet h) {
855 AltosAccel (AltosJson j) {
860 AltosAccel AltosAccel_fromHashSet(AltosHashSet h, AltosAccel def) {
861 if (h == null) return def;
862 return new AltosAccel(h);
865 AltosAccel AltosAccel_fromJson(AltosJson j, AltosAccel def) {
866 if (j == null) return def;
867 return new AltosAccel(j);
870 AltosAccel acceleration;
872 public double acceleration() {
873 return acceleration.value();
876 public double max_acceleration() {
877 return acceleration.max();
880 public AltosCValue orient;
882 public void set_orient(double new_orient) {
883 orient.set_measured(new_orient, time);
886 public double orient() {
887 return orient.value();
890 public double max_orient() {
894 public AltosValue kalman_height, kalman_speed, kalman_acceleration;
896 public void set_kalman(double height, double speed, double acceleration) {
897 kalman_height.set(height, time);
898 kalman_speed.set(speed, time);
899 kalman_acceleration.set(acceleration, time);
902 public double battery_voltage;
903 public double pyro_voltage;
904 public double temperature;
905 public double apogee_voltage;
906 public double main_voltage;
908 public double ignitor_voltage[];
911 public AltosGPS temp_gps;
912 public int temp_gps_sat_tick;
913 public boolean gps_pending;
914 public int gps_sequence;
919 public static final int MIN_PAD_SAMPLES = 10;
922 public int gps_waiting;
923 public boolean gps_ready;
927 public AltosGreatCircle from_pad;
928 public double elevation; /* from pad */
929 public double range; /* total distance */
931 public double gps_height;
933 public double pad_lat, pad_lon, pad_alt;
935 public int speak_tick;
936 public double speak_altitude;
938 public String callsign;
939 public String firmware_version;
941 public double accel_plus_g;
942 public double accel_minus_g;
944 public double ground_accel;
945 public double ground_accel_avg;
947 public int log_format;
948 public int log_space;
949 public String product;
951 public AltosMs5607 baro;
953 public AltosCompanion companion;
955 public int pyro_fired;
957 public void set_npad(int npad) {
959 gps_waiting = MIN_PAD_SAMPLES - npad;
960 if (this.gps_waiting < 0)
962 gps_ready = gps_waiting == 0;
968 received_time = System.currentTimeMillis();
969 time = AltosLib.MISSING;
970 time_change = AltosLib.MISSING;
971 prev_time = AltosLib.MISSING;
972 tick = AltosLib.MISSING;
973 prev_tick = AltosLib.MISSING;
974 boost_tick = AltosLib.MISSING;
975 state = AltosLib.ao_flight_invalid;
976 flight = AltosLib.MISSING;
979 rssi = AltosLib.MISSING;
981 device_type = AltosLib.MISSING;
982 config_major = AltosLib.MISSING;
983 config_minor = AltosLib.MISSING;
984 apogee_delay = AltosLib.MISSING;
985 main_deploy = AltosLib.MISSING;
986 flight_log_max = AltosLib.MISSING;
988 ground_altitude = new AltosCValue();
989 ground_pressure = new AltosGroundPressure();
990 altitude = new AltosAltitude();
991 pressure = new AltosPressure();
992 speed = new AltosSpeed();
993 acceleration = new AltosAccel();
994 orient = new AltosCValue();
996 temperature = AltosLib.MISSING;
997 battery_voltage = AltosLib.MISSING;
998 pyro_voltage = AltosLib.MISSING;
999 apogee_voltage = AltosLib.MISSING;
1000 main_voltage = AltosLib.MISSING;
1001 ignitor_voltage = null;
1003 kalman_height = new AltosValue();
1004 kalman_speed = new AltosValue();
1005 kalman_acceleration = new AltosValue();
1009 temp_gps_sat_tick = 0;
1011 gps_pending = false;
1014 last_imu_time = AltosLib.MISSING;
1016 ground_rotation = null;
1019 accel_zero_along = AltosLib.MISSING;
1020 accel_zero_across = AltosLib.MISSING;
1021 accel_zero_through = AltosLib.MISSING;
1023 accel_ground_along = AltosLib.MISSING;
1024 accel_ground_across = AltosLib.MISSING;
1025 accel_ground_through = AltosLib.MISSING;
1027 pad_orientation = AltosLib.MISSING;
1029 gyro_zero_roll = AltosLib.MISSING;
1030 gyro_zero_pitch = AltosLib.MISSING;
1031 gyro_zero_yaw = AltosLib.MISSING;
1037 elevation = AltosLib.MISSING;
1038 range = AltosLib.MISSING;
1039 gps_height = AltosLib.MISSING;
1041 pad_lat = AltosLib.MISSING;
1042 pad_lon = AltosLib.MISSING;
1043 pad_alt = AltosLib.MISSING;
1045 gps_altitude = new AltosGpsAltitude();
1046 gps_ground_altitude = new AltosGpsGroundAltitude();
1047 gps_ground_speed = new AltosValue();
1048 gps_speed = new AltosValue();
1049 gps_ascent_rate = new AltosValue();
1050 gps_course = new AltosValue();
1052 speak_tick = AltosLib.MISSING;
1053 speak_altitude = AltosLib.MISSING;
1056 firmware_version = null;
1058 accel_plus_g = AltosLib.MISSING;
1059 accel_minus_g = AltosLib.MISSING;
1060 accel = AltosLib.MISSING;
1062 ground_accel = AltosLib.MISSING;
1063 ground_accel_avg = AltosLib.MISSING;
1065 log_format = AltosLib.MISSING;
1066 log_space = AltosLib.MISSING;
1068 serial = AltosLib.MISSING;
1069 receiver_serial = AltosLib.MISSING;
1070 altitude_32 = AltosLib.MISSING;
1078 void finish_update() {
1081 ground_altitude.finish_update();
1082 altitude.finish_update();
1083 pressure.finish_update();
1084 speed.finish_update();
1085 acceleration.finish_update();
1086 orient.finish_update();
1088 kalman_height.finish_update();
1089 kalman_speed.finish_update();
1090 kalman_acceleration.finish_update();
1093 void copy(AltosState old) {
1100 received_time = old.received_time;
1102 time_change = old.time_change;
1103 prev_time = old.time;
1106 prev_tick = old.tick;
1107 boost_tick = old.boost_tick;
1110 flight = old.flight;
1111 landed = old.landed;
1112 ascent = old.ascent;
1115 status = old.status;
1116 device_type = old.device_type;
1117 config_major = old.config_major;
1118 config_minor = old.config_minor;
1119 apogee_delay = old.apogee_delay;
1120 main_deploy = old.main_deploy;
1121 flight_log_max = old.flight_log_max;
1125 ground_pressure.copy(old.ground_pressure);
1126 ground_altitude.copy(old.ground_altitude);
1127 altitude.copy(old.altitude);
1128 pressure.copy(old.pressure);
1129 speed.copy(old.speed);
1130 acceleration.copy(old.acceleration);
1131 orient.copy(old.orient);
1133 battery_voltage = old.battery_voltage;
1134 pyro_voltage = old.pyro_voltage;
1135 temperature = old.temperature;
1136 apogee_voltage = old.apogee_voltage;
1137 main_voltage = old.main_voltage;
1138 ignitor_voltage = old.ignitor_voltage;
1140 kalman_height.copy(old.kalman_height);
1141 kalman_speed.copy(old.kalman_speed);
1142 kalman_acceleration.copy(old.kalman_acceleration);
1144 if (old.gps != null)
1145 gps = old.gps.clone();
1148 if (old.temp_gps != null)
1149 temp_gps = old.temp_gps.clone();
1152 temp_gps_sat_tick = old.temp_gps_sat_tick;
1153 gps_sequence = old.gps_sequence;
1154 gps_pending = old.gps_pending;
1156 if (old.imu != null)
1157 imu = old.imu.clone();
1160 last_imu_time = old.last_imu_time;
1162 if (old.rotation != null)
1163 rotation = new AltosRotation (old.rotation);
1165 if (old.ground_rotation != null) {
1166 ground_rotation = new AltosRotation(old.ground_rotation);
1169 accel_zero_along = old.accel_zero_along;
1170 accel_zero_across = old.accel_zero_across;
1171 accel_zero_through = old.accel_zero_through;
1173 accel_ground_along = old.accel_ground_along;
1174 accel_ground_across = old.accel_ground_across;
1175 accel_ground_through = old.accel_ground_through;
1176 pad_orientation = old.pad_orientation;
1178 gyro_zero_roll = old.gyro_zero_roll;
1179 gyro_zero_pitch = old.gyro_zero_pitch;
1180 gyro_zero_yaw = old.gyro_zero_yaw;
1182 if (old.mag != null)
1183 mag = old.mag.clone();
1188 gps_waiting = old.gps_waiting;
1189 gps_ready = old.gps_ready;
1192 if (old.from_pad != null)
1193 from_pad = old.from_pad.clone();
1197 elevation = old.elevation;
1200 gps_height = old.gps_height;
1202 gps_altitude.copy(old.gps_altitude);
1203 gps_ground_altitude.copy(old.gps_ground_altitude);
1204 gps_ground_speed.copy(old.gps_ground_speed);
1205 gps_ascent_rate.copy(old.gps_ascent_rate);
1206 gps_course.copy(old.gps_course);
1207 gps_speed.copy(old.gps_speed);
1209 pad_lat = old.pad_lat;
1210 pad_lon = old.pad_lon;
1211 pad_alt = old.pad_alt;
1213 speak_tick = old.speak_tick;
1214 speak_altitude = old.speak_altitude;
1216 callsign = old.callsign;
1217 firmware_version = old.firmware_version;
1219 accel_plus_g = old.accel_plus_g;
1220 accel_minus_g = old.accel_minus_g;
1222 ground_accel = old.ground_accel;
1223 ground_accel_avg = old.ground_accel_avg;
1225 log_format = old.log_format;
1226 log_space = old.log_space;
1227 product = old.product;
1228 serial = old.serial;
1229 receiver_serial = old.receiver_serial;
1230 altitude_32 = old.altitude_32;
1233 companion = old.companion;
1235 pyro_fired = old.pyro_fired;
1238 void update_time() {
1242 elevation = AltosLib.MISSING;
1243 range = AltosLib.MISSING;
1248 if (gps.locked && gps.nsat >= 4) {
1249 /* Track consecutive 'good' gps reports, waiting for 10 of them */
1250 if (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_stateless) {
1252 if (pad_lat != AltosLib.MISSING && (npad < 10 || state == AltosLib.ao_flight_pad)) {
1253 pad_lat = (pad_lat * 31 + gps.lat) / 32;
1254 pad_lon = (pad_lon * 31 + gps.lon) / 32;
1255 gps_ground_altitude.set_filtered(gps.alt, time);
1258 if (pad_lat == AltosLib.MISSING) {
1261 gps_ground_altitude.set(gps.alt, time);
1263 gps_altitude.set(gps.alt, time);
1264 if (gps.climb_rate != AltosLib.MISSING)
1265 gps_ascent_rate.set(gps.climb_rate, time);
1266 if (gps.ground_speed != AltosLib.MISSING)
1267 gps_ground_speed.set(gps.ground_speed, time);
1268 if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
1269 gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
1270 gps.climb_rate * gps.climb_rate), time);
1271 if (gps.course != AltosLib.MISSING)
1272 gps_course.set(gps.course, time);
1274 if (gps.lat != 0 && gps.lon != 0 &&
1275 pad_lat != AltosLib.MISSING &&
1276 pad_lon != AltosLib.MISSING)
1278 double h = height();
1280 if (h == AltosLib.MISSING)
1282 from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
1283 elevation = from_pad.elevation;
1284 range = from_pad.range;
1288 public void set_tick(int new_tick) {
1289 if (new_tick != AltosLib.MISSING) {
1290 if (prev_tick != AltosLib.MISSING) {
1291 while (new_tick < prev_tick - 1000) {
1296 time = tick / 100.0;
1297 time_change = time - prev_time;
1301 public void set_boost_tick(int boost_tick) {
1302 if (boost_tick != AltosLib.MISSING)
1303 this.boost_tick = boost_tick;
1306 public String state_name() {
1307 return AltosLib.state_name(state);
1310 public void set_product(String product) {
1311 this.product = product;
1314 public void set_state(int state) {
1315 if (state != AltosLib.ao_flight_invalid) {
1317 ascent = (AltosLib.ao_flight_boost <= state &&
1318 state <= AltosLib.ao_flight_coast);
1319 boost = (AltosLib.ao_flight_boost == state);
1323 public int state() {
1327 public void set_device_type(int device_type) {
1328 this.device_type = device_type;
1329 switch (device_type) {
1330 case AltosLib.product_telegps:
1331 this.state = AltosLib.ao_flight_stateless;
1336 public void set_log_format(int log_format) {
1337 this.log_format = log_format;
1338 switch (log_format) {
1339 case AltosLib.AO_LOG_FORMAT_TELEGPS:
1340 this.state = AltosLib.ao_flight_stateless;
1345 public void set_log_space(int log_space) {
1346 this.log_space = log_space;
1349 public void set_flight_params(int apogee_delay, int main_deploy) {
1350 this.apogee_delay = apogee_delay;
1351 this.main_deploy = main_deploy;
1354 public void set_config(int major, int minor, int flight_log_max) {
1355 config_major = major;
1356 config_minor = minor;
1357 this.flight_log_max = flight_log_max;
1360 public void set_callsign(String callsign) {
1361 this.callsign = callsign;
1364 public void set_firmware_version(String version) {
1365 firmware_version = version;
1368 public int compare_version(String other_version) {
1369 if (firmware_version == null)
1370 return AltosLib.MISSING;
1371 return AltosLib.compare_version(firmware_version, other_version);
1374 private void re_init() {
1375 int bt = boost_tick;
1376 int rs = receiver_serial;
1379 receiver_serial = rs;
1382 public void set_flight(int flight) {
1384 /* When the flight changes, reset the state */
1385 if (flight != AltosLib.MISSING) {
1386 if (this.flight != AltosLib.MISSING &&
1387 this.flight != flight) {
1390 this.flight = flight;
1394 public void set_serial(int serial) {
1395 /* When the serial changes, reset the state */
1396 if (serial != AltosLib.MISSING) {
1397 if (this.serial != AltosLib.MISSING &&
1398 this.serial != serial) {
1401 this.serial = serial;
1405 public void set_receiver_serial(int serial) {
1406 if (serial != AltosLib.MISSING)
1407 receiver_serial = serial;
1410 public boolean altitude_32() {
1411 return altitude_32 == 1;
1414 public void set_altitude_32(int altitude_32) {
1415 if (altitude_32 != AltosLib.MISSING)
1416 this.altitude_32 = altitude_32;
1420 if (rssi == AltosLib.MISSING)
1425 public void set_rssi(int rssi, int status) {
1426 if (rssi != AltosLib.MISSING) {
1428 this.status = status;
1432 public void set_received_time(long ms) {
1436 public void set_gps(AltosGPS gps, int sequence) {
1438 this.gps = gps.clone();
1439 gps_sequence = sequence;
1446 public double accel_zero_along;
1447 public double accel_zero_across;
1448 public double accel_zero_through;
1450 public AltosRotation rotation;
1451 public AltosRotation ground_rotation;
1453 public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
1454 if (zero_along != AltosLib.MISSING) {
1455 accel_zero_along = zero_along;
1456 accel_zero_across = zero_across;
1457 accel_zero_through = zero_through;
1461 public int pad_orientation;
1463 public double accel_ground_along, accel_ground_across, accel_ground_through;
1465 void update_pad_rotation() {
1466 if (pad_orientation != AltosLib.MISSING && accel_ground_along != AltosLib.MISSING) {
1467 rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - accel_zero_across),
1468 AltosIMU.convert_accel(accel_ground_through - accel_zero_through),
1469 AltosIMU.convert_accel(accel_ground_along - accel_zero_along),
1471 ground_rotation = rotation;
1472 orient.set_computed(rotation.tilt(), time);
1476 public void set_accel_ground(double ground_along, double ground_across, double ground_through) {
1477 accel_ground_along = ground_along;
1478 accel_ground_across = ground_across;
1479 accel_ground_through = ground_through;
1480 update_pad_rotation();
1483 public void set_pad_orientation(int pad_orientation) {
1484 this.pad_orientation = pad_orientation;
1485 update_pad_rotation();
1488 public double gyro_zero_roll;
1489 public double gyro_zero_pitch;
1490 public double gyro_zero_yaw;
1492 public void set_gyro_zero(double roll, double pitch, double yaw) {
1493 if (roll != AltosLib.MISSING) {
1494 gyro_zero_roll = roll;
1495 gyro_zero_pitch = pitch;
1496 gyro_zero_yaw = yaw;
1500 public double last_imu_time;
1502 private double radians(double degrees) {
1503 if (degrees == AltosLib.MISSING)
1504 return AltosLib.MISSING;
1505 return degrees * Math.PI / 180.0;
1508 private void update_orient() {
1509 if (last_imu_time != AltosLib.MISSING) {
1510 double t = time - last_imu_time;
1512 double pitch = radians(gyro_pitch());
1513 double yaw = radians(gyro_yaw());
1514 double roll = radians(gyro_roll());
1516 if (t > 0 & pitch != AltosLib.MISSING && rotation != null) {
1517 rotation.rotate(t, pitch, yaw, roll);
1518 orient.set_computed(rotation.tilt(), time);
1521 last_imu_time = time;
1524 public void set_imu(AltosIMU imu) {
1531 private double gyro_zero_overflow(double first) {
1532 double v = first / 128.0;
1540 public void check_imu_wrap(AltosIMU imu) {
1541 if (this.imu == null) {
1542 gyro_zero_roll += gyro_zero_overflow(imu.gyro_roll);
1543 gyro_zero_pitch += gyro_zero_overflow(imu.gyro_pitch);
1544 gyro_zero_yaw += gyro_zero_overflow(imu.gyro_yaw);
1548 public double accel_along() {
1549 if (imu != null && accel_zero_along != AltosLib.MISSING)
1550 return AltosIMU.convert_accel(imu.accel_along - accel_zero_along);
1551 return AltosLib.MISSING;
1554 public double accel_across() {
1555 if (imu != null && accel_zero_across != AltosLib.MISSING)
1556 return AltosIMU.convert_accel(imu.accel_across - accel_zero_across);
1557 return AltosLib.MISSING;
1560 public double accel_through() {
1561 if (imu != null && accel_zero_through != AltosLib.MISSING)
1562 return AltosIMU.convert_accel(imu.accel_through - accel_zero_through);
1563 return AltosLib.MISSING;
1566 public double gyro_roll() {
1567 if (imu != null && gyro_zero_roll != AltosLib.MISSING) {
1568 return AltosIMU.convert_gyro(imu.gyro_roll - gyro_zero_roll);
1570 return AltosLib.MISSING;
1573 public double gyro_pitch() {
1574 if (imu != null && gyro_zero_pitch != AltosLib.MISSING) {
1575 return AltosIMU.convert_gyro(imu.gyro_pitch - gyro_zero_pitch);
1577 return AltosLib.MISSING;
1580 public double gyro_yaw() {
1581 if (imu != null && gyro_zero_yaw != AltosLib.MISSING) {
1582 return AltosIMU.convert_gyro(imu.gyro_yaw - gyro_zero_yaw);
1584 return AltosLib.MISSING;
1587 public void set_mag(AltosMag mag) {
1588 this.mag = mag.clone();
1591 public double mag_along() {
1593 return AltosMag.convert_gauss(mag.along);
1594 return AltosLib.MISSING;
1597 public double mag_across() {
1599 return AltosMag.convert_gauss(mag.across);
1600 return AltosLib.MISSING;
1603 public double mag_through() {
1605 return AltosMag.convert_gauss(mag.through);
1606 return AltosLib.MISSING;
1609 public AltosMs5607 make_baro() {
1611 baro = new AltosMs5607();
1615 public void set_ms5607(AltosMs5607 ms5607) {
1619 set_pressure(baro.pa);
1620 set_temperature(baro.cc / 100.0);
1624 public void set_ms5607(int pres, int temp) {
1626 baro.set(pres, temp);
1628 set_pressure(baro.pa);
1629 set_temperature(baro.cc / 100.0);
1633 public void set_companion(AltosCompanion companion) {
1634 this.companion = companion;
1637 void update_accel() {
1638 if (accel == AltosLib.MISSING)
1640 if (accel_plus_g == AltosLib.MISSING)
1642 if (accel_minus_g == AltosLib.MISSING)
1645 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
1646 double counts_per_mss = counts_per_g / 9.80665;
1647 acceleration.set_measured((accel_plus_g - accel) / counts_per_mss, time);
1650 public void set_accel_g(double accel_plus_g, double accel_minus_g) {
1651 if (accel_plus_g != AltosLib.MISSING) {
1652 this.accel_plus_g = accel_plus_g;
1653 this.accel_minus_g = accel_minus_g;
1658 public void set_ground_accel(double ground_accel) {
1659 if (ground_accel != AltosLib.MISSING)
1660 this.ground_accel = ground_accel;
1663 public void set_accel(double accel) {
1664 if (accel != AltosLib.MISSING) {
1666 if (state == AltosLib.ao_flight_pad) {
1667 if (ground_accel_avg == AltosLib.MISSING)
1668 ground_accel_avg = accel;
1670 ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
1676 public void set_temperature(double temperature) {
1677 if (temperature != AltosLib.MISSING) {
1678 this.temperature = temperature;
1683 public void set_battery_voltage(double battery_voltage) {
1684 if (battery_voltage != AltosLib.MISSING) {
1685 this.battery_voltage = battery_voltage;
1690 public void set_pyro_voltage(double pyro_voltage) {
1691 if (pyro_voltage != AltosLib.MISSING) {
1692 this.pyro_voltage = pyro_voltage;
1697 public void set_apogee_voltage(double apogee_voltage) {
1698 if (apogee_voltage != AltosLib.MISSING) {
1699 this.apogee_voltage = apogee_voltage;
1704 public void set_main_voltage(double main_voltage) {
1705 if (main_voltage != AltosLib.MISSING) {
1706 this.main_voltage = main_voltage;
1711 public void set_ignitor_voltage(double[] voltage) {
1712 this.ignitor_voltage = voltage;
1715 public void set_pyro_fired(int fired) {
1716 this.pyro_fired = fired;
1719 public double time_since_boost() {
1720 if (tick == AltosLib.MISSING)
1723 if (boost_tick == AltosLib.MISSING)
1724 return tick / 100.0;
1725 return (tick - boost_tick) / 100.0;
1728 public boolean valid() {
1729 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1732 public AltosGPS make_temp_gps(boolean sats) {
1733 if (temp_gps == null) {
1734 temp_gps = new AltosGPS(gps);
1738 if (tick != temp_gps_sat_tick)
1739 temp_gps.cc_gps_sat = null;
1740 temp_gps_sat_tick = tick;
1745 public void set_temp_gps() {
1746 set_gps(temp_gps, gps_sequence + 1);
1747 gps_pending = false;
1751 public AltosState clone() {
1752 AltosState s = new AltosState();
1756 AltosJson json = json();
1757 String onetrip = json.toPrettyString();
1758 AltosJson back = AltosJson.fromString(onetrip);
1759 AltosState tripstate = AltosState.fromJson(back);
1760 AltosJson tripjson = tripstate.json();
1761 String twotrip = tripjson.toPrettyString();
1763 if (!onetrip.equals(twotrip)) {
1764 System.out.printf("one:\n%s\ntwo:\n%s\n", onetrip, twotrip);
1769 AltosHashSet hash = hashSet();
1770 String onetrip = hash.toString();
1771 AltosHashSet back = AltosHashSet.fromString(onetrip);
1772 AltosState tripstate = AltosState.fromHashSet(back);
1773 AltosHashSet triphash = tripstate.hashSet();
1774 String twotrip = triphash.toString();
1776 if (!onetrip.equals(twotrip)) {
1777 System.out.printf("%s\n%s\n", onetrip, twotrip);
1784 public AltosState () {
1788 public AltosHashSet hashSet() {
1789 AltosHashSet h = new AltosHashSet();
1791 h.putBoolean("valid", true);
1792 h.putInt("set", set);
1793 h.putLong("received_time", received_time);
1794 h.putDouble("time", time);
1795 h.putDouble("prev_time", prev_time);
1796 h.putDouble("time_change", time_change);
1797 h.putInt("tick", tick);
1798 h.putInt("prev_tick", prev_tick);
1799 h.putInt("boost_tick", boost_tick);
1800 h.putInt("state", state);
1801 h.putInt("flight", flight);
1802 h.putInt("serial", serial);
1803 h.putInt("altitude_32", altitude_32);
1804 h.putInt("receiver_serial", receiver_serial);
1805 h.putBoolean("landed", landed);
1806 h.putBoolean("ascent", ascent);
1807 h.putBoolean("boost", boost);
1808 h.putInt("rssi", rssi);
1809 h.putInt("status", status);
1810 h.putInt("device_type", device_type);
1811 h.putInt("config_major", config_major);
1812 h.putInt("config_minor", config_minor);
1813 h.putInt("apogee_delay", apogee_delay);
1814 h.putInt("main_deploy", main_deploy);
1815 h.putInt("flight_log_max", flight_log_max);
1816 h.putHashable("ground_altitude", ground_altitude);
1817 h.putHashable("gps_ground_altitude", gps_ground_altitude);
1818 h.putHashable("ground_pressure", ground_pressure);
1819 h.putHashable("altitude", altitude);
1820 h.putHashable("gps_altitude", gps_altitude);
1821 h.putHashable("gps_ground_speed", gps_ground_speed);
1822 h.putHashable("gps_ascent_rate", gps_ascent_rate);
1823 h.putHashable("gps_course", gps_course);
1824 h.putHashable("gps_speed", gps_speed);
1825 h.putHashable("pressure", pressure);
1826 h.putHashable("speed", speed);
1827 h.putHashable("acceleration", acceleration);
1828 h.putHashable("orient", orient);
1829 h.putHashable("kalman_height", kalman_height);
1830 h.putHashable("kalman_speed", kalman_speed);
1831 h.putHashable("kalman_acceleration", kalman_acceleration);
1833 h.putDouble("battery_voltage",battery_voltage);
1834 h.putDouble("pyro_voltage",pyro_voltage);
1835 h.putDouble("temperature",temperature);
1836 h.putDouble("apogee_voltage",apogee_voltage);
1837 h.putDouble("main_voltage",main_voltage);
1838 h.putDoubleArray("ignitor_voltage",ignitor_voltage);
1839 h.putHashable("gps", gps);
1840 h.putHashable("temp_gps", temp_gps);
1841 h.putInt("temp_gps_sat_tick", temp_gps_sat_tick);
1842 h.putBoolean("gps_pending", gps_pending);
1843 h.putInt("gps_sequence", gps_sequence);
1844 h.putHashable("imu", imu);
1845 h.putHashable("mag", mag);
1847 h.putInt("npad", npad);
1848 h.putInt("gps_waiting", gps_waiting);
1849 h.putBoolean("gps_ready", gps_ready);
1850 h.putInt("ngps", ngps);
1851 h.putHashable("from_pad", from_pad);
1852 h.putDouble("elevation", elevation);
1853 h.putDouble("range", range);
1854 h.putDouble("gps_height", gps_height);
1855 h.putDouble("pad_lat", pad_lat);
1856 h.putDouble("pad_lon", pad_lon);
1857 h.putDouble("pad_alt", pad_alt);
1858 h.putInt("speak_tick", speak_tick);
1859 h.putDouble("speak_altitude", speak_altitude);
1860 h.putString("callsign", callsign);
1861 h.putString("firmware_version", firmware_version);
1862 h.putDouble("accel_plus_g", accel_plus_g);
1863 h.putDouble("accel_minus_g", accel_minus_g);
1864 h.putDouble("accel", accel);
1865 h.putDouble("ground_accel", ground_accel);
1866 h.putDouble("ground_accel_avg", ground_accel_avg);
1867 h.putInt("log_format", log_format);
1868 h.putInt("log_space", log_space);
1869 h.putString("product", product);
1870 h.putHashable("baro", baro);
1871 h.putHashable("companion", companion);
1872 h.putInt("pyro_fired", pyro_fired);
1873 h.putDouble("accel_zero_along", accel_zero_along);
1874 h.putDouble("accel_zero_across", accel_zero_across);
1875 h.putDouble("accel_zero_through", accel_zero_through);
1877 h.putHashable("rotation", rotation);
1878 h.putHashable("ground_rotation", ground_rotation);
1880 h.putInt("pad_orientation", pad_orientation);
1882 h.putDouble("accel_ground_along", accel_ground_along);
1883 h.putDouble("accel_ground_across", accel_ground_across);
1884 h.putDouble("accel_ground_through", accel_ground_through);
1886 h.putDouble("gyro_zero_roll", gyro_zero_roll);
1887 h.putDouble("gyro_zero_pitch", gyro_zero_pitch);
1888 h.putDouble("gyro_zero_yaw", gyro_zero_yaw);
1890 h.putDouble("last_imu_time", last_imu_time);
1894 public AltosJson json() {
1895 AltosJson j = new AltosJson();
1897 j.put("valid", true);
1899 j.put("received_time", received_time);
1900 j.put("time", time);
1901 j.put("prev_time", prev_time);
1902 j.put("time_change", time_change);
1903 j.put("tick", tick);
1904 j.put("prev_tick", prev_tick);
1905 j.put("boost_tick", boost_tick);
1906 j.put("state", state);
1907 j.put("flight", flight);
1908 j.put("serial", serial);
1909 j.put("altitude_32", altitude_32);
1910 j.put("receiver_serial", receiver_serial);
1911 j.put("landed", landed);
1912 j.put("ascent", ascent);
1913 j.put("boost", boost);
1914 j.put("rssi", rssi);
1915 j.put("status", status);
1916 j.put("device_type", device_type);
1917 j.put("config_major", config_major);
1918 j.put("config_minor", config_minor);
1919 j.put("apogee_delay", apogee_delay);
1920 j.put("main_deploy", main_deploy);
1921 j.put("flight_log_max", flight_log_max);
1922 j.put("ground_altitude", ground_altitude);
1923 j.put("gps_ground_altitude", gps_ground_altitude);
1924 j.put("ground_pressure", ground_pressure);
1925 j.put("altitude", altitude);
1926 j.put("gps_altitude", gps_altitude);
1927 j.put("gps_ground_speed", gps_ground_speed);
1928 j.put("gps_ascent_rate", gps_ascent_rate);
1929 j.put("gps_course", gps_course);
1930 j.put("gps_speed", gps_speed);
1931 j.put("pressure", pressure);
1932 j.put("speed", speed);
1933 j.put("acceleration", acceleration);
1934 j.put("orient", orient);
1935 j.put("kalman_height", kalman_height);
1936 j.put("kalman_speed", kalman_speed);
1937 j.put("kalman_acceleration", kalman_acceleration);
1939 j.put("battery_voltage",battery_voltage);
1940 j.put("pyro_voltage",pyro_voltage);
1941 j.put("temperature",temperature);
1942 j.put("apogee_voltage",apogee_voltage);
1943 j.put("main_voltage",main_voltage);
1944 j.put("ignitor_voltage",ignitor_voltage);
1946 j.put("temp_gps", temp_gps);
1947 j.put("temp_gps_sat_tick", temp_gps_sat_tick);
1948 j.put("gps_pending", gps_pending);
1949 j.put("gps_sequence", gps_sequence);
1953 j.put("npad", npad);
1954 j.put("gps_waiting", gps_waiting);
1955 j.put("gps_ready", gps_ready);
1956 j.put("ngps", ngps);
1957 j.put("from_pad", from_pad);
1958 j.put("elevation", elevation);
1959 j.put("range", range);
1960 j.put("gps_height", gps_height);
1961 j.put("pad_lat", pad_lat);
1962 j.put("pad_lon", pad_lon);
1963 j.put("pad_alt", pad_alt);
1964 j.put("speak_tick", speak_tick);
1965 j.put("speak_altitude", speak_altitude);
1966 j.put("callsign", callsign);
1967 j.put("firmware_version", firmware_version);
1968 j.put("accel_plus_g", accel_plus_g);
1969 j.put("accel_minus_g", accel_minus_g);
1970 j.put("accel", accel);
1971 j.put("ground_accel", ground_accel);
1972 j.put("ground_accel_avg", ground_accel_avg);
1973 j.put("log_format", log_format);
1974 j.put("log_space", log_space);
1975 j.put("product", product);
1976 j.put("baro", baro);
1977 j.put("companion", companion);
1978 j.put("pyro_fired", pyro_fired);
1979 j.put("accel_zero_along", accel_zero_along);
1980 j.put("accel_zero_across", accel_zero_across);
1981 j.put("accel_zero_through", accel_zero_through);
1983 j.put("rotation", rotation);
1984 j.put("ground_rotation", ground_rotation);
1986 j.put("pad_orientation", pad_orientation);
1988 j.put("accel_ground_along", accel_ground_along);
1989 j.put("accel_ground_across", accel_ground_across);
1990 j.put("accel_ground_through", accel_ground_through);
1992 j.put("gyro_zero_roll", gyro_zero_roll);
1993 j.put("gyro_zero_pitch", gyro_zero_pitch);
1994 j.put("gyro_zero_yaw", gyro_zero_yaw);
1996 j.put("last_imu_time", last_imu_time);
2000 public AltosState(AltosHashSet h) {
2003 set = h.getInt("set", set);
2004 received_time = h.getLong("received_time", received_time);
2005 time = h.getDouble("time", time);
2006 prev_time = h.getDouble("prev_time", prev_time);
2007 time_change = h.getDouble("time_change", time_change);
2008 tick = h.getInt("tick", tick);
2009 prev_tick = h.getInt("prev_tick", prev_tick);
2010 boost_tick = h.getInt("boost_tick", boost_tick);
2011 state = h.getInt("state", state);
2012 flight = h.getInt("flight", flight);
2013 serial = h.getInt("serial", serial);
2014 altitude_32 = h.getInt("altitude_32", altitude_32);
2015 receiver_serial = h.getInt("receiver_serial", receiver_serial);
2016 landed = h.getBoolean("landed", landed);
2017 ascent = h.getBoolean("ascent", ascent);
2018 boost = h.getBoolean("boost", boost);
2019 rssi = h.getInt("rssi", rssi);
2020 status = h.getInt("status", status);
2021 device_type = h.getInt("device_type", device_type);
2022 config_major = h.getInt("config_major", config_major);
2023 config_minor = h.getInt("config_minor", config_minor);
2024 apogee_delay = h.getInt("apogee_delay", apogee_delay);
2025 main_deploy = h.getInt("main_deploy", main_deploy);
2026 flight_log_max = h.getInt("flight_log_max", flight_log_max);
2027 ground_altitude = AltosCValue_fromHashSet(h.getHash("ground_altitude"), ground_altitude);
2028 gps_ground_altitude = AltosGpsGroundAltitude_fromHashSet(h.getHash("gps_ground_altitude"), gps_ground_altitude);
2029 ground_pressure = AltosGroundPressure_fromHashSet(h.getHash("ground_pressure"), ground_pressure);
2030 altitude = AltosAltitude_fromHashSet(h.getHash("altitude"), altitude);
2031 gps_altitude = AltosGpsAltitude_fromHashSet(h.getHash("gps_altitude"), gps_altitude);
2032 gps_ground_speed = AltosValue_fromHashSet(h.getHash("gps_ground_speed"), gps_ground_speed);
2033 gps_ascent_rate = AltosValue_fromHashSet(h.getHash("gps_ascent_rate"), gps_ascent_rate);
2034 gps_course = AltosValue_fromHashSet(h.getHash("gps_course"), gps_course);
2035 gps_speed = AltosValue_fromHashSet(h.getHash("gps_speed"), gps_speed);
2036 pressure = AltosPressure_fromHashSet(h.getHash("pressure"), pressure);
2037 speed = AltosSpeed_fromHashSet(h.getHash("speed"), speed);
2038 acceleration = AltosAccel_fromHashSet(h.getHash("acceleration"), acceleration);
2039 orient = AltosCValue_fromHashSet(h.getHash("orient"), orient);
2040 kalman_height = AltosValue_fromHashSet(h.getHash("kalman_height"), kalman_height);
2041 kalman_speed = AltosValue_fromHashSet(h.getHash("kalman_speed"), kalman_speed);
2042 kalman_acceleration = AltosValue_fromHashSet(h.getHash("kalman_acceleration"), kalman_acceleration);
2044 battery_voltage = h.getDouble("battery_voltage", battery_voltage);
2045 pyro_voltage = h.getDouble("pyro_voltage", pyro_voltage);
2046 temperature = h.getDouble("temperature", temperature);
2047 apogee_voltage = h.getDouble("apogee_voltage", apogee_voltage);
2048 main_voltage= h.getDouble("main_voltage", main_voltage);
2049 ignitor_voltage = h.getDoubleArray("ignitor_voltage", ignitor_voltage);
2050 gps = AltosGPS.fromHashSet(h.getHash("gps"), gps);
2051 temp_gps = AltosGPS.fromHashSet(h.getHash("temp_gps"), temp_gps);
2052 temp_gps_sat_tick = h.getInt("temp_gps_sat_tick", temp_gps_sat_tick);
2053 gps_pending = h.getBoolean("gps_pending", gps_pending);
2054 gps_sequence = h.getInt("gps_sequence", gps_sequence);
2055 imu = AltosIMU.fromHashSet(h.getHash("imu"), imu);
2056 mag = AltosMag.fromHashSet(h.getHash("mag"), mag);
2058 npad = h.getInt("npad", npad);
2059 gps_waiting = h.getInt("gps_waiting", gps_waiting);
2060 gps_ready = h.getBoolean("gps_ready", gps_ready);
2061 ngps = h.getInt("ngps", ngps);
2062 from_pad = AltosGreatCircle.fromHashSet(h.getHash("from_pad"), from_pad);
2063 elevation = h.getDouble("elevation", elevation);
2064 range = h.getDouble("range", range);
2065 gps_height = h.getDouble("gps_height", gps_height);
2066 pad_lat = h.getDouble("pad_lat", pad_lat);
2067 pad_lon = h.getDouble("pad_lon", pad_lon);
2068 pad_alt = h.getDouble("pad_alt", pad_alt);
2069 speak_tick = h.getInt("speak_tick", speak_tick);
2070 speak_altitude = h.getDouble("speak_altitude", speak_altitude);
2071 callsign = h.getString("callsign", callsign);
2072 firmware_version = h.getString("firmware_version", firmware_version);
2073 accel_plus_g = h.getDouble("accel_plus_g", accel_plus_g);
2074 accel_minus_g = h.getDouble("accel_minus_g", accel_minus_g);
2075 accel = h.getDouble("accel", accel);
2076 ground_accel = h.getDouble("ground_accel", ground_accel);
2077 ground_accel_avg = h.getDouble("ground_accel_avg", ground_accel_avg);
2078 log_format = h.getInt("log_format", log_format);
2079 log_space = h.getInt("log_space", log_space);
2080 product = h.getString("product", product);
2081 baro = AltosMs5607.fromHashSet(h.getHash("baro"), baro);
2082 companion = AltosCompanion.fromHashSet(h.getHash("companion"), companion);
2083 pyro_fired = h.getInt("pyro_fired", pyro_fired);
2084 accel_zero_along = h.getDouble("accel_zero_along", accel_zero_along);
2085 accel_zero_across = h.getDouble("accel_zero_across", accel_zero_across);
2086 accel_zero_through = h.getDouble("accel_zero_through", accel_zero_through);
2088 rotation = AltosRotation.fromHashSet(h.getHash("rotation"), rotation);
2089 ground_rotation = AltosRotation.fromHashSet(h.getHash("ground_rotation"), ground_rotation);
2091 pad_orientation = h.getInt("pad_orientation", pad_orientation);
2093 accel_ground_along = h.getDouble("accel_ground_along", accel_ground_along);
2094 accel_ground_across = h.getDouble("accel_ground_across", accel_ground_across);
2095 accel_ground_through = h.getDouble("accel_ground_through", accel_ground_through);
2097 gyro_zero_roll = h.getDouble("gyro_zero_roll", gyro_zero_roll);
2098 gyro_zero_pitch = h.getDouble("gyro_zero_pitch", gyro_zero_pitch);
2099 gyro_zero_yaw = h.getDouble("gyro_zero_yaw", gyro_zero_yaw);
2101 last_imu_time = h.getDouble("last_imu_time", last_imu_time);
2104 public AltosState(AltosJson j) {
2107 set = j.get_int("set", set);
2108 received_time = j.get_long("received_time", received_time);
2109 time = j.get_double("time", time);
2110 prev_time = j.get_double("prev_time", prev_time);
2111 time_change = j.get_double("time_change", time_change);
2112 tick = j.get_int("tick", tick);
2113 prev_tick = j.get_int("prev_tick", prev_tick);
2114 boost_tick = j.get_int("boost_tick", boost_tick);
2115 state = j.get_int("state", state);
2116 flight = j.get_int("flight", flight);
2117 serial = j.get_int("serial", serial);
2118 altitude_32 = j.get_int("altitude_32", altitude_32);
2119 receiver_serial = j.get_int("receiver_serial", receiver_serial);
2120 landed = j.get_boolean("landed", landed);
2121 ascent = j.get_boolean("ascent", ascent);
2122 boost = j.get_boolean("boost", boost);
2123 rssi = j.get_int("rssi", rssi);
2124 status = j.get_int("status", status);
2125 device_type = j.get_int("device_type", device_type);
2126 config_major = j.get_int("config_major", config_major);
2127 config_minor = j.get_int("config_minor", config_minor);
2128 apogee_delay = j.get_int("apogee_delay", apogee_delay);
2129 main_deploy = j.get_int("main_deploy", main_deploy);
2130 flight_log_max = j.get_int("flight_log_max", flight_log_max);
2131 ground_altitude = AltosCValue_fromJson(j.get("ground_altitude"), ground_altitude);
2132 gps_ground_altitude = AltosGpsGroundAltitude_fromJson(j.get("gps_ground_altitude"), gps_ground_altitude);
2133 ground_pressure = AltosGroundPressure_fromJson(j.get("ground_pressure"), ground_pressure);
2134 altitude = AltosAltitude_fromJson(j.get("altitude"), altitude);
2135 gps_altitude = AltosGpsAltitude_fromJson(j.get("gps_altitude"), gps_altitude);
2136 gps_ground_speed = AltosValue_fromJson(j.get("gps_ground_speed"), gps_ground_speed);
2137 gps_ascent_rate = AltosValue_fromJson(j.get("gps_ascent_rate"), gps_ascent_rate);
2138 gps_course = AltosValue_fromJson(j.get("gps_course"), gps_course);
2139 gps_speed = AltosValue_fromJson(j.get("gps_speed"), gps_speed);
2140 pressure = AltosPressure_fromJson(j.get("pressure"), pressure);
2141 speed = AltosSpeed_fromJson(j.get("speed"), speed);
2142 acceleration = AltosAccel_fromJson(j.get("acceleration"), acceleration);
2143 orient = AltosCValue_fromJson(j.get("orient"), orient);
2144 kalman_height = AltosValue_fromJson(j.get("kalman_height"), kalman_height);
2145 kalman_speed = AltosValue_fromJson(j.get("kalman_speed"), kalman_speed);
2146 kalman_acceleration = AltosValue_fromJson(j.get("kalman_acceleration"), kalman_acceleration);
2148 battery_voltage = j.get_double("battery_voltage", battery_voltage);
2149 pyro_voltage = j.get_double("pyro_voltage", pyro_voltage);
2150 temperature = j.get_double("temperature", temperature);
2151 apogee_voltage = j.get_double("apogee_voltage", apogee_voltage);
2152 main_voltage= j.get_double("main_voltage", main_voltage);
2153 ignitor_voltage = j.get_double_array("ignitor_voltage", ignitor_voltage);
2154 gps = AltosGPS.fromJson(j.get("gps"), gps);
2155 temp_gps = AltosGPS.fromJson(j.get("temp_gps"), temp_gps);
2156 temp_gps_sat_tick = j.get_int("temp_gps_sat_tick", temp_gps_sat_tick);
2157 gps_pending = j.get_boolean("gps_pending", gps_pending);
2158 gps_sequence = j.get_int("gps_sequence", gps_sequence);
2159 imu = AltosIMU.fromJson(j.get("imu"), imu);
2160 mag = AltosMag.fromJson(j.get("mag"), mag);
2162 npad = j.get_int("npad", npad);
2163 gps_waiting = j.get_int("gps_waiting", gps_waiting);
2164 gps_ready = j.get_boolean("gps_ready", gps_ready);
2165 ngps = j.get_int("ngps", ngps);
2166 from_pad = AltosGreatCircle.fromJson(j.get("from_pad"), from_pad);
2167 elevation = j.get_double("elevation", elevation);
2168 range = j.get_double("range", range);
2169 gps_height = j.get_double("gps_height", gps_height);
2170 pad_lat = j.get_double("pad_lat", pad_lat);
2171 pad_lon = j.get_double("pad_lon", pad_lon);
2172 pad_alt = j.get_double("pad_alt", pad_alt);
2173 speak_tick = j.get_int("speak_tick", speak_tick);
2174 speak_altitude = j.get_double("speak_altitude", speak_altitude);
2175 callsign = j.get_string("callsign", callsign);
2176 firmware_version = j.get_string("firmware_version", firmware_version);
2177 accel_plus_g = j.get_double("accel_plus_g", accel_plus_g);
2178 accel_minus_g = j.get_double("accel_minus_g", accel_minus_g);
2179 accel = j.get_double("accel", accel);
2180 ground_accel = j.get_double("ground_accel", ground_accel);
2181 ground_accel_avg = j.get_double("ground_accel_avg", ground_accel_avg);
2182 log_format = j.get_int("log_format", log_format);
2183 log_space = j.get_int("log_space", log_space);
2184 product = j.get_string("product", product);
2185 baro = AltosMs5607.fromJson(j.get("baro"), baro);
2186 companion = AltosCompanion.fromJson(j.get("companion"), companion);
2187 pyro_fired = j.get_int("pyro_fired", pyro_fired);
2188 accel_zero_along = j.get_double("accel_zero_along", accel_zero_along);
2189 accel_zero_across = j.get_double("accel_zero_across", accel_zero_across);
2190 accel_zero_through = j.get_double("accel_zero_through", accel_zero_through);
2192 rotation = AltosRotation.fromJson(j.get("rotation"), rotation);
2193 ground_rotation = AltosRotation.fromJson(j.get("ground_rotation"), ground_rotation);
2195 pad_orientation = j.get_int("pad_orientation", pad_orientation);
2197 accel_ground_along = j.get_double("accel_ground_along", accel_ground_along);
2198 accel_ground_across = j.get_double("accel_ground_across", accel_ground_across);
2199 accel_ground_through = j.get_double("accel_ground_through", accel_ground_through);
2201 gyro_zero_roll = j.get_double("gyro_zero_roll", gyro_zero_roll);
2202 gyro_zero_pitch = j.get_double("gyro_zero_pitch", gyro_zero_pitch);
2203 gyro_zero_yaw = j.get_double("gyro_zero_yaw", gyro_zero_yaw);
2205 last_imu_time = j.get_double("last_imu_time", last_imu_time);
2207 public static AltosState fromHashSet(AltosHashSet h) {
2210 if (!h.getBoolean("valid", false))
2212 return new AltosState(h);
2215 public static AltosState fromJson(AltosJson j) {
2218 if (!j.get_boolean("valid", false))
2220 return new AltosState(j);