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_6;
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_state(int state) {
1044 if (state != AltosLib.ao_flight_invalid) {
1046 ascent = (AltosLib.ao_flight_boost <= state &&
1047 state <= AltosLib.ao_flight_coast);
1048 boost = (AltosLib.ao_flight_boost == state);
1052 public void set_device_type(int device_type) {
1053 this.device_type = device_type;
1054 switch (device_type) {
1055 case AltosLib.product_telegps:
1056 this.state = AltosLib.ao_flight_stateless;
1061 public void set_log_format(int log_format) {
1062 this.log_format = log_format;
1063 switch (log_format) {
1064 case AltosLib.AO_LOG_FORMAT_TELEGPS:
1065 this.state = AltosLib.ao_flight_stateless;
1070 public void set_flight_params(int apogee_delay, int main_deploy) {
1071 this.apogee_delay = apogee_delay;
1072 this.main_deploy = main_deploy;
1075 public void set_config(int major, int minor, int flight_log_max) {
1076 config_major = major;
1077 config_minor = minor;
1078 this.flight_log_max = flight_log_max;
1081 public void set_callsign(String callsign) {
1082 this.callsign = callsign;
1085 public void set_firmware_version(String version) {
1086 firmware_version = version;
1089 public int compare_version(String other_version) {
1090 if (firmware_version == null)
1091 return AltosLib.MISSING;
1092 return AltosLib.compare_version(firmware_version, other_version);
1095 private void re_init() {
1096 int bt = boost_tick;
1097 int rs = receiver_serial;
1100 receiver_serial = rs;
1103 public void set_flight(int flight) {
1105 /* When the flight changes, reset the state */
1106 if (flight != AltosLib.MISSING) {
1107 if (this.flight != AltosLib.MISSING &&
1108 this.flight != flight) {
1111 this.flight = flight;
1115 public void set_serial(int serial) {
1116 /* When the serial changes, reset the state */
1117 if (serial != AltosLib.MISSING) {
1118 if (this.serial != AltosLib.MISSING &&
1119 this.serial != serial) {
1122 this.serial = serial;
1126 public void set_receiver_serial(int serial) {
1127 if (serial != AltosLib.MISSING)
1128 receiver_serial = serial;
1131 public boolean altitude_32() {
1132 return altitude_32 == 1;
1135 public void set_altitude_32(int altitude_32) {
1136 if (altitude_32 != AltosLib.MISSING)
1137 this.altitude_32 = altitude_32;
1141 if (rssi == AltosLib.MISSING)
1146 public void set_rssi(int rssi, int status) {
1147 if (rssi != AltosLib.MISSING) {
1149 this.status = status;
1153 public void set_received_time(long ms) {
1157 public void set_gps(AltosGPS gps, int sequence) {
1159 this.gps = gps.clone();
1160 gps_sequence = sequence;
1167 public double accel_zero_along;
1168 public double accel_zero_across;
1169 public double accel_zero_through;
1171 public AltosRotation rotation;
1172 public AltosRotation ground_rotation;
1174 public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
1175 if (zero_along != AltosLib.MISSING) {
1176 accel_zero_along = zero_along;
1177 accel_zero_across = zero_across;
1178 accel_zero_through = zero_through;
1182 public int pad_orientation;
1184 public double accel_ground_along, accel_ground_across, accel_ground_through;
1186 void update_pad_rotation() {
1187 if (pad_orientation != AltosLib.MISSING && accel_ground_along != AltosLib.MISSING) {
1188 rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - accel_zero_across),
1189 AltosIMU.convert_accel(accel_ground_through - accel_zero_through),
1190 AltosIMU.convert_accel(accel_ground_along - accel_zero_along),
1192 ground_rotation = rotation;
1193 orient.set_computed(rotation.tilt(), time);
1197 public void set_accel_ground(double ground_along, double ground_across, double ground_through) {
1198 accel_ground_along = ground_along;
1199 accel_ground_across = ground_across;
1200 accel_ground_through = ground_through;
1201 update_pad_rotation();
1204 public void set_pad_orientation(int pad_orientation) {
1205 this.pad_orientation = pad_orientation;
1206 update_pad_rotation();
1209 public double gyro_zero_roll;
1210 public double gyro_zero_pitch;
1211 public double gyro_zero_yaw;
1213 public void set_gyro_zero(double roll, double pitch, double yaw) {
1214 if (roll != AltosLib.MISSING) {
1215 gyro_zero_roll = roll;
1216 gyro_zero_pitch = pitch;
1217 gyro_zero_yaw = yaw;
1221 public double last_imu_time;
1223 private double radians(double degrees) {
1224 if (degrees == AltosLib.MISSING)
1225 return AltosLib.MISSING;
1226 return degrees * Math.PI / 180.0;
1229 private void update_orient() {
1230 if (last_imu_time != AltosLib.MISSING) {
1231 double t = time - last_imu_time;
1233 double pitch = radians(gyro_pitch());
1234 double yaw = radians(gyro_yaw());
1235 double roll = radians(gyro_roll());
1237 if (t > 0 & pitch != AltosLib.MISSING && rotation != null) {
1238 rotation.rotate(t, pitch, yaw, roll);
1239 orient.set_computed(rotation.tilt(), time);
1242 last_imu_time = time;
1245 public void set_imu(AltosIMU imu) {
1252 private double gyro_zero_overflow(double first) {
1253 double v = first / 128.0;
1261 public void check_imu_wrap(AltosIMU imu) {
1262 if (this.imu == null) {
1263 gyro_zero_roll += gyro_zero_overflow(imu.gyro_roll);
1264 gyro_zero_pitch += gyro_zero_overflow(imu.gyro_pitch);
1265 gyro_zero_yaw += gyro_zero_overflow(imu.gyro_yaw);
1269 public double accel_along() {
1270 if (imu != null && accel_zero_along != AltosLib.MISSING)
1271 return AltosIMU.convert_accel(imu.accel_along - accel_zero_along);
1272 return AltosLib.MISSING;
1275 public double accel_across() {
1276 if (imu != null && accel_zero_across != AltosLib.MISSING)
1277 return AltosIMU.convert_accel(imu.accel_across - accel_zero_across);
1278 return AltosLib.MISSING;
1281 public double accel_through() {
1282 if (imu != null && accel_zero_through != AltosLib.MISSING)
1283 return AltosIMU.convert_accel(imu.accel_through - accel_zero_through);
1284 return AltosLib.MISSING;
1287 public double gyro_roll() {
1288 if (imu != null && gyro_zero_roll != AltosLib.MISSING) {
1289 return AltosIMU.convert_gyro(imu.gyro_roll - gyro_zero_roll);
1291 return AltosLib.MISSING;
1294 public double gyro_pitch() {
1295 if (imu != null && gyro_zero_pitch != AltosLib.MISSING) {
1296 return AltosIMU.convert_gyro(imu.gyro_pitch - gyro_zero_pitch);
1298 return AltosLib.MISSING;
1301 public double gyro_yaw() {
1302 if (imu != null && gyro_zero_yaw != AltosLib.MISSING) {
1303 return AltosIMU.convert_gyro(imu.gyro_yaw - gyro_zero_yaw);
1305 return AltosLib.MISSING;
1308 public void set_mag(AltosMag mag) {
1309 this.mag = mag.clone();
1312 public double mag_along() {
1314 return AltosMag.convert_gauss(mag.along);
1315 return AltosLib.MISSING;
1318 public double mag_across() {
1320 return AltosMag.convert_gauss(mag.across);
1321 return AltosLib.MISSING;
1324 public double mag_through() {
1326 return AltosMag.convert_gauss(mag.through);
1327 return AltosLib.MISSING;
1330 public AltosMs5607 make_baro() {
1332 baro = new AltosMs5607();
1336 public void set_ms5607(AltosMs5607 ms5607) {
1340 set_pressure(baro.pa);
1341 set_temperature(baro.cc / 100.0);
1345 public void set_ms5607(int pres, int temp) {
1347 baro.set(pres, temp);
1349 set_pressure(baro.pa);
1350 set_temperature(baro.cc / 100.0);
1354 public void make_companion (int nchannels) {
1355 if (companion == null)
1356 companion = new AltosCompanion(nchannels);
1359 public void set_companion(AltosCompanion companion) {
1360 this.companion = companion;
1363 void update_accel() {
1364 if (accel == AltosLib.MISSING)
1366 if (accel_plus_g == AltosLib.MISSING)
1368 if (accel_minus_g == AltosLib.MISSING)
1371 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
1372 double counts_per_mss = counts_per_g / 9.80665;
1373 acceleration.set_measured((accel_plus_g - accel) / counts_per_mss, time);
1376 public void set_accel_g(double accel_plus_g, double accel_minus_g) {
1377 if (accel_plus_g != AltosLib.MISSING) {
1378 this.accel_plus_g = accel_plus_g;
1379 this.accel_minus_g = accel_minus_g;
1384 public void set_ground_accel(double ground_accel) {
1385 if (ground_accel != AltosLib.MISSING)
1386 this.ground_accel = ground_accel;
1389 public void set_accel(double accel) {
1390 if (accel != AltosLib.MISSING) {
1392 if (state == AltosLib.ao_flight_pad) {
1393 if (ground_accel_avg == AltosLib.MISSING)
1394 ground_accel_avg = accel;
1396 ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
1402 public void set_temperature(double temperature) {
1403 if (temperature != AltosLib.MISSING) {
1404 this.temperature = temperature;
1409 public void set_battery_voltage(double battery_voltage) {
1410 if (battery_voltage != AltosLib.MISSING) {
1411 this.battery_voltage = battery_voltage;
1416 public void set_pyro_voltage(double pyro_voltage) {
1417 if (pyro_voltage != AltosLib.MISSING) {
1418 this.pyro_voltage = pyro_voltage;
1423 public void set_apogee_voltage(double apogee_voltage) {
1424 if (apogee_voltage != AltosLib.MISSING) {
1425 this.apogee_voltage = apogee_voltage;
1430 public void set_main_voltage(double main_voltage) {
1431 if (main_voltage != AltosLib.MISSING) {
1432 this.main_voltage = main_voltage;
1437 public void set_ignitor_voltage(double[] voltage) {
1438 this.ignitor_voltage = voltage;
1441 public void set_pyro_fired(int fired) {
1442 this.pyro_fired = fired;
1445 public double time_since_boost() {
1446 if (tick == AltosLib.MISSING)
1449 if (boost_tick == AltosLib.MISSING)
1450 return tick / 100.0;
1451 return (tick - boost_tick) / 100.0;
1454 public boolean valid() {
1455 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1458 public AltosGPS make_temp_gps(boolean sats) {
1459 if (temp_gps == null) {
1460 temp_gps = new AltosGPS(gps);
1464 if (tick != temp_gps_sat_tick)
1465 temp_gps.cc_gps_sat = null;
1466 temp_gps_sat_tick = tick;
1471 public void set_temp_gps() {
1472 set_gps(temp_gps, gps_sequence + 1);
1473 gps_pending = false;
1477 public AltosState clone() {
1478 AltosState s = new AltosState();
1483 public AltosState () {