altoslib: Switch preserved state format to JSON
[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, AltosJsonable {
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                 public AltosJson json() {
192                         AltosJson j = new AltosJson();
193
194                         j.put("value", value);
195                         j.put("prev_value", prev_value);
196                         j.put("max_value", max_value);
197                         j.put("set_time", set_time);
198                         j.put("prev_set_time", prev_set_time);
199                         return j;
200                 }
201
202                 AltosValue(AltosHashSet h) {
203                         this();
204                         if (h != null) {
205                                 value = h.getDouble("value", value);
206                                 prev_value = h.getDouble("prev_value", prev_value);
207                                 max_value = h.getDouble("max_value", max_value);
208                                 set_time = h.getDouble("set_time", 0);
209                                 prev_set_time = h.getDouble("prev_set_time", 0);
210                         }
211                 }
212
213                 AltosValue(AltosJson j) {
214                         this();
215                         if (j != null) {
216                                 value = j.get_double("value", value);
217                                 prev_value = j.get_double("prev_value", prev_value);
218                                 max_value = j.get_double("max_value", max_value);
219                                 set_time = j.get_double("set_time", 0);
220                                 prev_set_time = j.get_double("prev_set_time", 0);
221                         }
222                 }
223
224                 AltosValue() {
225                         value = AltosLib.MISSING;
226                         prev_value = AltosLib.MISSING;
227                         max_value = AltosLib.MISSING;
228                 }
229
230         }
231
232         AltosValue AltosValue_fromHashSet(AltosHashSet h, AltosValue def) {
233                 if (h == null)
234                         return def;
235                 return new AltosValue(h);
236         }
237
238         AltosValue AltosValue_fromJson(AltosJson j, AltosValue def) {
239                 if (j == null)
240                         return def;
241                 return new AltosValue(j);
242         }
243
244         class AltosCValue implements AltosHashable, AltosJsonable {
245
246                 class AltosIValue extends AltosValue implements AltosHashable, AltosJsonable {
247                         boolean can_max() {
248                                 return c_can_max();
249                         }
250
251                         AltosIValue() {
252                                 super();
253                         }
254
255                         AltosIValue(AltosHashSet h) {
256                                 super(h);
257                         }
258
259                         AltosIValue(AltosJson j) {
260                                 super(j);
261                         }
262                 };
263
264                 public AltosIValue      measured;
265                 public AltosIValue      computed;
266
267                 boolean can_max() { return true; }
268
269                 boolean c_can_max() { return can_max(); }
270
271                 double value() {
272                         double v = measured.value();
273                         if (v != AltosLib.MISSING)
274                                 return v;
275                         return computed.value();
276                 }
277
278                 boolean is_measured() {
279                         return measured.value() != AltosLib.MISSING;
280                 }
281
282                 double max() {
283                         double m = measured.max();
284
285                         if (m != AltosLib.MISSING)
286                                 return m;
287                         return computed.max();
288                 }
289
290                 double prev_value() {
291                         if (measured.value != AltosLib.MISSING && measured.prev_value != AltosLib.MISSING)
292                                 return measured.prev_value;
293                         return computed.prev_value;
294                 }
295
296                 AltosValue altos_value() {
297                         if (measured.value() != AltosLib.MISSING)
298                                 return measured;
299                         return computed;
300                 }
301
302                 double change() {
303                         double c = measured.change();
304                         if (c == AltosLib.MISSING)
305                                 c = computed.change();
306                         return c;
307                 }
308
309                 double rate() {
310                         double r = measured.rate();
311                         if (r == AltosLib.MISSING)
312                                 r = computed.rate();
313                         return r;
314                 }
315
316                 void set_measured(double new_value, double time) {
317                         measured.set(new_value, time);
318                 }
319
320                 void set_computed(double new_value, double time) {
321                         computed.set(new_value, time);
322                 }
323
324                 void set_derivative(AltosValue in) {
325                         computed.set_derivative(in);
326                 }
327
328                 void set_derivative(AltosCValue in) {
329                         set_derivative(in.altos_value());
330                 }
331
332                 void set_integral(AltosValue in) {
333                         computed.set_integral(in);
334                 }
335
336                 void set_integral(AltosCValue in) {
337                         set_integral(in.altos_value());
338                 }
339
340                 void copy(AltosCValue old) {
341                         measured.copy(old.measured);
342                         computed.copy(old.computed);
343                 }
344
345                 void finish_update() {
346                         measured.finish_update();
347                         computed.finish_update();
348                 }
349
350                 AltosCValue() {
351                         measured = new AltosIValue();
352                         computed = new AltosIValue();
353                 }
354
355                 public AltosHashSet hashSet() {
356                         AltosHashSet h = new AltosHashSet();
357
358                         h.putHashable("measured", measured);
359                         h.putHashable("computed", computed);
360                         return h;
361                 }
362
363                 public AltosJson json() {
364                         AltosJson       j = new AltosJson();
365
366                         j.put("measured", measured.json());
367                         j.put("computed", computed.json());
368                         return j;
369                 }
370
371                 AltosCValue(AltosHashSet h) {
372                         measured = new AltosIValue(h.getHash("measured"));
373                         computed = new AltosIValue(h.getHash("computed"));
374                 }
375
376                 AltosCValue(AltosJson j) {
377                         measured = new AltosIValue(j.get("measured"));
378                         computed = new AltosIValue(j.get("computed"));
379                 }
380         }
381
382         AltosCValue AltosCValue_fromHashSet(AltosHashSet h, AltosCValue def) {
383                 if (h == null)
384                         return def;
385                 return new AltosCValue(h);
386         }
387
388         AltosCValue AltosCValue_fromJson(AltosJson j, AltosCValue def) {
389                 if (j == null)
390                         return def;
391                 return new AltosCValue(j);
392         }
393
394         private int     state;
395         public int      flight;
396         public int      serial;
397         public int      altitude_32;
398         public int      receiver_serial;
399         public boolean  landed;
400         public boolean  ascent; /* going up? */
401         public boolean  boost;  /* under power */
402         public int      rssi;
403         public int      status;
404         public int      device_type;
405         public int      config_major;
406         public int      config_minor;
407         public int      apogee_delay;
408         public int      main_deploy;
409         public int      flight_log_max;
410
411         private double pressure_to_altitude(double p) {
412                 if (p == AltosLib.MISSING)
413                         return AltosLib.MISSING;
414                 return AltosConvert.pressure_to_altitude(p);
415         }
416
417         private AltosCValue ground_altitude;
418
419         public double ground_altitude() {
420                 return ground_altitude.value();
421         }
422
423         public void set_ground_altitude(double a) {
424                 ground_altitude.set_measured(a, time);
425         }
426
427         class AltosGpsGroundAltitude extends AltosValue {
428                 void set(double a, double t) {
429                         super.set(a, t);
430                         pad_alt = value();
431                         gps_altitude.set_gps_height();
432                 }
433
434                 void set_filtered(double a, double t) {
435                         super.set_filtered(a, t);
436                         pad_alt = value();
437                         gps_altitude.set_gps_height();
438                 }
439
440                 AltosGpsGroundAltitude() {
441                         super();
442                 }
443
444                 AltosGpsGroundAltitude (AltosHashSet h) {
445                         super(h);
446                 }
447
448                 AltosGpsGroundAltitude (AltosJson j) {
449                         super(j);
450                 }
451         }
452
453         AltosGpsGroundAltitude AltosGpsGroundAltitude_fromHashSet(AltosHashSet h, AltosGpsGroundAltitude def) {
454                 if (h == null) return def;
455                 return new AltosGpsGroundAltitude(h);
456         }
457
458         AltosGpsGroundAltitude AltosGpsGroundAltitude_fromJson(AltosJson j, AltosGpsGroundAltitude def) {
459                 if (j == null) return def;
460                 return new AltosGpsGroundAltitude(j);
461         }
462
463         private AltosGpsGroundAltitude gps_ground_altitude;
464
465         public double gps_ground_altitude() {
466                 return gps_ground_altitude.value();
467         }
468
469         public void set_gps_ground_altitude(double a) {
470                 gps_ground_altitude.set(a, time);
471         }
472
473         class AltosGroundPressure extends AltosCValue {
474                 void set_filtered(double p, double time) {
475                         computed.set_filtered(p, time);
476                         if (!is_measured())
477                                 ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
478                 }
479
480                 void set_measured(double p, double time) {
481                         super.set_measured(p, time);
482                         ground_altitude.set_computed(pressure_to_altitude(p), time);
483                 }
484
485                 AltosGroundPressure () {
486                         super();
487                 }
488
489                 AltosGroundPressure (AltosHashSet h) {
490                         super(h);
491                 }
492
493                 AltosGroundPressure (AltosJson j) {
494                         super(j);
495                 }
496         }
497
498         AltosGroundPressure AltosGroundPressure_fromHashSet(AltosHashSet h, AltosGroundPressure def) {
499                 if (h == null) return def;
500                 return new AltosGroundPressure(h);
501         }
502
503         AltosGroundPressure AltosGroundPressure_fromJson(AltosJson j, AltosGroundPressure def) {
504                 if (j == null) return def;
505                 return new AltosGroundPressure(j);
506         }
507
508         private AltosGroundPressure ground_pressure;
509
510         public double ground_pressure() {
511                 return ground_pressure.value();
512         }
513
514         public void set_ground_pressure (double pressure) {
515                 ground_pressure.set_measured(pressure, time);
516         }
517
518         class AltosAltitude extends AltosCValue implements AltosHashable {
519
520                 private void set_speed(AltosValue v) {
521                         if (!acceleration.is_measured() || !ascent)
522                                 speed.set_derivative(this);
523                 }
524
525                 void set_computed(double a, double time) {
526                         super.set_computed(a,time);
527                         set_speed(computed);
528                         set |= set_position;
529                 }
530
531                 void set_measured(double a, double time) {
532                         super.set_measured(a,time);
533                         set_speed(measured);
534                         set |= set_position;
535                 }
536
537                 AltosAltitude() {
538                         super();
539                 }
540
541                 AltosAltitude (AltosHashSet h) {
542                         super(h);
543                 }
544
545                 AltosAltitude (AltosJson j) {
546                         super(j);
547                 }
548         }
549
550         AltosAltitude AltosAltitude_fromHashSet(AltosHashSet h, AltosAltitude def) {
551                 if (h == null) return def;
552                 return new AltosAltitude(h);
553         }
554
555         AltosAltitude AltosAltitude_fromJson(AltosJson j, AltosAltitude def) {
556                 if (j == null) return def;
557                 return new AltosAltitude(j);
558         }
559
560         private AltosAltitude   altitude;
561
562         class AltosGpsAltitude extends AltosValue implements AltosHashable {
563
564                 private void set_gps_height() {
565                         double  a = value();
566                         double  g = gps_ground_altitude.value();
567
568                         if (a != AltosLib.MISSING && g != AltosLib.MISSING)
569                                 gps_height = a - g;
570                         else
571                                 gps_height = AltosLib.MISSING;
572                 }
573
574                 void set(double a, double t) {
575                         super.set(a, t);
576                         set_gps_height();
577                 }
578
579                 AltosGpsAltitude() {
580                         super();
581                 }
582
583                 AltosGpsAltitude (AltosHashSet h) {
584                         super(h);
585                 }
586
587                 AltosGpsAltitude (AltosJson j) {
588                         super(j);
589                 }
590         }
591
592         AltosGpsAltitude AltosGpsAltitude_fromHashSet(AltosHashSet h, AltosGpsAltitude def) {
593                 if (h == null) return def;
594                 return new AltosGpsAltitude(h);
595         }
596
597         AltosGpsAltitude AltosGpsAltitude_fromJson(AltosJson j, AltosGpsAltitude def) {
598                 if (j == null) return def;
599                 return new AltosGpsAltitude(j);
600         }
601
602         private AltosGpsAltitude        gps_altitude;
603
604         private AltosValue              gps_ground_speed;
605         private AltosValue              gps_ascent_rate;
606         private AltosValue              gps_course;
607         private AltosValue              gps_speed;
608
609         public double altitude() {
610                 double a = altitude.value();
611                 if (a != AltosLib.MISSING)
612                         return a;
613                 return gps_altitude.value();
614         }
615
616         public double max_altitude() {
617                 double a = altitude.max();
618                 if (a != AltosLib.MISSING)
619                         return a;
620                 return gps_altitude.max();
621         }
622
623         public void set_altitude(double new_altitude) {
624                 altitude.set_measured(new_altitude, time);
625         }
626
627         public double gps_altitude() {
628                 return gps_altitude.value();
629         }
630
631         public double max_gps_altitude() {
632                 return gps_altitude.max();
633         }
634
635         public void set_gps_altitude(double new_gps_altitude) {
636                 gps_altitude.set(new_gps_altitude, time);
637         }
638
639         public double gps_ground_speed() {
640                 return gps_ground_speed.value();
641         }
642
643         public double max_gps_ground_speed() {
644                 return gps_ground_speed.max();
645         }
646
647         public double gps_ascent_rate() {
648                 return gps_ascent_rate.value();
649         }
650
651         public double max_gps_ascent_rate() {
652                 return gps_ascent_rate.max();
653         }
654
655         public double gps_course() {
656                 return gps_course.value();
657         }
658
659         public double gps_speed() {
660                 return gps_speed.value();
661         }
662
663         public double max_gps_speed() {
664                 return gps_speed.max();
665         }
666
667         class AltosPressure extends AltosValue {
668                 void set(double p, double time) {
669                         super.set(p, time);
670                         if (state == AltosLib.ao_flight_pad)
671                                 ground_pressure.set_filtered(p, time);
672                         double a = pressure_to_altitude(p);
673                         altitude.set_computed(a, time);
674                 }
675
676                 AltosPressure() {
677                         super();
678                 }
679
680                 AltosPressure (AltosHashSet h) {
681                         super(h);
682                 }
683
684                 AltosPressure (AltosJson j) {
685                         super(j);
686                 }
687         }
688
689         AltosPressure AltosPressure_fromHashSet(AltosHashSet h, AltosPressure def) {
690                 if (h == null) return def;
691                 return new AltosPressure(h);
692         }
693
694         AltosPressure AltosPressure_fromJson(AltosJson j, AltosPressure def) {
695                 if (j == null) return def;
696                 return new AltosPressure(j);
697         }
698
699         private AltosPressure   pressure;
700
701         public double pressure() {
702                 return pressure.value();
703         }
704
705         public void set_pressure(double p) {
706                 pressure.set(p, time);
707         }
708
709         public double baro_height() {
710                 double a = altitude();
711                 double g = ground_altitude();
712                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
713                         return a - g;
714                 return AltosLib.MISSING;
715         }
716
717         public double height() {
718                 double k = kalman_height.value();
719                 if (k != AltosLib.MISSING)
720                         return k;
721
722                 double b = baro_height();
723                 if (b != AltosLib.MISSING)
724                         return b;
725
726                 return gps_height();
727         }
728
729         public double max_height() {
730                 double  k = kalman_height.max();
731                 if (k != AltosLib.MISSING)
732                         return k;
733
734                 double a = altitude.max();
735                 double g = ground_altitude();
736                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
737                         return a - g;
738                 return max_gps_height();
739         }
740
741         public double gps_height() {
742                 double a = gps_altitude();
743                 double g = gps_ground_altitude();
744
745                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
746                         return a - g;
747                 return AltosLib.MISSING;
748         }
749
750         public double max_gps_height() {
751                 double a = gps_altitude.max();
752                 double g = gps_ground_altitude();
753
754                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
755                         return a - g;
756                 return AltosLib.MISSING;
757         }
758
759         class AltosSpeed extends AltosCValue implements AltosHashable {
760
761                 boolean can_max() {
762                         return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
763                 }
764
765                 void set_accel() {
766                         acceleration.set_derivative(this);
767                 }
768
769                 void set_derivative(AltosCValue in) {
770                         super.set_derivative(in);
771                         set_accel();
772                 }
773
774                 void set_computed(double new_value, double time) {
775                         super.set_computed(new_value, time);
776                         set_accel();
777                 }
778
779                 void set_measured(double new_value, double time) {
780                         super.set_measured(new_value, time);
781                         set_accel();
782                 }
783
784                 AltosSpeed() {
785                         super();
786                 }
787
788                 AltosSpeed (AltosHashSet h) {
789                         super(h);
790                 }
791
792                 AltosSpeed (AltosJson j) {
793                         super(j);
794                 }
795         }
796
797         AltosSpeed AltosSpeed_fromHashSet(AltosHashSet h, AltosSpeed def) {
798                 if (h == null) return def;
799                 return new AltosSpeed(h);
800         }
801
802         AltosSpeed AltosSpeed_fromJson(AltosJson j, AltosSpeed def) {
803                 if (j == null) return def;
804                 return new AltosSpeed(j);
805         }
806
807         private AltosSpeed speed;
808
809         public double speed() {
810                 double v = kalman_speed.value();
811                 if (v != AltosLib.MISSING)
812                         return v;
813                 v = speed.value();
814                 if (v != AltosLib.MISSING)
815                         return v;
816                 v = gps_speed();
817                 if (v != AltosLib.MISSING)
818                         return v;
819                 return AltosLib.MISSING;
820         }
821
822         public double max_speed() {
823                 double v = kalman_speed.max();
824                 if (v != AltosLib.MISSING)
825                         return v;
826                 v = speed.max();
827                 if (v != AltosLib.MISSING)
828                         return v;
829                 v = max_gps_speed();
830                 if (v != AltosLib.MISSING)
831                         return v;
832                 return AltosLib.MISSING;
833         }
834
835         class AltosAccel extends AltosCValue implements AltosHashable {
836
837                 boolean can_max() {
838                         return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
839                 }
840
841                 void set_measured(double a, double time) {
842                         super.set_measured(a, time);
843                         if (ascent)
844                                 speed.set_integral(this.measured);
845                 }
846
847                 AltosAccel() {
848                         super();
849                 }
850
851                 AltosAccel (AltosHashSet h) {
852                         super(h);
853                 }
854
855                 AltosAccel (AltosJson j) {
856                         super(j);
857                 }
858         }
859
860         AltosAccel AltosAccel_fromHashSet(AltosHashSet h, AltosAccel def) {
861                 if (h == null) return def;
862                 return new AltosAccel(h);
863         }
864
865         AltosAccel AltosAccel_fromJson(AltosJson j, AltosAccel def) {
866                 if (j == null) return def;
867                 return new AltosAccel(j);
868         }
869
870         AltosAccel acceleration;
871
872         public double acceleration() {
873                 return acceleration.value();
874         }
875
876         public double max_acceleration() {
877                 return acceleration.max();
878         }
879
880         public AltosCValue      orient;
881
882         public void set_orient(double new_orient) {
883                 orient.set_measured(new_orient, time);
884         }
885
886         public double orient() {
887                 return orient.value();
888         }
889
890         public double max_orient() {
891                 return orient.max();
892         }
893
894         public AltosValue       kalman_height, kalman_speed, kalman_acceleration;
895
896         public void set_kalman(double height, double speed, double acceleration) {
897                 kalman_height.set(height, time);
898                 kalman_speed.set(speed, time);
899                 kalman_acceleration.set(acceleration, time);
900         }
901
902         public double   battery_voltage;
903         public double   pyro_voltage;
904         public double   temperature;
905         public double   apogee_voltage;
906         public double   main_voltage;
907
908         public double   ignitor_voltage[];
909
910         public AltosGPS gps;
911         public AltosGPS temp_gps;
912         public int temp_gps_sat_tick;
913         public boolean  gps_pending;
914         public int gps_sequence;
915
916         public AltosIMU imu;
917         public AltosMag mag;
918
919         public static final int MIN_PAD_SAMPLES = 10;
920
921         public int      npad;
922         public int      gps_waiting;
923         public boolean  gps_ready;
924
925         public int      ngps;
926
927         public AltosGreatCircle from_pad;
928         public double   elevation;      /* from pad */
929         public double   range;          /* total distance */
930
931         public double   gps_height;
932
933         public double pad_lat, pad_lon, pad_alt;
934
935         public int      speak_tick;
936         public double   speak_altitude;
937
938         public String   callsign;
939         public String   firmware_version;
940
941         public double   accel_plus_g;
942         public double   accel_minus_g;
943         public double   accel;
944         public double   ground_accel;
945         public double   ground_accel_avg;
946
947         public int      log_format;
948         public int      log_space;
949         public String   product;
950
951         public AltosMs5607      baro;
952
953         public AltosCompanion   companion;
954
955         public int      pyro_fired;
956
957         public void set_npad(int npad) {
958                 this.npad = npad;
959                 gps_waiting = MIN_PAD_SAMPLES - npad;
960                 if (this.gps_waiting < 0)
961                         gps_waiting = 0;
962                 gps_ready = gps_waiting == 0;
963         }
964
965         public void init() {
966                 set = 0;
967
968                 received_time = System.currentTimeMillis();
969                 time = AltosLib.MISSING;
970                 time_change = AltosLib.MISSING;
971                 prev_time = AltosLib.MISSING;
972                 tick = AltosLib.MISSING;
973                 prev_tick = AltosLib.MISSING;
974                 boost_tick = AltosLib.MISSING;
975                 state = AltosLib.ao_flight_invalid;
976                 flight = AltosLib.MISSING;
977                 landed = false;
978                 boost = false;
979                 rssi = AltosLib.MISSING;
980                 status = 0;
981                 device_type = AltosLib.MISSING;
982                 config_major = AltosLib.MISSING;
983                 config_minor = AltosLib.MISSING;
984                 apogee_delay = AltosLib.MISSING;
985                 main_deploy = AltosLib.MISSING;
986                 flight_log_max = AltosLib.MISSING;
987
988                 ground_altitude = new AltosCValue();
989                 ground_pressure = new AltosGroundPressure();
990                 altitude = new AltosAltitude();
991                 pressure = new AltosPressure();
992                 speed = new AltosSpeed();
993                 acceleration = new AltosAccel();
994                 orient = new AltosCValue();
995
996                 temperature = AltosLib.MISSING;
997                 battery_voltage = AltosLib.MISSING;
998                 pyro_voltage = AltosLib.MISSING;
999                 apogee_voltage = AltosLib.MISSING;
1000                 main_voltage = AltosLib.MISSING;
1001                 ignitor_voltage = null;
1002
1003                 kalman_height = new AltosValue();
1004                 kalman_speed = new AltosValue();
1005                 kalman_acceleration = new AltosValue();
1006
1007                 gps = null;
1008                 temp_gps = null;
1009                 temp_gps_sat_tick = 0;
1010                 gps_sequence = 0;
1011                 gps_pending = false;
1012
1013                 imu = null;
1014                 last_imu_time = AltosLib.MISSING;
1015                 rotation = null;
1016                 ground_rotation = null;
1017
1018                 mag = null;
1019                 accel_zero_along = AltosLib.MISSING;
1020                 accel_zero_across = AltosLib.MISSING;
1021                 accel_zero_through = AltosLib.MISSING;
1022
1023                 accel_ground_along = AltosLib.MISSING;
1024                 accel_ground_across = AltosLib.MISSING;
1025                 accel_ground_through = AltosLib.MISSING;
1026
1027                 pad_orientation = AltosLib.MISSING;
1028
1029                 gyro_zero_roll = AltosLib.MISSING;
1030                 gyro_zero_pitch = AltosLib.MISSING;
1031                 gyro_zero_yaw = AltosLib.MISSING;
1032
1033                 set_npad(0);
1034                 ngps = 0;
1035
1036                 from_pad = null;
1037                 elevation = AltosLib.MISSING;
1038                 range = AltosLib.MISSING;
1039                 gps_height = AltosLib.MISSING;
1040
1041                 pad_lat = AltosLib.MISSING;
1042                 pad_lon = AltosLib.MISSING;
1043                 pad_alt = AltosLib.MISSING;
1044
1045                 gps_altitude = new AltosGpsAltitude();
1046                 gps_ground_altitude = new AltosGpsGroundAltitude();
1047                 gps_ground_speed = new AltosValue();
1048                 gps_speed = new AltosValue();
1049                 gps_ascent_rate = new AltosValue();
1050                 gps_course = new AltosValue();
1051
1052                 speak_tick = AltosLib.MISSING;
1053                 speak_altitude = AltosLib.MISSING;
1054
1055                 callsign = null;
1056                 firmware_version = null;
1057
1058                 accel_plus_g = AltosLib.MISSING;
1059                 accel_minus_g = AltosLib.MISSING;
1060                 accel = AltosLib.MISSING;
1061
1062                 ground_accel = AltosLib.MISSING;
1063                 ground_accel_avg = AltosLib.MISSING;
1064
1065                 log_format = AltosLib.MISSING;
1066                 log_space = AltosLib.MISSING;
1067                 product = null;
1068                 serial = AltosLib.MISSING;
1069                 receiver_serial = AltosLib.MISSING;
1070                 altitude_32 = AltosLib.MISSING;
1071
1072                 baro = null;
1073                 companion = null;
1074
1075                 pyro_fired = 0;
1076         }
1077
1078         void finish_update() {
1079                 prev_tick = tick;
1080
1081                 ground_altitude.finish_update();
1082                 altitude.finish_update();
1083                 pressure.finish_update();
1084                 speed.finish_update();
1085                 acceleration.finish_update();
1086                 orient.finish_update();
1087
1088                 kalman_height.finish_update();
1089                 kalman_speed.finish_update();
1090                 kalman_acceleration.finish_update();
1091         }
1092
1093         void copy(AltosState old) {
1094
1095                 if (old == null) {
1096                         init();
1097                         return;
1098                 }
1099
1100                 received_time = old.received_time;
1101                 time = old.time;
1102                 time_change = old.time_change;
1103                 prev_time = old.time;
1104
1105                 tick = old.tick;
1106                 prev_tick = old.tick;
1107                 boost_tick = old.boost_tick;
1108
1109                 state = old.state;
1110                 flight = old.flight;
1111                 landed = old.landed;
1112                 ascent = old.ascent;
1113                 boost = old.boost;
1114                 rssi = old.rssi;
1115                 status = old.status;
1116                 device_type = old.device_type;
1117                 config_major = old.config_major;
1118                 config_minor = old.config_minor;
1119                 apogee_delay = old.apogee_delay;
1120                 main_deploy = old.main_deploy;
1121                 flight_log_max = old.flight_log_max;
1122
1123                 set = 0;
1124
1125                 ground_pressure.copy(old.ground_pressure);
1126                 ground_altitude.copy(old.ground_altitude);
1127                 altitude.copy(old.altitude);
1128                 pressure.copy(old.pressure);
1129                 speed.copy(old.speed);
1130                 acceleration.copy(old.acceleration);
1131                 orient.copy(old.orient);
1132
1133                 battery_voltage = old.battery_voltage;
1134                 pyro_voltage = old.pyro_voltage;
1135                 temperature = old.temperature;
1136                 apogee_voltage = old.apogee_voltage;
1137                 main_voltage = old.main_voltage;
1138                 ignitor_voltage = old.ignitor_voltage;
1139
1140                 kalman_height.copy(old.kalman_height);
1141                 kalman_speed.copy(old.kalman_speed);
1142                 kalman_acceleration.copy(old.kalman_acceleration);
1143
1144                 if (old.gps != null)
1145                         gps = old.gps.clone();
1146                 else
1147                         gps = null;
1148                 if (old.temp_gps != null)
1149                         temp_gps = old.temp_gps.clone();
1150                 else
1151                         temp_gps = null;
1152                 temp_gps_sat_tick = old.temp_gps_sat_tick;
1153                 gps_sequence = old.gps_sequence;
1154                 gps_pending = old.gps_pending;
1155
1156                 if (old.imu != null)
1157                         imu = old.imu.clone();
1158                 else
1159                         imu = null;
1160                 last_imu_time = old.last_imu_time;
1161
1162                 if (old.rotation != null)
1163                         rotation = new AltosRotation (old.rotation);
1164
1165                 if (old.ground_rotation != null) {
1166                         ground_rotation = new AltosRotation(old.ground_rotation);
1167                 }
1168
1169                 accel_zero_along = old.accel_zero_along;
1170                 accel_zero_across = old.accel_zero_across;
1171                 accel_zero_through = old.accel_zero_through;
1172
1173                 accel_ground_along = old.accel_ground_along;
1174                 accel_ground_across = old.accel_ground_across;
1175                 accel_ground_through = old.accel_ground_through;
1176                 pad_orientation = old.pad_orientation;
1177
1178                 gyro_zero_roll = old.gyro_zero_roll;
1179                 gyro_zero_pitch = old.gyro_zero_pitch;
1180                 gyro_zero_yaw = old.gyro_zero_yaw;
1181
1182                 if (old.mag != null)
1183                         mag = old.mag.clone();
1184                 else
1185                         mag = null;
1186
1187                 npad = old.npad;
1188                 gps_waiting = old.gps_waiting;
1189                 gps_ready = old.gps_ready;
1190                 ngps = old.ngps;
1191
1192                 if (old.from_pad != null)
1193                         from_pad = old.from_pad.clone();
1194                 else
1195                         from_pad = null;
1196
1197                 elevation = old.elevation;
1198                 range = old.range;
1199
1200                 gps_height = old.gps_height;
1201
1202                 gps_altitude.copy(old.gps_altitude);
1203                 gps_ground_altitude.copy(old.gps_ground_altitude);
1204                 gps_ground_speed.copy(old.gps_ground_speed);
1205                 gps_ascent_rate.copy(old.gps_ascent_rate);
1206                 gps_course.copy(old.gps_course);
1207                 gps_speed.copy(old.gps_speed);
1208
1209                 pad_lat = old.pad_lat;
1210                 pad_lon = old.pad_lon;
1211                 pad_alt = old.pad_alt;
1212
1213                 speak_tick = old.speak_tick;
1214                 speak_altitude = old.speak_altitude;
1215
1216                 callsign = old.callsign;
1217                 firmware_version = old.firmware_version;
1218
1219                 accel_plus_g = old.accel_plus_g;
1220                 accel_minus_g = old.accel_minus_g;
1221                 accel = old.accel;
1222                 ground_accel = old.ground_accel;
1223                 ground_accel_avg = old.ground_accel_avg;
1224
1225                 log_format = old.log_format;
1226                 log_space = old.log_space;
1227                 product = old.product;
1228                 serial = old.serial;
1229                 receiver_serial = old.receiver_serial;
1230                 altitude_32 = old.altitude_32;
1231
1232                 baro = old.baro;
1233                 companion = old.companion;
1234
1235                 pyro_fired = old.pyro_fired;
1236         }
1237
1238         void update_time() {
1239         }
1240
1241         void update_gps() {
1242                 elevation = AltosLib.MISSING;
1243                 range = AltosLib.MISSING;
1244
1245                 if (gps == null)
1246                         return;
1247
1248                 if (gps.locked && gps.nsat >= 4) {
1249                         /* Track consecutive 'good' gps reports, waiting for 10 of them */
1250                         if (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_stateless) {
1251                                 set_npad(npad+1);
1252                                 if (pad_lat != AltosLib.MISSING && (npad < 10 || state == AltosLib.ao_flight_pad)) {
1253                                         pad_lat = (pad_lat * 31 + gps.lat) / 32;
1254                                         pad_lon = (pad_lon * 31 + gps.lon) / 32;
1255                                         gps_ground_altitude.set_filtered(gps.alt, time);
1256                                 }
1257                         }
1258                         if (pad_lat == AltosLib.MISSING) {
1259                                 pad_lat = gps.lat;
1260                                 pad_lon = gps.lon;
1261                                 gps_ground_altitude.set(gps.alt, time);
1262                         }
1263                         gps_altitude.set(gps.alt, time);
1264                         if (gps.climb_rate != AltosLib.MISSING)
1265                                 gps_ascent_rate.set(gps.climb_rate, time);
1266                         if (gps.ground_speed != AltosLib.MISSING)
1267                                 gps_ground_speed.set(gps.ground_speed, time);
1268                         if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
1269                                 gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
1270                                                         gps.climb_rate * gps.climb_rate), time);
1271                         if (gps.course != AltosLib.MISSING)
1272                                 gps_course.set(gps.course, time);
1273                 }
1274                 if (gps.lat != 0 && gps.lon != 0 &&
1275                     pad_lat != AltosLib.MISSING &&
1276                     pad_lon != AltosLib.MISSING)
1277                 {
1278                         double h = height();
1279
1280                         if (h == AltosLib.MISSING)
1281                                 h = 0;
1282                         from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
1283                         elevation = from_pad.elevation;
1284                         range = from_pad.range;
1285                 }
1286         }
1287
1288         public void set_tick(int new_tick) {
1289                 if (new_tick != AltosLib.MISSING) {
1290                         if (prev_tick != AltosLib.MISSING) {
1291                                 while (new_tick < prev_tick - 1000) {
1292                                         new_tick += 65536;
1293                                 }
1294                         }
1295                         tick = new_tick;
1296                         time = tick / 100.0;
1297                         time_change = time - prev_time;
1298                 }
1299         }
1300
1301         public void set_boost_tick(int boost_tick) {
1302                 if (boost_tick != AltosLib.MISSING)
1303                         this.boost_tick = boost_tick;
1304         }
1305
1306         public String state_name() {
1307                 return AltosLib.state_name(state);
1308         }
1309
1310         public void set_product(String product) {
1311                 this.product = product;
1312         }
1313
1314         public void set_state(int state) {
1315                 if (state != AltosLib.ao_flight_invalid) {
1316                         this.state = state;
1317                         ascent = (AltosLib.ao_flight_boost <= state &&
1318                                   state <= AltosLib.ao_flight_coast);
1319                         boost = (AltosLib.ao_flight_boost == state);
1320                 }
1321         }
1322
1323         public int state() {
1324                 return state;
1325         }
1326
1327         public void set_device_type(int device_type) {
1328                 this.device_type = device_type;
1329                 switch (device_type) {
1330                 case AltosLib.product_telegps:
1331                         this.state = AltosLib.ao_flight_stateless;
1332                         break;
1333                 }
1334         }
1335
1336         public void set_log_format(int log_format) {
1337                 this.log_format = log_format;
1338                 switch (log_format) {
1339                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
1340                         this.state = AltosLib.ao_flight_stateless;
1341                         break;
1342                 }
1343         }
1344
1345         public void set_log_space(int log_space) {
1346                 this.log_space = log_space;
1347         }
1348
1349         public void set_flight_params(int apogee_delay, int main_deploy) {
1350                 this.apogee_delay = apogee_delay;
1351                 this.main_deploy = main_deploy;
1352         }
1353
1354         public void set_config(int major, int minor, int flight_log_max) {
1355                 config_major = major;
1356                 config_minor = minor;
1357                 this.flight_log_max = flight_log_max;
1358         }
1359
1360         public void set_callsign(String callsign) {
1361                 this.callsign = callsign;
1362         }
1363
1364         public void set_firmware_version(String version) {
1365                 firmware_version = version;
1366         }
1367
1368         public int compare_version(String other_version) {
1369                 if (firmware_version == null)
1370                         return AltosLib.MISSING;
1371                 return AltosLib.compare_version(firmware_version, other_version);
1372         }
1373
1374         private void re_init() {
1375                 int bt = boost_tick;
1376                 int rs = receiver_serial;
1377                 init();
1378                 boost_tick = bt;
1379                 receiver_serial = rs;
1380         }
1381
1382         public void set_flight(int flight) {
1383
1384                 /* When the flight changes, reset the state */
1385                 if (flight != AltosLib.MISSING) {
1386                         if (this.flight != AltosLib.MISSING &&
1387                             this.flight != flight) {
1388                                 re_init();
1389                         }
1390                         this.flight = flight;
1391                 }
1392         }
1393
1394         public void set_serial(int serial) {
1395                 /* When the serial changes, reset the state */
1396                 if (serial != AltosLib.MISSING) {
1397                         if (this.serial != AltosLib.MISSING &&
1398                             this.serial != serial) {
1399                                 re_init();
1400                         }
1401                         this.serial = serial;
1402                 }
1403         }
1404
1405         public void set_receiver_serial(int serial) {
1406                 if (serial != AltosLib.MISSING)
1407                         receiver_serial = serial;
1408         }
1409
1410         public boolean altitude_32() {
1411                 return altitude_32 == 1;
1412         }
1413
1414         public void set_altitude_32(int altitude_32) {
1415                 if (altitude_32 != AltosLib.MISSING)
1416                         this.altitude_32 = altitude_32;
1417         }
1418
1419         public int rssi() {
1420                 if (rssi == AltosLib.MISSING)
1421                         return 0;
1422                 return rssi;
1423         }
1424
1425         public void set_rssi(int rssi, int status) {
1426                 if (rssi != AltosLib.MISSING) {
1427                         this.rssi = rssi;
1428                         this.status = status;
1429                 }
1430         }
1431
1432         public void set_received_time(long ms) {
1433                 received_time = ms;
1434         }
1435
1436         public void set_gps(AltosGPS gps, int sequence) {
1437                 if (gps != null) {
1438                         this.gps = gps.clone();
1439                         gps_sequence = sequence;
1440                         update_gps();
1441                         set |= set_gps;
1442                 }
1443         }
1444
1445
1446         public double   accel_zero_along;
1447         public double   accel_zero_across;
1448         public double   accel_zero_through;
1449
1450         public AltosRotation    rotation;
1451         public AltosRotation    ground_rotation;
1452
1453         public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
1454                 if (zero_along != AltosLib.MISSING) {
1455                         accel_zero_along = zero_along;
1456                         accel_zero_across = zero_across;
1457                         accel_zero_through = zero_through;
1458                 }
1459         }
1460
1461         public int pad_orientation;
1462
1463         public double   accel_ground_along, accel_ground_across, accel_ground_through;
1464
1465         void update_pad_rotation() {
1466                 if (pad_orientation != AltosLib.MISSING && accel_ground_along != AltosLib.MISSING) {
1467                         rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - accel_zero_across),
1468                                                      AltosIMU.convert_accel(accel_ground_through - accel_zero_through),
1469                                                      AltosIMU.convert_accel(accel_ground_along - accel_zero_along),
1470                                                      pad_orientation);
1471                         ground_rotation = rotation;
1472                         orient.set_computed(rotation.tilt(), time);
1473                 }
1474         }
1475
1476         public void set_accel_ground(double ground_along, double ground_across, double ground_through) {
1477                 accel_ground_along = ground_along;
1478                 accel_ground_across = ground_across;
1479                 accel_ground_through = ground_through;
1480                 update_pad_rotation();
1481         }
1482
1483         public void set_pad_orientation(int pad_orientation) {
1484                 this.pad_orientation = pad_orientation;
1485                 update_pad_rotation();
1486         }
1487
1488         public double   gyro_zero_roll;
1489         public double   gyro_zero_pitch;
1490         public double   gyro_zero_yaw;
1491
1492         public void set_gyro_zero(double roll, double pitch, double yaw) {
1493                 if (roll != AltosLib.MISSING) {
1494                         gyro_zero_roll = roll;
1495                         gyro_zero_pitch = pitch;
1496                         gyro_zero_yaw = yaw;
1497                 }
1498         }
1499
1500         public double   last_imu_time;
1501
1502         private double radians(double degrees) {
1503                 if (degrees == AltosLib.MISSING)
1504                         return AltosLib.MISSING;
1505                 return degrees * Math.PI / 180.0;
1506         }
1507
1508         private void update_orient() {
1509                 if (last_imu_time != AltosLib.MISSING) {
1510                         double  t = time - last_imu_time;
1511
1512                         double  pitch = radians(gyro_pitch());
1513                         double  yaw = radians(gyro_yaw());
1514                         double  roll = radians(gyro_roll());
1515
1516                         if (t > 0 & pitch != AltosLib.MISSING && rotation != null) {
1517                                 rotation.rotate(t, pitch, yaw, roll);
1518                                 orient.set_computed(rotation.tilt(), time);
1519                         }
1520                 }
1521                 last_imu_time = time;
1522         }
1523
1524         public void set_imu(AltosIMU imu) {
1525                 if (imu != null)
1526                         imu = imu.clone();
1527                 this.imu = imu;
1528                 update_orient();
1529         }
1530
1531         private double gyro_zero_overflow(double first) {
1532                 double v = first / 128.0;
1533                 if (v < 0)
1534                         v = Math.ceil(v);
1535                 else
1536                         v = Math.floor(v);
1537                 return v * 128.0;
1538         }
1539
1540         public void check_imu_wrap(AltosIMU imu) {
1541                 if (this.imu == null) {
1542                         gyro_zero_roll += gyro_zero_overflow(imu.gyro_roll);
1543                         gyro_zero_pitch += gyro_zero_overflow(imu.gyro_pitch);
1544                         gyro_zero_yaw += gyro_zero_overflow(imu.gyro_yaw);
1545                 }
1546         }
1547
1548         public double accel_along() {
1549                 if (imu != null && accel_zero_along != AltosLib.MISSING)
1550                         return AltosIMU.convert_accel(imu.accel_along - accel_zero_along);
1551                 return AltosLib.MISSING;
1552         }
1553
1554         public double accel_across() {
1555                 if (imu != null && accel_zero_across != AltosLib.MISSING)
1556                         return AltosIMU.convert_accel(imu.accel_across - accel_zero_across);
1557                 return AltosLib.MISSING;
1558         }
1559
1560         public double accel_through() {
1561                 if (imu != null && accel_zero_through != AltosLib.MISSING)
1562                         return AltosIMU.convert_accel(imu.accel_through - accel_zero_through);
1563                 return AltosLib.MISSING;
1564         }
1565
1566         public double gyro_roll() {
1567                 if (imu != null && gyro_zero_roll != AltosLib.MISSING) {
1568                         return AltosIMU.convert_gyro(imu.gyro_roll - gyro_zero_roll);
1569                 }
1570                 return AltosLib.MISSING;
1571         }
1572
1573         public double gyro_pitch() {
1574                 if (imu != null && gyro_zero_pitch != AltosLib.MISSING) {
1575                         return AltosIMU.convert_gyro(imu.gyro_pitch - gyro_zero_pitch);
1576                 }
1577                 return AltosLib.MISSING;
1578         }
1579
1580         public double gyro_yaw() {
1581                 if (imu != null && gyro_zero_yaw != AltosLib.MISSING) {
1582                         return AltosIMU.convert_gyro(imu.gyro_yaw - gyro_zero_yaw);
1583                 }
1584                 return AltosLib.MISSING;
1585         }
1586
1587         public void set_mag(AltosMag mag) {
1588                 this.mag = mag.clone();
1589         }
1590
1591         public double mag_along() {
1592                 if (mag != null)
1593                         return AltosMag.convert_gauss(mag.along);
1594                 return AltosLib.MISSING;
1595         }
1596
1597         public double mag_across() {
1598                 if (mag != null)
1599                         return AltosMag.convert_gauss(mag.across);
1600                 return AltosLib.MISSING;
1601         }
1602
1603         public double mag_through() {
1604                 if (mag != null)
1605                         return AltosMag.convert_gauss(mag.through);
1606                 return AltosLib.MISSING;
1607         }
1608
1609         public AltosMs5607 make_baro() {
1610                 if (baro == null)
1611                         baro = new AltosMs5607();
1612                 return baro;
1613         }
1614
1615         public void set_ms5607(AltosMs5607 ms5607) {
1616                 baro = ms5607;
1617
1618                 if (baro != null) {
1619                         set_pressure(baro.pa);
1620                         set_temperature(baro.cc / 100.0);
1621                 }
1622         }
1623
1624         public void set_ms5607(int pres, int temp) {
1625                 if (baro != null) {
1626                         baro.set(pres, temp);
1627
1628                         set_pressure(baro.pa);
1629                         set_temperature(baro.cc / 100.0);
1630                 }
1631         }
1632
1633         public void set_companion(AltosCompanion companion) {
1634                 this.companion = companion;
1635         }
1636
1637         void update_accel() {
1638                 if (accel == AltosLib.MISSING)
1639                         return;
1640                 if (accel_plus_g == AltosLib.MISSING)
1641                         return;
1642                 if (accel_minus_g == AltosLib.MISSING)
1643                         return;
1644
1645                 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
1646                 double counts_per_mss = counts_per_g / 9.80665;
1647                 acceleration.set_measured((accel_plus_g - accel) / counts_per_mss, time);
1648         }
1649
1650         public void set_accel_g(double accel_plus_g, double accel_minus_g) {
1651                 if (accel_plus_g != AltosLib.MISSING) {
1652                         this.accel_plus_g = accel_plus_g;
1653                         this.accel_minus_g = accel_minus_g;
1654                         update_accel();
1655                 }
1656         }
1657
1658         public void set_ground_accel(double ground_accel) {
1659                 if (ground_accel != AltosLib.MISSING)
1660                         this.ground_accel = ground_accel;
1661         }
1662
1663         public void set_accel(double accel) {
1664                 if (accel != AltosLib.MISSING) {
1665                         this.accel = accel;
1666                         if (state == AltosLib.ao_flight_pad) {
1667                                 if (ground_accel_avg == AltosLib.MISSING)
1668                                         ground_accel_avg = accel;
1669                                 else
1670                                         ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
1671                         }
1672                 }
1673                 update_accel();
1674         }
1675
1676         public void set_temperature(double temperature) {
1677                 if (temperature != AltosLib.MISSING) {
1678                         this.temperature = temperature;
1679                         set |= set_data;
1680                 }
1681         }
1682
1683         public void set_battery_voltage(double battery_voltage) {
1684                 if (battery_voltage != AltosLib.MISSING) {
1685                         this.battery_voltage = battery_voltage;
1686                         set |= set_data;
1687                 }
1688         }
1689
1690         public void set_pyro_voltage(double pyro_voltage) {
1691                 if (pyro_voltage != AltosLib.MISSING) {
1692                         this.pyro_voltage = pyro_voltage;
1693                         set |= set_data;
1694                 }
1695         }
1696
1697         public void set_apogee_voltage(double apogee_voltage) {
1698                 if (apogee_voltage != AltosLib.MISSING) {
1699                         this.apogee_voltage = apogee_voltage;
1700                         set |= set_data;
1701                 }
1702         }
1703
1704         public void set_main_voltage(double main_voltage) {
1705                 if (main_voltage != AltosLib.MISSING) {
1706                         this.main_voltage = main_voltage;
1707                         set |= set_data;
1708                 }
1709         }
1710
1711         public void set_ignitor_voltage(double[] voltage) {
1712                 this.ignitor_voltage = voltage;
1713         }
1714
1715         public void set_pyro_fired(int fired) {
1716                 this.pyro_fired = fired;
1717         }
1718
1719         public double time_since_boost() {
1720                 if (tick == AltosLib.MISSING)
1721                         return 0.0;
1722
1723                 if (boost_tick == AltosLib.MISSING)
1724                         return tick / 100.0;
1725                 return (tick - boost_tick) / 100.0;
1726         }
1727
1728         public boolean valid() {
1729                 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1730         }
1731
1732         public AltosGPS make_temp_gps(boolean sats) {
1733                 if (temp_gps == null) {
1734                         temp_gps = new AltosGPS(gps);
1735                 }
1736                 gps_pending = true;
1737                 if (sats) {
1738                         if (tick != temp_gps_sat_tick)
1739                                 temp_gps.cc_gps_sat = null;
1740                         temp_gps_sat_tick = tick;
1741                 }
1742                 return temp_gps;
1743         }
1744
1745         public void set_temp_gps() {
1746                 set_gps(temp_gps, gps_sequence + 1);
1747                 gps_pending = false;
1748                 temp_gps = null;
1749         }
1750
1751         public AltosState clone() {
1752                 AltosState s = new AltosState();
1753                 s.copy(this);
1754
1755                 if (false) {
1756                         AltosJson       json = json();
1757                         String          onetrip = json.toPrettyString();
1758                         AltosJson       back = AltosJson.fromString(onetrip);
1759                         AltosState      tripstate = AltosState.fromJson(back);
1760                         AltosJson       tripjson = tripstate.json();
1761                         String          twotrip = tripjson.toPrettyString();
1762
1763                         if (!onetrip.equals(twotrip)) {
1764                                 System.out.printf("one:\n%s\ntwo:\n%s\n", onetrip, twotrip);
1765                                 System.exit(1);
1766                         }
1767                 }
1768                 if (false) {
1769                         AltosHashSet    hash = hashSet();
1770                         String          onetrip = hash.toString();
1771                         AltosHashSet    back = AltosHashSet.fromString(onetrip);
1772                         AltosState      tripstate = AltosState.fromHashSet(back);
1773                         AltosHashSet    triphash = tripstate.hashSet();
1774                         String          twotrip = triphash.toString();
1775
1776                         if (!onetrip.equals(twotrip)) {
1777                                 System.out.printf("%s\n%s\n", onetrip, twotrip);
1778                                 System.exit(1);
1779                         }
1780                 }
1781                 return s;
1782         }
1783
1784         public AltosState () {
1785                 init();
1786         }
1787
1788         public AltosHashSet hashSet() {
1789                 AltosHashSet    h = new AltosHashSet();
1790
1791                 h.putBoolean("valid", true);
1792                 h.putInt("set", set);
1793                 h.putLong("received_time", received_time);
1794                 h.putDouble("time", time);
1795                 h.putDouble("prev_time", prev_time);
1796                 h.putDouble("time_change", time_change);
1797                 h.putInt("tick", tick);
1798                 h.putInt("prev_tick", prev_tick);
1799                 h.putInt("boost_tick", boost_tick);
1800                 h.putInt("state", state);
1801                 h.putInt("flight", flight);
1802                 h.putInt("serial", serial);
1803                 h.putInt("altitude_32", altitude_32);
1804                 h.putInt("receiver_serial", receiver_serial);
1805                 h.putBoolean("landed", landed);
1806                 h.putBoolean("ascent", ascent);
1807                 h.putBoolean("boost", boost);
1808                 h.putInt("rssi", rssi);
1809                 h.putInt("status", status);
1810                 h.putInt("device_type", device_type);
1811                 h.putInt("config_major", config_major);
1812                 h.putInt("config_minor", config_minor);
1813                 h.putInt("apogee_delay", apogee_delay);
1814                 h.putInt("main_deploy", main_deploy);
1815                 h.putInt("flight_log_max", flight_log_max);
1816                 h.putHashable("ground_altitude", ground_altitude);
1817                 h.putHashable("gps_ground_altitude", gps_ground_altitude);
1818                 h.putHashable("ground_pressure", ground_pressure);
1819                 h.putHashable("altitude", altitude);
1820                 h.putHashable("gps_altitude", gps_altitude);
1821                 h.putHashable("gps_ground_speed", gps_ground_speed);
1822                 h.putHashable("gps_ascent_rate", gps_ascent_rate);
1823                 h.putHashable("gps_course", gps_course);
1824                 h.putHashable("gps_speed", gps_speed);
1825                 h.putHashable("pressure", pressure);
1826                 h.putHashable("speed", speed);
1827                 h.putHashable("acceleration", acceleration);
1828                 h.putHashable("orient", orient);
1829                 h.putHashable("kalman_height", kalman_height);
1830                 h.putHashable("kalman_speed", kalman_speed);
1831                 h.putHashable("kalman_acceleration", kalman_acceleration);
1832
1833                 h.putDouble("battery_voltage",battery_voltage);
1834                 h.putDouble("pyro_voltage",pyro_voltage);
1835                 h.putDouble("temperature",temperature);
1836                 h.putDouble("apogee_voltage",apogee_voltage);
1837                 h.putDouble("main_voltage",main_voltage);
1838                 h.putDoubleArray("ignitor_voltage",ignitor_voltage);
1839                 h.putHashable("gps", gps);
1840                 h.putHashable("temp_gps", temp_gps);
1841                 h.putInt("temp_gps_sat_tick", temp_gps_sat_tick);
1842                 h.putBoolean("gps_pending", gps_pending);
1843                 h.putInt("gps_sequence", gps_sequence);
1844                 h.putHashable("imu", imu);
1845                 h.putHashable("mag", mag);
1846
1847                 h.putInt("npad", npad);
1848                 h.putInt("gps_waiting", gps_waiting);
1849                 h.putBoolean("gps_ready", gps_ready);
1850                 h.putInt("ngps", ngps);
1851                 h.putHashable("from_pad", from_pad);
1852                 h.putDouble("elevation", elevation);
1853                 h.putDouble("range", range);
1854                 h.putDouble("gps_height", gps_height);
1855                 h.putDouble("pad_lat", pad_lat);
1856                 h.putDouble("pad_lon", pad_lon);
1857                 h.putDouble("pad_alt", pad_alt);
1858                 h.putInt("speak_tick", speak_tick);
1859                 h.putDouble("speak_altitude", speak_altitude);
1860                 h.putString("callsign", callsign);
1861                 h.putString("firmware_version", firmware_version);
1862                 h.putDouble("accel_plus_g", accel_plus_g);
1863                 h.putDouble("accel_minus_g", accel_minus_g);
1864                 h.putDouble("accel", accel);
1865                 h.putDouble("ground_accel", ground_accel);
1866                 h.putDouble("ground_accel_avg", ground_accel_avg);
1867                 h.putInt("log_format", log_format);
1868                 h.putInt("log_space", log_space);
1869                 h.putString("product", product);
1870                 h.putHashable("baro", baro);
1871                 h.putHashable("companion", companion);
1872                 h.putInt("pyro_fired", pyro_fired);
1873                 h.putDouble("accel_zero_along", accel_zero_along);
1874                 h.putDouble("accel_zero_across", accel_zero_across);
1875                 h.putDouble("accel_zero_through", accel_zero_through);
1876
1877                 h.putHashable("rotation", rotation);
1878                 h.putHashable("ground_rotation", ground_rotation);
1879
1880                 h.putInt("pad_orientation", pad_orientation);
1881
1882                 h.putDouble("accel_ground_along", accel_ground_along);
1883                 h.putDouble("accel_ground_across", accel_ground_across);
1884                 h.putDouble("accel_ground_through", accel_ground_through);
1885
1886                 h.putDouble("gyro_zero_roll", gyro_zero_roll);
1887                 h.putDouble("gyro_zero_pitch", gyro_zero_pitch);
1888                 h.putDouble("gyro_zero_yaw", gyro_zero_yaw);
1889
1890                 h.putDouble("last_imu_time", last_imu_time);
1891                 return h;
1892         }
1893
1894         public AltosJson json() {
1895                 AltosJson       j = new AltosJson();
1896
1897                 j.put("valid", true);
1898                 j.put("set", set);
1899                 j.put("received_time", received_time);
1900                 j.put("time", time);
1901                 j.put("prev_time", prev_time);
1902                 j.put("time_change", time_change);
1903                 j.put("tick", tick);
1904                 j.put("prev_tick", prev_tick);
1905                 j.put("boost_tick", boost_tick);
1906                 j.put("state", state);
1907                 j.put("flight", flight);
1908                 j.put("serial", serial);
1909                 j.put("altitude_32", altitude_32);
1910                 j.put("receiver_serial", receiver_serial);
1911                 j.put("landed", landed);
1912                 j.put("ascent", ascent);
1913                 j.put("boost", boost);
1914                 j.put("rssi", rssi);
1915                 j.put("status", status);
1916                 j.put("device_type", device_type);
1917                 j.put("config_major", config_major);
1918                 j.put("config_minor", config_minor);
1919                 j.put("apogee_delay", apogee_delay);
1920                 j.put("main_deploy", main_deploy);
1921                 j.put("flight_log_max", flight_log_max);
1922                 j.put("ground_altitude", ground_altitude);
1923                 j.put("gps_ground_altitude", gps_ground_altitude);
1924                 j.put("ground_pressure", ground_pressure);
1925                 j.put("altitude", altitude);
1926                 j.put("gps_altitude", gps_altitude);
1927                 j.put("gps_ground_speed", gps_ground_speed);
1928                 j.put("gps_ascent_rate", gps_ascent_rate);
1929                 j.put("gps_course", gps_course);
1930                 j.put("gps_speed", gps_speed);
1931                 j.put("pressure", pressure);
1932                 j.put("speed", speed);
1933                 j.put("acceleration", acceleration);
1934                 j.put("orient", orient);
1935                 j.put("kalman_height", kalman_height);
1936                 j.put("kalman_speed", kalman_speed);
1937                 j.put("kalman_acceleration", kalman_acceleration);
1938
1939                 j.put("battery_voltage",battery_voltage);
1940                 j.put("pyro_voltage",pyro_voltage);
1941                 j.put("temperature",temperature);
1942                 j.put("apogee_voltage",apogee_voltage);
1943                 j.put("main_voltage",main_voltage);
1944                 j.put("ignitor_voltage",ignitor_voltage);
1945                 j.put("gps", gps);
1946                 j.put("temp_gps", temp_gps);
1947                 j.put("temp_gps_sat_tick", temp_gps_sat_tick);
1948                 j.put("gps_pending", gps_pending);
1949                 j.put("gps_sequence", gps_sequence);
1950                 j.put("imu", imu);
1951                 j.put("mag", mag);
1952
1953                 j.put("npad", npad);
1954                 j.put("gps_waiting", gps_waiting);
1955                 j.put("gps_ready", gps_ready);
1956                 j.put("ngps", ngps);
1957                 j.put("from_pad", from_pad);
1958                 j.put("elevation", elevation);
1959                 j.put("range", range);
1960                 j.put("gps_height", gps_height);
1961                 j.put("pad_lat", pad_lat);
1962                 j.put("pad_lon", pad_lon);
1963                 j.put("pad_alt", pad_alt);
1964                 j.put("speak_tick", speak_tick);
1965                 j.put("speak_altitude", speak_altitude);
1966                 j.put("callsign", callsign);
1967                 j.put("firmware_version", firmware_version);
1968                 j.put("accel_plus_g", accel_plus_g);
1969                 j.put("accel_minus_g", accel_minus_g);
1970                 j.put("accel", accel);
1971                 j.put("ground_accel", ground_accel);
1972                 j.put("ground_accel_avg", ground_accel_avg);
1973                 j.put("log_format", log_format);
1974                 j.put("log_space", log_space);
1975                 j.put("product", product);
1976                 j.put("baro", baro);
1977                 j.put("companion", companion);
1978                 j.put("pyro_fired", pyro_fired);
1979                 j.put("accel_zero_along", accel_zero_along);
1980                 j.put("accel_zero_across", accel_zero_across);
1981                 j.put("accel_zero_through", accel_zero_through);
1982
1983                 j.put("rotation", rotation);
1984                 j.put("ground_rotation", ground_rotation);
1985
1986                 j.put("pad_orientation", pad_orientation);
1987
1988                 j.put("accel_ground_along", accel_ground_along);
1989                 j.put("accel_ground_across", accel_ground_across);
1990                 j.put("accel_ground_through", accel_ground_through);
1991
1992                 j.put("gyro_zero_roll", gyro_zero_roll);
1993                 j.put("gyro_zero_pitch", gyro_zero_pitch);
1994                 j.put("gyro_zero_yaw", gyro_zero_yaw);
1995
1996                 j.put("last_imu_time", last_imu_time);
1997                 return j;
1998         }
1999
2000         public AltosState(AltosHashSet h) {
2001                 this();
2002
2003                 set = h.getInt("set", set);
2004                 received_time = h.getLong("received_time", received_time);
2005                 time = h.getDouble("time", time);
2006                 prev_time = h.getDouble("prev_time", prev_time);
2007                 time_change = h.getDouble("time_change", time_change);
2008                 tick = h.getInt("tick", tick);
2009                 prev_tick = h.getInt("prev_tick", prev_tick);
2010                 boost_tick = h.getInt("boost_tick", boost_tick);
2011                 state = h.getInt("state", state);
2012                 flight = h.getInt("flight", flight);
2013                 serial = h.getInt("serial", serial);
2014                 altitude_32 = h.getInt("altitude_32", altitude_32);
2015                 receiver_serial = h.getInt("receiver_serial", receiver_serial);
2016                 landed = h.getBoolean("landed", landed);
2017                 ascent = h.getBoolean("ascent", ascent);
2018                 boost = h.getBoolean("boost", boost);
2019                 rssi = h.getInt("rssi", rssi);
2020                 status = h.getInt("status", status);
2021                 device_type = h.getInt("device_type", device_type);
2022                 config_major = h.getInt("config_major", config_major);
2023                 config_minor = h.getInt("config_minor", config_minor);
2024                 apogee_delay = h.getInt("apogee_delay", apogee_delay);
2025                 main_deploy = h.getInt("main_deploy", main_deploy);
2026                 flight_log_max = h.getInt("flight_log_max", flight_log_max);
2027                 ground_altitude = AltosCValue_fromHashSet(h.getHash("ground_altitude"), ground_altitude);
2028                 gps_ground_altitude = AltosGpsGroundAltitude_fromHashSet(h.getHash("gps_ground_altitude"), gps_ground_altitude);
2029                 ground_pressure = AltosGroundPressure_fromHashSet(h.getHash("ground_pressure"), ground_pressure);
2030                 altitude = AltosAltitude_fromHashSet(h.getHash("altitude"), altitude);
2031                 gps_altitude = AltosGpsAltitude_fromHashSet(h.getHash("gps_altitude"), gps_altitude);
2032                 gps_ground_speed = AltosValue_fromHashSet(h.getHash("gps_ground_speed"), gps_ground_speed);
2033                 gps_ascent_rate = AltosValue_fromHashSet(h.getHash("gps_ascent_rate"), gps_ascent_rate);
2034                 gps_course = AltosValue_fromHashSet(h.getHash("gps_course"), gps_course);
2035                 gps_speed = AltosValue_fromHashSet(h.getHash("gps_speed"), gps_speed);
2036                 pressure = AltosPressure_fromHashSet(h.getHash("pressure"), pressure);
2037                 speed = AltosSpeed_fromHashSet(h.getHash("speed"), speed);
2038                 acceleration = AltosAccel_fromHashSet(h.getHash("acceleration"), acceleration);
2039                 orient = AltosCValue_fromHashSet(h.getHash("orient"), orient);
2040                 kalman_height = AltosValue_fromHashSet(h.getHash("kalman_height"), kalman_height);
2041                 kalman_speed = AltosValue_fromHashSet(h.getHash("kalman_speed"), kalman_speed);
2042                 kalman_acceleration = AltosValue_fromHashSet(h.getHash("kalman_acceleration"), kalman_acceleration);
2043
2044                 battery_voltage = h.getDouble("battery_voltage", battery_voltage);
2045                 pyro_voltage = h.getDouble("pyro_voltage", pyro_voltage);
2046                 temperature = h.getDouble("temperature", temperature);
2047                 apogee_voltage = h.getDouble("apogee_voltage", apogee_voltage);
2048                 main_voltage=  h.getDouble("main_voltage", main_voltage);
2049                 ignitor_voltage = h.getDoubleArray("ignitor_voltage", ignitor_voltage);
2050                 gps = AltosGPS.fromHashSet(h.getHash("gps"), gps);
2051                 temp_gps = AltosGPS.fromHashSet(h.getHash("temp_gps"), temp_gps);
2052                 temp_gps_sat_tick = h.getInt("temp_gps_sat_tick", temp_gps_sat_tick);
2053                 gps_pending = h.getBoolean("gps_pending", gps_pending);
2054                 gps_sequence = h.getInt("gps_sequence", gps_sequence);
2055                 imu = AltosIMU.fromHashSet(h.getHash("imu"), imu);
2056                 mag = AltosMag.fromHashSet(h.getHash("mag"), mag);
2057
2058                 npad = h.getInt("npad", npad);
2059                 gps_waiting = h.getInt("gps_waiting", gps_waiting);
2060                 gps_ready = h.getBoolean("gps_ready", gps_ready);
2061                 ngps = h.getInt("ngps", ngps);
2062                 from_pad = AltosGreatCircle.fromHashSet(h.getHash("from_pad"), from_pad);
2063                 elevation = h.getDouble("elevation", elevation);
2064                 range = h.getDouble("range", range);
2065                 gps_height = h.getDouble("gps_height", gps_height);
2066                 pad_lat = h.getDouble("pad_lat", pad_lat);
2067                 pad_lon = h.getDouble("pad_lon", pad_lon);
2068                 pad_alt = h.getDouble("pad_alt", pad_alt);
2069                 speak_tick = h.getInt("speak_tick", speak_tick);
2070                 speak_altitude = h.getDouble("speak_altitude", speak_altitude);
2071                 callsign = h.getString("callsign", callsign);
2072                 firmware_version = h.getString("firmware_version", firmware_version);
2073                 accel_plus_g = h.getDouble("accel_plus_g", accel_plus_g);
2074                 accel_minus_g = h.getDouble("accel_minus_g", accel_minus_g);
2075                 accel = h.getDouble("accel", accel);
2076                 ground_accel = h.getDouble("ground_accel", ground_accel);
2077                 ground_accel_avg = h.getDouble("ground_accel_avg", ground_accel_avg);
2078                 log_format = h.getInt("log_format", log_format);
2079                 log_space = h.getInt("log_space", log_space);
2080                 product = h.getString("product", product);
2081                 baro = AltosMs5607.fromHashSet(h.getHash("baro"), baro);
2082                 companion = AltosCompanion.fromHashSet(h.getHash("companion"), companion);
2083                 pyro_fired = h.getInt("pyro_fired", pyro_fired);
2084                 accel_zero_along = h.getDouble("accel_zero_along", accel_zero_along);
2085                 accel_zero_across = h.getDouble("accel_zero_across", accel_zero_across);
2086                 accel_zero_through = h.getDouble("accel_zero_through", accel_zero_through);
2087
2088                 rotation = AltosRotation.fromHashSet(h.getHash("rotation"), rotation);
2089                 ground_rotation = AltosRotation.fromHashSet(h.getHash("ground_rotation"), ground_rotation);
2090
2091                 pad_orientation = h.getInt("pad_orientation", pad_orientation);
2092
2093                 accel_ground_along = h.getDouble("accel_ground_along", accel_ground_along);
2094                 accel_ground_across = h.getDouble("accel_ground_across", accel_ground_across);
2095                 accel_ground_through = h.getDouble("accel_ground_through", accel_ground_through);
2096
2097                 gyro_zero_roll = h.getDouble("gyro_zero_roll", gyro_zero_roll);
2098                 gyro_zero_pitch = h.getDouble("gyro_zero_pitch", gyro_zero_pitch);
2099                 gyro_zero_yaw = h.getDouble("gyro_zero_yaw", gyro_zero_yaw);
2100
2101                 last_imu_time = h.getDouble("last_imu_time", last_imu_time);
2102         }
2103
2104         public AltosState(AltosJson j) {
2105                 this();
2106
2107                 set = j.get_int("set", set);
2108                 received_time = j.get_long("received_time", received_time);
2109                 time = j.get_double("time", time);
2110                 prev_time = j.get_double("prev_time", prev_time);
2111                 time_change = j.get_double("time_change", time_change);
2112                 tick = j.get_int("tick", tick);
2113                 prev_tick = j.get_int("prev_tick", prev_tick);
2114                 boost_tick = j.get_int("boost_tick", boost_tick);
2115                 state = j.get_int("state", state);
2116                 flight = j.get_int("flight", flight);
2117                 serial = j.get_int("serial", serial);
2118                 altitude_32 = j.get_int("altitude_32", altitude_32);
2119                 receiver_serial = j.get_int("receiver_serial", receiver_serial);
2120                 landed = j.get_boolean("landed", landed);
2121                 ascent = j.get_boolean("ascent", ascent);
2122                 boost = j.get_boolean("boost", boost);
2123                 rssi = j.get_int("rssi", rssi);
2124                 status = j.get_int("status", status);
2125                 device_type = j.get_int("device_type", device_type);
2126                 config_major = j.get_int("config_major", config_major);
2127                 config_minor = j.get_int("config_minor", config_minor);
2128                 apogee_delay = j.get_int("apogee_delay", apogee_delay);
2129                 main_deploy = j.get_int("main_deploy", main_deploy);
2130                 flight_log_max = j.get_int("flight_log_max", flight_log_max);
2131                 ground_altitude = AltosCValue_fromJson(j.get("ground_altitude"), ground_altitude);
2132                 gps_ground_altitude = AltosGpsGroundAltitude_fromJson(j.get("gps_ground_altitude"), gps_ground_altitude);
2133                 ground_pressure = AltosGroundPressure_fromJson(j.get("ground_pressure"), ground_pressure);
2134                 altitude = AltosAltitude_fromJson(j.get("altitude"), altitude);
2135                 gps_altitude = AltosGpsAltitude_fromJson(j.get("gps_altitude"), gps_altitude);
2136                 gps_ground_speed = AltosValue_fromJson(j.get("gps_ground_speed"), gps_ground_speed);
2137                 gps_ascent_rate = AltosValue_fromJson(j.get("gps_ascent_rate"), gps_ascent_rate);
2138                 gps_course = AltosValue_fromJson(j.get("gps_course"), gps_course);
2139                 gps_speed = AltosValue_fromJson(j.get("gps_speed"), gps_speed);
2140                 pressure = AltosPressure_fromJson(j.get("pressure"), pressure);
2141                 speed = AltosSpeed_fromJson(j.get("speed"), speed);
2142                 acceleration = AltosAccel_fromJson(j.get("acceleration"), acceleration);
2143                 orient = AltosCValue_fromJson(j.get("orient"), orient);
2144                 kalman_height = AltosValue_fromJson(j.get("kalman_height"), kalman_height);
2145                 kalman_speed = AltosValue_fromJson(j.get("kalman_speed"), kalman_speed);
2146                 kalman_acceleration = AltosValue_fromJson(j.get("kalman_acceleration"), kalman_acceleration);
2147
2148                 battery_voltage = j.get_double("battery_voltage", battery_voltage);
2149                 pyro_voltage = j.get_double("pyro_voltage", pyro_voltage);
2150                 temperature = j.get_double("temperature", temperature);
2151                 apogee_voltage = j.get_double("apogee_voltage", apogee_voltage);
2152                 main_voltage=  j.get_double("main_voltage", main_voltage);
2153                 ignitor_voltage = j.get_double_array("ignitor_voltage", ignitor_voltage);
2154                 gps = AltosGPS.fromJson(j.get("gps"), gps);
2155                 temp_gps = AltosGPS.fromJson(j.get("temp_gps"), temp_gps);
2156                 temp_gps_sat_tick = j.get_int("temp_gps_sat_tick", temp_gps_sat_tick);
2157                 gps_pending = j.get_boolean("gps_pending", gps_pending);
2158                 gps_sequence = j.get_int("gps_sequence", gps_sequence);
2159                 imu = AltosIMU.fromJson(j.get("imu"), imu);
2160                 mag = AltosMag.fromJson(j.get("mag"), mag);
2161
2162                 npad = j.get_int("npad", npad);
2163                 gps_waiting = j.get_int("gps_waiting", gps_waiting);
2164                 gps_ready = j.get_boolean("gps_ready", gps_ready);
2165                 ngps = j.get_int("ngps", ngps);
2166                 from_pad = AltosGreatCircle.fromJson(j.get("from_pad"), from_pad);
2167                 elevation = j.get_double("elevation", elevation);
2168                 range = j.get_double("range", range);
2169                 gps_height = j.get_double("gps_height", gps_height);
2170                 pad_lat = j.get_double("pad_lat", pad_lat);
2171                 pad_lon = j.get_double("pad_lon", pad_lon);
2172                 pad_alt = j.get_double("pad_alt", pad_alt);
2173                 speak_tick = j.get_int("speak_tick", speak_tick);
2174                 speak_altitude = j.get_double("speak_altitude", speak_altitude);
2175                 callsign = j.get_string("callsign", callsign);
2176                 firmware_version = j.get_string("firmware_version", firmware_version);
2177                 accel_plus_g = j.get_double("accel_plus_g", accel_plus_g);
2178                 accel_minus_g = j.get_double("accel_minus_g", accel_minus_g);
2179                 accel = j.get_double("accel", accel);
2180                 ground_accel = j.get_double("ground_accel", ground_accel);
2181                 ground_accel_avg = j.get_double("ground_accel_avg", ground_accel_avg);
2182                 log_format = j.get_int("log_format", log_format);
2183                 log_space = j.get_int("log_space", log_space);
2184                 product = j.get_string("product", product);
2185                 baro = AltosMs5607.fromJson(j.get("baro"), baro);
2186                 companion = AltosCompanion.fromJson(j.get("companion"), companion);
2187                 pyro_fired = j.get_int("pyro_fired", pyro_fired);
2188                 accel_zero_along = j.get_double("accel_zero_along", accel_zero_along);
2189                 accel_zero_across = j.get_double("accel_zero_across", accel_zero_across);
2190                 accel_zero_through = j.get_double("accel_zero_through", accel_zero_through);
2191
2192                 rotation = AltosRotation.fromJson(j.get("rotation"), rotation);
2193                 ground_rotation = AltosRotation.fromJson(j.get("ground_rotation"), ground_rotation);
2194
2195                 pad_orientation = j.get_int("pad_orientation", pad_orientation);
2196
2197                 accel_ground_along = j.get_double("accel_ground_along", accel_ground_along);
2198                 accel_ground_across = j.get_double("accel_ground_across", accel_ground_across);
2199                 accel_ground_through = j.get_double("accel_ground_through", accel_ground_through);
2200
2201                 gyro_zero_roll = j.get_double("gyro_zero_roll", gyro_zero_roll);
2202                 gyro_zero_pitch = j.get_double("gyro_zero_pitch", gyro_zero_pitch);
2203                 gyro_zero_yaw = j.get_double("gyro_zero_yaw", gyro_zero_yaw);
2204
2205                 last_imu_time = j.get_double("last_imu_time", last_imu_time);
2206         }
2207         public static AltosState fromHashSet(AltosHashSet h) {
2208                 if (h == null)
2209                         return null;
2210                 if (!h.getBoolean("valid", false))
2211                         return null;
2212                 return new AltosState(h);
2213         }
2214
2215         public static AltosState fromJson(AltosJson j) {
2216                 if (j == null)
2217                         return null;
2218                 if (!j.get_boolean("valid", false))
2219                         return null;
2220                 return new AltosState(j);
2221         }
2222 }