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_5;
24 public class AltosState implements Cloneable {
26 public static final int set_position = 1;
27 public static final int set_gps = 2;
28 public static final int set_data = 4;
32 static final double ascent_filter_len = 0.5;
33 static final double descent_filter_len = 0.5;
37 public long received_time;
40 public double prev_time;
41 public double time_change;
43 private int prev_tick;
44 public int boost_tick;
49 private double max_value;
50 private double set_time;
51 private double prev_set_time;
53 boolean can_max() { return true; }
55 void set(double new_value, double time) {
56 if (new_value != AltosLib.MISSING) {
58 if (can_max() && (max_value == AltosLib.MISSING || value > max_value))
64 void set_filtered(double new_value, double time) {
65 if (prev_value != AltosLib.MISSING)
66 new_value = (prev_value * 15.0 + new_value) / 16.0;
83 if (value != AltosLib.MISSING && prev_value != AltosLib.MISSING)
84 return value - prev_value;
85 return AltosLib.MISSING;
90 double t = set_time - prev_set_time;
92 if (c != AltosLib.MISSING && t != 0)
94 return AltosLib.MISSING;
98 if (value == AltosLib.MISSING)
99 return AltosLib.MISSING;
100 if (prev_value == AltosLib.MISSING)
101 return AltosLib.MISSING;
103 return (value + prev_value) / 2 * (set_time - prev_set_time);
110 void set_derivative(AltosValue in) {
111 double n = in.rate();
113 if (n == AltosLib.MISSING)
116 double p = prev_value;
117 double pt = prev_set_time;
119 if (p == AltosLib.MISSING) {
121 pt = in.time() - 0.01;
124 /* Clip changes to reduce noise */
125 double ddt = in.time() - pt;
126 double ddv = (n - p) / ddt;
128 final double max = 100000;
131 if (Math.abs(ddv) > max) {
141 filter_len = ascent_filter_len;
143 filter_len = descent_filter_len;
145 double f = 1/Math.exp(ddt/ filter_len);
146 n = p * f + n * (1-f);
151 void set_integral(AltosValue in) {
152 double change = in.integrate();
154 if (change != AltosLib.MISSING) {
155 double prev = prev_value;
156 if (prev == AltosLib.MISSING)
158 set(prev + change, in.time());
162 void copy(AltosValue old) {
164 set_time = old.set_time;
165 prev_value = old.value;
166 prev_set_time = old.set_time;
167 max_value = old.max_value;
170 void finish_update() {
172 prev_set_time = set_time;
176 value = AltosLib.MISSING;
177 prev_value = AltosLib.MISSING;
178 max_value = AltosLib.MISSING;
184 class AltosIValue extends AltosValue {
190 public AltosIValue measured;
191 public AltosIValue computed;
193 boolean can_max() { return true; }
195 boolean c_can_max() { return can_max(); }
198 double v = measured.value();
199 if (v != AltosLib.MISSING)
201 return computed.value();
204 boolean is_measured() {
205 return measured.value() != AltosLib.MISSING;
209 double m = measured.max();
211 if (m != AltosLib.MISSING)
213 return computed.max();
216 double prev_value() {
217 if (measured.value != AltosLib.MISSING && measured.prev_value != AltosLib.MISSING)
218 return measured.prev_value;
219 return computed.prev_value;
222 AltosValue altos_value() {
223 if (measured.value() != AltosLib.MISSING)
229 double c = measured.change();
230 if (c == AltosLib.MISSING)
231 c = computed.change();
236 double r = measured.rate();
237 if (r == AltosLib.MISSING)
242 void set_measured(double new_value, double time) {
243 measured.set(new_value, time);
246 void set_computed(double new_value, double time) {
247 computed.set(new_value, time);
250 void set_derivative(AltosValue in) {
251 computed.set_derivative(in);
254 void set_derivative(AltosCValue in) {
255 set_derivative(in.altos_value());
258 void set_integral(AltosValue in) {
259 computed.set_integral(in);
262 void set_integral(AltosCValue in) {
263 set_integral(in.altos_value());
266 void copy(AltosCValue old) {
267 measured.copy(old.measured);
268 computed.copy(old.computed);
271 void finish_update() {
272 measured.finish_update();
273 computed.finish_update();
277 measured = new AltosIValue();
278 computed = new AltosIValue();
285 public int altitude_32;
286 public int receiver_serial;
287 public boolean landed;
288 public boolean ascent; /* going up? */
289 public boolean boost; /* under power */
292 public int device_type;
293 public int config_major;
294 public int config_minor;
295 public int apogee_delay;
296 public int main_deploy;
297 public int flight_log_max;
299 private double pressure_to_altitude(double p) {
300 if (p == AltosLib.MISSING)
301 return AltosLib.MISSING;
302 return AltosConvert.pressure_to_altitude(p);
305 private AltosCValue ground_altitude;
307 public double ground_altitude() {
308 return ground_altitude.value();
311 public void set_ground_altitude(double a) {
312 ground_altitude.set_measured(a, time);
315 class AltosGpsGroundAltitude extends AltosValue {
316 void set(double a, double t) {
319 gps_altitude.set_gps_height();
322 void set_filtered(double a, double t) {
323 super.set_filtered(a, t);
325 gps_altitude.set_gps_height();
329 private AltosGpsGroundAltitude gps_ground_altitude;
331 public double gps_ground_altitude() {
332 return gps_ground_altitude.value();
335 public void set_gps_ground_altitude(double a) {
336 gps_ground_altitude.set(a, time);
339 class AltosGroundPressure extends AltosCValue {
340 void set_filtered(double p, double time) {
341 computed.set_filtered(p, time);
343 ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
346 void set_measured(double p, double time) {
347 super.set_measured(p, time);
348 ground_altitude.set_computed(pressure_to_altitude(p), time);
352 private AltosGroundPressure ground_pressure;
354 public double ground_pressure() {
355 return ground_pressure.value();
358 public void set_ground_pressure (double pressure) {
359 ground_pressure.set_measured(pressure, time);
362 class AltosAltitude extends AltosCValue {
364 private void set_speed(AltosValue v) {
365 if (!acceleration.is_measured() || !ascent)
366 speed.set_derivative(this);
369 void set_computed(double a, double time) {
370 super.set_computed(a,time);
375 void set_measured(double a, double time) {
376 super.set_measured(a,time);
382 private AltosAltitude altitude;
384 class AltosGpsAltitude extends AltosValue {
386 private void set_gps_height() {
388 double g = gps_ground_altitude.value();
390 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
393 gps_height = AltosLib.MISSING;
396 void set(double a, double t) {
402 private AltosGpsAltitude gps_altitude;
404 private AltosValue gps_ground_speed;
405 private AltosValue gps_ascent_rate;
406 private AltosValue gps_course;
407 private AltosValue gps_speed;
409 public double altitude() {
410 double a = altitude.value();
411 if (a != AltosLib.MISSING)
413 return gps_altitude.value();
416 public double max_altitude() {
417 double a = altitude.max();
418 if (a != AltosLib.MISSING)
420 return gps_altitude.max();
423 public void set_altitude(double new_altitude) {
424 altitude.set_measured(new_altitude, time);
427 public double gps_altitude() {
428 return gps_altitude.value();
431 public double max_gps_altitude() {
432 return gps_altitude.max();
435 public void set_gps_altitude(double new_gps_altitude) {
436 gps_altitude.set(new_gps_altitude, time);
439 public double gps_ground_speed() {
440 return gps_ground_speed.value();
443 public double max_gps_ground_speed() {
444 return gps_ground_speed.max();
447 public double gps_ascent_rate() {
448 return gps_ascent_rate.value();
451 public double max_gps_ascent_rate() {
452 return gps_ascent_rate.max();
455 public double gps_course() {
456 return gps_course.value();
459 public double gps_speed() {
460 return gps_speed.value();
463 public double max_gps_speed() {
464 return gps_speed.max();
467 class AltosPressure extends AltosValue {
468 void set(double p, double time) {
470 if (state == AltosLib.ao_flight_pad)
471 ground_pressure.set_filtered(p, time);
472 double a = pressure_to_altitude(p);
473 altitude.set_computed(a, time);
477 private AltosPressure pressure;
479 public double pressure() {
480 return pressure.value();
483 public void set_pressure(double p) {
484 pressure.set(p, time);
487 public double baro_height() {
488 double a = altitude();
489 double g = ground_altitude();
490 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
492 return AltosLib.MISSING;
495 public double height() {
496 double k = kalman_height.value();
497 if (k != AltosLib.MISSING)
500 double b = baro_height();
501 if (b != AltosLib.MISSING)
507 public double max_height() {
508 double k = kalman_height.max();
509 if (k != AltosLib.MISSING)
512 double a = altitude.max();
513 double g = ground_altitude();
514 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
516 return max_gps_height();
519 public double gps_height() {
520 double a = gps_altitude();
521 double g = gps_ground_altitude();
523 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
525 return AltosLib.MISSING;
528 public double max_gps_height() {
529 double a = gps_altitude.max();
530 double g = gps_ground_altitude();
532 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
534 return AltosLib.MISSING;
537 class AltosSpeed extends AltosCValue {
540 return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
544 acceleration.set_derivative(this);
547 void set_derivative(AltosCValue in) {
548 super.set_derivative(in);
552 void set_computed(double new_value, double time) {
553 super.set_computed(new_value, time);
557 void set_measured(double new_value, double time) {
558 super.set_measured(new_value, time);
563 private AltosSpeed speed;
565 public double speed() {
566 double v = kalman_speed.value();
567 if (v != AltosLib.MISSING)
570 if (v != AltosLib.MISSING)
573 if (v != AltosLib.MISSING)
575 return AltosLib.MISSING;
578 public double max_speed() {
579 double v = kalman_speed.max();
580 if (v != AltosLib.MISSING)
583 if (v != AltosLib.MISSING)
586 if (v != AltosLib.MISSING)
588 return AltosLib.MISSING;
591 class AltosAccel extends AltosCValue {
594 return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
597 void set_measured(double a, double time) {
598 super.set_measured(a, time);
600 speed.set_integral(this.measured);
604 AltosAccel acceleration;
606 public double acceleration() {
607 return acceleration.value();
610 public double max_acceleration() {
611 return acceleration.max();
614 public AltosCValue orient;
616 public void set_orient(double new_orient) {
617 orient.set_measured(new_orient, time);
620 public double orient() {
621 return orient.value();
624 public double max_orient() {
628 public AltosValue kalman_height, kalman_speed, kalman_acceleration;
630 public void set_kalman(double height, double speed, double acceleration) {
631 kalman_height.set(height, time);
632 kalman_speed.set(speed, time);
633 kalman_acceleration.set(acceleration, time);
636 public double battery_voltage;
637 public double pyro_voltage;
638 public double temperature;
639 public double apogee_voltage;
640 public double main_voltage;
642 public double ignitor_voltage[];
645 public AltosGPS temp_gps;
646 public int temp_gps_sat_tick;
647 public boolean gps_pending;
648 public int gps_sequence;
653 public static final int MIN_PAD_SAMPLES = 10;
656 public int gps_waiting;
657 public boolean gps_ready;
661 public AltosGreatCircle from_pad;
662 public double elevation; /* from pad */
663 public double range; /* total distance */
665 public double gps_height;
667 public double pad_lat, pad_lon, pad_alt;
669 public int speak_tick;
670 public double speak_altitude;
672 public String callsign;
673 public String firmware_version;
675 public double accel_plus_g;
676 public double accel_minus_g;
678 public double ground_accel;
679 public double ground_accel_avg;
681 public int log_format;
682 public String product;
684 public AltosMs5607 baro;
686 public AltosCompanion companion;
688 public int pyro_fired;
690 public void set_npad(int npad) {
692 gps_waiting = MIN_PAD_SAMPLES - npad;
693 if (this.gps_waiting < 0)
695 gps_ready = gps_waiting == 0;
701 received_time = System.currentTimeMillis();
702 time = AltosLib.MISSING;
703 time_change = AltosLib.MISSING;
704 prev_time = AltosLib.MISSING;
705 tick = AltosLib.MISSING;
706 prev_tick = AltosLib.MISSING;
707 boost_tick = AltosLib.MISSING;
708 state = AltosLib.ao_flight_invalid;
709 flight = AltosLib.MISSING;
712 rssi = AltosLib.MISSING;
714 device_type = AltosLib.MISSING;
715 config_major = AltosLib.MISSING;
716 config_minor = AltosLib.MISSING;
717 apogee_delay = AltosLib.MISSING;
718 main_deploy = AltosLib.MISSING;
719 flight_log_max = AltosLib.MISSING;
721 ground_altitude = new AltosCValue();
722 ground_pressure = new AltosGroundPressure();
723 altitude = new AltosAltitude();
724 pressure = new AltosPressure();
725 speed = new AltosSpeed();
726 acceleration = new AltosAccel();
727 orient = new AltosCValue();
729 temperature = AltosLib.MISSING;
730 battery_voltage = AltosLib.MISSING;
731 pyro_voltage = AltosLib.MISSING;
732 apogee_voltage = AltosLib.MISSING;
733 main_voltage = AltosLib.MISSING;
734 ignitor_voltage = null;
736 kalman_height = new AltosValue();
737 kalman_speed = new AltosValue();
738 kalman_acceleration = new AltosValue();
742 temp_gps_sat_tick = 0;
747 last_imu_time = AltosLib.MISSING;
749 ground_rotation = null;
752 accel_zero_along = AltosLib.MISSING;
753 accel_zero_across = AltosLib.MISSING;
754 accel_zero_through = AltosLib.MISSING;
756 accel_ground_along = AltosLib.MISSING;
757 accel_ground_across = AltosLib.MISSING;
758 accel_ground_through = AltosLib.MISSING;
760 pad_orientation = AltosLib.MISSING;
762 gyro_zero_roll = AltosLib.MISSING;
763 gyro_zero_pitch = AltosLib.MISSING;
764 gyro_zero_yaw = AltosLib.MISSING;
770 elevation = AltosLib.MISSING;
771 range = AltosLib.MISSING;
772 gps_height = AltosLib.MISSING;
774 pad_lat = AltosLib.MISSING;
775 pad_lon = AltosLib.MISSING;
776 pad_alt = AltosLib.MISSING;
778 gps_altitude = new AltosGpsAltitude();
779 gps_ground_altitude = new AltosGpsGroundAltitude();
780 gps_ground_speed = new AltosValue();
781 gps_speed = new AltosValue();
782 gps_ascent_rate = new AltosValue();
783 gps_course = new AltosValue();
785 speak_tick = AltosLib.MISSING;
786 speak_altitude = AltosLib.MISSING;
789 firmware_version = null;
791 accel_plus_g = AltosLib.MISSING;
792 accel_minus_g = AltosLib.MISSING;
793 accel = AltosLib.MISSING;
795 ground_accel = AltosLib.MISSING;
796 ground_accel_avg = AltosLib.MISSING;
798 log_format = AltosLib.MISSING;
800 serial = AltosLib.MISSING;
801 receiver_serial = AltosLib.MISSING;
802 altitude_32 = AltosLib.MISSING;
810 void finish_update() {
813 ground_altitude.finish_update();
814 altitude.finish_update();
815 pressure.finish_update();
816 speed.finish_update();
817 acceleration.finish_update();
818 orient.finish_update();
820 kalman_height.finish_update();
821 kalman_speed.finish_update();
822 kalman_acceleration.finish_update();
825 void copy(AltosState old) {
832 received_time = old.received_time;
834 time_change = old.time_change;
835 prev_time = old.time;
838 prev_tick = old.tick;
839 boost_tick = old.boost_tick;
848 device_type = old.device_type;
849 config_major = old.config_major;
850 config_minor = old.config_minor;
851 apogee_delay = old.apogee_delay;
852 main_deploy = old.main_deploy;
853 flight_log_max = old.flight_log_max;
857 ground_pressure.copy(old.ground_pressure);
858 ground_altitude.copy(old.ground_altitude);
859 altitude.copy(old.altitude);
860 pressure.copy(old.pressure);
861 speed.copy(old.speed);
862 acceleration.copy(old.acceleration);
863 orient.copy(old.orient);
865 battery_voltage = old.battery_voltage;
866 pyro_voltage = old.pyro_voltage;
867 temperature = old.temperature;
868 apogee_voltage = old.apogee_voltage;
869 main_voltage = old.main_voltage;
870 ignitor_voltage = old.ignitor_voltage;
872 kalman_height.copy(old.kalman_height);
873 kalman_speed.copy(old.kalman_speed);
874 kalman_acceleration.copy(old.kalman_acceleration);
877 gps = old.gps.clone();
880 if (old.temp_gps != null)
881 temp_gps = old.temp_gps.clone();
884 temp_gps_sat_tick = old.temp_gps_sat_tick;
885 gps_sequence = old.gps_sequence;
886 gps_pending = old.gps_pending;
889 imu = old.imu.clone();
892 last_imu_time = old.last_imu_time;
894 if (old.rotation != null)
895 rotation = new AltosRotation (old.rotation);
897 if (old.ground_rotation != null) {
898 ground_rotation = new AltosRotation(old.ground_rotation);
901 accel_zero_along = old.accel_zero_along;
902 accel_zero_across = old.accel_zero_across;
903 accel_zero_through = old.accel_zero_through;
905 accel_ground_along = old.accel_ground_along;
906 accel_ground_across = old.accel_ground_across;
907 accel_ground_through = old.accel_ground_through;
908 pad_orientation = old.pad_orientation;
910 gyro_zero_roll = old.gyro_zero_roll;
911 gyro_zero_pitch = old.gyro_zero_pitch;
912 gyro_zero_yaw = old.gyro_zero_yaw;
915 mag = old.mag.clone();
920 gps_waiting = old.gps_waiting;
921 gps_ready = old.gps_ready;
924 if (old.from_pad != null)
925 from_pad = old.from_pad.clone();
929 elevation = old.elevation;
932 gps_height = old.gps_height;
934 gps_altitude.copy(old.gps_altitude);
935 gps_ground_altitude.copy(old.gps_ground_altitude);
936 gps_ground_speed.copy(old.gps_ground_speed);
937 gps_ascent_rate.copy(old.gps_ascent_rate);
938 gps_course.copy(old.gps_course);
939 gps_speed.copy(old.gps_speed);
941 pad_lat = old.pad_lat;
942 pad_lon = old.pad_lon;
943 pad_alt = old.pad_alt;
945 speak_tick = old.speak_tick;
946 speak_altitude = old.speak_altitude;
948 callsign = old.callsign;
949 firmware_version = old.firmware_version;
951 accel_plus_g = old.accel_plus_g;
952 accel_minus_g = old.accel_minus_g;
954 ground_accel = old.ground_accel;
955 ground_accel_avg = old.ground_accel_avg;
957 log_format = old.log_format;
958 product = old.product;
960 receiver_serial = old.receiver_serial;
961 altitude_32 = old.altitude_32;
964 companion = old.companion;
966 pyro_fired = old.pyro_fired;
973 elevation = AltosLib.MISSING;
974 range = AltosLib.MISSING;
979 if (gps.locked && gps.nsat >= 4) {
980 /* Track consecutive 'good' gps reports, waiting for 10 of them */
981 if (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_stateless) {
983 if (pad_lat != AltosLib.MISSING && (npad < 10 || state == AltosLib.ao_flight_pad)) {
984 pad_lat = (pad_lat * 31 + gps.lat) / 32;
985 pad_lon = (pad_lon * 31 + gps.lon) / 32;
986 gps_ground_altitude.set_filtered(gps.alt, time);
989 if (pad_lat == AltosLib.MISSING) {
992 gps_ground_altitude.set(gps.alt, time);
994 gps_altitude.set(gps.alt, time);
995 if (gps.climb_rate != AltosLib.MISSING)
996 gps_ascent_rate.set(gps.climb_rate, time);
997 if (gps.ground_speed != AltosLib.MISSING)
998 gps_ground_speed.set(gps.ground_speed, time);
999 if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
1000 gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
1001 gps.climb_rate * gps.climb_rate), time);
1002 if (gps.course != AltosLib.MISSING)
1003 gps_course.set(gps.course, time);
1005 if (gps.lat != 0 && gps.lon != 0 &&
1006 pad_lat != AltosLib.MISSING &&
1007 pad_lon != AltosLib.MISSING)
1009 double h = height();
1011 if (h == AltosLib.MISSING)
1013 from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
1014 elevation = from_pad.elevation;
1015 range = from_pad.range;
1019 public void set_tick(int new_tick) {
1020 if (new_tick != AltosLib.MISSING) {
1021 if (prev_tick != AltosLib.MISSING) {
1022 while (new_tick < prev_tick - 1000) {
1027 time = tick / 100.0;
1028 time_change = time - prev_time;
1032 public void set_boost_tick(int boost_tick) {
1033 if (boost_tick != AltosLib.MISSING)
1034 this.boost_tick = boost_tick;
1037 public String state_name() {
1038 return AltosLib.state_name(state);
1041 public void set_state(int state) {
1042 if (state != AltosLib.ao_flight_invalid) {
1044 ascent = (AltosLib.ao_flight_boost <= state &&
1045 state <= AltosLib.ao_flight_coast);
1046 boost = (AltosLib.ao_flight_boost == state);
1050 public void set_device_type(int device_type) {
1051 this.device_type = device_type;
1052 switch (device_type) {
1053 case AltosLib.product_telegps:
1054 this.state = AltosLib.ao_flight_stateless;
1059 public void set_log_format(int log_format) {
1060 this.log_format = log_format;
1061 switch (log_format) {
1062 case AltosLib.AO_LOG_FORMAT_TELEGPS:
1063 this.state = AltosLib.ao_flight_stateless;
1068 public void set_flight_params(int apogee_delay, int main_deploy) {
1069 this.apogee_delay = apogee_delay;
1070 this.main_deploy = main_deploy;
1073 public void set_config(int major, int minor, int flight_log_max) {
1074 config_major = major;
1075 config_minor = minor;
1076 this.flight_log_max = flight_log_max;
1079 public void set_callsign(String callsign) {
1080 this.callsign = callsign;
1083 public void set_firmware_version(String version) {
1084 firmware_version = version;
1087 public int compare_version(String other_version) {
1088 if (firmware_version == null)
1089 return AltosLib.MISSING;
1090 return AltosLib.compare_version(firmware_version, other_version);
1093 private void re_init() {
1094 int bt = boost_tick;
1095 int rs = receiver_serial;
1098 receiver_serial = rs;
1101 public void set_flight(int flight) {
1103 /* When the flight changes, reset the state */
1104 if (flight != AltosLib.MISSING) {
1105 if (this.flight != AltosLib.MISSING &&
1106 this.flight != flight) {
1109 this.flight = flight;
1113 public void set_serial(int serial) {
1114 /* When the serial changes, reset the state */
1115 if (serial != AltosLib.MISSING) {
1116 if (this.serial != AltosLib.MISSING &&
1117 this.serial != serial) {
1120 this.serial = serial;
1124 public void set_receiver_serial(int serial) {
1125 if (serial != AltosLib.MISSING)
1126 receiver_serial = serial;
1129 public boolean altitude_32() {
1130 return altitude_32 == 1;
1133 public void set_altitude_32(int altitude_32) {
1134 if (altitude_32 != AltosLib.MISSING)
1135 this.altitude_32 = altitude_32;
1139 if (rssi == AltosLib.MISSING)
1144 public void set_rssi(int rssi, int status) {
1145 if (rssi != AltosLib.MISSING) {
1147 this.status = status;
1151 public void set_received_time(long ms) {
1155 public void set_gps(AltosGPS gps, int sequence) {
1157 this.gps = gps.clone();
1158 gps_sequence = sequence;
1165 public double accel_zero_along;
1166 public double accel_zero_across;
1167 public double accel_zero_through;
1169 public AltosRotation rotation;
1170 public AltosRotation ground_rotation;
1172 public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
1173 if (zero_along != AltosLib.MISSING) {
1174 accel_zero_along = zero_along;
1175 accel_zero_across = zero_across;
1176 accel_zero_through = zero_through;
1180 public int pad_orientation;
1182 public double accel_ground_along, accel_ground_across, accel_ground_through;
1184 void update_pad_rotation() {
1185 if (pad_orientation != AltosLib.MISSING && accel_ground_along != AltosLib.MISSING) {
1186 rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - accel_zero_across),
1187 AltosIMU.convert_accel(accel_ground_through - accel_zero_through),
1188 AltosIMU.convert_accel(accel_ground_along - accel_zero_along),
1190 ground_rotation = rotation;
1191 orient.set_computed(rotation.tilt(), time);
1195 public void set_accel_ground(double ground_along, double ground_across, double ground_through) {
1196 accel_ground_along = ground_along;
1197 accel_ground_across = ground_across;
1198 accel_ground_through = ground_through;
1199 update_pad_rotation();
1202 public void set_pad_orientation(int pad_orientation) {
1203 this.pad_orientation = pad_orientation;
1204 update_pad_rotation();
1207 public double gyro_zero_roll;
1208 public double gyro_zero_pitch;
1209 public double gyro_zero_yaw;
1211 public void set_gyro_zero(double roll, double pitch, double yaw) {
1212 if (roll != AltosLib.MISSING) {
1213 gyro_zero_roll = roll;
1214 gyro_zero_pitch = pitch;
1215 gyro_zero_yaw = yaw;
1219 public double last_imu_time;
1221 private double radians(double degrees) {
1222 if (degrees == AltosLib.MISSING)
1223 return AltosLib.MISSING;
1224 return degrees * Math.PI / 180.0;
1227 private void update_orient() {
1228 if (last_imu_time != AltosLib.MISSING) {
1229 double t = time - last_imu_time;
1231 double pitch = radians(gyro_pitch());
1232 double yaw = radians(gyro_yaw());
1233 double roll = radians(gyro_roll());
1235 if (t > 0 & pitch != AltosLib.MISSING && rotation != null) {
1236 rotation.rotate(t, pitch, yaw, roll);
1237 orient.set_computed(rotation.tilt(), time);
1240 last_imu_time = time;
1243 public void set_imu(AltosIMU imu) {
1250 private double gyro_zero_overflow(double first) {
1251 double v = first / 128.0;
1259 public void check_imu_wrap(AltosIMU imu) {
1260 if (this.imu == null) {
1261 gyro_zero_roll += gyro_zero_overflow(imu.gyro_roll);
1262 gyro_zero_pitch += gyro_zero_overflow(imu.gyro_pitch);
1263 gyro_zero_yaw += gyro_zero_overflow(imu.gyro_yaw);
1267 public double accel_along() {
1268 if (imu != null && accel_zero_along != AltosLib.MISSING)
1269 return AltosIMU.convert_accel(imu.accel_along - accel_zero_along);
1270 return AltosLib.MISSING;
1273 public double accel_across() {
1274 if (imu != null && accel_zero_across != AltosLib.MISSING)
1275 return AltosIMU.convert_accel(imu.accel_across - accel_zero_across);
1276 return AltosLib.MISSING;
1279 public double accel_through() {
1280 if (imu != null && accel_zero_through != AltosLib.MISSING)
1281 return AltosIMU.convert_accel(imu.accel_through - accel_zero_through);
1282 return AltosLib.MISSING;
1285 public double gyro_roll() {
1286 if (imu != null && gyro_zero_roll != AltosLib.MISSING) {
1287 return AltosIMU.convert_gyro(imu.gyro_roll - gyro_zero_roll);
1289 return AltosLib.MISSING;
1292 public double gyro_pitch() {
1293 if (imu != null && gyro_zero_pitch != AltosLib.MISSING) {
1294 return AltosIMU.convert_gyro(imu.gyro_pitch - gyro_zero_pitch);
1296 return AltosLib.MISSING;
1299 public double gyro_yaw() {
1300 if (imu != null && gyro_zero_yaw != AltosLib.MISSING) {
1301 return AltosIMU.convert_gyro(imu.gyro_yaw - gyro_zero_yaw);
1303 return AltosLib.MISSING;
1306 public void set_mag(AltosMag mag) {
1307 this.mag = mag.clone();
1310 public double mag_along() {
1312 return AltosMag.convert_gauss(mag.along);
1313 return AltosLib.MISSING;
1316 public double mag_across() {
1318 return AltosMag.convert_gauss(mag.across);
1319 return AltosLib.MISSING;
1322 public double mag_through() {
1324 return AltosMag.convert_gauss(mag.through);
1325 return AltosLib.MISSING;
1328 public AltosMs5607 make_baro() {
1330 baro = new AltosMs5607();
1334 public void set_ms5607(AltosMs5607 ms5607) {
1338 set_pressure(baro.pa);
1339 set_temperature(baro.cc / 100.0);
1343 public void set_ms5607(int pres, int temp) {
1345 baro.set(pres, temp);
1347 set_pressure(baro.pa);
1348 set_temperature(baro.cc / 100.0);
1352 public void make_companion (int nchannels) {
1353 if (companion == null)
1354 companion = new AltosCompanion(nchannels);
1357 public void set_companion(AltosCompanion companion) {
1358 this.companion = companion;
1361 void update_accel() {
1362 if (accel == AltosLib.MISSING)
1364 if (accel_plus_g == AltosLib.MISSING)
1366 if (accel_minus_g == AltosLib.MISSING)
1369 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
1370 double counts_per_mss = counts_per_g / 9.80665;
1371 acceleration.set_measured((accel_plus_g - accel) / counts_per_mss, time);
1374 public void set_accel_g(double accel_plus_g, double accel_minus_g) {
1375 if (accel_plus_g != AltosLib.MISSING) {
1376 this.accel_plus_g = accel_plus_g;
1377 this.accel_minus_g = accel_minus_g;
1382 public void set_ground_accel(double ground_accel) {
1383 if (ground_accel != AltosLib.MISSING)
1384 this.ground_accel = ground_accel;
1387 public void set_accel(double accel) {
1388 if (accel != AltosLib.MISSING) {
1390 if (state == AltosLib.ao_flight_pad) {
1391 if (ground_accel_avg == AltosLib.MISSING)
1392 ground_accel_avg = accel;
1394 ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
1400 public void set_temperature(double temperature) {
1401 if (temperature != AltosLib.MISSING) {
1402 this.temperature = temperature;
1407 public void set_battery_voltage(double battery_voltage) {
1408 if (battery_voltage != AltosLib.MISSING) {
1409 this.battery_voltage = battery_voltage;
1414 public void set_pyro_voltage(double pyro_voltage) {
1415 if (pyro_voltage != AltosLib.MISSING) {
1416 this.pyro_voltage = pyro_voltage;
1421 public void set_apogee_voltage(double apogee_voltage) {
1422 if (apogee_voltage != AltosLib.MISSING) {
1423 this.apogee_voltage = apogee_voltage;
1428 public void set_main_voltage(double main_voltage) {
1429 if (main_voltage != AltosLib.MISSING) {
1430 this.main_voltage = main_voltage;
1435 public void set_ignitor_voltage(double[] voltage) {
1436 this.ignitor_voltage = voltage;
1439 public void set_pyro_fired(int fired) {
1440 this.pyro_fired = fired;
1443 public double time_since_boost() {
1444 if (tick == AltosLib.MISSING)
1447 if (boost_tick == AltosLib.MISSING)
1448 return tick / 100.0;
1449 return (tick - boost_tick) / 100.0;
1452 public boolean valid() {
1453 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1456 public AltosGPS make_temp_gps(boolean sats) {
1457 if (temp_gps == null) {
1458 temp_gps = new AltosGPS(gps);
1462 if (tick != temp_gps_sat_tick)
1463 temp_gps.cc_gps_sat = null;
1464 temp_gps_sat_tick = tick;
1469 public void set_temp_gps() {
1470 set_gps(temp_gps, gps_sequence + 1);
1471 gps_pending = false;
1475 public AltosState clone() {
1476 AltosState s = new AltosState();
1481 public AltosState () {