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_9;
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 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 Serializable{
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;
181 value = AltosLib.MISSING;
182 prev_value = AltosLib.MISSING;
183 max_value = AltosLib.MISSING;
187 class AltosCValue implements Serializable {
189 class AltosIValue extends AltosValue implements Serializable {
195 public AltosIValue measured;
196 public AltosIValue computed;
198 boolean can_max() { return true; }
200 boolean c_can_max() { return can_max(); }
203 double v = measured.value();
204 if (v != AltosLib.MISSING)
206 return computed.value();
209 boolean is_measured() {
210 return measured.value() != AltosLib.MISSING;
214 double m = measured.max();
216 if (m != AltosLib.MISSING)
218 return computed.max();
221 double prev_value() {
222 if (measured.value != AltosLib.MISSING && measured.prev_value != AltosLib.MISSING)
223 return measured.prev_value;
224 return computed.prev_value;
227 AltosValue altos_value() {
228 if (measured.value() != AltosLib.MISSING)
234 double c = measured.change();
235 if (c == AltosLib.MISSING)
236 c = computed.change();
241 double r = measured.rate();
242 if (r == AltosLib.MISSING)
247 void set_measured(double new_value, double time) {
248 measured.set(new_value, time);
251 void set_computed(double new_value, double time) {
252 computed.set(new_value, time);
255 void set_derivative(AltosValue in) {
256 computed.set_derivative(in);
259 void set_derivative(AltosCValue in) {
260 set_derivative(in.altos_value());
263 void set_integral(AltosValue in) {
264 computed.set_integral(in);
267 void set_integral(AltosCValue in) {
268 set_integral(in.altos_value());
271 void copy(AltosCValue old) {
272 measured.copy(old.measured);
273 computed.copy(old.computed);
276 void finish_update() {
277 measured.finish_update();
278 computed.finish_update();
282 measured = new AltosIValue();
283 computed = new AltosIValue();
290 public int altitude_32;
291 public int receiver_serial;
292 public boolean landed;
293 public boolean ascent; /* going up? */
294 public boolean boost; /* under power */
297 public int device_type;
298 public int config_major;
299 public int config_minor;
300 public int apogee_delay;
301 public int main_deploy;
302 public int flight_log_max;
304 private double pressure_to_altitude(double p) {
305 if (p == AltosLib.MISSING)
306 return AltosLib.MISSING;
307 return AltosConvert.pressure_to_altitude(p);
310 private AltosCValue ground_altitude;
312 public double ground_altitude() {
313 return ground_altitude.value();
316 public void set_ground_altitude(double a) {
317 ground_altitude.set_measured(a, time);
320 class AltosGpsGroundAltitude extends AltosValue implements Serializable {
321 void set(double a, double t) {
324 gps_altitude.set_gps_height();
327 void set_filtered(double a, double t) {
328 super.set_filtered(a, t);
330 gps_altitude.set_gps_height();
334 private AltosGpsGroundAltitude gps_ground_altitude;
336 public double gps_ground_altitude() {
337 return gps_ground_altitude.value();
340 public void set_gps_ground_altitude(double a) {
341 gps_ground_altitude.set(a, time);
344 class AltosGroundPressure extends AltosCValue implements Serializable {
345 void set_filtered(double p, double time) {
346 computed.set_filtered(p, time);
348 ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
351 void set_measured(double p, double time) {
352 super.set_measured(p, time);
353 ground_altitude.set_computed(pressure_to_altitude(p), time);
357 private AltosGroundPressure ground_pressure;
359 public double ground_pressure() {
360 return ground_pressure.value();
363 public void set_ground_pressure (double pressure) {
364 ground_pressure.set_measured(pressure, time);
367 class AltosAltitude extends AltosCValue implements Serializable {
369 private void set_speed(AltosValue v) {
370 if (!acceleration.is_measured() || !ascent)
371 speed.set_derivative(this);
374 void set_computed(double a, double time) {
375 super.set_computed(a,time);
380 void set_measured(double a, double time) {
381 super.set_measured(a,time);
387 private AltosAltitude altitude;
389 class AltosGpsAltitude extends AltosValue implements Serializable {
391 private void set_gps_height() {
393 double g = gps_ground_altitude.value();
395 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
398 gps_height = AltosLib.MISSING;
401 void set(double a, double t) {
407 private AltosGpsAltitude gps_altitude;
409 private AltosValue gps_ground_speed;
410 private AltosValue gps_ascent_rate;
411 private AltosValue gps_course;
412 private AltosValue gps_speed;
414 public double altitude() {
415 double a = altitude.value();
416 if (a != AltosLib.MISSING)
418 return gps_altitude.value();
421 public double max_altitude() {
422 double a = altitude.max();
423 if (a != AltosLib.MISSING)
425 return gps_altitude.max();
428 public void set_altitude(double new_altitude) {
429 altitude.set_measured(new_altitude, time);
432 public double gps_altitude() {
433 return gps_altitude.value();
436 public double max_gps_altitude() {
437 return gps_altitude.max();
440 public void set_gps_altitude(double new_gps_altitude) {
441 gps_altitude.set(new_gps_altitude, time);
444 public double gps_ground_speed() {
445 return gps_ground_speed.value();
448 public double max_gps_ground_speed() {
449 return gps_ground_speed.max();
452 public double gps_ascent_rate() {
453 return gps_ascent_rate.value();
456 public double max_gps_ascent_rate() {
457 return gps_ascent_rate.max();
460 public double gps_course() {
461 return gps_course.value();
464 public double gps_speed() {
465 return gps_speed.value();
468 public double max_gps_speed() {
469 return gps_speed.max();
472 class AltosPressure extends AltosValue implements Serializable {
473 void set(double p, double time) {
475 if (state == AltosLib.ao_flight_pad)
476 ground_pressure.set_filtered(p, time);
477 double a = pressure_to_altitude(p);
478 altitude.set_computed(a, time);
482 private AltosPressure pressure;
484 public double pressure() {
485 return pressure.value();
488 public void set_pressure(double p) {
489 pressure.set(p, time);
492 public double baro_height() {
493 double a = altitude();
494 double g = ground_altitude();
495 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
497 return AltosLib.MISSING;
500 public double height() {
501 double k = kalman_height.value();
502 if (k != AltosLib.MISSING)
505 double b = baro_height();
506 if (b != AltosLib.MISSING)
512 public double max_height() {
513 double k = kalman_height.max();
514 if (k != AltosLib.MISSING)
517 double a = altitude.max();
518 double g = ground_altitude();
519 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
521 return max_gps_height();
524 public double gps_height() {
525 double a = gps_altitude();
526 double g = gps_ground_altitude();
528 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
530 return AltosLib.MISSING;
533 public double max_gps_height() {
534 double a = gps_altitude.max();
535 double g = gps_ground_altitude();
537 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
539 return AltosLib.MISSING;
542 class AltosSpeed extends AltosCValue implements Serializable {
545 return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
549 acceleration.set_derivative(this);
552 void set_derivative(AltosCValue in) {
553 super.set_derivative(in);
557 void set_computed(double new_value, double time) {
558 super.set_computed(new_value, time);
562 void set_measured(double new_value, double time) {
563 super.set_measured(new_value, time);
568 private AltosSpeed speed;
570 public double speed() {
571 double v = kalman_speed.value();
572 if (v != AltosLib.MISSING)
575 if (v != AltosLib.MISSING)
578 if (v != AltosLib.MISSING)
580 return AltosLib.MISSING;
583 public double max_speed() {
584 double v = kalman_speed.max();
585 if (v != AltosLib.MISSING)
588 if (v != AltosLib.MISSING)
591 if (v != AltosLib.MISSING)
593 return AltosLib.MISSING;
596 class AltosAccel extends AltosCValue implements Serializable {
599 return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
602 void set_measured(double a, double time) {
603 super.set_measured(a, time);
605 speed.set_integral(this.measured);
609 AltosAccel acceleration;
611 public double acceleration() {
612 return acceleration.value();
615 public double max_acceleration() {
616 return acceleration.max();
619 public AltosCValue orient;
621 public void set_orient(double new_orient) {
622 orient.set_measured(new_orient, time);
625 public double orient() {
626 return orient.value();
629 public double max_orient() {
633 public AltosValue kalman_height, kalman_speed, kalman_acceleration;
635 public void set_kalman(double height, double speed, double acceleration) {
636 kalman_height.set(height, time);
637 kalman_speed.set(speed, time);
638 kalman_acceleration.set(acceleration, time);
641 public double battery_voltage;
642 public double pyro_voltage;
643 public double temperature;
644 public double apogee_voltage;
645 public double main_voltage;
647 public double ignitor_voltage[];
650 public AltosGPS temp_gps;
651 public int temp_gps_sat_tick;
652 public boolean gps_pending;
653 public int gps_sequence;
658 public static final int MIN_PAD_SAMPLES = 10;
661 public int gps_waiting;
662 public boolean gps_ready;
666 public AltosGreatCircle from_pad;
667 public double elevation; /* from pad */
668 public double range; /* total distance */
670 public double gps_height;
672 public double pad_lat, pad_lon, pad_alt;
674 public int speak_tick;
675 public double speak_altitude;
677 public String callsign;
678 public String firmware_version;
680 public double accel_plus_g;
681 public double accel_minus_g;
683 public double ground_accel;
684 public double ground_accel_avg;
686 public int log_format;
687 public String product;
689 public AltosMs5607 baro;
691 public AltosCompanion companion;
693 public int pyro_fired;
695 public void set_npad(int npad) {
697 gps_waiting = MIN_PAD_SAMPLES - npad;
698 if (this.gps_waiting < 0)
700 gps_ready = gps_waiting == 0;
706 received_time = System.currentTimeMillis();
707 time = AltosLib.MISSING;
708 time_change = AltosLib.MISSING;
709 prev_time = AltosLib.MISSING;
710 tick = AltosLib.MISSING;
711 prev_tick = AltosLib.MISSING;
712 boost_tick = AltosLib.MISSING;
713 state = AltosLib.ao_flight_invalid;
714 flight = AltosLib.MISSING;
717 rssi = AltosLib.MISSING;
719 device_type = AltosLib.MISSING;
720 config_major = AltosLib.MISSING;
721 config_minor = AltosLib.MISSING;
722 apogee_delay = AltosLib.MISSING;
723 main_deploy = AltosLib.MISSING;
724 flight_log_max = AltosLib.MISSING;
726 ground_altitude = new AltosCValue();
727 ground_pressure = new AltosGroundPressure();
728 altitude = new AltosAltitude();
729 pressure = new AltosPressure();
730 speed = new AltosSpeed();
731 acceleration = new AltosAccel();
732 orient = new AltosCValue();
734 temperature = AltosLib.MISSING;
735 battery_voltage = AltosLib.MISSING;
736 pyro_voltage = AltosLib.MISSING;
737 apogee_voltage = AltosLib.MISSING;
738 main_voltage = AltosLib.MISSING;
739 ignitor_voltage = null;
741 kalman_height = new AltosValue();
742 kalman_speed = new AltosValue();
743 kalman_acceleration = new AltosValue();
747 temp_gps_sat_tick = 0;
752 last_imu_time = AltosLib.MISSING;
754 ground_rotation = null;
757 accel_zero_along = AltosLib.MISSING;
758 accel_zero_across = AltosLib.MISSING;
759 accel_zero_through = AltosLib.MISSING;
761 accel_ground_along = AltosLib.MISSING;
762 accel_ground_across = AltosLib.MISSING;
763 accel_ground_through = AltosLib.MISSING;
765 pad_orientation = AltosLib.MISSING;
767 gyro_zero_roll = AltosLib.MISSING;
768 gyro_zero_pitch = AltosLib.MISSING;
769 gyro_zero_yaw = AltosLib.MISSING;
775 elevation = AltosLib.MISSING;
776 range = AltosLib.MISSING;
777 gps_height = AltosLib.MISSING;
779 pad_lat = AltosLib.MISSING;
780 pad_lon = AltosLib.MISSING;
781 pad_alt = AltosLib.MISSING;
783 gps_altitude = new AltosGpsAltitude();
784 gps_ground_altitude = new AltosGpsGroundAltitude();
785 gps_ground_speed = new AltosValue();
786 gps_speed = new AltosValue();
787 gps_ascent_rate = new AltosValue();
788 gps_course = new AltosValue();
790 speak_tick = AltosLib.MISSING;
791 speak_altitude = AltosLib.MISSING;
794 firmware_version = null;
796 accel_plus_g = AltosLib.MISSING;
797 accel_minus_g = AltosLib.MISSING;
798 accel = AltosLib.MISSING;
800 ground_accel = AltosLib.MISSING;
801 ground_accel_avg = AltosLib.MISSING;
803 log_format = AltosLib.MISSING;
805 serial = AltosLib.MISSING;
806 receiver_serial = AltosLib.MISSING;
807 altitude_32 = AltosLib.MISSING;
815 void finish_update() {
818 ground_altitude.finish_update();
819 altitude.finish_update();
820 pressure.finish_update();
821 speed.finish_update();
822 acceleration.finish_update();
823 orient.finish_update();
825 kalman_height.finish_update();
826 kalman_speed.finish_update();
827 kalman_acceleration.finish_update();
830 void copy(AltosState old) {
837 received_time = old.received_time;
839 time_change = old.time_change;
840 prev_time = old.time;
843 prev_tick = old.tick;
844 boost_tick = old.boost_tick;
853 device_type = old.device_type;
854 config_major = old.config_major;
855 config_minor = old.config_minor;
856 apogee_delay = old.apogee_delay;
857 main_deploy = old.main_deploy;
858 flight_log_max = old.flight_log_max;
862 ground_pressure.copy(old.ground_pressure);
863 ground_altitude.copy(old.ground_altitude);
864 altitude.copy(old.altitude);
865 pressure.copy(old.pressure);
866 speed.copy(old.speed);
867 acceleration.copy(old.acceleration);
868 orient.copy(old.orient);
870 battery_voltage = old.battery_voltage;
871 pyro_voltage = old.pyro_voltage;
872 temperature = old.temperature;
873 apogee_voltage = old.apogee_voltage;
874 main_voltage = old.main_voltage;
875 ignitor_voltage = old.ignitor_voltage;
877 kalman_height.copy(old.kalman_height);
878 kalman_speed.copy(old.kalman_speed);
879 kalman_acceleration.copy(old.kalman_acceleration);
882 gps = old.gps.clone();
885 if (old.temp_gps != null)
886 temp_gps = old.temp_gps.clone();
889 temp_gps_sat_tick = old.temp_gps_sat_tick;
890 gps_sequence = old.gps_sequence;
891 gps_pending = old.gps_pending;
894 imu = old.imu.clone();
897 last_imu_time = old.last_imu_time;
899 if (old.rotation != null)
900 rotation = new AltosRotation (old.rotation);
902 if (old.ground_rotation != null) {
903 ground_rotation = new AltosRotation(old.ground_rotation);
906 accel_zero_along = old.accel_zero_along;
907 accel_zero_across = old.accel_zero_across;
908 accel_zero_through = old.accel_zero_through;
910 accel_ground_along = old.accel_ground_along;
911 accel_ground_across = old.accel_ground_across;
912 accel_ground_through = old.accel_ground_through;
913 pad_orientation = old.pad_orientation;
915 gyro_zero_roll = old.gyro_zero_roll;
916 gyro_zero_pitch = old.gyro_zero_pitch;
917 gyro_zero_yaw = old.gyro_zero_yaw;
920 mag = old.mag.clone();
925 gps_waiting = old.gps_waiting;
926 gps_ready = old.gps_ready;
929 if (old.from_pad != null)
930 from_pad = old.from_pad.clone();
934 elevation = old.elevation;
937 gps_height = old.gps_height;
939 gps_altitude.copy(old.gps_altitude);
940 gps_ground_altitude.copy(old.gps_ground_altitude);
941 gps_ground_speed.copy(old.gps_ground_speed);
942 gps_ascent_rate.copy(old.gps_ascent_rate);
943 gps_course.copy(old.gps_course);
944 gps_speed.copy(old.gps_speed);
946 pad_lat = old.pad_lat;
947 pad_lon = old.pad_lon;
948 pad_alt = old.pad_alt;
950 speak_tick = old.speak_tick;
951 speak_altitude = old.speak_altitude;
953 callsign = old.callsign;
954 firmware_version = old.firmware_version;
956 accel_plus_g = old.accel_plus_g;
957 accel_minus_g = old.accel_minus_g;
959 ground_accel = old.ground_accel;
960 ground_accel_avg = old.ground_accel_avg;
962 log_format = old.log_format;
963 product = old.product;
965 receiver_serial = old.receiver_serial;
966 altitude_32 = old.altitude_32;
969 companion = old.companion;
971 pyro_fired = old.pyro_fired;
978 elevation = AltosLib.MISSING;
979 range = AltosLib.MISSING;
984 if (gps.locked && gps.nsat >= 4) {
985 /* Track consecutive 'good' gps reports, waiting for 10 of them */
986 if (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_stateless) {
988 if (pad_lat != AltosLib.MISSING && (npad < 10 || state == AltosLib.ao_flight_pad)) {
989 pad_lat = (pad_lat * 31 + gps.lat) / 32;
990 pad_lon = (pad_lon * 31 + gps.lon) / 32;
991 gps_ground_altitude.set_filtered(gps.alt, time);
994 if (pad_lat == AltosLib.MISSING) {
997 gps_ground_altitude.set(gps.alt, time);
999 gps_altitude.set(gps.alt, time);
1000 if (gps.climb_rate != AltosLib.MISSING)
1001 gps_ascent_rate.set(gps.climb_rate, time);
1002 if (gps.ground_speed != AltosLib.MISSING)
1003 gps_ground_speed.set(gps.ground_speed, time);
1004 if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
1005 gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
1006 gps.climb_rate * gps.climb_rate), time);
1007 if (gps.course != AltosLib.MISSING)
1008 gps_course.set(gps.course, time);
1010 if (gps.lat != 0 && gps.lon != 0 &&
1011 pad_lat != AltosLib.MISSING &&
1012 pad_lon != AltosLib.MISSING)
1014 double h = height();
1016 if (h == AltosLib.MISSING)
1018 from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
1019 elevation = from_pad.elevation;
1020 range = from_pad.range;
1024 public void set_tick(int new_tick) {
1025 if (new_tick != AltosLib.MISSING) {
1026 if (prev_tick != AltosLib.MISSING) {
1027 while (new_tick < prev_tick - 1000) {
1032 time = tick / 100.0;
1033 time_change = time - prev_time;
1037 public void set_boost_tick(int boost_tick) {
1038 if (boost_tick != AltosLib.MISSING)
1039 this.boost_tick = boost_tick;
1042 public String state_name() {
1043 return AltosLib.state_name(state);
1046 public void set_product(String product) {
1047 this.product = product;
1050 public void set_state(int state) {
1051 if (state != AltosLib.ao_flight_invalid) {
1053 ascent = (AltosLib.ao_flight_boost <= state &&
1054 state <= AltosLib.ao_flight_coast);
1055 boost = (AltosLib.ao_flight_boost == state);
1059 public int state() {
1063 public void set_device_type(int device_type) {
1064 this.device_type = device_type;
1065 switch (device_type) {
1066 case AltosLib.product_telegps:
1067 this.state = AltosLib.ao_flight_stateless;
1072 public void set_log_format(int log_format) {
1073 this.log_format = log_format;
1074 switch (log_format) {
1075 case AltosLib.AO_LOG_FORMAT_TELEGPS:
1076 this.state = AltosLib.ao_flight_stateless;
1081 public void set_flight_params(int apogee_delay, int main_deploy) {
1082 this.apogee_delay = apogee_delay;
1083 this.main_deploy = main_deploy;
1086 public void set_config(int major, int minor, int flight_log_max) {
1087 config_major = major;
1088 config_minor = minor;
1089 this.flight_log_max = flight_log_max;
1092 public void set_callsign(String callsign) {
1093 this.callsign = callsign;
1096 public void set_firmware_version(String version) {
1097 firmware_version = version;
1100 public int compare_version(String other_version) {
1101 if (firmware_version == null)
1102 return AltosLib.MISSING;
1103 return AltosLib.compare_version(firmware_version, other_version);
1106 private void re_init() {
1107 int bt = boost_tick;
1108 int rs = receiver_serial;
1111 receiver_serial = rs;
1114 public void set_flight(int flight) {
1116 /* When the flight changes, reset the state */
1117 if (flight != AltosLib.MISSING) {
1118 if (this.flight != AltosLib.MISSING &&
1119 this.flight != flight) {
1122 this.flight = flight;
1126 public void set_serial(int serial) {
1127 /* When the serial changes, reset the state */
1128 if (serial != AltosLib.MISSING) {
1129 if (this.serial != AltosLib.MISSING &&
1130 this.serial != serial) {
1133 this.serial = serial;
1137 public void set_receiver_serial(int serial) {
1138 if (serial != AltosLib.MISSING)
1139 receiver_serial = serial;
1142 public boolean altitude_32() {
1143 return altitude_32 == 1;
1146 public void set_altitude_32(int altitude_32) {
1147 if (altitude_32 != AltosLib.MISSING)
1148 this.altitude_32 = altitude_32;
1152 if (rssi == AltosLib.MISSING)
1157 public void set_rssi(int rssi, int status) {
1158 if (rssi != AltosLib.MISSING) {
1160 this.status = status;
1164 public void set_received_time(long ms) {
1168 public void set_gps(AltosGPS gps, int sequence) {
1170 this.gps = gps.clone();
1171 gps_sequence = sequence;
1178 public double accel_zero_along;
1179 public double accel_zero_across;
1180 public double accel_zero_through;
1182 public AltosRotation rotation;
1183 public AltosRotation ground_rotation;
1185 public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
1186 if (zero_along != AltosLib.MISSING) {
1187 accel_zero_along = zero_along;
1188 accel_zero_across = zero_across;
1189 accel_zero_through = zero_through;
1193 public int pad_orientation;
1195 public double accel_ground_along, accel_ground_across, accel_ground_through;
1197 void update_pad_rotation() {
1198 if (pad_orientation != AltosLib.MISSING && accel_ground_along != AltosLib.MISSING) {
1199 rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - accel_zero_across),
1200 AltosIMU.convert_accel(accel_ground_through - accel_zero_through),
1201 AltosIMU.convert_accel(accel_ground_along - accel_zero_along),
1203 ground_rotation = rotation;
1204 orient.set_computed(rotation.tilt(), time);
1208 public void set_accel_ground(double ground_along, double ground_across, double ground_through) {
1209 accel_ground_along = ground_along;
1210 accel_ground_across = ground_across;
1211 accel_ground_through = ground_through;
1212 update_pad_rotation();
1215 public void set_pad_orientation(int pad_orientation) {
1216 this.pad_orientation = pad_orientation;
1217 update_pad_rotation();
1220 public double gyro_zero_roll;
1221 public double gyro_zero_pitch;
1222 public double gyro_zero_yaw;
1224 public void set_gyro_zero(double roll, double pitch, double yaw) {
1225 if (roll != AltosLib.MISSING) {
1226 gyro_zero_roll = roll;
1227 gyro_zero_pitch = pitch;
1228 gyro_zero_yaw = yaw;
1232 public double last_imu_time;
1234 private double radians(double degrees) {
1235 if (degrees == AltosLib.MISSING)
1236 return AltosLib.MISSING;
1237 return degrees * Math.PI / 180.0;
1240 private void update_orient() {
1241 if (last_imu_time != AltosLib.MISSING) {
1242 double t = time - last_imu_time;
1244 double pitch = radians(gyro_pitch());
1245 double yaw = radians(gyro_yaw());
1246 double roll = radians(gyro_roll());
1248 if (t > 0 & pitch != AltosLib.MISSING && rotation != null) {
1249 rotation.rotate(t, pitch, yaw, roll);
1250 orient.set_computed(rotation.tilt(), time);
1253 last_imu_time = time;
1256 public void set_imu(AltosIMU imu) {
1263 private double gyro_zero_overflow(double first) {
1264 double v = first / 128.0;
1272 public void check_imu_wrap(AltosIMU imu) {
1273 if (this.imu == null) {
1274 gyro_zero_roll += gyro_zero_overflow(imu.gyro_roll);
1275 gyro_zero_pitch += gyro_zero_overflow(imu.gyro_pitch);
1276 gyro_zero_yaw += gyro_zero_overflow(imu.gyro_yaw);
1280 public double accel_along() {
1281 if (imu != null && accel_zero_along != AltosLib.MISSING)
1282 return AltosIMU.convert_accel(imu.accel_along - accel_zero_along);
1283 return AltosLib.MISSING;
1286 public double accel_across() {
1287 if (imu != null && accel_zero_across != AltosLib.MISSING)
1288 return AltosIMU.convert_accel(imu.accel_across - accel_zero_across);
1289 return AltosLib.MISSING;
1292 public double accel_through() {
1293 if (imu != null && accel_zero_through != AltosLib.MISSING)
1294 return AltosIMU.convert_accel(imu.accel_through - accel_zero_through);
1295 return AltosLib.MISSING;
1298 public double gyro_roll() {
1299 if (imu != null && gyro_zero_roll != AltosLib.MISSING) {
1300 return AltosIMU.convert_gyro(imu.gyro_roll - gyro_zero_roll);
1302 return AltosLib.MISSING;
1305 public double gyro_pitch() {
1306 if (imu != null && gyro_zero_pitch != AltosLib.MISSING) {
1307 return AltosIMU.convert_gyro(imu.gyro_pitch - gyro_zero_pitch);
1309 return AltosLib.MISSING;
1312 public double gyro_yaw() {
1313 if (imu != null && gyro_zero_yaw != AltosLib.MISSING) {
1314 return AltosIMU.convert_gyro(imu.gyro_yaw - gyro_zero_yaw);
1316 return AltosLib.MISSING;
1319 public void set_mag(AltosMag mag) {
1320 this.mag = mag.clone();
1323 public double mag_along() {
1325 return AltosMag.convert_gauss(mag.along);
1326 return AltosLib.MISSING;
1329 public double mag_across() {
1331 return AltosMag.convert_gauss(mag.across);
1332 return AltosLib.MISSING;
1335 public double mag_through() {
1337 return AltosMag.convert_gauss(mag.through);
1338 return AltosLib.MISSING;
1341 public AltosMs5607 make_baro() {
1343 baro = new AltosMs5607();
1347 public void set_ms5607(AltosMs5607 ms5607) {
1351 set_pressure(baro.pa);
1352 set_temperature(baro.cc / 100.0);
1356 public void set_ms5607(int pres, int temp) {
1358 baro.set(pres, temp);
1360 set_pressure(baro.pa);
1361 set_temperature(baro.cc / 100.0);
1365 public void set_companion(AltosCompanion companion) {
1366 this.companion = companion;
1369 void update_accel() {
1370 if (accel == AltosLib.MISSING)
1372 if (accel_plus_g == AltosLib.MISSING)
1374 if (accel_minus_g == AltosLib.MISSING)
1377 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
1378 double counts_per_mss = counts_per_g / 9.80665;
1379 acceleration.set_measured((accel_plus_g - accel) / counts_per_mss, time);
1382 public void set_accel_g(double accel_plus_g, double accel_minus_g) {
1383 if (accel_plus_g != AltosLib.MISSING) {
1384 this.accel_plus_g = accel_plus_g;
1385 this.accel_minus_g = accel_minus_g;
1390 public void set_ground_accel(double ground_accel) {
1391 if (ground_accel != AltosLib.MISSING)
1392 this.ground_accel = ground_accel;
1395 public void set_accel(double accel) {
1396 if (accel != AltosLib.MISSING) {
1398 if (state == AltosLib.ao_flight_pad) {
1399 if (ground_accel_avg == AltosLib.MISSING)
1400 ground_accel_avg = accel;
1402 ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
1408 public void set_temperature(double temperature) {
1409 if (temperature != AltosLib.MISSING) {
1410 this.temperature = temperature;
1415 public void set_battery_voltage(double battery_voltage) {
1416 if (battery_voltage != AltosLib.MISSING) {
1417 this.battery_voltage = battery_voltage;
1422 public void set_pyro_voltage(double pyro_voltage) {
1423 if (pyro_voltage != AltosLib.MISSING) {
1424 this.pyro_voltage = pyro_voltage;
1429 public void set_apogee_voltage(double apogee_voltage) {
1430 if (apogee_voltage != AltosLib.MISSING) {
1431 this.apogee_voltage = apogee_voltage;
1436 public void set_main_voltage(double main_voltage) {
1437 if (main_voltage != AltosLib.MISSING) {
1438 this.main_voltage = main_voltage;
1443 public void set_ignitor_voltage(double[] voltage) {
1444 this.ignitor_voltage = voltage;
1447 public void set_pyro_fired(int fired) {
1448 this.pyro_fired = fired;
1451 public double time_since_boost() {
1452 if (tick == AltosLib.MISSING)
1455 if (boost_tick == AltosLib.MISSING)
1456 return tick / 100.0;
1457 return (tick - boost_tick) / 100.0;
1460 public boolean valid() {
1461 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1464 public AltosGPS make_temp_gps(boolean sats) {
1465 if (temp_gps == null) {
1466 temp_gps = new AltosGPS(gps);
1470 if (tick != temp_gps_sat_tick)
1471 temp_gps.cc_gps_sat = null;
1472 temp_gps_sat_tick = tick;
1477 public void set_temp_gps() {
1478 set_gps(temp_gps, gps_sequence + 1);
1479 gps_pending = false;
1483 public AltosState clone() {
1484 AltosState s = new AltosState();
1489 public AltosState () {