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_7;
26 public class AltosState implements Cloneable, Serializable {
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 ascent_filter_len = 0.5;
35 static final double descent_filter_len = 0.5;
39 public long received_time;
42 public double prev_time;
43 public double time_change;
45 private int prev_tick;
46 public int boost_tick;
48 class AltosValue implements Serializable{
51 private double max_value;
52 private double set_time;
53 private double prev_set_time;
55 boolean can_max() { return true; }
57 void set(double new_value, double time) {
58 if (new_value != AltosLib.MISSING) {
60 if (can_max() && (max_value == AltosLib.MISSING || value > max_value))
66 void set_filtered(double new_value, double time) {
67 if (prev_value != AltosLib.MISSING)
68 new_value = (prev_value * 15.0 + new_value) / 16.0;
85 if (value != AltosLib.MISSING && prev_value != AltosLib.MISSING)
86 return value - prev_value;
87 return AltosLib.MISSING;
92 double t = set_time - prev_set_time;
94 if (c != AltosLib.MISSING && t != 0)
96 return AltosLib.MISSING;
100 if (value == AltosLib.MISSING)
101 return AltosLib.MISSING;
102 if (prev_value == AltosLib.MISSING)
103 return AltosLib.MISSING;
105 return (value + prev_value) / 2 * (set_time - prev_set_time);
112 void set_derivative(AltosValue in) {
113 double n = in.rate();
115 if (n == AltosLib.MISSING)
118 double p = prev_value;
119 double pt = prev_set_time;
121 if (p == AltosLib.MISSING) {
123 pt = in.time() - 0.01;
126 /* Clip changes to reduce noise */
127 double ddt = in.time() - pt;
128 double ddv = (n - p) / ddt;
130 final double max = 100000;
133 if (Math.abs(ddv) > max) {
143 filter_len = ascent_filter_len;
145 filter_len = descent_filter_len;
147 double f = 1/Math.exp(ddt/ filter_len);
148 n = p * f + n * (1-f);
153 void set_integral(AltosValue in) {
154 double change = in.integrate();
156 if (change != AltosLib.MISSING) {
157 double prev = prev_value;
158 if (prev == AltosLib.MISSING)
160 set(prev + change, in.time());
164 void copy(AltosValue old) {
166 set_time = old.set_time;
167 prev_value = old.value;
168 prev_set_time = old.set_time;
169 max_value = old.max_value;
172 void finish_update() {
174 prev_set_time = set_time;
178 value = AltosLib.MISSING;
179 prev_value = AltosLib.MISSING;
180 max_value = AltosLib.MISSING;
184 class AltosCValue implements Serializable {
186 class AltosIValue extends AltosValue implements Serializable {
192 public AltosIValue measured;
193 public AltosIValue computed;
195 boolean can_max() { return true; }
197 boolean c_can_max() { return can_max(); }
200 double v = measured.value();
201 if (v != AltosLib.MISSING)
203 return computed.value();
206 boolean is_measured() {
207 return measured.value() != AltosLib.MISSING;
211 double m = measured.max();
213 if (m != AltosLib.MISSING)
215 return computed.max();
218 double prev_value() {
219 if (measured.value != AltosLib.MISSING && measured.prev_value != AltosLib.MISSING)
220 return measured.prev_value;
221 return computed.prev_value;
224 AltosValue altos_value() {
225 if (measured.value() != AltosLib.MISSING)
231 double c = measured.change();
232 if (c == AltosLib.MISSING)
233 c = computed.change();
238 double r = measured.rate();
239 if (r == AltosLib.MISSING)
244 void set_measured(double new_value, double time) {
245 measured.set(new_value, time);
248 void set_computed(double new_value, double time) {
249 computed.set(new_value, time);
252 void set_derivative(AltosValue in) {
253 computed.set_derivative(in);
256 void set_derivative(AltosCValue in) {
257 set_derivative(in.altos_value());
260 void set_integral(AltosValue in) {
261 computed.set_integral(in);
264 void set_integral(AltosCValue in) {
265 set_integral(in.altos_value());
268 void copy(AltosCValue old) {
269 measured.copy(old.measured);
270 computed.copy(old.computed);
273 void finish_update() {
274 measured.finish_update();
275 computed.finish_update();
279 measured = new AltosIValue();
280 computed = new AltosIValue();
287 public int altitude_32;
288 public int receiver_serial;
289 public boolean landed;
290 public boolean ascent; /* going up? */
291 public boolean boost; /* under power */
294 public int device_type;
295 public int config_major;
296 public int config_minor;
297 public int apogee_delay;
298 public int main_deploy;
299 public int flight_log_max;
301 private double pressure_to_altitude(double p) {
302 if (p == AltosLib.MISSING)
303 return AltosLib.MISSING;
304 return AltosConvert.pressure_to_altitude(p);
307 private AltosCValue ground_altitude;
309 public double ground_altitude() {
310 return ground_altitude.value();
313 public void set_ground_altitude(double a) {
314 ground_altitude.set_measured(a, time);
317 class AltosGpsGroundAltitude extends AltosValue implements Serializable {
318 void set(double a, double t) {
321 gps_altitude.set_gps_height();
324 void set_filtered(double a, double t) {
325 super.set_filtered(a, t);
327 gps_altitude.set_gps_height();
331 private AltosGpsGroundAltitude gps_ground_altitude;
333 public double gps_ground_altitude() {
334 return gps_ground_altitude.value();
337 public void set_gps_ground_altitude(double a) {
338 gps_ground_altitude.set(a, time);
341 class AltosGroundPressure extends AltosCValue implements Serializable {
342 void set_filtered(double p, double time) {
343 computed.set_filtered(p, time);
345 ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
348 void set_measured(double p, double time) {
349 super.set_measured(p, time);
350 ground_altitude.set_computed(pressure_to_altitude(p), time);
354 private AltosGroundPressure ground_pressure;
356 public double ground_pressure() {
357 return ground_pressure.value();
360 public void set_ground_pressure (double pressure) {
361 ground_pressure.set_measured(pressure, time);
364 class AltosAltitude extends AltosCValue implements Serializable {
366 private void set_speed(AltosValue v) {
367 if (!acceleration.is_measured() || !ascent)
368 speed.set_derivative(this);
371 void set_computed(double a, double time) {
372 super.set_computed(a,time);
377 void set_measured(double a, double time) {
378 super.set_measured(a,time);
384 private AltosAltitude altitude;
386 class AltosGpsAltitude extends AltosValue implements Serializable {
388 private void set_gps_height() {
390 double g = gps_ground_altitude.value();
392 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
395 gps_height = AltosLib.MISSING;
398 void set(double a, double t) {
404 private AltosGpsAltitude gps_altitude;
406 private AltosValue gps_ground_speed;
407 private AltosValue gps_ascent_rate;
408 private AltosValue gps_course;
409 private AltosValue gps_speed;
411 public double altitude() {
412 double a = altitude.value();
413 if (a != AltosLib.MISSING)
415 return gps_altitude.value();
418 public double max_altitude() {
419 double a = altitude.max();
420 if (a != AltosLib.MISSING)
422 return gps_altitude.max();
425 public void set_altitude(double new_altitude) {
426 altitude.set_measured(new_altitude, time);
429 public double gps_altitude() {
430 return gps_altitude.value();
433 public double max_gps_altitude() {
434 return gps_altitude.max();
437 public void set_gps_altitude(double new_gps_altitude) {
438 gps_altitude.set(new_gps_altitude, time);
441 public double gps_ground_speed() {
442 return gps_ground_speed.value();
445 public double max_gps_ground_speed() {
446 return gps_ground_speed.max();
449 public double gps_ascent_rate() {
450 return gps_ascent_rate.value();
453 public double max_gps_ascent_rate() {
454 return gps_ascent_rate.max();
457 public double gps_course() {
458 return gps_course.value();
461 public double gps_speed() {
462 return gps_speed.value();
465 public double max_gps_speed() {
466 return gps_speed.max();
469 class AltosPressure extends AltosValue implements Serializable {
470 void set(double p, double time) {
472 if (state == AltosLib.ao_flight_pad)
473 ground_pressure.set_filtered(p, time);
474 double a = pressure_to_altitude(p);
475 altitude.set_computed(a, time);
479 private AltosPressure pressure;
481 public double pressure() {
482 return pressure.value();
485 public void set_pressure(double p) {
486 pressure.set(p, time);
489 public double baro_height() {
490 double a = altitude();
491 double g = ground_altitude();
492 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
494 return AltosLib.MISSING;
497 public double height() {
498 double k = kalman_height.value();
499 if (k != AltosLib.MISSING)
502 double b = baro_height();
503 if (b != AltosLib.MISSING)
509 public double max_height() {
510 double k = kalman_height.max();
511 if (k != AltosLib.MISSING)
514 double a = altitude.max();
515 double g = ground_altitude();
516 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
518 return max_gps_height();
521 public double gps_height() {
522 double a = gps_altitude();
523 double g = gps_ground_altitude();
525 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
527 return AltosLib.MISSING;
530 public double max_gps_height() {
531 double a = gps_altitude.max();
532 double g = gps_ground_altitude();
534 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
536 return AltosLib.MISSING;
539 class AltosSpeed extends AltosCValue implements Serializable {
542 return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
546 acceleration.set_derivative(this);
549 void set_derivative(AltosCValue in) {
550 super.set_derivative(in);
554 void set_computed(double new_value, double time) {
555 super.set_computed(new_value, time);
559 void set_measured(double new_value, double time) {
560 super.set_measured(new_value, time);
565 private AltosSpeed speed;
567 public double speed() {
568 double v = kalman_speed.value();
569 if (v != AltosLib.MISSING)
572 if (v != AltosLib.MISSING)
575 if (v != AltosLib.MISSING)
577 return AltosLib.MISSING;
580 public double max_speed() {
581 double v = kalman_speed.max();
582 if (v != AltosLib.MISSING)
585 if (v != AltosLib.MISSING)
588 if (v != AltosLib.MISSING)
590 return AltosLib.MISSING;
593 class AltosAccel extends AltosCValue implements Serializable {
596 return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
599 void set_measured(double a, double time) {
600 super.set_measured(a, time);
602 speed.set_integral(this.measured);
606 AltosAccel acceleration;
608 public double acceleration() {
609 return acceleration.value();
612 public double max_acceleration() {
613 return acceleration.max();
616 public AltosCValue orient;
618 public void set_orient(double new_orient) {
619 orient.set_measured(new_orient, time);
622 public double orient() {
623 return orient.value();
626 public double max_orient() {
630 public AltosValue kalman_height, kalman_speed, kalman_acceleration;
632 public void set_kalman(double height, double speed, double acceleration) {
633 kalman_height.set(height, time);
634 kalman_speed.set(speed, time);
635 kalman_acceleration.set(acceleration, time);
638 public double battery_voltage;
639 public double pyro_voltage;
640 public double temperature;
641 public double apogee_voltage;
642 public double main_voltage;
644 public double ignitor_voltage[];
647 public AltosGPS temp_gps;
648 public int temp_gps_sat_tick;
649 public boolean gps_pending;
650 public int gps_sequence;
655 public static final int MIN_PAD_SAMPLES = 10;
658 public int gps_waiting;
659 public boolean gps_ready;
663 public AltosGreatCircle from_pad;
664 public double elevation; /* from pad */
665 public double range; /* total distance */
667 public double gps_height;
669 public double pad_lat, pad_lon, pad_alt;
671 public int speak_tick;
672 public double speak_altitude;
674 public String callsign;
675 public String firmware_version;
677 public double accel_plus_g;
678 public double accel_minus_g;
680 public double ground_accel;
681 public double ground_accel_avg;
683 public int log_format;
684 public String product;
686 public AltosMs5607 baro;
688 public AltosCompanion companion;
690 public int pyro_fired;
692 public void set_npad(int npad) {
694 gps_waiting = MIN_PAD_SAMPLES - npad;
695 if (this.gps_waiting < 0)
697 gps_ready = gps_waiting == 0;
703 received_time = System.currentTimeMillis();
704 time = AltosLib.MISSING;
705 time_change = AltosLib.MISSING;
706 prev_time = AltosLib.MISSING;
707 tick = AltosLib.MISSING;
708 prev_tick = AltosLib.MISSING;
709 boost_tick = AltosLib.MISSING;
710 state = AltosLib.ao_flight_invalid;
711 flight = AltosLib.MISSING;
714 rssi = AltosLib.MISSING;
716 device_type = AltosLib.MISSING;
717 config_major = AltosLib.MISSING;
718 config_minor = AltosLib.MISSING;
719 apogee_delay = AltosLib.MISSING;
720 main_deploy = AltosLib.MISSING;
721 flight_log_max = AltosLib.MISSING;
723 ground_altitude = new AltosCValue();
724 ground_pressure = new AltosGroundPressure();
725 altitude = new AltosAltitude();
726 pressure = new AltosPressure();
727 speed = new AltosSpeed();
728 acceleration = new AltosAccel();
729 orient = new AltosCValue();
731 temperature = AltosLib.MISSING;
732 battery_voltage = AltosLib.MISSING;
733 pyro_voltage = AltosLib.MISSING;
734 apogee_voltage = AltosLib.MISSING;
735 main_voltage = AltosLib.MISSING;
736 ignitor_voltage = null;
738 kalman_height = new AltosValue();
739 kalman_speed = new AltosValue();
740 kalman_acceleration = new AltosValue();
744 temp_gps_sat_tick = 0;
749 last_imu_time = AltosLib.MISSING;
751 ground_rotation = null;
754 accel_zero_along = AltosLib.MISSING;
755 accel_zero_across = AltosLib.MISSING;
756 accel_zero_through = AltosLib.MISSING;
758 accel_ground_along = AltosLib.MISSING;
759 accel_ground_across = AltosLib.MISSING;
760 accel_ground_through = AltosLib.MISSING;
762 pad_orientation = AltosLib.MISSING;
764 gyro_zero_roll = AltosLib.MISSING;
765 gyro_zero_pitch = AltosLib.MISSING;
766 gyro_zero_yaw = AltosLib.MISSING;
772 elevation = AltosLib.MISSING;
773 range = AltosLib.MISSING;
774 gps_height = AltosLib.MISSING;
776 pad_lat = AltosLib.MISSING;
777 pad_lon = AltosLib.MISSING;
778 pad_alt = AltosLib.MISSING;
780 gps_altitude = new AltosGpsAltitude();
781 gps_ground_altitude = new AltosGpsGroundAltitude();
782 gps_ground_speed = new AltosValue();
783 gps_speed = new AltosValue();
784 gps_ascent_rate = new AltosValue();
785 gps_course = new AltosValue();
787 speak_tick = AltosLib.MISSING;
788 speak_altitude = AltosLib.MISSING;
791 firmware_version = null;
793 accel_plus_g = AltosLib.MISSING;
794 accel_minus_g = AltosLib.MISSING;
795 accel = AltosLib.MISSING;
797 ground_accel = AltosLib.MISSING;
798 ground_accel_avg = AltosLib.MISSING;
800 log_format = AltosLib.MISSING;
802 serial = AltosLib.MISSING;
803 receiver_serial = AltosLib.MISSING;
804 altitude_32 = AltosLib.MISSING;
812 void finish_update() {
815 ground_altitude.finish_update();
816 altitude.finish_update();
817 pressure.finish_update();
818 speed.finish_update();
819 acceleration.finish_update();
820 orient.finish_update();
822 kalman_height.finish_update();
823 kalman_speed.finish_update();
824 kalman_acceleration.finish_update();
827 void copy(AltosState old) {
834 received_time = old.received_time;
836 time_change = old.time_change;
837 prev_time = old.time;
840 prev_tick = old.tick;
841 boost_tick = old.boost_tick;
850 device_type = old.device_type;
851 config_major = old.config_major;
852 config_minor = old.config_minor;
853 apogee_delay = old.apogee_delay;
854 main_deploy = old.main_deploy;
855 flight_log_max = old.flight_log_max;
859 ground_pressure.copy(old.ground_pressure);
860 ground_altitude.copy(old.ground_altitude);
861 altitude.copy(old.altitude);
862 pressure.copy(old.pressure);
863 speed.copy(old.speed);
864 acceleration.copy(old.acceleration);
865 orient.copy(old.orient);
867 battery_voltage = old.battery_voltage;
868 pyro_voltage = old.pyro_voltage;
869 temperature = old.temperature;
870 apogee_voltage = old.apogee_voltage;
871 main_voltage = old.main_voltage;
872 ignitor_voltage = old.ignitor_voltage;
874 kalman_height.copy(old.kalman_height);
875 kalman_speed.copy(old.kalman_speed);
876 kalman_acceleration.copy(old.kalman_acceleration);
879 gps = old.gps.clone();
882 if (old.temp_gps != null)
883 temp_gps = old.temp_gps.clone();
886 temp_gps_sat_tick = old.temp_gps_sat_tick;
887 gps_sequence = old.gps_sequence;
888 gps_pending = old.gps_pending;
891 imu = old.imu.clone();
894 last_imu_time = old.last_imu_time;
896 if (old.rotation != null)
897 rotation = new AltosRotation (old.rotation);
899 if (old.ground_rotation != null) {
900 ground_rotation = new AltosRotation(old.ground_rotation);
903 accel_zero_along = old.accel_zero_along;
904 accel_zero_across = old.accel_zero_across;
905 accel_zero_through = old.accel_zero_through;
907 accel_ground_along = old.accel_ground_along;
908 accel_ground_across = old.accel_ground_across;
909 accel_ground_through = old.accel_ground_through;
910 pad_orientation = old.pad_orientation;
912 gyro_zero_roll = old.gyro_zero_roll;
913 gyro_zero_pitch = old.gyro_zero_pitch;
914 gyro_zero_yaw = old.gyro_zero_yaw;
917 mag = old.mag.clone();
922 gps_waiting = old.gps_waiting;
923 gps_ready = old.gps_ready;
926 if (old.from_pad != null)
927 from_pad = old.from_pad.clone();
931 elevation = old.elevation;
934 gps_height = old.gps_height;
936 gps_altitude.copy(old.gps_altitude);
937 gps_ground_altitude.copy(old.gps_ground_altitude);
938 gps_ground_speed.copy(old.gps_ground_speed);
939 gps_ascent_rate.copy(old.gps_ascent_rate);
940 gps_course.copy(old.gps_course);
941 gps_speed.copy(old.gps_speed);
943 pad_lat = old.pad_lat;
944 pad_lon = old.pad_lon;
945 pad_alt = old.pad_alt;
947 speak_tick = old.speak_tick;
948 speak_altitude = old.speak_altitude;
950 callsign = old.callsign;
951 firmware_version = old.firmware_version;
953 accel_plus_g = old.accel_plus_g;
954 accel_minus_g = old.accel_minus_g;
956 ground_accel = old.ground_accel;
957 ground_accel_avg = old.ground_accel_avg;
959 log_format = old.log_format;
960 product = old.product;
962 receiver_serial = old.receiver_serial;
963 altitude_32 = old.altitude_32;
966 companion = old.companion;
968 pyro_fired = old.pyro_fired;
975 elevation = AltosLib.MISSING;
976 range = AltosLib.MISSING;
981 if (gps.locked && gps.nsat >= 4) {
982 /* Track consecutive 'good' gps reports, waiting for 10 of them */
983 if (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_stateless) {
985 if (pad_lat != AltosLib.MISSING && (npad < 10 || state == AltosLib.ao_flight_pad)) {
986 pad_lat = (pad_lat * 31 + gps.lat) / 32;
987 pad_lon = (pad_lon * 31 + gps.lon) / 32;
988 gps_ground_altitude.set_filtered(gps.alt, time);
991 if (pad_lat == AltosLib.MISSING) {
994 gps_ground_altitude.set(gps.alt, time);
996 gps_altitude.set(gps.alt, time);
997 if (gps.climb_rate != AltosLib.MISSING)
998 gps_ascent_rate.set(gps.climb_rate, time);
999 if (gps.ground_speed != AltosLib.MISSING)
1000 gps_ground_speed.set(gps.ground_speed, time);
1001 if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
1002 gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
1003 gps.climb_rate * gps.climb_rate), time);
1004 if (gps.course != AltosLib.MISSING)
1005 gps_course.set(gps.course, time);
1007 if (gps.lat != 0 && gps.lon != 0 &&
1008 pad_lat != AltosLib.MISSING &&
1009 pad_lon != AltosLib.MISSING)
1011 double h = height();
1013 if (h == AltosLib.MISSING)
1015 from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
1016 elevation = from_pad.elevation;
1017 range = from_pad.range;
1021 public void set_tick(int new_tick) {
1022 if (new_tick != AltosLib.MISSING) {
1023 if (prev_tick != AltosLib.MISSING) {
1024 while (new_tick < prev_tick - 1000) {
1029 time = tick / 100.0;
1030 time_change = time - prev_time;
1034 public void set_boost_tick(int boost_tick) {
1035 if (boost_tick != AltosLib.MISSING)
1036 this.boost_tick = boost_tick;
1039 public String state_name() {
1040 return AltosLib.state_name(state);
1043 public void set_product(String product) {
1044 this.product = product;
1047 public void set_state(int state) {
1048 if (state != AltosLib.ao_flight_invalid) {
1050 ascent = (AltosLib.ao_flight_boost <= state &&
1051 state <= AltosLib.ao_flight_coast);
1052 boost = (AltosLib.ao_flight_boost == state);
1056 public void set_device_type(int device_type) {
1057 this.device_type = device_type;
1058 switch (device_type) {
1059 case AltosLib.product_telegps:
1060 this.state = AltosLib.ao_flight_stateless;
1065 public void set_log_format(int log_format) {
1066 this.log_format = log_format;
1067 switch (log_format) {
1068 case AltosLib.AO_LOG_FORMAT_TELEGPS:
1069 this.state = AltosLib.ao_flight_stateless;
1074 public void set_flight_params(int apogee_delay, int main_deploy) {
1075 this.apogee_delay = apogee_delay;
1076 this.main_deploy = main_deploy;
1079 public void set_config(int major, int minor, int flight_log_max) {
1080 config_major = major;
1081 config_minor = minor;
1082 this.flight_log_max = flight_log_max;
1085 public void set_callsign(String callsign) {
1086 this.callsign = callsign;
1089 public void set_firmware_version(String version) {
1090 firmware_version = version;
1093 public int compare_version(String other_version) {
1094 if (firmware_version == null)
1095 return AltosLib.MISSING;
1096 return AltosLib.compare_version(firmware_version, other_version);
1099 private void re_init() {
1100 int bt = boost_tick;
1101 int rs = receiver_serial;
1104 receiver_serial = rs;
1107 public void set_flight(int flight) {
1109 /* When the flight changes, reset the state */
1110 if (flight != AltosLib.MISSING) {
1111 if (this.flight != AltosLib.MISSING &&
1112 this.flight != flight) {
1115 this.flight = flight;
1119 public void set_serial(int serial) {
1120 /* When the serial changes, reset the state */
1121 if (serial != AltosLib.MISSING) {
1122 if (this.serial != AltosLib.MISSING &&
1123 this.serial != serial) {
1126 this.serial = serial;
1130 public void set_receiver_serial(int serial) {
1131 if (serial != AltosLib.MISSING)
1132 receiver_serial = serial;
1135 public boolean altitude_32() {
1136 return altitude_32 == 1;
1139 public void set_altitude_32(int altitude_32) {
1140 if (altitude_32 != AltosLib.MISSING)
1141 this.altitude_32 = altitude_32;
1145 if (rssi == AltosLib.MISSING)
1150 public void set_rssi(int rssi, int status) {
1151 if (rssi != AltosLib.MISSING) {
1153 this.status = status;
1157 public void set_received_time(long ms) {
1161 public void set_gps(AltosGPS gps, int sequence) {
1163 this.gps = gps.clone();
1164 gps_sequence = sequence;
1171 public double accel_zero_along;
1172 public double accel_zero_across;
1173 public double accel_zero_through;
1175 public AltosRotation rotation;
1176 public AltosRotation ground_rotation;
1178 public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
1179 if (zero_along != AltosLib.MISSING) {
1180 accel_zero_along = zero_along;
1181 accel_zero_across = zero_across;
1182 accel_zero_through = zero_through;
1186 public int pad_orientation;
1188 public double accel_ground_along, accel_ground_across, accel_ground_through;
1190 void update_pad_rotation() {
1191 if (pad_orientation != AltosLib.MISSING && accel_ground_along != AltosLib.MISSING) {
1192 rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - accel_zero_across),
1193 AltosIMU.convert_accel(accel_ground_through - accel_zero_through),
1194 AltosIMU.convert_accel(accel_ground_along - accel_zero_along),
1196 ground_rotation = rotation;
1197 orient.set_computed(rotation.tilt(), time);
1201 public void set_accel_ground(double ground_along, double ground_across, double ground_through) {
1202 accel_ground_along = ground_along;
1203 accel_ground_across = ground_across;
1204 accel_ground_through = ground_through;
1205 update_pad_rotation();
1208 public void set_pad_orientation(int pad_orientation) {
1209 this.pad_orientation = pad_orientation;
1210 update_pad_rotation();
1213 public double gyro_zero_roll;
1214 public double gyro_zero_pitch;
1215 public double gyro_zero_yaw;
1217 public void set_gyro_zero(double roll, double pitch, double yaw) {
1218 if (roll != AltosLib.MISSING) {
1219 gyro_zero_roll = roll;
1220 gyro_zero_pitch = pitch;
1221 gyro_zero_yaw = yaw;
1225 public double last_imu_time;
1227 private double radians(double degrees) {
1228 if (degrees == AltosLib.MISSING)
1229 return AltosLib.MISSING;
1230 return degrees * Math.PI / 180.0;
1233 private void update_orient() {
1234 if (last_imu_time != AltosLib.MISSING) {
1235 double t = time - last_imu_time;
1237 double pitch = radians(gyro_pitch());
1238 double yaw = radians(gyro_yaw());
1239 double roll = radians(gyro_roll());
1241 if (t > 0 & pitch != AltosLib.MISSING && rotation != null) {
1242 rotation.rotate(t, pitch, yaw, roll);
1243 orient.set_computed(rotation.tilt(), time);
1246 last_imu_time = time;
1249 public void set_imu(AltosIMU imu) {
1256 private double gyro_zero_overflow(double first) {
1257 double v = first / 128.0;
1265 public void check_imu_wrap(AltosIMU imu) {
1266 if (this.imu == null) {
1267 gyro_zero_roll += gyro_zero_overflow(imu.gyro_roll);
1268 gyro_zero_pitch += gyro_zero_overflow(imu.gyro_pitch);
1269 gyro_zero_yaw += gyro_zero_overflow(imu.gyro_yaw);
1273 public double accel_along() {
1274 if (imu != null && accel_zero_along != AltosLib.MISSING)
1275 return AltosIMU.convert_accel(imu.accel_along - accel_zero_along);
1276 return AltosLib.MISSING;
1279 public double accel_across() {
1280 if (imu != null && accel_zero_across != AltosLib.MISSING)
1281 return AltosIMU.convert_accel(imu.accel_across - accel_zero_across);
1282 return AltosLib.MISSING;
1285 public double accel_through() {
1286 if (imu != null && accel_zero_through != AltosLib.MISSING)
1287 return AltosIMU.convert_accel(imu.accel_through - accel_zero_through);
1288 return AltosLib.MISSING;
1291 public double gyro_roll() {
1292 if (imu != null && gyro_zero_roll != AltosLib.MISSING) {
1293 return AltosIMU.convert_gyro(imu.gyro_roll - gyro_zero_roll);
1295 return AltosLib.MISSING;
1298 public double gyro_pitch() {
1299 if (imu != null && gyro_zero_pitch != AltosLib.MISSING) {
1300 return AltosIMU.convert_gyro(imu.gyro_pitch - gyro_zero_pitch);
1302 return AltosLib.MISSING;
1305 public double gyro_yaw() {
1306 if (imu != null && gyro_zero_yaw != AltosLib.MISSING) {
1307 return AltosIMU.convert_gyro(imu.gyro_yaw - gyro_zero_yaw);
1309 return AltosLib.MISSING;
1312 public void set_mag(AltosMag mag) {
1313 this.mag = mag.clone();
1316 public double mag_along() {
1318 return AltosMag.convert_gauss(mag.along);
1319 return AltosLib.MISSING;
1322 public double mag_across() {
1324 return AltosMag.convert_gauss(mag.across);
1325 return AltosLib.MISSING;
1328 public double mag_through() {
1330 return AltosMag.convert_gauss(mag.through);
1331 return AltosLib.MISSING;
1334 public AltosMs5607 make_baro() {
1336 baro = new AltosMs5607();
1340 public void set_ms5607(AltosMs5607 ms5607) {
1344 set_pressure(baro.pa);
1345 set_temperature(baro.cc / 100.0);
1349 public void set_ms5607(int pres, int temp) {
1351 baro.set(pres, temp);
1353 set_pressure(baro.pa);
1354 set_temperature(baro.cc / 100.0);
1358 public void make_companion (int nchannels) {
1359 if (companion == null)
1360 companion = new AltosCompanion(nchannels);
1363 public void set_companion(AltosCompanion companion) {
1364 this.companion = companion;
1367 void update_accel() {
1368 if (accel == AltosLib.MISSING)
1370 if (accel_plus_g == AltosLib.MISSING)
1372 if (accel_minus_g == AltosLib.MISSING)
1375 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
1376 double counts_per_mss = counts_per_g / 9.80665;
1377 acceleration.set_measured((accel_plus_g - accel) / counts_per_mss, time);
1380 public void set_accel_g(double accel_plus_g, double accel_minus_g) {
1381 if (accel_plus_g != AltosLib.MISSING) {
1382 this.accel_plus_g = accel_plus_g;
1383 this.accel_minus_g = accel_minus_g;
1388 public void set_ground_accel(double ground_accel) {
1389 if (ground_accel != AltosLib.MISSING)
1390 this.ground_accel = ground_accel;
1393 public void set_accel(double accel) {
1394 if (accel != AltosLib.MISSING) {
1396 if (state == AltosLib.ao_flight_pad) {
1397 if (ground_accel_avg == AltosLib.MISSING)
1398 ground_accel_avg = accel;
1400 ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
1406 public void set_temperature(double temperature) {
1407 if (temperature != AltosLib.MISSING) {
1408 this.temperature = temperature;
1413 public void set_battery_voltage(double battery_voltage) {
1414 if (battery_voltage != AltosLib.MISSING) {
1415 this.battery_voltage = battery_voltage;
1420 public void set_pyro_voltage(double pyro_voltage) {
1421 if (pyro_voltage != AltosLib.MISSING) {
1422 this.pyro_voltage = pyro_voltage;
1427 public void set_apogee_voltage(double apogee_voltage) {
1428 if (apogee_voltage != AltosLib.MISSING) {
1429 this.apogee_voltage = apogee_voltage;
1434 public void set_main_voltage(double main_voltage) {
1435 if (main_voltage != AltosLib.MISSING) {
1436 this.main_voltage = main_voltage;
1441 public void set_ignitor_voltage(double[] voltage) {
1442 this.ignitor_voltage = voltage;
1445 public void set_pyro_fired(int fired) {
1446 this.pyro_fired = fired;
1449 public double time_since_boost() {
1450 if (tick == AltosLib.MISSING)
1453 if (boost_tick == AltosLib.MISSING)
1454 return tick / 100.0;
1455 return (tick - boost_tick) / 100.0;
1458 public boolean valid() {
1459 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1462 public AltosGPS make_temp_gps(boolean sats) {
1463 if (temp_gps == null) {
1464 temp_gps = new AltosGPS(gps);
1468 if (tick != temp_gps_sat_tick)
1469 temp_gps.cc_gps_sat = null;
1470 temp_gps_sat_tick = tick;
1475 public void set_temp_gps() {
1476 set_gps(temp_gps, gps_sequence + 1);
1477 gps_pending = false;
1481 public AltosState clone() {
1482 AltosState s = new AltosState();
1487 public AltosState () {