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