altoslib: Store saved state in version-independent format
[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 String   product;
836
837         public AltosMs5607      baro;
838
839         public AltosCompanion   companion;
840
841         public int      pyro_fired;
842
843         public void set_npad(int npad) {
844                 this.npad = npad;
845                 gps_waiting = MIN_PAD_SAMPLES - npad;
846                 if (this.gps_waiting < 0)
847                         gps_waiting = 0;
848                 gps_ready = gps_waiting == 0;
849         }
850
851         public void init() {
852                 set = 0;
853
854                 received_time = System.currentTimeMillis();
855                 time = AltosLib.MISSING;
856                 time_change = AltosLib.MISSING;
857                 prev_time = AltosLib.MISSING;
858                 tick = AltosLib.MISSING;
859                 prev_tick = AltosLib.MISSING;
860                 boost_tick = AltosLib.MISSING;
861                 state = AltosLib.ao_flight_invalid;
862                 flight = AltosLib.MISSING;
863                 landed = false;
864                 boost = false;
865                 rssi = AltosLib.MISSING;
866                 status = 0;
867                 device_type = AltosLib.MISSING;
868                 config_major = AltosLib.MISSING;
869                 config_minor = AltosLib.MISSING;
870                 apogee_delay = AltosLib.MISSING;
871                 main_deploy = AltosLib.MISSING;
872                 flight_log_max = AltosLib.MISSING;
873
874                 ground_altitude = new AltosCValue();
875                 ground_pressure = new AltosGroundPressure();
876                 altitude = new AltosAltitude();
877                 pressure = new AltosPressure();
878                 speed = new AltosSpeed();
879                 acceleration = new AltosAccel();
880                 orient = new AltosCValue();
881
882                 temperature = AltosLib.MISSING;
883                 battery_voltage = AltosLib.MISSING;
884                 pyro_voltage = AltosLib.MISSING;
885                 apogee_voltage = AltosLib.MISSING;
886                 main_voltage = AltosLib.MISSING;
887                 ignitor_voltage = null;
888
889                 kalman_height = new AltosValue();
890                 kalman_speed = new AltosValue();
891                 kalman_acceleration = new AltosValue();
892
893                 gps = null;
894                 temp_gps = null;
895                 temp_gps_sat_tick = 0;
896                 gps_sequence = 0;
897                 gps_pending = false;
898
899                 imu = null;
900                 last_imu_time = AltosLib.MISSING;
901                 rotation = null;
902                 ground_rotation = null;
903
904                 mag = null;
905                 accel_zero_along = AltosLib.MISSING;
906                 accel_zero_across = AltosLib.MISSING;
907                 accel_zero_through = AltosLib.MISSING;
908
909                 accel_ground_along = AltosLib.MISSING;
910                 accel_ground_across = AltosLib.MISSING;
911                 accel_ground_through = AltosLib.MISSING;
912
913                 pad_orientation = AltosLib.MISSING;
914
915                 gyro_zero_roll = AltosLib.MISSING;
916                 gyro_zero_pitch = AltosLib.MISSING;
917                 gyro_zero_yaw = AltosLib.MISSING;
918
919                 set_npad(0);
920                 ngps = 0;
921
922                 from_pad = null;
923                 elevation = AltosLib.MISSING;
924                 range = AltosLib.MISSING;
925                 gps_height = AltosLib.MISSING;
926
927                 pad_lat = AltosLib.MISSING;
928                 pad_lon = AltosLib.MISSING;
929                 pad_alt = AltosLib.MISSING;
930
931                 gps_altitude = new AltosGpsAltitude();
932                 gps_ground_altitude = new AltosGpsGroundAltitude();
933                 gps_ground_speed = new AltosValue();
934                 gps_speed = new AltosValue();
935                 gps_ascent_rate = new AltosValue();
936                 gps_course = new AltosValue();
937
938                 speak_tick = AltosLib.MISSING;
939                 speak_altitude = AltosLib.MISSING;
940
941                 callsign = null;
942                 firmware_version = null;
943
944                 accel_plus_g = AltosLib.MISSING;
945                 accel_minus_g = AltosLib.MISSING;
946                 accel = AltosLib.MISSING;
947
948                 ground_accel = AltosLib.MISSING;
949                 ground_accel_avg = AltosLib.MISSING;
950
951                 log_format = AltosLib.MISSING;
952                 product = null;
953                 serial = AltosLib.MISSING;
954                 receiver_serial = AltosLib.MISSING;
955                 altitude_32 = AltosLib.MISSING;
956
957                 baro = null;
958                 companion = null;
959
960                 pyro_fired = 0;
961         }
962
963         void finish_update() {
964                 prev_tick = tick;
965
966                 ground_altitude.finish_update();
967                 altitude.finish_update();
968                 pressure.finish_update();
969                 speed.finish_update();
970                 acceleration.finish_update();
971                 orient.finish_update();
972
973                 kalman_height.finish_update();
974                 kalman_speed.finish_update();
975                 kalman_acceleration.finish_update();
976         }
977
978         void copy(AltosState old) {
979
980                 if (old == null) {
981                         init();
982                         return;
983                 }
984
985                 received_time = old.received_time;
986                 time = old.time;
987                 time_change = old.time_change;
988                 prev_time = old.time;
989
990                 tick = old.tick;
991                 prev_tick = old.tick;
992                 boost_tick = old.boost_tick;
993
994                 state = old.state;
995                 flight = old.flight;
996                 landed = old.landed;
997                 ascent = old.ascent;
998                 boost = old.boost;
999                 rssi = old.rssi;
1000                 status = old.status;
1001                 device_type = old.device_type;
1002                 config_major = old.config_major;
1003                 config_minor = old.config_minor;
1004                 apogee_delay = old.apogee_delay;
1005                 main_deploy = old.main_deploy;
1006                 flight_log_max = old.flight_log_max;
1007
1008                 set = 0;
1009
1010                 ground_pressure.copy(old.ground_pressure);
1011                 ground_altitude.copy(old.ground_altitude);
1012                 altitude.copy(old.altitude);
1013                 pressure.copy(old.pressure);
1014                 speed.copy(old.speed);
1015                 acceleration.copy(old.acceleration);
1016                 orient.copy(old.orient);
1017
1018                 battery_voltage = old.battery_voltage;
1019                 pyro_voltage = old.pyro_voltage;
1020                 temperature = old.temperature;
1021                 apogee_voltage = old.apogee_voltage;
1022                 main_voltage = old.main_voltage;
1023                 ignitor_voltage = old.ignitor_voltage;
1024
1025                 kalman_height.copy(old.kalman_height);
1026                 kalman_speed.copy(old.kalman_speed);
1027                 kalman_acceleration.copy(old.kalman_acceleration);
1028
1029                 if (old.gps != null)
1030                         gps = old.gps.clone();
1031                 else
1032                         gps = null;
1033                 if (old.temp_gps != null)
1034                         temp_gps = old.temp_gps.clone();
1035                 else
1036                         temp_gps = null;
1037                 temp_gps_sat_tick = old.temp_gps_sat_tick;
1038                 gps_sequence = old.gps_sequence;
1039                 gps_pending = old.gps_pending;
1040
1041                 if (old.imu != null)
1042                         imu = old.imu.clone();
1043                 else
1044                         imu = null;
1045                 last_imu_time = old.last_imu_time;
1046
1047                 if (old.rotation != null)
1048                         rotation = new AltosRotation (old.rotation);
1049
1050                 if (old.ground_rotation != null) {
1051                         ground_rotation = new AltosRotation(old.ground_rotation);
1052                 }
1053
1054                 accel_zero_along = old.accel_zero_along;
1055                 accel_zero_across = old.accel_zero_across;
1056                 accel_zero_through = old.accel_zero_through;
1057
1058                 accel_ground_along = old.accel_ground_along;
1059                 accel_ground_across = old.accel_ground_across;
1060                 accel_ground_through = old.accel_ground_through;
1061                 pad_orientation = old.pad_orientation;
1062
1063                 gyro_zero_roll = old.gyro_zero_roll;
1064                 gyro_zero_pitch = old.gyro_zero_pitch;
1065                 gyro_zero_yaw = old.gyro_zero_yaw;
1066
1067                 if (old.mag != null)
1068                         mag = old.mag.clone();
1069                 else
1070                         mag = null;
1071
1072                 npad = old.npad;
1073                 gps_waiting = old.gps_waiting;
1074                 gps_ready = old.gps_ready;
1075                 ngps = old.ngps;
1076
1077                 if (old.from_pad != null)
1078                         from_pad = old.from_pad.clone();
1079                 else
1080                         from_pad = null;
1081
1082                 elevation = old.elevation;
1083                 range = old.range;
1084
1085                 gps_height = old.gps_height;
1086
1087                 gps_altitude.copy(old.gps_altitude);
1088                 gps_ground_altitude.copy(old.gps_ground_altitude);
1089                 gps_ground_speed.copy(old.gps_ground_speed);
1090                 gps_ascent_rate.copy(old.gps_ascent_rate);
1091                 gps_course.copy(old.gps_course);
1092                 gps_speed.copy(old.gps_speed);
1093
1094                 pad_lat = old.pad_lat;
1095                 pad_lon = old.pad_lon;
1096                 pad_alt = old.pad_alt;
1097
1098                 speak_tick = old.speak_tick;
1099                 speak_altitude = old.speak_altitude;
1100
1101                 callsign = old.callsign;
1102                 firmware_version = old.firmware_version;
1103
1104                 accel_plus_g = old.accel_plus_g;
1105                 accel_minus_g = old.accel_minus_g;
1106                 accel = old.accel;
1107                 ground_accel = old.ground_accel;
1108                 ground_accel_avg = old.ground_accel_avg;
1109
1110                 log_format = old.log_format;
1111                 product = old.product;
1112                 serial = old.serial;
1113                 receiver_serial = old.receiver_serial;
1114                 altitude_32 = old.altitude_32;
1115
1116                 baro = old.baro;
1117                 companion = old.companion;
1118
1119                 pyro_fired = old.pyro_fired;
1120         }
1121
1122         void update_time() {
1123         }
1124
1125         void update_gps() {
1126                 elevation = AltosLib.MISSING;
1127                 range = AltosLib.MISSING;
1128
1129                 if (gps == null)
1130                         return;
1131
1132                 if (gps.locked && gps.nsat >= 4) {
1133                         /* Track consecutive 'good' gps reports, waiting for 10 of them */
1134                         if (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_stateless) {
1135                                 set_npad(npad+1);
1136                                 if (pad_lat != AltosLib.MISSING && (npad < 10 || state == AltosLib.ao_flight_pad)) {
1137                                         pad_lat = (pad_lat * 31 + gps.lat) / 32;
1138                                         pad_lon = (pad_lon * 31 + gps.lon) / 32;
1139                                         gps_ground_altitude.set_filtered(gps.alt, time);
1140                                 }
1141                         }
1142                         if (pad_lat == AltosLib.MISSING) {
1143                                 pad_lat = gps.lat;
1144                                 pad_lon = gps.lon;
1145                                 gps_ground_altitude.set(gps.alt, time);
1146                         }
1147                         gps_altitude.set(gps.alt, time);
1148                         if (gps.climb_rate != AltosLib.MISSING)
1149                                 gps_ascent_rate.set(gps.climb_rate, time);
1150                         if (gps.ground_speed != AltosLib.MISSING)
1151                                 gps_ground_speed.set(gps.ground_speed, time);
1152                         if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
1153                                 gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
1154                                                         gps.climb_rate * gps.climb_rate), time);
1155                         if (gps.course != AltosLib.MISSING)
1156                                 gps_course.set(gps.course, time);
1157                 }
1158                 if (gps.lat != 0 && gps.lon != 0 &&
1159                     pad_lat != AltosLib.MISSING &&
1160                     pad_lon != AltosLib.MISSING)
1161                 {
1162                         double h = height();
1163
1164                         if (h == AltosLib.MISSING)
1165                                 h = 0;
1166                         from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
1167                         elevation = from_pad.elevation;
1168                         range = from_pad.range;
1169                 }
1170         }
1171
1172         public void set_tick(int new_tick) {
1173                 if (new_tick != AltosLib.MISSING) {
1174                         if (prev_tick != AltosLib.MISSING) {
1175                                 while (new_tick < prev_tick - 1000) {
1176                                         new_tick += 65536;
1177                                 }
1178                         }
1179                         tick = new_tick;
1180                         time = tick / 100.0;
1181                         time_change = time - prev_time;
1182                 }
1183         }
1184
1185         public void set_boost_tick(int boost_tick) {
1186                 if (boost_tick != AltosLib.MISSING)
1187                         this.boost_tick = boost_tick;
1188         }
1189
1190         public String state_name() {
1191                 return AltosLib.state_name(state);
1192         }
1193
1194         public void set_product(String product) {
1195                 this.product = product;
1196         }
1197
1198         public void set_state(int state) {
1199                 if (state != AltosLib.ao_flight_invalid) {
1200                         this.state = state;
1201                         ascent = (AltosLib.ao_flight_boost <= state &&
1202                                   state <= AltosLib.ao_flight_coast);
1203                         boost = (AltosLib.ao_flight_boost == state);
1204                 }
1205         }
1206
1207         public int state() {
1208                 return state;
1209         }
1210
1211         public void set_device_type(int device_type) {
1212                 this.device_type = device_type;
1213                 switch (device_type) {
1214                 case AltosLib.product_telegps:
1215                         this.state = AltosLib.ao_flight_stateless;
1216                         break;
1217                 }
1218         }
1219
1220         public void set_log_format(int log_format) {
1221                 this.log_format = log_format;
1222                 switch (log_format) {
1223                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
1224                         this.state = AltosLib.ao_flight_stateless;
1225                         break;
1226                 }
1227         }
1228
1229         public void set_flight_params(int apogee_delay, int main_deploy) {
1230                 this.apogee_delay = apogee_delay;
1231                 this.main_deploy = main_deploy;
1232         }
1233
1234         public void set_config(int major, int minor, int flight_log_max) {
1235                 config_major = major;
1236                 config_minor = minor;
1237                 this.flight_log_max = flight_log_max;
1238         }
1239
1240         public void set_callsign(String callsign) {
1241                 this.callsign = callsign;
1242         }
1243
1244         public void set_firmware_version(String version) {
1245                 firmware_version = version;
1246         }
1247
1248         public int compare_version(String other_version) {
1249                 if (firmware_version == null)
1250                         return AltosLib.MISSING;
1251                 return AltosLib.compare_version(firmware_version, other_version);
1252         }
1253
1254         private void re_init() {
1255                 int bt = boost_tick;
1256                 int rs = receiver_serial;
1257                 init();
1258                 boost_tick = bt;
1259                 receiver_serial = rs;
1260         }
1261
1262         public void set_flight(int flight) {
1263
1264                 /* When the flight changes, reset the state */
1265                 if (flight != AltosLib.MISSING) {
1266                         if (this.flight != AltosLib.MISSING &&
1267                             this.flight != flight) {
1268                                 re_init();
1269                         }
1270                         this.flight = flight;
1271                 }
1272         }
1273
1274         public void set_serial(int serial) {
1275                 /* When the serial changes, reset the state */
1276                 if (serial != AltosLib.MISSING) {
1277                         if (this.serial != AltosLib.MISSING &&
1278                             this.serial != serial) {
1279                                 re_init();
1280                         }
1281                         this.serial = serial;
1282                 }
1283         }
1284
1285         public void set_receiver_serial(int serial) {
1286                 if (serial != AltosLib.MISSING)
1287                         receiver_serial = serial;
1288         }
1289
1290         public boolean altitude_32() {
1291                 return altitude_32 == 1;
1292         }
1293
1294         public void set_altitude_32(int altitude_32) {
1295                 if (altitude_32 != AltosLib.MISSING)
1296                         this.altitude_32 = altitude_32;
1297         }
1298
1299         public int rssi() {
1300                 if (rssi == AltosLib.MISSING)
1301                         return 0;
1302                 return rssi;
1303         }
1304
1305         public void set_rssi(int rssi, int status) {
1306                 if (rssi != AltosLib.MISSING) {
1307                         this.rssi = rssi;
1308                         this.status = status;
1309                 }
1310         }
1311
1312         public void set_received_time(long ms) {
1313                 received_time = ms;
1314         }
1315
1316         public void set_gps(AltosGPS gps, int sequence) {
1317                 if (gps != null) {
1318                         this.gps = gps.clone();
1319                         gps_sequence = sequence;
1320                         update_gps();
1321                         set |= set_gps;
1322                 }
1323         }
1324
1325
1326         public double   accel_zero_along;
1327         public double   accel_zero_across;
1328         public double   accel_zero_through;
1329
1330         public AltosRotation    rotation;
1331         public AltosRotation    ground_rotation;
1332
1333         public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
1334                 if (zero_along != AltosLib.MISSING) {
1335                         accel_zero_along = zero_along;
1336                         accel_zero_across = zero_across;
1337                         accel_zero_through = zero_through;
1338                 }
1339         }
1340
1341         public int pad_orientation;
1342
1343         public double   accel_ground_along, accel_ground_across, accel_ground_through;
1344
1345         void update_pad_rotation() {
1346                 if (pad_orientation != AltosLib.MISSING && accel_ground_along != AltosLib.MISSING) {
1347                         rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - accel_zero_across),
1348                                                      AltosIMU.convert_accel(accel_ground_through - accel_zero_through),
1349                                                      AltosIMU.convert_accel(accel_ground_along - accel_zero_along),
1350                                                      pad_orientation);
1351                         ground_rotation = rotation;
1352                         orient.set_computed(rotation.tilt(), time);
1353                 }
1354         }
1355
1356         public void set_accel_ground(double ground_along, double ground_across, double ground_through) {
1357                 accel_ground_along = ground_along;
1358                 accel_ground_across = ground_across;
1359                 accel_ground_through = ground_through;
1360                 update_pad_rotation();
1361         }
1362
1363         public void set_pad_orientation(int pad_orientation) {
1364                 this.pad_orientation = pad_orientation;
1365                 update_pad_rotation();
1366         }
1367
1368         public double   gyro_zero_roll;
1369         public double   gyro_zero_pitch;
1370         public double   gyro_zero_yaw;
1371
1372         public void set_gyro_zero(double roll, double pitch, double yaw) {
1373                 if (roll != AltosLib.MISSING) {
1374                         gyro_zero_roll = roll;
1375                         gyro_zero_pitch = pitch;
1376                         gyro_zero_yaw = yaw;
1377                 }
1378         }
1379
1380         public double   last_imu_time;
1381
1382         private double radians(double degrees) {
1383                 if (degrees == AltosLib.MISSING)
1384                         return AltosLib.MISSING;
1385                 return degrees * Math.PI / 180.0;
1386         }
1387
1388         private void update_orient() {
1389                 if (last_imu_time != AltosLib.MISSING) {
1390                         double  t = time - last_imu_time;
1391
1392                         double  pitch = radians(gyro_pitch());
1393                         double  yaw = radians(gyro_yaw());
1394                         double  roll = radians(gyro_roll());
1395
1396                         if (t > 0 & pitch != AltosLib.MISSING && rotation != null) {
1397                                 rotation.rotate(t, pitch, yaw, roll);
1398                                 orient.set_computed(rotation.tilt(), time);
1399                         }
1400                 }
1401                 last_imu_time = time;
1402         }
1403
1404         public void set_imu(AltosIMU imu) {
1405                 if (imu != null)
1406                         imu = imu.clone();
1407                 this.imu = imu;
1408                 update_orient();
1409         }
1410
1411         private double gyro_zero_overflow(double first) {
1412                 double v = first / 128.0;
1413                 if (v < 0)
1414                         v = Math.ceil(v);
1415                 else
1416                         v = Math.floor(v);
1417                 return v * 128.0;
1418         }
1419
1420         public void check_imu_wrap(AltosIMU imu) {
1421                 if (this.imu == null) {
1422                         gyro_zero_roll += gyro_zero_overflow(imu.gyro_roll);
1423                         gyro_zero_pitch += gyro_zero_overflow(imu.gyro_pitch);
1424                         gyro_zero_yaw += gyro_zero_overflow(imu.gyro_yaw);
1425                 }
1426         }
1427
1428         public double accel_along() {
1429                 if (imu != null && accel_zero_along != AltosLib.MISSING)
1430                         return AltosIMU.convert_accel(imu.accel_along - accel_zero_along);
1431                 return AltosLib.MISSING;
1432         }
1433
1434         public double accel_across() {
1435                 if (imu != null && accel_zero_across != AltosLib.MISSING)
1436                         return AltosIMU.convert_accel(imu.accel_across - accel_zero_across);
1437                 return AltosLib.MISSING;
1438         }
1439
1440         public double accel_through() {
1441                 if (imu != null && accel_zero_through != AltosLib.MISSING)
1442                         return AltosIMU.convert_accel(imu.accel_through - accel_zero_through);
1443                 return AltosLib.MISSING;
1444         }
1445
1446         public double gyro_roll() {
1447                 if (imu != null && gyro_zero_roll != AltosLib.MISSING) {
1448                         return AltosIMU.convert_gyro(imu.gyro_roll - gyro_zero_roll);
1449                 }
1450                 return AltosLib.MISSING;
1451         }
1452
1453         public double gyro_pitch() {
1454                 if (imu != null && gyro_zero_pitch != AltosLib.MISSING) {
1455                         return AltosIMU.convert_gyro(imu.gyro_pitch - gyro_zero_pitch);
1456                 }
1457                 return AltosLib.MISSING;
1458         }
1459
1460         public double gyro_yaw() {
1461                 if (imu != null && gyro_zero_yaw != AltosLib.MISSING) {
1462                         return AltosIMU.convert_gyro(imu.gyro_yaw - gyro_zero_yaw);
1463                 }
1464                 return AltosLib.MISSING;
1465         }
1466
1467         public void set_mag(AltosMag mag) {
1468                 this.mag = mag.clone();
1469         }
1470
1471         public double mag_along() {
1472                 if (mag != null)
1473                         return AltosMag.convert_gauss(mag.along);
1474                 return AltosLib.MISSING;
1475         }
1476
1477         public double mag_across() {
1478                 if (mag != null)
1479                         return AltosMag.convert_gauss(mag.across);
1480                 return AltosLib.MISSING;
1481         }
1482
1483         public double mag_through() {
1484                 if (mag != null)
1485                         return AltosMag.convert_gauss(mag.through);
1486                 return AltosLib.MISSING;
1487         }
1488
1489         public AltosMs5607 make_baro() {
1490                 if (baro == null)
1491                         baro = new AltosMs5607();
1492                 return baro;
1493         }
1494
1495         public void set_ms5607(AltosMs5607 ms5607) {
1496                 baro = ms5607;
1497
1498                 if (baro != null) {
1499                         set_pressure(baro.pa);
1500                         set_temperature(baro.cc / 100.0);
1501                 }
1502         }
1503
1504         public void set_ms5607(int pres, int temp) {
1505                 if (baro != null) {
1506                         baro.set(pres, temp);
1507
1508                         set_pressure(baro.pa);
1509                         set_temperature(baro.cc / 100.0);
1510                 }
1511         }
1512
1513         public void set_companion(AltosCompanion companion) {
1514                 this.companion = companion;
1515         }
1516
1517         void update_accel() {
1518                 if (accel == AltosLib.MISSING)
1519                         return;
1520                 if (accel_plus_g == AltosLib.MISSING)
1521                         return;
1522                 if (accel_minus_g == AltosLib.MISSING)
1523                         return;
1524
1525                 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
1526                 double counts_per_mss = counts_per_g / 9.80665;
1527                 acceleration.set_measured((accel_plus_g - accel) / counts_per_mss, time);
1528         }
1529
1530         public void set_accel_g(double accel_plus_g, double accel_minus_g) {
1531                 if (accel_plus_g != AltosLib.MISSING) {
1532                         this.accel_plus_g = accel_plus_g;
1533                         this.accel_minus_g = accel_minus_g;
1534                         update_accel();
1535                 }
1536         }
1537
1538         public void set_ground_accel(double ground_accel) {
1539                 if (ground_accel != AltosLib.MISSING)
1540                         this.ground_accel = ground_accel;
1541         }
1542
1543         public void set_accel(double accel) {
1544                 if (accel != AltosLib.MISSING) {
1545                         this.accel = accel;
1546                         if (state == AltosLib.ao_flight_pad) {
1547                                 if (ground_accel_avg == AltosLib.MISSING)
1548                                         ground_accel_avg = accel;
1549                                 else
1550                                         ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
1551                         }
1552                 }
1553                 update_accel();
1554         }
1555
1556         public void set_temperature(double temperature) {
1557                 if (temperature != AltosLib.MISSING) {
1558                         this.temperature = temperature;
1559                         set |= set_data;
1560                 }
1561         }
1562
1563         public void set_battery_voltage(double battery_voltage) {
1564                 if (battery_voltage != AltosLib.MISSING) {
1565                         this.battery_voltage = battery_voltage;
1566                         set |= set_data;
1567                 }
1568         }
1569
1570         public void set_pyro_voltage(double pyro_voltage) {
1571                 if (pyro_voltage != AltosLib.MISSING) {
1572                         this.pyro_voltage = pyro_voltage;
1573                         set |= set_data;
1574                 }
1575         }
1576
1577         public void set_apogee_voltage(double apogee_voltage) {
1578                 if (apogee_voltage != AltosLib.MISSING) {
1579                         this.apogee_voltage = apogee_voltage;
1580                         set |= set_data;
1581                 }
1582         }
1583
1584         public void set_main_voltage(double main_voltage) {
1585                 if (main_voltage != AltosLib.MISSING) {
1586                         this.main_voltage = main_voltage;
1587                         set |= set_data;
1588                 }
1589         }
1590
1591         public void set_ignitor_voltage(double[] voltage) {
1592                 this.ignitor_voltage = voltage;
1593         }
1594
1595         public void set_pyro_fired(int fired) {
1596                 this.pyro_fired = fired;
1597         }
1598
1599         public double time_since_boost() {
1600                 if (tick == AltosLib.MISSING)
1601                         return 0.0;
1602
1603                 if (boost_tick == AltosLib.MISSING)
1604                         return tick / 100.0;
1605                 return (tick - boost_tick) / 100.0;
1606         }
1607
1608         public boolean valid() {
1609                 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1610         }
1611
1612         public AltosGPS make_temp_gps(boolean sats) {
1613                 if (temp_gps == null) {
1614                         temp_gps = new AltosGPS(gps);
1615                 }
1616                 gps_pending = true;
1617                 if (sats) {
1618                         if (tick != temp_gps_sat_tick)
1619                                 temp_gps.cc_gps_sat = null;
1620                         temp_gps_sat_tick = tick;
1621                 }
1622                 return temp_gps;
1623         }
1624
1625         public void set_temp_gps() {
1626                 set_gps(temp_gps, gps_sequence + 1);
1627                 gps_pending = false;
1628                 temp_gps = null;
1629         }
1630
1631         public AltosState clone() {
1632                 AltosState s = new AltosState();
1633                 s.copy(this);
1634
1635                 AltosHashSet    hash = hashSet();
1636                 String          onetrip = hash.toString();
1637                 AltosHashSet    back = AltosHashSet.fromString(onetrip);
1638                 AltosState      tripstate = AltosState.fromHashSet(back);
1639                 AltosHashSet    triphash = tripstate.hashSet();
1640                 String          twotrip = triphash.toString();
1641
1642                 if (!onetrip.equals(twotrip)) {
1643                         System.out.printf("%s\n%s\n", onetrip, twotrip);
1644                         System.exit(1);
1645                 }
1646                 return s;
1647         }
1648
1649         public AltosState () {
1650                 init();
1651         }
1652
1653         public AltosHashSet hashSet() {
1654                 AltosHashSet    h = new AltosHashSet();
1655
1656                 h.putBoolean("valid", true);
1657                 h.putInt("set", set);
1658                 h.putLong("received_time", received_time);
1659                 h.putDouble("time", time);
1660                 h.putDouble("prev_time", prev_time);
1661                 h.putDouble("time_change", time_change);
1662                 h.putInt("tick", tick);
1663                 h.putInt("prev_tick", prev_tick);
1664                 h.putInt("boost_tick", boost_tick);
1665                 h.putInt("state", state);
1666                 h.putInt("flight", flight);
1667                 h.putInt("serial", serial);
1668                 h.putInt("altitude_32", altitude_32);
1669                 h.putInt("receiver_serial", receiver_serial);
1670                 h.putBoolean("landed", landed);
1671                 h.putBoolean("ascent", ascent);
1672                 h.putBoolean("boost", boost);
1673                 h.putInt("rssi", rssi);
1674                 h.putInt("status", status);
1675                 h.putInt("device_type", device_type);
1676                 h.putInt("config_major", config_major);
1677                 h.putInt("config_minor", config_minor);
1678                 h.putInt("apogee_delay", apogee_delay);
1679                 h.putInt("main_deploy", main_deploy);
1680                 h.putInt("flight_log_max", flight_log_max);
1681                 h.putHashable("ground_altitude", ground_altitude);
1682                 h.putHashable("gps_ground_altitude", gps_ground_altitude);
1683                 h.putHashable("ground_pressure", ground_pressure);
1684                 h.putHashable("altitude", altitude);
1685                 h.putHashable("gps_altitude", gps_altitude);
1686                 h.putHashable("gps_ground_speed", gps_ground_speed);
1687                 h.putHashable("gps_ascent_rate", gps_ascent_rate);
1688                 h.putHashable("gps_course", gps_course);
1689                 h.putHashable("gps_speed", gps_speed);
1690                 h.putHashable("pressure", pressure);
1691                 h.putHashable("speed", speed);
1692                 h.putHashable("acceleration", acceleration);
1693                 h.putHashable("orient", orient);
1694                 h.putHashable("kalman_height", kalman_height);
1695                 h.putHashable("kalman_speed", kalman_speed);
1696                 h.putHashable("kalman_acceleration", kalman_acceleration);
1697
1698                 h.putDouble("battery_voltage",battery_voltage);
1699                 h.putDouble("pyro_voltage",pyro_voltage);
1700                 h.putDouble("temperature",temperature);
1701                 h.putDouble("apogee_voltage",apogee_voltage);
1702                 h.putDouble("main_voltage",main_voltage);
1703                 h.putDoubleArray("ignitor_voltage",ignitor_voltage);
1704                 h.putHashable("gps", gps);
1705                 h.putHashable("temp_gps", temp_gps);
1706                 h.putInt("temp_gps_sat_tick", temp_gps_sat_tick);
1707                 h.putBoolean("gps_pending", gps_pending);
1708                 h.putInt("gps_sequence", gps_sequence);
1709                 h.putHashable("imu", imu);
1710                 h.putHashable("mag", mag);
1711
1712                 h.putInt("npad", npad);
1713                 h.putInt("gps_waiting", gps_waiting);
1714                 h.putBoolean("gps_ready", gps_ready);
1715                 h.putInt("ngps", ngps);
1716                 h.putHashable("from_pad", from_pad);
1717                 h.putDouble("elevation", elevation);
1718                 h.putDouble("range", range);
1719                 h.putDouble("gps_height", gps_height);
1720                 h.putDouble("pad_lat", pad_lat);
1721                 h.putDouble("pad_lon", pad_lon);
1722                 h.putDouble("pad_alt", pad_alt);
1723                 h.putInt("speak_tick", speak_tick);
1724                 h.putDouble("speak_altitude", speak_altitude);
1725                 h.putString("callsign", callsign);
1726                 h.putString("firmware_version", firmware_version);
1727                 h.putDouble("accel_plus_g", accel_plus_g);
1728                 h.putDouble("accel_minus_g", accel_minus_g);
1729                 h.putDouble("accel", accel);
1730                 h.putDouble("ground_accel", ground_accel);
1731                 h.putDouble("ground_accel_avg", ground_accel_avg);
1732                 h.putInt("log_format", log_format);
1733                 h.putString("product", product);
1734                 h.putHashable("baro", baro);
1735                 h.putHashable("companion", companion);
1736                 h.putInt("pyro_fired", pyro_fired);
1737                 h.putDouble("accel_zero_along", accel_zero_along);
1738                 h.putDouble("accel_zero_across", accel_zero_across);
1739                 h.putDouble("accel_zero_through", accel_zero_through);
1740
1741                 h.putHashable("rotation", rotation);
1742                 h.putHashable("ground_rotation", ground_rotation);
1743
1744                 h.putInt("pad_orientation", pad_orientation);
1745
1746                 h.putDouble("accel_ground_along", accel_ground_along);
1747                 h.putDouble("accel_ground_across", accel_ground_across);
1748                 h.putDouble("accel_ground_through", accel_ground_through);
1749
1750                 h.putDouble("gyro_zero_roll", gyro_zero_roll);
1751                 h.putDouble("gyro_zero_pitch", gyro_zero_pitch);
1752                 h.putDouble("gyro_zero_yaw", gyro_zero_yaw);
1753
1754                 h.putDouble("last_imu_time", last_imu_time);
1755                 return h;
1756         }
1757
1758         public AltosState(AltosHashSet h) {
1759                 this();
1760
1761                 set = h.getInt("set", set);
1762                 received_time = h.getLong("received_time", received_time);
1763                 time = h.getDouble("time", time);
1764                 prev_time = h.getDouble("prev_time", prev_time);
1765                 time_change = h.getDouble("time_change", time_change);
1766                 tick = h.getInt("tick", tick);
1767                 prev_tick = h.getInt("prev_tick", prev_tick);
1768                 boost_tick = h.getInt("boost_tick", boost_tick);
1769                 state = h.getInt("state", state);
1770                 flight = h.getInt("flight", flight);
1771                 serial = h.getInt("serial", serial);
1772                 altitude_32 = h.getInt("altitude_32", altitude_32);
1773                 receiver_serial = h.getInt("receiver_serial", receiver_serial);
1774                 landed = h.getBoolean("landed", landed);
1775                 ascent = h.getBoolean("ascent", ascent);
1776                 boost = h.getBoolean("boost", boost);
1777                 rssi = h.getInt("rssi", rssi);
1778                 status = h.getInt("status", status);
1779                 device_type = h.getInt("device_type", device_type);
1780                 config_major = h.getInt("config_major", config_major);
1781                 config_minor = h.getInt("config_minor", config_minor);
1782                 apogee_delay = h.getInt("apogee_delay", apogee_delay);
1783                 main_deploy = h.getInt("main_deploy", main_deploy);
1784                 flight_log_max = h.getInt("flight_log_max", flight_log_max);
1785                 ground_altitude = AltosCValue_fromHashSet(h.getHash("ground_altitude"), ground_altitude);
1786                 gps_ground_altitude = AltosGpsGroundAltitude_fromHashSet(h.getHash("gps_ground_altitude"), gps_ground_altitude);
1787                 ground_pressure = AltosGroundPressure_fromHashSet(h.getHash("ground_pressure"), ground_pressure);
1788                 altitude = AltosAltitude_fromHashSet(h.getHash("altitude"), altitude);
1789                 gps_altitude = AltosGpsAltitude_fromHashSet(h.getHash("gps_altitude"), gps_altitude);
1790                 gps_ground_speed = AltosValue_fromHashSet(h.getHash("gps_ground_speed"), gps_ground_speed);
1791                 gps_ascent_rate = AltosValue_fromHashSet(h.getHash("gps_ascent_rate"), gps_ascent_rate);
1792                 gps_course = AltosValue_fromHashSet(h.getHash("gps_course"), gps_course);
1793                 gps_speed = AltosValue_fromHashSet(h.getHash("gps_speed"), gps_speed);
1794                 pressure = AltosPressure_fromHashSet(h.getHash("pressure"), pressure);
1795                 speed = AltosSpeed_fromHashSet(h.getHash("speed"), speed);
1796                 acceleration = AltosAccel_fromHashSet(h.getHash("acceleration"), acceleration);
1797                 orient = AltosCValue_fromHashSet(h.getHash("orient"), orient);
1798                 kalman_height = AltosValue_fromHashSet(h.getHash("kalman_height"), kalman_height);
1799                 kalman_speed = AltosValue_fromHashSet(h.getHash("kalman_speed"), kalman_speed);
1800                 kalman_acceleration = AltosValue_fromHashSet(h.getHash("kalman_acceleration"), kalman_acceleration);
1801
1802                 battery_voltage = h.getDouble("battery_voltage", battery_voltage);
1803                 pyro_voltage = h.getDouble("pyro_voltage", pyro_voltage);
1804                 temperature = h.getDouble("temperature", temperature);
1805                 apogee_voltage = h.getDouble("apogee_voltage", apogee_voltage);
1806                 main_voltage=  h.getDouble("main_voltage", main_voltage);
1807                 ignitor_voltage = h.getDoubleArray("ignitor_voltage", ignitor_voltage);
1808                 gps = AltosGPS.fromHashSet(h.getHash("gps"), gps);
1809                 temp_gps = AltosGPS.fromHashSet(h.getHash("temp_gps"), temp_gps);
1810                 temp_gps_sat_tick = h.getInt("temp_gps_sat_tick", temp_gps_sat_tick);
1811                 gps_pending = h.getBoolean("gps_pending", gps_pending);
1812                 gps_sequence = h.getInt("gps_sequence", gps_sequence);
1813                 imu = AltosIMU.fromHashSet(h.getHash("imu"), imu);
1814                 mag = AltosMag.fromHashSet(h.getHash("mag"), mag);
1815
1816                 npad = h.getInt("npad", npad);
1817                 gps_waiting = h.getInt("gps_waiting", gps_waiting);
1818                 gps_ready = h.getBoolean("gps_ready", gps_ready);
1819                 ngps = h.getInt("ngps", ngps);
1820                 from_pad = AltosGreatCircle.fromHashSet(h.getHash("from_pad"), from_pad);
1821                 elevation = h.getDouble("elevation", elevation);
1822                 range = h.getDouble("range", range);
1823                 gps_height = h.getDouble("gps_height", gps_height);
1824                 pad_lat = h.getDouble("pad_lat", pad_lat);
1825                 pad_lon = h.getDouble("pad_lon", pad_lon);
1826                 pad_alt = h.getDouble("pad_alt", pad_alt);
1827                 speak_tick = h.getInt("speak_tick", speak_tick);
1828                 speak_altitude = h.getDouble("speak_altitude", speak_altitude);
1829                 callsign = h.getString("callsign", callsign);
1830                 firmware_version = h.getString("firmware_version", firmware_version);
1831                 accel_plus_g = h.getDouble("accel_plus_g", accel_plus_g);
1832                 accel_minus_g = h.getDouble("accel_minus_g", accel_minus_g);
1833                 accel = h.getDouble("accel", accel);
1834                 ground_accel = h.getDouble("ground_accel", ground_accel);
1835                 ground_accel_avg = h.getDouble("ground_accel_avg", ground_accel_avg);
1836                 log_format = h.getInt("log_format", log_format);
1837                 product = h.getString("product", product);
1838                 baro = AltosMs5607.fromHashSet(h.getHash("baro"), baro);
1839                 companion = AltosCompanion.fromHashSet(h.getHash("companion"), companion);
1840                 pyro_fired = h.getInt("pyro_fired", pyro_fired);
1841                 accel_zero_along = h.getDouble("accel_zero_along", accel_zero_along);
1842                 accel_zero_across = h.getDouble("accel_zero_across", accel_zero_across);
1843                 accel_zero_through = h.getDouble("accel_zero_through", accel_zero_through);
1844
1845                 rotation = AltosRotation.fromHashSet(h.getHash("rotation"), rotation);
1846                 ground_rotation = AltosRotation.fromHashSet(h.getHash("ground_rotation"), ground_rotation);
1847
1848                 pad_orientation = h.getInt("pad_orientation", pad_orientation);
1849
1850                 accel_ground_along = h.getDouble("accel_ground_along", accel_ground_along);
1851                 accel_ground_across = h.getDouble("accel_ground_across", accel_ground_across);
1852                 accel_ground_through = h.getDouble("accel_ground_through", accel_ground_through);
1853
1854                 gyro_zero_roll = h.getDouble("gyro_zero_roll", gyro_zero_roll);
1855                 gyro_zero_pitch = h.getDouble("gyro_zero_pitch", gyro_zero_pitch);
1856                 gyro_zero_yaw = h.getDouble("gyro_zero_yaw", gyro_zero_yaw);
1857
1858                 last_imu_time = h.getDouble("last_imu_time", last_imu_time);
1859         }
1860
1861         public static AltosState fromHashSet(AltosHashSet h) {
1862                 if (h == null)
1863                         return null;
1864                 if (!h.getBoolean("valid", false))
1865                         return null;
1866                 return new AltosState(h);
1867         }
1868 }