846bda42dd834f454c8dc225de5bc4a42a44643f
[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         class AltosForce extends AltosValue {
517                 void set(double p, double time) {
518                         super.set(p, time);
519                 }
520
521                 AltosForce() {
522                         super();
523                 }
524         }
525         private AltosForce      thrust;
526
527         public double thrust() {
528                 return thrust.value();
529         }
530
531         public void set_thrust(double N) {
532                 thrust.set(N, time);
533         }
534
535         public double baro_height() {
536                 double a = altitude();
537                 double g = ground_altitude();
538                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
539                         return a - g;
540                 return AltosLib.MISSING;
541         }
542
543         public double height() {
544                 double k = kalman_height.value();
545                 if (k != AltosLib.MISSING)
546                         return k;
547
548                 double b = baro_height();
549                 if (b != AltosLib.MISSING)
550                         return b;
551
552                 return gps_height();
553         }
554
555         public double max_height() {
556                 double  k = kalman_height.max();
557                 if (k != AltosLib.MISSING)
558                         return k;
559
560                 double a = altitude.max();
561                 double g = ground_altitude();
562                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
563                         return a - g;
564                 return max_gps_height();
565         }
566
567         public double gps_height() {
568                 double a = gps_altitude();
569                 double g = gps_ground_altitude();
570
571                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
572                         return a - g;
573                 return AltosLib.MISSING;
574         }
575
576         public double max_gps_height() {
577                 double a = gps_altitude.max();
578                 double g = gps_ground_altitude();
579
580                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
581                         return a - g;
582                 return AltosLib.MISSING;
583         }
584
585         class AltosSpeed extends AltosCValue {
586
587                 boolean can_max() {
588                         return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
589                 }
590
591                 void set_accel() {
592                         acceleration.set_derivative(this);
593                 }
594
595                 void set_derivative(AltosCValue in) {
596                         super.set_derivative(in);
597                         set_accel();
598                 }
599
600                 void set_computed(double new_value, double time) {
601                         super.set_computed(new_value, time);
602                         set_accel();
603                 }
604
605                 void set_measured(double new_value, double time) {
606                         super.set_measured(new_value, time);
607                         set_accel();
608                 }
609
610                 AltosSpeed() {
611                         super();
612                 }
613         }
614
615         private AltosSpeed speed;
616
617         public double speed() {
618                 double v = kalman_speed.value();
619                 if (v != AltosLib.MISSING)
620                         return v;
621                 v = speed.value();
622                 if (v != AltosLib.MISSING)
623                         return v;
624                 v = gps_speed();
625                 if (v != AltosLib.MISSING)
626                         return v;
627                 return AltosLib.MISSING;
628         }
629
630         public double max_speed() {
631                 double v = kalman_speed.max();
632                 if (v != AltosLib.MISSING)
633                         return v;
634                 v = speed.max();
635                 if (v != AltosLib.MISSING)
636                         return v;
637                 v = max_gps_speed();
638                 if (v != AltosLib.MISSING)
639                         return v;
640                 return AltosLib.MISSING;
641         }
642
643         class AltosAccel extends AltosCValue {
644
645                 boolean can_max() {
646                         return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
647                 }
648
649                 void set_measured(double a, double time) {
650                         super.set_measured(a, time);
651                         if (ascent)
652                                 speed.set_integral(this.measured);
653                 }
654
655                 AltosAccel() {
656                         super();
657                 }
658         }
659
660         AltosAccel acceleration;
661
662         public double acceleration() {
663                 return acceleration.value();
664         }
665
666         public double max_acceleration() {
667                 return acceleration.max();
668         }
669
670         public AltosCValue      orient;
671
672         public void set_orient(double new_orient) {
673                 orient.set_measured(new_orient, time);
674         }
675
676         public double orient() {
677                 return orient.value();
678         }
679
680         public double max_orient() {
681                 return orient.max();
682         }
683
684         public AltosValue       kalman_height, kalman_speed, kalman_acceleration;
685
686         public void set_kalman(double height, double speed, double acceleration) {
687                 kalman_height.set(height, time);
688                 kalman_speed.set(speed, time);
689                 kalman_acceleration.set(acceleration, time);
690         }
691
692         public double   battery_voltage;
693         public double   pyro_voltage;
694         public double   temperature;
695         public double   apogee_voltage;
696         public double   main_voltage;
697
698         public double   ignitor_voltage[];
699
700         public AltosGPS gps;
701         public AltosGPS temp_gps;
702         public int temp_gps_sat_tick;
703         public boolean  gps_pending;
704         public int gps_sequence;
705
706         public AltosIMU imu;
707         public AltosMag mag;
708
709         public static final int MIN_PAD_SAMPLES = 10;
710
711         public int      npad;
712         public int      gps_waiting;
713         public boolean  gps_ready;
714
715         public int      ngps;
716
717         public AltosGreatCircle from_pad;
718         public double   elevation;      /* from pad */
719         public double   range;          /* total distance */
720
721         public double   gps_height;
722
723         public double pad_lat, pad_lon, pad_alt;
724
725         public int      speak_tick;
726         public double   speak_altitude;
727
728         public String   callsign;
729         public String   firmware_version;
730
731         public double   accel_plus_g;
732         public double   accel_minus_g;
733         public double   accel;
734         public double   ground_accel;
735         public double   ground_accel_avg;
736
737         public int      log_format;
738         public int      log_space;
739         public String   product;
740
741         public AltosMs5607      baro;
742
743         public AltosCompanion   companion;
744
745         public int      pyro_fired;
746
747         public void set_npad(int npad) {
748                 this.npad = npad;
749                 gps_waiting = MIN_PAD_SAMPLES - npad;
750                 if (this.gps_waiting < 0)
751                         gps_waiting = 0;
752                 gps_ready = gps_waiting == 0;
753         }
754
755         public void init() {
756                 set = 0;
757
758                 received_time = System.currentTimeMillis();
759                 time = AltosLib.MISSING;
760                 time_change = AltosLib.MISSING;
761                 prev_time = AltosLib.MISSING;
762                 tick = AltosLib.MISSING;
763                 prev_tick = AltosLib.MISSING;
764                 boost_tick = AltosLib.MISSING;
765                 state = AltosLib.ao_flight_invalid;
766                 flight = AltosLib.MISSING;
767                 landed = false;
768                 boost = false;
769                 rssi = AltosLib.MISSING;
770                 status = 0;
771                 device_type = AltosLib.MISSING;
772                 config_major = AltosLib.MISSING;
773                 config_minor = AltosLib.MISSING;
774                 apogee_delay = AltosLib.MISSING;
775                 main_deploy = AltosLib.MISSING;
776                 flight_log_max = AltosLib.MISSING;
777
778                 ground_altitude = new AltosCValue();
779                 ground_pressure = new AltosGroundPressure();
780                 altitude = new AltosAltitude();
781                 pressure = new AltosPressure();
782                 thrust = new AltosForce();
783                 speed = new AltosSpeed();
784                 acceleration = new AltosAccel();
785                 orient = new AltosCValue();
786
787                 temperature = AltosLib.MISSING;
788                 battery_voltage = AltosLib.MISSING;
789                 pyro_voltage = AltosLib.MISSING;
790                 apogee_voltage = AltosLib.MISSING;
791                 main_voltage = AltosLib.MISSING;
792                 ignitor_voltage = null;
793
794                 kalman_height = new AltosValue();
795                 kalman_speed = new AltosValue();
796                 kalman_acceleration = new AltosValue();
797
798                 gps = null;
799                 temp_gps = null;
800                 temp_gps_sat_tick = 0;
801                 gps_sequence = 0;
802                 gps_pending = false;
803
804                 imu = null;
805                 last_imu_time = AltosLib.MISSING;
806                 rotation = null;
807                 ground_rotation = null;
808
809                 mag = null;
810                 accel_zero_along = AltosLib.MISSING;
811                 accel_zero_across = AltosLib.MISSING;
812                 accel_zero_through = AltosLib.MISSING;
813
814                 accel_ground_along = AltosLib.MISSING;
815                 accel_ground_across = AltosLib.MISSING;
816                 accel_ground_through = AltosLib.MISSING;
817
818                 pad_orientation = AltosLib.MISSING;
819
820                 gyro_zero_roll = AltosLib.MISSING;
821                 gyro_zero_pitch = AltosLib.MISSING;
822                 gyro_zero_yaw = AltosLib.MISSING;
823
824                 set_npad(0);
825                 ngps = 0;
826
827                 from_pad = null;
828                 elevation = AltosLib.MISSING;
829                 range = AltosLib.MISSING;
830                 gps_height = AltosLib.MISSING;
831
832                 pad_lat = AltosLib.MISSING;
833                 pad_lon = AltosLib.MISSING;
834                 pad_alt = AltosLib.MISSING;
835
836                 gps_altitude = new AltosGpsAltitude();
837                 gps_ground_altitude = new AltosGpsGroundAltitude();
838                 gps_ground_speed = new AltosValue();
839                 gps_speed = new AltosValue();
840                 gps_ascent_rate = new AltosValue();
841                 gps_course = new AltosValue();
842
843                 speak_tick = AltosLib.MISSING;
844                 speak_altitude = AltosLib.MISSING;
845
846                 callsign = null;
847                 firmware_version = null;
848
849                 accel_plus_g = AltosLib.MISSING;
850                 accel_minus_g = AltosLib.MISSING;
851                 accel = AltosLib.MISSING;
852
853                 ground_accel = AltosLib.MISSING;
854                 ground_accel_avg = AltosLib.MISSING;
855
856                 log_format = AltosLib.MISSING;
857                 log_space = AltosLib.MISSING;
858                 product = null;
859                 serial = AltosLib.MISSING;
860                 receiver_serial = AltosLib.MISSING;
861                 altitude_32 = AltosLib.MISSING;
862
863                 baro = null;
864                 companion = null;
865
866                 pyro_fired = 0;
867         }
868
869         void finish_update() {
870                 prev_tick = tick;
871
872                 ground_altitude.finish_update();
873                 altitude.finish_update();
874                 pressure.finish_update();
875                 speed.finish_update();
876                 acceleration.finish_update();
877                 orient.finish_update();
878
879                 kalman_height.finish_update();
880                 kalman_speed.finish_update();
881                 kalman_acceleration.finish_update();
882         }
883
884         void copy(AltosState old) {
885
886                 if (old == null) {
887                         init();
888                         return;
889                 }
890
891                 super.copy(old);
892
893                 received_time = old.received_time;
894                 time = old.time;
895                 time_change = old.time_change;
896                 prev_time = old.time;
897
898                 tick = old.tick;
899                 prev_tick = old.tick;
900                 boost_tick = old.boost_tick;
901
902                 state = old.state;
903                 flight = old.flight;
904                 landed = old.landed;
905                 ascent = old.ascent;
906                 boost = old.boost;
907                 rssi = old.rssi;
908                 status = old.status;
909                 device_type = old.device_type;
910                 config_major = old.config_major;
911                 config_minor = old.config_minor;
912                 apogee_delay = old.apogee_delay;
913                 main_deploy = old.main_deploy;
914                 flight_log_max = old.flight_log_max;
915
916                 set = 0;
917
918                 ground_pressure.copy(old.ground_pressure);
919                 ground_altitude.copy(old.ground_altitude);
920                 altitude.copy(old.altitude);
921                 pressure.copy(old.pressure);
922                 speed.copy(old.speed);
923                 acceleration.copy(old.acceleration);
924                 orient.copy(old.orient);
925
926                 battery_voltage = old.battery_voltage;
927                 pyro_voltage = old.pyro_voltage;
928                 temperature = old.temperature;
929                 apogee_voltage = old.apogee_voltage;
930                 main_voltage = old.main_voltage;
931                 ignitor_voltage = old.ignitor_voltage;
932
933                 kalman_height.copy(old.kalman_height);
934                 kalman_speed.copy(old.kalman_speed);
935                 kalman_acceleration.copy(old.kalman_acceleration);
936
937                 if (old.gps != null)
938                         gps = old.gps.clone();
939                 else
940                         gps = null;
941                 if (old.temp_gps != null)
942                         temp_gps = old.temp_gps.clone();
943                 else
944                         temp_gps = null;
945                 temp_gps_sat_tick = old.temp_gps_sat_tick;
946                 gps_sequence = old.gps_sequence;
947                 gps_pending = old.gps_pending;
948
949                 if (old.imu != null)
950                         imu = old.imu.clone();
951                 else
952                         imu = null;
953                 last_imu_time = old.last_imu_time;
954
955                 if (old.rotation != null)
956                         rotation = new AltosRotation (old.rotation);
957
958                 if (old.ground_rotation != null) {
959                         ground_rotation = new AltosRotation(old.ground_rotation);
960                 }
961
962                 accel_zero_along = old.accel_zero_along;
963                 accel_zero_across = old.accel_zero_across;
964                 accel_zero_through = old.accel_zero_through;
965
966                 accel_ground_along = old.accel_ground_along;
967                 accel_ground_across = old.accel_ground_across;
968                 accel_ground_through = old.accel_ground_through;
969                 pad_orientation = old.pad_orientation;
970
971                 gyro_zero_roll = old.gyro_zero_roll;
972                 gyro_zero_pitch = old.gyro_zero_pitch;
973                 gyro_zero_yaw = old.gyro_zero_yaw;
974
975                 if (old.mag != null)
976                         mag = old.mag.clone();
977                 else
978                         mag = null;
979
980                 npad = old.npad;
981                 gps_waiting = old.gps_waiting;
982                 gps_ready = old.gps_ready;
983                 ngps = old.ngps;
984
985                 if (old.from_pad != null)
986                         from_pad = old.from_pad.clone();
987                 else
988                         from_pad = null;
989
990                 elevation = old.elevation;
991                 range = old.range;
992
993                 gps_height = old.gps_height;
994
995                 gps_altitude.copy(old.gps_altitude);
996                 gps_ground_altitude.copy(old.gps_ground_altitude);
997                 gps_ground_speed.copy(old.gps_ground_speed);
998                 gps_ascent_rate.copy(old.gps_ascent_rate);
999                 gps_course.copy(old.gps_course);
1000                 gps_speed.copy(old.gps_speed);
1001
1002                 pad_lat = old.pad_lat;
1003                 pad_lon = old.pad_lon;
1004                 pad_alt = old.pad_alt;
1005
1006                 speak_tick = old.speak_tick;
1007                 speak_altitude = old.speak_altitude;
1008
1009                 callsign = old.callsign;
1010                 firmware_version = old.firmware_version;
1011
1012                 accel_plus_g = old.accel_plus_g;
1013                 accel_minus_g = old.accel_minus_g;
1014                 accel = old.accel;
1015                 ground_accel = old.ground_accel;
1016                 ground_accel_avg = old.ground_accel_avg;
1017
1018                 log_format = old.log_format;
1019                 log_space = old.log_space;
1020                 product = old.product;
1021                 serial = old.serial;
1022                 receiver_serial = old.receiver_serial;
1023                 altitude_32 = old.altitude_32;
1024
1025                 baro = old.baro;
1026                 companion = old.companion;
1027
1028                 pyro_fired = old.pyro_fired;
1029         }
1030
1031         void update_time() {
1032         }
1033
1034         void update_gps() {
1035                 elevation = AltosLib.MISSING;
1036                 range = AltosLib.MISSING;
1037
1038                 if (gps == null)
1039                         return;
1040
1041                 if (gps.locked && gps.nsat >= 4) {
1042                         /* Track consecutive 'good' gps reports, waiting for 10 of them */
1043                         if (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_stateless) {
1044                                 set_npad(npad+1);
1045                                 if (pad_lat != AltosLib.MISSING && (npad < 10 || state == AltosLib.ao_flight_pad)) {
1046                                         pad_lat = (pad_lat * 31 + gps.lat) / 32;
1047                                         pad_lon = (pad_lon * 31 + gps.lon) / 32;
1048                                         gps_ground_altitude.set_filtered(gps.alt, time);
1049                                 }
1050                         }
1051                         if (pad_lat == AltosLib.MISSING) {
1052                                 pad_lat = gps.lat;
1053                                 pad_lon = gps.lon;
1054                                 gps_ground_altitude.set(gps.alt, time);
1055                         }
1056                         gps_altitude.set(gps.alt, time);
1057                         if (gps.climb_rate != AltosLib.MISSING)
1058                                 gps_ascent_rate.set(gps.climb_rate, time);
1059                         if (gps.ground_speed != AltosLib.MISSING)
1060                                 gps_ground_speed.set(gps.ground_speed, time);
1061                         if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
1062                                 gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
1063                                                         gps.climb_rate * gps.climb_rate), time);
1064                         if (gps.course != AltosLib.MISSING)
1065                                 gps_course.set(gps.course, time);
1066                 }
1067                 if (gps.lat != 0 && gps.lon != 0 &&
1068                     pad_lat != AltosLib.MISSING &&
1069                     pad_lon != AltosLib.MISSING)
1070                 {
1071                         double h = height();
1072
1073                         if (h == AltosLib.MISSING)
1074                                 h = 0;
1075                         from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
1076                         elevation = from_pad.elevation;
1077                         range = from_pad.range;
1078                 }
1079         }
1080
1081         public void set_tick(int new_tick) {
1082                 if (new_tick != AltosLib.MISSING) {
1083                         if (prev_tick != AltosLib.MISSING) {
1084                                 while (new_tick < prev_tick - 1000) {
1085                                         new_tick += 65536;
1086                                 }
1087                         }
1088                         tick = new_tick;
1089                         time = tick / 100.0;
1090                         time_change = time - prev_time;
1091                 }
1092         }
1093
1094         public String state_name() {
1095                 return AltosLib.state_name(state);
1096         }
1097
1098         public void set_product(String product) {
1099                 this.product = product;
1100         }
1101
1102         public void set_state(int state) {
1103                 if (state != AltosLib.ao_flight_invalid) {
1104                         this.state = state;
1105                         ascent = (AltosLib.ao_flight_boost <= state &&
1106                                   state <= AltosLib.ao_flight_coast);
1107                         boost = (AltosLib.ao_flight_boost == state);
1108                 }
1109         }
1110
1111         public int state() {
1112                 return state;
1113         }
1114
1115         public void set_device_type(int device_type) {
1116                 this.device_type = device_type;
1117                 switch (device_type) {
1118                 case AltosLib.product_telegps:
1119                         this.state = AltosLib.ao_flight_stateless;
1120                         break;
1121                 }
1122         }
1123
1124         public void set_log_format(int log_format) {
1125                 this.log_format = log_format;
1126                 switch (log_format) {
1127                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
1128                         this.state = AltosLib.ao_flight_stateless;
1129                         break;
1130                 }
1131         }
1132
1133         public void set_log_space(int log_space) {
1134                 this.log_space = log_space;
1135         }
1136
1137         public void set_flight_params(int apogee_delay, int main_deploy) {
1138                 this.apogee_delay = apogee_delay;
1139                 this.main_deploy = main_deploy;
1140         }
1141
1142         public void set_config(int major, int minor, int flight_log_max) {
1143                 config_major = major;
1144                 config_minor = minor;
1145                 this.flight_log_max = flight_log_max;
1146         }
1147
1148         public void set_callsign(String callsign) {
1149                 this.callsign = callsign;
1150         }
1151
1152         public void set_firmware_version(String version) {
1153                 firmware_version = version;
1154         }
1155
1156         public int compare_version(String other_version) {
1157                 if (firmware_version == null)
1158                         return AltosLib.MISSING;
1159                 return AltosLib.compare_version(firmware_version, other_version);
1160         }
1161
1162         private void re_init() {
1163                 int bt = boost_tick;
1164                 int rs = receiver_serial;
1165                 init();
1166                 boost_tick = bt;
1167                 receiver_serial = rs;
1168         }
1169
1170         public void set_flight(int flight) {
1171
1172                 /* When the flight changes, reset the state */
1173                 if (flight != AltosLib.MISSING) {
1174                         if (this.flight != AltosLib.MISSING &&
1175                             this.flight != flight) {
1176                                 re_init();
1177                         }
1178                         this.flight = flight;
1179                 }
1180         }
1181
1182         public void set_serial(int serial) {
1183                 /* When the serial changes, reset the state */
1184                 if (serial != AltosLib.MISSING) {
1185                         if (this.serial != AltosLib.MISSING &&
1186                             this.serial != serial) {
1187                                 re_init();
1188                         }
1189                         this.serial = serial;
1190                 }
1191         }
1192
1193         public void set_receiver_serial(int serial) {
1194                 if (serial != AltosLib.MISSING)
1195                         receiver_serial = serial;
1196         }
1197
1198         public boolean altitude_32() {
1199                 return altitude_32 == 1;
1200         }
1201
1202         public void set_altitude_32(int altitude_32) {
1203                 if (altitude_32 != AltosLib.MISSING)
1204                         this.altitude_32 = altitude_32;
1205         }
1206
1207         public int rssi() {
1208                 if (rssi == AltosLib.MISSING)
1209                         return 0;
1210                 return rssi;
1211         }
1212
1213         public void set_rssi(int rssi, int status) {
1214                 if (rssi != AltosLib.MISSING) {
1215                         this.rssi = rssi;
1216                         this.status = status;
1217                 }
1218         }
1219
1220         public void set_received_time(long ms) {
1221                 received_time = ms;
1222         }
1223
1224         public void set_gps(AltosGPS gps, int sequence) {
1225                 if (gps != null) {
1226                         this.gps = gps.clone();
1227                         gps_sequence = sequence;
1228                         update_gps();
1229                         set |= set_gps;
1230                 }
1231         }
1232
1233
1234         public double   accel_zero_along;
1235         public double   accel_zero_across;
1236         public double   accel_zero_through;
1237
1238         public AltosRotation    rotation;
1239         public AltosRotation    ground_rotation;
1240
1241         public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
1242                 if (zero_along != AltosLib.MISSING) {
1243                         accel_zero_along = zero_along;
1244                         accel_zero_across = zero_across;
1245                         accel_zero_through = zero_through;
1246                 }
1247         }
1248
1249         public int pad_orientation;
1250
1251         public double   accel_ground_along, accel_ground_across, accel_ground_through;
1252
1253         void update_pad_rotation() {
1254                 if (pad_orientation != AltosLib.MISSING && accel_ground_along != AltosLib.MISSING) {
1255                         rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - accel_zero_across),
1256                                                      AltosIMU.convert_accel(accel_ground_through - accel_zero_through),
1257                                                      AltosIMU.convert_accel(accel_ground_along - accel_zero_along),
1258                                                      pad_orientation);
1259                         ground_rotation = rotation;
1260                         orient.set_computed(rotation.tilt(), time);
1261                 }
1262         }
1263
1264         public void set_accel_ground(double ground_along, double ground_across, double ground_through) {
1265                 accel_ground_along = ground_along;
1266                 accel_ground_across = ground_across;
1267                 accel_ground_through = ground_through;
1268                 update_pad_rotation();
1269         }
1270
1271         public void set_pad_orientation(int pad_orientation) {
1272                 this.pad_orientation = pad_orientation;
1273                 update_pad_rotation();
1274         }
1275
1276         public double   gyro_zero_roll;
1277         public double   gyro_zero_pitch;
1278         public double   gyro_zero_yaw;
1279
1280         public void set_gyro_zero(double roll, double pitch, double yaw) {
1281                 if (roll != AltosLib.MISSING) {
1282                         gyro_zero_roll = roll;
1283                         gyro_zero_pitch = pitch;
1284                         gyro_zero_yaw = yaw;
1285                 }
1286         }
1287
1288         public double   last_imu_time;
1289
1290         private double radians(double degrees) {
1291                 if (degrees == AltosLib.MISSING)
1292                         return AltosLib.MISSING;
1293                 return degrees * Math.PI / 180.0;
1294         }
1295
1296         private void update_orient() {
1297                 if (last_imu_time != AltosLib.MISSING) {
1298                         double  t = time - last_imu_time;
1299
1300                         double  pitch = radians(gyro_pitch());
1301                         double  yaw = radians(gyro_yaw());
1302                         double  roll = radians(gyro_roll());
1303
1304                         if (t > 0 & pitch != AltosLib.MISSING && rotation != null) {
1305                                 rotation.rotate(t, pitch, yaw, roll);
1306                                 orient.set_computed(rotation.tilt(), time);
1307                         }
1308                 }
1309                 last_imu_time = time;
1310         }
1311
1312         public void set_imu(AltosIMU imu) {
1313                 if (imu != null)
1314                         imu = imu.clone();
1315                 this.imu = imu;
1316                 update_orient();
1317         }
1318
1319         private double gyro_zero_overflow(double first) {
1320                 double v = first / 128.0;
1321                 if (v < 0)
1322                         v = Math.ceil(v);
1323                 else
1324                         v = Math.floor(v);
1325                 return v * 128.0;
1326         }
1327
1328         public void check_imu_wrap(AltosIMU imu) {
1329                 if (this.imu == null) {
1330                         gyro_zero_roll += gyro_zero_overflow(imu.gyro_roll);
1331                         gyro_zero_pitch += gyro_zero_overflow(imu.gyro_pitch);
1332                         gyro_zero_yaw += gyro_zero_overflow(imu.gyro_yaw);
1333                 }
1334         }
1335
1336         public double accel_along() {
1337                 if (imu != null && accel_zero_along != AltosLib.MISSING)
1338                         return AltosIMU.convert_accel(imu.accel_along - accel_zero_along);
1339                 return AltosLib.MISSING;
1340         }
1341
1342         public double accel_across() {
1343                 if (imu != null && accel_zero_across != AltosLib.MISSING)
1344                         return AltosIMU.convert_accel(imu.accel_across - accel_zero_across);
1345                 return AltosLib.MISSING;
1346         }
1347
1348         public double accel_through() {
1349                 if (imu != null && accel_zero_through != AltosLib.MISSING)
1350                         return AltosIMU.convert_accel(imu.accel_through - accel_zero_through);
1351                 return AltosLib.MISSING;
1352         }
1353
1354         public double gyro_roll() {
1355                 if (imu != null && gyro_zero_roll != AltosLib.MISSING) {
1356                         return AltosIMU.convert_gyro(imu.gyro_roll - gyro_zero_roll);
1357                 }
1358                 return AltosLib.MISSING;
1359         }
1360
1361         public double gyro_pitch() {
1362                 if (imu != null && gyro_zero_pitch != AltosLib.MISSING) {
1363                         return AltosIMU.convert_gyro(imu.gyro_pitch - gyro_zero_pitch);
1364                 }
1365                 return AltosLib.MISSING;
1366         }
1367
1368         public double gyro_yaw() {
1369                 if (imu != null && gyro_zero_yaw != AltosLib.MISSING) {
1370                         return AltosIMU.convert_gyro(imu.gyro_yaw - gyro_zero_yaw);
1371                 }
1372                 return AltosLib.MISSING;
1373         }
1374
1375         public void set_mag(AltosMag mag) {
1376                 this.mag = mag.clone();
1377         }
1378
1379         public double mag_along() {
1380                 if (mag != null)
1381                         return AltosMag.convert_gauss(mag.along);
1382                 return AltosLib.MISSING;
1383         }
1384
1385         public double mag_across() {
1386                 if (mag != null)
1387                         return AltosMag.convert_gauss(mag.across);
1388                 return AltosLib.MISSING;
1389         }
1390
1391         public double mag_through() {
1392                 if (mag != null)
1393                         return AltosMag.convert_gauss(mag.through);
1394                 return AltosLib.MISSING;
1395         }
1396
1397         public AltosMs5607 make_baro() {
1398                 if (baro == null)
1399                         baro = new AltosMs5607();
1400                 return baro;
1401         }
1402
1403         public void set_ms5607(AltosMs5607 ms5607) {
1404                 baro = ms5607;
1405
1406                 if (baro != null && baro.pa != AltosLib.MISSING && baro.cc != AltosLib.MISSING) {
1407                         set_pressure(baro.pa);
1408                         set_temperature(baro.cc / 100.0);
1409                 }
1410         }
1411
1412         public void set_ms5607(int pres, int temp) {
1413                 if (baro != null) {
1414                         baro.set(pres, temp);
1415
1416                         set_pressure(baro.pa);
1417                         set_temperature(baro.cc / 100.0);
1418                 }
1419         }
1420
1421         public void set_companion(AltosCompanion companion) {
1422                 this.companion = companion;
1423         }
1424
1425         void update_accel() {
1426                 if (accel == AltosLib.MISSING)
1427                         return;
1428                 if (accel_plus_g == AltosLib.MISSING)
1429                         return;
1430                 if (accel_minus_g == AltosLib.MISSING)
1431                         return;
1432
1433                 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
1434                 double counts_per_mss = counts_per_g / 9.80665;
1435                 acceleration.set_measured((accel_plus_g - accel) / counts_per_mss, time);
1436         }
1437
1438         public void set_accel_g(double accel_plus_g, double accel_minus_g) {
1439                 if (accel_plus_g != AltosLib.MISSING) {
1440                         this.accel_plus_g = accel_plus_g;
1441                         this.accel_minus_g = accel_minus_g;
1442                         update_accel();
1443                 }
1444         }
1445
1446         public void set_ground_accel(double ground_accel) {
1447                 if (ground_accel != AltosLib.MISSING)
1448                         this.ground_accel = ground_accel;
1449         }
1450
1451         public void set_accel(double accel) {
1452                 if (accel != AltosLib.MISSING) {
1453                         this.accel = accel;
1454                         if (state == AltosLib.ao_flight_pad) {
1455                                 if (ground_accel_avg == AltosLib.MISSING)
1456                                         ground_accel_avg = accel;
1457                                 else
1458                                         ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
1459                         }
1460                 }
1461                 update_accel();
1462         }
1463
1464         public void set_temperature(double temperature) {
1465                 if (temperature != AltosLib.MISSING) {
1466                         this.temperature = temperature;
1467                         set |= set_data;
1468                 }
1469         }
1470
1471         public void set_battery_voltage(double battery_voltage) {
1472                 if (battery_voltage != AltosLib.MISSING) {
1473                         this.battery_voltage = battery_voltage;
1474                         set |= set_data;
1475                 }
1476         }
1477
1478         public void set_pyro_voltage(double pyro_voltage) {
1479                 if (pyro_voltage != AltosLib.MISSING) {
1480                         this.pyro_voltage = pyro_voltage;
1481                         set |= set_data;
1482                 }
1483         }
1484
1485         public void set_apogee_voltage(double apogee_voltage) {
1486                 if (apogee_voltage != AltosLib.MISSING) {
1487                         this.apogee_voltage = apogee_voltage;
1488                         set |= set_data;
1489                 }
1490         }
1491
1492         public void set_main_voltage(double main_voltage) {
1493                 if (main_voltage != AltosLib.MISSING) {
1494                         this.main_voltage = main_voltage;
1495                         set |= set_data;
1496                 }
1497         }
1498
1499         public void set_ignitor_voltage(double[] voltage) {
1500                 this.ignitor_voltage = voltage;
1501         }
1502
1503         public void set_pyro_fired(int fired) {
1504                 this.pyro_fired = fired;
1505         }
1506
1507         public double time_since_boost() {
1508                 if (tick == AltosLib.MISSING)
1509                         return 0.0;
1510
1511                 if (boost_tick == AltosLib.MISSING)
1512                         return tick / 100.0;
1513                 return (tick - boost_tick) / 100.0;
1514         }
1515
1516         public boolean valid() {
1517                 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1518         }
1519
1520         public void set_temp_gps() {
1521                 set_gps(temp_gps, gps_sequence + 1);
1522                 gps_pending = false;
1523                 super.set_temp_gps();
1524         }
1525
1526         public void set_config_data(AltosConfigData config_data) {
1527                 if (config_data.callsign != null)
1528                         set_callsign(config_data.callsign);
1529                 if (config_data.accel_cal_plus != AltosLib.MISSING &&
1530                     config_data.accel_cal_minus != AltosLib.MISSING)
1531                         set_accel_g(config_data.accel_cal_plus, config_data.accel_cal_minus);
1532                 if (config_data.product != null)
1533                         set_product(config_data.product);
1534                 if (config_data.log_format != AltosLib.MISSING)
1535                         set_log_format(config_data.log_format);
1536                 if (config_data.serial != AltosLib.MISSING)
1537                         set_serial(config_data.serial);
1538                 AltosMs5607 ms5607 = new AltosMs5607(config_data);
1539                 if (ms5607.valid_config())
1540                         set_ms5607(ms5607);
1541         }
1542
1543         public AltosState clone() {
1544                 AltosState s = new AltosState();
1545                 s.copy(this);
1546
1547                 /* Code to test state save/restore. Enable only for that purpose
1548                  */
1549                 if (false) {
1550                         AltosJson       json = new AltosJson(this);
1551                         String          onetrip = json.toPrettyString();
1552                         AltosJson       back = AltosJson.fromString(onetrip);
1553                         AltosState      tripstate = (AltosState) back.make(this.getClass());
1554                         AltosJson       tripjson = new AltosJson(tripstate);
1555                         String          twotrip = tripjson.toPrettyString();
1556
1557                         if (!onetrip.equals(twotrip)) {
1558                                 try {
1559                                         FileWriter one_file = new FileWriter("one.json", true);
1560                                         one_file.write(onetrip);
1561                                         one_file.flush();
1562                                         FileWriter two_file = new FileWriter("two.json", true);
1563                                         two_file.write(twotrip);
1564                                         two_file.flush();
1565                                 } catch (Exception e) {
1566                                 }
1567                                 System.out.printf("json error\n");
1568                                 System.exit(1);
1569                         }
1570                 }
1571                 return s;
1572         }
1573
1574         public AltosState () {
1575                 init();
1576         }
1577 }