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