altoslib: Compute tilt angle from eeprom data
[fw/altos] / altoslib / AltosState.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 /*
19  * Track flight state from telemetry or eeprom data stream
20  */
21
22 package org.altusmetrum.altoslib_5;
23
24 public class AltosState implements Cloneable {
25
26         public static final int set_position = 1;
27         public static final int set_gps = 2;
28         public static final int set_data = 4;
29
30         public int set;
31
32         static final double ascent_filter_len = 0.5;
33         static final double descent_filter_len = 0.5;
34
35         /* derived data */
36
37         public long     received_time;
38
39         public double   time;
40         public double   prev_time;
41         public double   time_change;
42         public int      tick;
43         private int     prev_tick;
44         public int      boost_tick;
45
46         class AltosValue {
47                 double  value;
48                 double  prev_value;
49                 private double  max_value;
50                 private double  set_time;
51                 private double  prev_set_time;
52
53                 boolean can_max() { return true; }
54
55                 void set(double new_value, double time) {
56                         if (new_value != AltosLib.MISSING) {
57                                 value = new_value;
58                                 if (can_max() && (max_value == AltosLib.MISSING || value > max_value))
59                                         max_value = value;
60                                 set_time = time;
61                         }
62                 }
63
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;
67                         set(new_value, time);
68                 }
69
70                 double value() {
71                         return value;
72                 }
73
74                 double max() {
75                         return max_value;
76                 }
77
78                 double prev() {
79                         return prev_value;
80                 }
81
82                 double change() {
83                         if (value != AltosLib.MISSING && prev_value != AltosLib.MISSING)
84                                 return value - prev_value;
85                         return AltosLib.MISSING;
86                 }
87
88                 double rate() {
89                         double c = change();
90                         double t = set_time - prev_set_time;
91
92                         if (c != AltosLib.MISSING && t != 0)
93                                 return c / t;
94                         return AltosLib.MISSING;
95                 }
96
97                 double integrate() {
98                         if (value == AltosLib.MISSING)
99                                 return AltosLib.MISSING;
100                         if (prev_value == AltosLib.MISSING)
101                                 return AltosLib.MISSING;
102
103                         return (value + prev_value) / 2 * (set_time - prev_set_time);
104                 }
105
106                 double time() {
107                         return set_time;
108                 }
109
110                 void set_derivative(AltosValue in) {
111                         double  n = in.rate();
112
113                         if (n == AltosLib.MISSING)
114                                 return;
115
116                         double  p = prev_value;
117                         double  pt = prev_set_time;
118
119                         if (p == AltosLib.MISSING) {
120                                 p = 0;
121                                 pt = in.time() - 0.01;
122                         }
123
124                         /* Clip changes to reduce noise */
125                         double  ddt = in.time() - pt;
126                         double  ddv = (n - p) / ddt;
127
128                         final double max = 100000;
129
130                         /* 100gs */
131                         if (Math.abs(ddv) > max) {
132                                 if (n > p)
133                                         n = p + ddt * max;
134                                 else
135                                         n = p - ddt * max;
136                         }
137
138                         double filter_len;
139
140                         if (ascent)
141                                 filter_len = ascent_filter_len;
142                         else
143                                 filter_len = descent_filter_len;
144
145                         double f = 1/Math.exp(ddt/ filter_len);
146                         n = p * f + n * (1-f);
147
148                         set(n, in.time());
149                 }
150
151                 void set_integral(AltosValue in) {
152                         double  change = in.integrate();
153
154                         if (change != AltosLib.MISSING) {
155                                 double  prev = prev_value;
156                                 if (prev == AltosLib.MISSING)
157                                         prev = 0;
158                                 set(prev + change, in.time());
159                         }
160                 }
161
162                 void copy(AltosValue old) {
163                         value = old.value;
164                         set_time = old.set_time;
165                         prev_value = old.value;
166                         prev_set_time = old.set_time;
167                         max_value = old.max_value;
168                 }
169
170                 void finish_update() {
171                         prev_value = value;
172                         prev_set_time = set_time;
173                 }
174
175                 AltosValue() {
176                         value = AltosLib.MISSING;
177                         prev_value = AltosLib.MISSING;
178                         max_value = AltosLib.MISSING;
179                 }
180         }
181
182         class AltosCValue {
183
184                 class AltosIValue extends AltosValue {
185                         boolean can_max() {
186                                 return c_can_max();
187                         }
188                 };
189
190                 public AltosIValue      measured;
191                 public AltosIValue      computed;
192
193                 boolean can_max() { return true; }
194
195                 boolean c_can_max() { return can_max(); }
196
197                 double value() {
198                         double v = measured.value();
199                         if (v != AltosLib.MISSING)
200                                 return v;
201                         return computed.value();
202                 }
203
204                 boolean is_measured() {
205                         return measured.value() != AltosLib.MISSING;
206                 }
207
208                 double max() {
209                         double m = measured.max();
210
211                         if (m != AltosLib.MISSING)
212                                 return m;
213                         return computed.max();
214                 }
215
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;
220                 }
221
222                 AltosValue altos_value() {
223                         if (measured.value() != AltosLib.MISSING)
224                                 return measured;
225                         return computed;
226                 }
227
228                 double change() {
229                         double c = measured.change();
230                         if (c == AltosLib.MISSING)
231                                 c = computed.change();
232                         return c;
233                 }
234
235                 double rate() {
236                         double r = measured.rate();
237                         if (r == AltosLib.MISSING)
238                                 r = computed.rate();
239                         return r;
240                 }
241
242                 void set_measured(double new_value, double time) {
243                         measured.set(new_value, time);
244                 }
245
246                 void set_computed(double new_value, double time) {
247                         computed.set(new_value, time);
248                 }
249
250                 void set_derivative(AltosValue in) {
251                         computed.set_derivative(in);
252                 }
253
254                 void set_derivative(AltosCValue in) {
255                         set_derivative(in.altos_value());
256                 }
257
258                 void set_integral(AltosValue in) {
259                         computed.set_integral(in);
260                 }
261
262                 void set_integral(AltosCValue in) {
263                         set_integral(in.altos_value());
264                 }
265
266                 void copy(AltosCValue old) {
267                         measured.copy(old.measured);
268                         computed.copy(old.computed);
269                 }
270
271                 void finish_update() {
272                         measured.finish_update();
273                         computed.finish_update();
274                 }
275
276                 AltosCValue() {
277                         measured = new AltosIValue();
278                         computed = new AltosIValue();
279                 }
280         }
281
282         public int      state;
283         public int      flight;
284         public int      serial;
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 */
290         public int      rssi;
291         public int      status;
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;
298
299         private double pressure_to_altitude(double p) {
300                 if (p == AltosLib.MISSING)
301                         return AltosLib.MISSING;
302                 return AltosConvert.pressure_to_altitude(p);
303         }
304
305         private AltosCValue ground_altitude;
306
307         public double ground_altitude() {
308                 return ground_altitude.value();
309         }
310
311         public void set_ground_altitude(double a) {
312                 ground_altitude.set_measured(a, time);
313         }
314
315         class AltosGpsGroundAltitude extends AltosValue {
316                 void set(double a, double t) {
317                         super.set(a, t);
318                         pad_alt = value();
319                         gps_altitude.set_gps_height();
320                 }
321
322                 void set_filtered(double a, double t) {
323                         super.set_filtered(a, t);
324                         pad_alt = value();
325                         gps_altitude.set_gps_height();
326                 }
327         }
328
329         private AltosGpsGroundAltitude gps_ground_altitude;
330
331         public double gps_ground_altitude() {
332                 return gps_ground_altitude.value();
333         }
334
335         public void set_gps_ground_altitude(double a) {
336                 gps_ground_altitude.set(a, time);
337         }
338
339         class AltosGroundPressure extends AltosCValue {
340                 void set_filtered(double p, double time) {
341                         computed.set_filtered(p, time);
342                         if (!is_measured())
343                                 ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
344                 }
345
346                 void set_measured(double p, double time) {
347                         super.set_measured(p, time);
348                         ground_altitude.set_computed(pressure_to_altitude(p), time);
349                 }
350         }
351
352         private AltosGroundPressure ground_pressure;
353
354         public double ground_pressure() {
355                 return ground_pressure.value();
356         }
357
358         public void set_ground_pressure (double pressure) {
359                 ground_pressure.set_measured(pressure, time);
360         }
361
362         class AltosAltitude extends AltosCValue {
363
364                 private void set_speed(AltosValue v) {
365                         if (!acceleration.is_measured() || !ascent)
366                                 speed.set_derivative(this);
367                 }
368
369                 void set_computed(double a, double time) {
370                         super.set_computed(a,time);
371                         set_speed(computed);
372                         set |= set_position;
373                 }
374
375                 void set_measured(double a, double time) {
376                         super.set_measured(a,time);
377                         set_speed(measured);
378                         set |= set_position;
379                 }
380         }
381
382         private AltosAltitude   altitude;
383
384         class AltosGpsAltitude extends AltosValue {
385
386                 private void set_gps_height() {
387                         double  a = value();
388                         double  g = gps_ground_altitude.value();
389
390                         if (a != AltosLib.MISSING && g != AltosLib.MISSING)
391                                 gps_height = a - g;
392                         else
393                                 gps_height = AltosLib.MISSING;
394                 }
395
396                 void set(double a, double t) {
397                         super.set(a, t);
398                         set_gps_height();
399                 }
400         }
401
402         private AltosGpsAltitude        gps_altitude;
403
404         private AltosValue              gps_ground_speed;
405         private AltosValue              gps_ascent_rate;
406         private AltosValue              gps_course;
407         private AltosValue              gps_speed;
408
409         public double altitude() {
410                 double a = altitude.value();
411                 if (a != AltosLib.MISSING)
412                         return a;
413                 return gps_altitude.value();
414         }
415
416         public double max_altitude() {
417                 double a = altitude.max();
418                 if (a != AltosLib.MISSING)
419                         return a;
420                 return gps_altitude.max();
421         }
422
423         public void set_altitude(double new_altitude) {
424                 altitude.set_measured(new_altitude, time);
425         }
426
427         public double gps_altitude() {
428                 return gps_altitude.value();
429         }
430
431         public double max_gps_altitude() {
432                 return gps_altitude.max();
433         }
434
435         public void set_gps_altitude(double new_gps_altitude) {
436                 gps_altitude.set(new_gps_altitude, time);
437         }
438
439         public double gps_ground_speed() {
440                 return gps_ground_speed.value();
441         }
442
443         public double max_gps_ground_speed() {
444                 return gps_ground_speed.max();
445         }
446
447         public double gps_ascent_rate() {
448                 return gps_ascent_rate.value();
449         }
450
451         public double max_gps_ascent_rate() {
452                 return gps_ascent_rate.max();
453         }
454
455         public double gps_course() {
456                 return gps_course.value();
457         }
458
459         public double gps_speed() {
460                 return gps_speed.value();
461         }
462
463         public double max_gps_speed() {
464                 return gps_speed.max();
465         }
466
467         class AltosPressure extends AltosValue {
468                 void set(double p, double time) {
469                         super.set(p, 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);
474                 }
475         }
476
477         private AltosPressure   pressure;
478
479         public double pressure() {
480                 return pressure.value();
481         }
482
483         public void set_pressure(double p) {
484                 pressure.set(p, time);
485         }
486
487         public double baro_height() {
488                 double a = altitude();
489                 double g = ground_altitude();
490                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
491                         return a - g;
492                 return AltosLib.MISSING;
493         }
494
495         public double height() {
496                 double k = kalman_height.value();
497                 if (k != AltosLib.MISSING)
498                         return k;
499
500                 double b = baro_height();
501                 if (b != AltosLib.MISSING)
502                         return b;
503
504                 return gps_height();
505         }
506
507         public double max_height() {
508                 double  k = kalman_height.max();
509                 if (k != AltosLib.MISSING)
510                         return k;
511
512                 double a = altitude.max();
513                 double g = ground_altitude();
514                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
515                         return a - g;
516                 return max_gps_height();
517         }
518
519         public double gps_height() {
520                 double a = gps_altitude();
521                 double g = gps_ground_altitude();
522
523                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
524                         return a - g;
525                 return AltosLib.MISSING;
526         }
527
528         public double max_gps_height() {
529                 double a = gps_altitude.max();
530                 double g = gps_ground_altitude();
531
532                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
533                         return a - g;
534                 return AltosLib.MISSING;
535         }
536
537         class AltosSpeed extends AltosCValue {
538
539                 boolean can_max() {
540                         return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
541                 }
542
543                 void set_accel() {
544                         acceleration.set_derivative(this);
545                 }
546
547                 void set_derivative(AltosCValue in) {
548                         super.set_derivative(in);
549                         set_accel();
550                 }
551
552                 void set_computed(double new_value, double time) {
553                         super.set_computed(new_value, time);
554                         set_accel();
555                 }
556
557                 void set_measured(double new_value, double time) {
558                         super.set_measured(new_value, time);
559                         set_accel();
560                 }
561         }
562
563         private AltosSpeed speed;
564
565         public double speed() {
566                 double v = kalman_speed.value();
567                 if (v != AltosLib.MISSING)
568                         return v;
569                 v = speed.value();
570                 if (v != AltosLib.MISSING)
571                         return v;
572                 v = gps_speed();
573                 if (v != AltosLib.MISSING)
574                         return v;
575                 return AltosLib.MISSING;
576         }
577
578         public double max_speed() {
579                 double v = kalman_speed.max();
580                 if (v != AltosLib.MISSING)
581                         return v;
582                 v = speed.max();
583                 if (v != AltosLib.MISSING)
584                         return v;
585                 v = max_gps_speed();
586                 if (v != AltosLib.MISSING)
587                         return v;
588                 return AltosLib.MISSING;
589         }
590
591         class AltosAccel extends AltosCValue {
592
593                 boolean can_max() {
594                         return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
595                 }
596
597                 void set_measured(double a, double time) {
598                         super.set_measured(a, time);
599                         if (ascent)
600                                 speed.set_integral(this.measured);
601                 }
602         }
603
604         AltosAccel acceleration;
605
606         public double acceleration() {
607                 return acceleration.value();
608         }
609
610         public double max_acceleration() {
611                 return acceleration.max();
612         }
613
614         public AltosCValue      orient;
615
616         public void set_orient(double new_orient) {
617                 orient.set_measured(new_orient, time);
618         }
619
620         public double orient() {
621                 return orient.value();
622         }
623
624         public double max_orient() {
625                 return orient.max();
626         }
627
628         public AltosValue       kalman_height, kalman_speed, kalman_acceleration;
629
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);
634         }
635
636         public double   battery_voltage;
637         public double   pyro_voltage;
638         public double   temperature;
639         public double   apogee_voltage;
640         public double   main_voltage;
641
642         public double   ignitor_voltage[];
643
644         public AltosGPS gps;
645         public AltosGPS temp_gps;
646         public int temp_gps_sat_tick;
647         public boolean  gps_pending;
648         public int gps_sequence;
649
650         public AltosIMU imu;
651         public AltosMag mag;
652
653         public static final int MIN_PAD_SAMPLES = 10;
654
655         public int      npad;
656         public int      gps_waiting;
657         public boolean  gps_ready;
658
659         public int      ngps;
660
661         public AltosGreatCircle from_pad;
662         public double   elevation;      /* from pad */
663         public double   range;          /* total distance */
664
665         public double   gps_height;
666
667         public double pad_lat, pad_lon, pad_alt;
668
669         public int      speak_tick;
670         public double   speak_altitude;
671
672         public String   callsign;
673         public String   firmware_version;
674
675         public double   accel_plus_g;
676         public double   accel_minus_g;
677         public double   accel;
678         public double   ground_accel;
679         public double   ground_accel_avg;
680
681         public int      log_format;
682         public String   product;
683
684         public AltosMs5607      baro;
685
686         public AltosCompanion   companion;
687
688         public int      pyro_fired;
689
690         public void set_npad(int npad) {
691                 this.npad = npad;
692                 gps_waiting = MIN_PAD_SAMPLES - npad;
693                 if (this.gps_waiting < 0)
694                         gps_waiting = 0;
695                 gps_ready = gps_waiting == 0;
696         }
697
698         public void init() {
699                 set = 0;
700
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;
710                 landed = false;
711                 boost = false;
712                 rssi = AltosLib.MISSING;
713                 status = 0;
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;
720
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();
728
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;
735
736                 kalman_height = new AltosValue();
737                 kalman_speed = new AltosValue();
738                 kalman_acceleration = new AltosValue();
739
740                 gps = null;
741                 temp_gps = null;
742                 temp_gps_sat_tick = 0;
743                 gps_sequence = 0;
744                 gps_pending = false;
745
746                 imu = null;
747                 last_imu_time = AltosLib.MISSING;
748                 rotation = null;
749                 ground_rotation = null;
750
751                 mag = null;
752                 accel_zero_along = AltosLib.MISSING;
753                 accel_zero_across = AltosLib.MISSING;
754                 accel_zero_through = AltosLib.MISSING;
755
756                 accel_ground_along = AltosLib.MISSING;
757                 accel_ground_across = AltosLib.MISSING;
758                 accel_ground_through = AltosLib.MISSING;
759
760                 pad_orientation = AltosLib.MISSING;
761
762                 gyro_zero_roll = AltosLib.MISSING;
763                 gyro_zero_pitch = AltosLib.MISSING;
764                 gyro_zero_yaw = AltosLib.MISSING;
765
766                 set_npad(0);
767                 ngps = 0;
768
769                 from_pad = null;
770                 elevation = AltosLib.MISSING;
771                 range = AltosLib.MISSING;
772                 gps_height = AltosLib.MISSING;
773
774                 pad_lat = AltosLib.MISSING;
775                 pad_lon = AltosLib.MISSING;
776                 pad_alt = AltosLib.MISSING;
777
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();
784
785                 speak_tick = AltosLib.MISSING;
786                 speak_altitude = AltosLib.MISSING;
787
788                 callsign = null;
789                 firmware_version = null;
790
791                 accel_plus_g = AltosLib.MISSING;
792                 accel_minus_g = AltosLib.MISSING;
793                 accel = AltosLib.MISSING;
794
795                 ground_accel = AltosLib.MISSING;
796                 ground_accel_avg = AltosLib.MISSING;
797
798                 log_format = AltosLib.MISSING;
799                 product = null;
800                 serial = AltosLib.MISSING;
801                 receiver_serial = AltosLib.MISSING;
802                 altitude_32 = AltosLib.MISSING;
803
804                 baro = null;
805                 companion = null;
806
807                 pyro_fired = 0;
808         }
809
810         void finish_update() {
811                 prev_tick = tick;
812
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();
819
820                 kalman_height.finish_update();
821                 kalman_speed.finish_update();
822                 kalman_acceleration.finish_update();
823         }
824
825         void copy(AltosState old) {
826
827                 if (old == null) {
828                         init();
829                         return;
830                 }
831
832                 received_time = old.received_time;
833                 time = old.time;
834                 time_change = old.time_change;
835                 prev_time = old.time;
836
837                 tick = old.tick;
838                 prev_tick = old.tick;
839                 boost_tick = old.boost_tick;
840
841                 state = old.state;
842                 flight = old.flight;
843                 landed = old.landed;
844                 ascent = old.ascent;
845                 boost = old.boost;
846                 rssi = old.rssi;
847                 status = old.status;
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;
854
855                 set = 0;
856
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);
864
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;
871
872                 kalman_height.copy(old.kalman_height);
873                 kalman_speed.copy(old.kalman_speed);
874                 kalman_acceleration.copy(old.kalman_acceleration);
875
876                 if (old.gps != null)
877                         gps = old.gps.clone();
878                 else
879                         gps = null;
880                 if (old.temp_gps != null)
881                         temp_gps = old.temp_gps.clone();
882                 else
883                         temp_gps = null;
884                 temp_gps_sat_tick = old.temp_gps_sat_tick;
885                 gps_sequence = old.gps_sequence;
886                 gps_pending = old.gps_pending;
887
888                 if (old.imu != null)
889                         imu = old.imu.clone();
890                 else
891                         imu = null;
892                 last_imu_time = old.last_imu_time;
893
894                 if (old.rotation != null)
895                         rotation = new AltosRotation (old.rotation);
896
897                 if (old.ground_rotation != null) {
898                         ground_rotation = new AltosRotation(old.ground_rotation);
899                 }
900
901                 accel_zero_along = old.accel_zero_along;
902                 accel_zero_across = old.accel_zero_across;
903                 accel_zero_through = old.accel_zero_through;
904
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;
909
910                 gyro_zero_roll = old.gyro_zero_roll;
911                 gyro_zero_pitch = old.gyro_zero_pitch;
912                 gyro_zero_yaw = old.gyro_zero_yaw;
913
914                 if (old.mag != null)
915                         mag = old.mag.clone();
916                 else
917                         mag = null;
918
919                 npad = old.npad;
920                 gps_waiting = old.gps_waiting;
921                 gps_ready = old.gps_ready;
922                 ngps = old.ngps;
923
924                 if (old.from_pad != null)
925                         from_pad = old.from_pad.clone();
926                 else
927                         from_pad = null;
928
929                 elevation = old.elevation;
930                 range = old.range;
931
932                 gps_height = old.gps_height;
933
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);
940
941                 pad_lat = old.pad_lat;
942                 pad_lon = old.pad_lon;
943                 pad_alt = old.pad_alt;
944
945                 speak_tick = old.speak_tick;
946                 speak_altitude = old.speak_altitude;
947
948                 callsign = old.callsign;
949                 firmware_version = old.firmware_version;
950
951                 accel_plus_g = old.accel_plus_g;
952                 accel_minus_g = old.accel_minus_g;
953                 accel = old.accel;
954                 ground_accel = old.ground_accel;
955                 ground_accel_avg = old.ground_accel_avg;
956
957                 log_format = old.log_format;
958                 product = old.product;
959                 serial = old.serial;
960                 receiver_serial = old.receiver_serial;
961                 altitude_32 = old.altitude_32;
962
963                 baro = old.baro;
964                 companion = old.companion;
965
966                 pyro_fired = old.pyro_fired;
967         }
968
969         void update_time() {
970         }
971
972         void update_gps() {
973                 elevation = AltosLib.MISSING;
974                 range = AltosLib.MISSING;
975
976                 if (gps == null)
977                         return;
978
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) {
982                                 set_npad(npad+1);
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);
987                                 }
988                         }
989                         if (pad_lat == AltosLib.MISSING) {
990                                 pad_lat = gps.lat;
991                                 pad_lon = gps.lon;
992                                 gps_ground_altitude.set(gps.alt, time);
993                         }
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);
1004                 }
1005                 if (gps.lat != 0 && gps.lon != 0 &&
1006                     pad_lat != AltosLib.MISSING &&
1007                     pad_lon != AltosLib.MISSING)
1008                 {
1009                         double h = height();
1010
1011                         if (h == AltosLib.MISSING)
1012                                 h = 0;
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;
1016                 }
1017         }
1018
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) {
1023                                         new_tick += 65536;
1024                                 }
1025                         }
1026                         tick = new_tick;
1027                         time = tick / 100.0;
1028                         time_change = time - prev_time;
1029                 }
1030         }
1031
1032         public void set_boost_tick(int boost_tick) {
1033                 if (boost_tick != AltosLib.MISSING)
1034                         this.boost_tick = boost_tick;
1035         }
1036
1037         public String state_name() {
1038                 return AltosLib.state_name(state);
1039         }
1040
1041         public void set_state(int state) {
1042                 if (state != AltosLib.ao_flight_invalid) {
1043                         this.state = state;
1044                         ascent = (AltosLib.ao_flight_boost <= state &&
1045                                   state <= AltosLib.ao_flight_coast);
1046                         boost = (AltosLib.ao_flight_boost == state);
1047                 }
1048         }
1049
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;
1055                         break;
1056                 }
1057         }
1058
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;
1064                         break;
1065                 }
1066         }
1067
1068         public void set_flight_params(int apogee_delay, int main_deploy) {
1069                 this.apogee_delay = apogee_delay;
1070                 this.main_deploy = main_deploy;
1071         }
1072
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;
1077         }
1078
1079         public void set_callsign(String callsign) {
1080                 this.callsign = callsign;
1081         }
1082
1083         public void set_firmware_version(String version) {
1084                 firmware_version = version;
1085         }
1086
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);
1091         }
1092
1093         private void re_init() {
1094                 int bt = boost_tick;
1095                 int rs = receiver_serial;
1096                 init();
1097                 boost_tick = bt;
1098                 receiver_serial = rs;
1099         }
1100
1101         public void set_flight(int flight) {
1102
1103                 /* When the flight changes, reset the state */
1104                 if (flight != AltosLib.MISSING) {
1105                         if (this.flight != AltosLib.MISSING &&
1106                             this.flight != flight) {
1107                                 re_init();
1108                         }
1109                         this.flight = flight;
1110                 }
1111         }
1112
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) {
1118                                 re_init();
1119                         }
1120                         this.serial = serial;
1121                 }
1122         }
1123
1124         public void set_receiver_serial(int serial) {
1125                 if (serial != AltosLib.MISSING)
1126                         receiver_serial = serial;
1127         }
1128
1129         public boolean altitude_32() {
1130                 return altitude_32 == 1;
1131         }
1132
1133         public void set_altitude_32(int altitude_32) {
1134                 if (altitude_32 != AltosLib.MISSING)
1135                         this.altitude_32 = altitude_32;
1136         }
1137
1138         public int rssi() {
1139                 if (rssi == AltosLib.MISSING)
1140                         return 0;
1141                 return rssi;
1142         }
1143
1144         public void set_rssi(int rssi, int status) {
1145                 if (rssi != AltosLib.MISSING) {
1146                         this.rssi = rssi;
1147                         this.status = status;
1148                 }
1149         }
1150
1151         public void set_received_time(long ms) {
1152                 received_time = ms;
1153         }
1154
1155         public void set_gps(AltosGPS gps, int sequence) {
1156                 if (gps != null) {
1157                         this.gps = gps.clone();
1158                         gps_sequence = sequence;
1159                         update_gps();
1160                         set |= set_gps;
1161                 }
1162         }
1163
1164
1165         public double   accel_zero_along;
1166         public double   accel_zero_across;
1167         public double   accel_zero_through;
1168
1169         public AltosRotation    rotation;
1170         public AltosRotation    ground_rotation;
1171
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;
1177                 }
1178         }
1179
1180         public int pad_orientation;
1181
1182         public double   accel_ground_along, accel_ground_across, accel_ground_through;
1183
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),
1189                                                      pad_orientation);
1190                         ground_rotation = rotation;
1191                         orient.set_computed(rotation.tilt(), time);
1192                 }
1193         }
1194
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();
1200         }
1201
1202         public void set_pad_orientation(int pad_orientation) {
1203                 this.pad_orientation = pad_orientation;
1204                 update_pad_rotation();
1205         }
1206
1207         public double   gyro_zero_roll;
1208         public double   gyro_zero_pitch;
1209         public double   gyro_zero_yaw;
1210
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;
1216                 }
1217         }
1218
1219         public double   last_imu_time;
1220
1221         private double radians(double degrees) {
1222                 if (degrees == AltosLib.MISSING)
1223                         return AltosLib.MISSING;
1224                 return degrees * Math.PI / 180.0;
1225         }
1226
1227         private void update_orient() {
1228                 if (last_imu_time != AltosLib.MISSING) {
1229                         double  t = time - last_imu_time;
1230
1231                         double  pitch = radians(gyro_pitch());
1232                         double  yaw = radians(gyro_yaw());
1233                         double  roll = radians(gyro_roll());
1234
1235                         if (t > 0 & pitch != AltosLib.MISSING && rotation != null) {
1236                                 rotation.rotate(t, pitch, yaw, roll);
1237                                 orient.set_computed(rotation.tilt(), time);
1238                         }
1239                 }
1240                 last_imu_time = time;
1241         }
1242
1243         public void set_imu(AltosIMU imu) {
1244                 if (imu != null)
1245                         imu = imu.clone();
1246                 this.imu = imu;
1247                 update_orient();
1248         }
1249
1250         private double gyro_zero_overflow(double first) {
1251                 double v = first / 128.0;
1252                 if (v < 0)
1253                         v = Math.ceil(v);
1254                 else
1255                         v = Math.floor(v);
1256                 return v * 128.0;
1257         }
1258
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);
1264                 }
1265         }
1266
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;
1271         }
1272
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;
1277         }
1278
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;
1283         }
1284
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);
1288                 }
1289                 return AltosLib.MISSING;
1290         }
1291
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);
1295                 }
1296                 return AltosLib.MISSING;
1297         }
1298
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);
1302                 }
1303                 return AltosLib.MISSING;
1304         }
1305
1306         public void set_mag(AltosMag mag) {
1307                 this.mag = mag.clone();
1308         }
1309
1310         public double mag_along() {
1311                 if (mag != null)
1312                         return AltosMag.convert_gauss(mag.along);
1313                 return AltosLib.MISSING;
1314         }
1315
1316         public double mag_across() {
1317                 if (mag != null)
1318                         return AltosMag.convert_gauss(mag.across);
1319                 return AltosLib.MISSING;
1320         }
1321
1322         public double mag_through() {
1323                 if (mag != null)
1324                         return AltosMag.convert_gauss(mag.through);
1325                 return AltosLib.MISSING;
1326         }
1327
1328         public AltosMs5607 make_baro() {
1329                 if (baro == null)
1330                         baro = new AltosMs5607();
1331                 return baro;
1332         }
1333
1334         public void set_ms5607(AltosMs5607 ms5607) {
1335                 baro = ms5607;
1336
1337                 if (baro != null) {
1338                         set_pressure(baro.pa);
1339                         set_temperature(baro.cc / 100.0);
1340                 }
1341         }
1342
1343         public void set_ms5607(int pres, int temp) {
1344                 if (baro != null) {
1345                         baro.set(pres, temp);
1346
1347                         set_pressure(baro.pa);
1348                         set_temperature(baro.cc / 100.0);
1349                 }
1350         }
1351
1352         public void make_companion (int nchannels) {
1353                 if (companion == null)
1354                         companion = new AltosCompanion(nchannels);
1355         }
1356
1357         public void set_companion(AltosCompanion companion) {
1358                 this.companion = companion;
1359         }
1360
1361         void update_accel() {
1362                 if (accel == AltosLib.MISSING)
1363                         return;
1364                 if (accel_plus_g == AltosLib.MISSING)
1365                         return;
1366                 if (accel_minus_g == AltosLib.MISSING)
1367                         return;
1368
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);
1372         }
1373
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;
1378                         update_accel();
1379                 }
1380         }
1381
1382         public void set_ground_accel(double ground_accel) {
1383                 if (ground_accel != AltosLib.MISSING)
1384                         this.ground_accel = ground_accel;
1385         }
1386
1387         public void set_accel(double accel) {
1388                 if (accel != AltosLib.MISSING) {
1389                         this.accel = accel;
1390                         if (state == AltosLib.ao_flight_pad) {
1391                                 if (ground_accel_avg == AltosLib.MISSING)
1392                                         ground_accel_avg = accel;
1393                                 else
1394                                         ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
1395                         }
1396                 }
1397                 update_accel();
1398         }
1399
1400         public void set_temperature(double temperature) {
1401                 if (temperature != AltosLib.MISSING) {
1402                         this.temperature = temperature;
1403                         set |= set_data;
1404                 }
1405         }
1406
1407         public void set_battery_voltage(double battery_voltage) {
1408                 if (battery_voltage != AltosLib.MISSING) {
1409                         this.battery_voltage = battery_voltage;
1410                         set |= set_data;
1411                 }
1412         }
1413
1414         public void set_pyro_voltage(double pyro_voltage) {
1415                 if (pyro_voltage != AltosLib.MISSING) {
1416                         this.pyro_voltage = pyro_voltage;
1417                         set |= set_data;
1418                 }
1419         }
1420
1421         public void set_apogee_voltage(double apogee_voltage) {
1422                 if (apogee_voltage != AltosLib.MISSING) {
1423                         this.apogee_voltage = apogee_voltage;
1424                         set |= set_data;
1425                 }
1426         }
1427
1428         public void set_main_voltage(double main_voltage) {
1429                 if (main_voltage != AltosLib.MISSING) {
1430                         this.main_voltage = main_voltage;
1431                         set |= set_data;
1432                 }
1433         }
1434
1435         public void set_ignitor_voltage(double[] voltage) {
1436                 this.ignitor_voltage = voltage;
1437         }
1438
1439         public void set_pyro_fired(int fired) {
1440                 this.pyro_fired = fired;
1441         }
1442
1443         public double time_since_boost() {
1444                 if (tick == AltosLib.MISSING)
1445                         return 0.0;
1446
1447                 if (boost_tick == AltosLib.MISSING)
1448                         return tick / 100.0;
1449                 return (tick - boost_tick) / 100.0;
1450         }
1451
1452         public boolean valid() {
1453                 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1454         }
1455
1456         public AltosGPS make_temp_gps(boolean sats) {
1457                 if (temp_gps == null) {
1458                         temp_gps = new AltosGPS(gps);
1459                 }
1460                 gps_pending = true;
1461                 if (sats) {
1462                         if (tick != temp_gps_sat_tick)
1463                                 temp_gps.cc_gps_sat = null;
1464                         temp_gps_sat_tick = tick;
1465                 }
1466                 return temp_gps;
1467         }
1468
1469         public void set_temp_gps() {
1470                 set_gps(temp_gps, gps_sequence + 1);
1471                 gps_pending = false;
1472                 temp_gps = null;
1473         }
1474
1475         public AltosState clone() {
1476                 AltosState s = new AltosState();
1477                 s.copy(this);
1478                 return s;
1479         }
1480
1481         public AltosState () {
1482                 init();
1483         }
1484 }