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