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