5fce15c43865e63b43447db93f59faf725f838f7
[fw/altos] / altoslib / AltosState.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 /*
19  * Track flight state from telemetry or eeprom data stream
20  */
21
22 package org.altusmetrum.altoslib_5;
23
24 public class AltosState implements Cloneable {
25
26         public static final int set_position = 1;
27         public static final int set_gps = 2;
28         public static final int set_data = 4;
29
30         public int set;
31
32         static final double ascent_filter_len = 0.5;
33         static final double descent_filter_len = 0.5;
34
35         /* derived data */
36
37         public long     received_time;
38
39         public double   time;
40         public double   prev_time;
41         public double   time_change;
42         public int      tick;
43         private int     prev_tick;
44         public int      boost_tick;
45
46         class AltosValue {
47                 private double  value;
48                 private double  prev_value;
49                 private double  max_value;
50                 private double  set_time;
51                 private double  prev_set_time;
52
53                 boolean can_max() { return true; }
54
55                 void set(double new_value, double time) {
56                         if (new_value != AltosLib.MISSING) {
57                                 value = new_value;
58                                 if (can_max() && (max_value == AltosLib.MISSING || value > max_value))
59                                         max_value = value;
60                                 set_time = time;
61                         }
62                 }
63
64                 void set_filtered(double new_value, double time) {
65                         if (prev_value != AltosLib.MISSING)
66                                 new_value = (prev_value * 15.0 + new_value) / 16.0;
67                         set(new_value, time);
68                 }
69
70                 double value() {
71                         return value;
72                 }
73
74                 double max() {
75                         return max_value;
76                 }
77
78                 double prev() {
79                         return prev_value;
80                 }
81
82                 double change() {
83                         if (value != AltosLib.MISSING && prev_value != AltosLib.MISSING)
84                                 return value - prev_value;
85                         return AltosLib.MISSING;
86                 }
87
88                 double rate() {
89                         double c = change();
90                         double t = set_time - prev_set_time;
91
92                         if (c != AltosLib.MISSING && t != 0)
93                                 return c / t;
94                         return AltosLib.MISSING;
95                 }
96
97                 double integrate() {
98                         if (value == AltosLib.MISSING)
99                                 return AltosLib.MISSING;
100                         if (prev_value == AltosLib.MISSING)
101                                 return AltosLib.MISSING;
102
103                         return (value + prev_value) / 2 * (set_time - prev_set_time);
104                 }
105
106                 double time() {
107                         return set_time;
108                 }
109
110                 void set_derivative(AltosValue in) {
111                         double  n = in.rate();
112
113                         if (n == AltosLib.MISSING)
114                                 return;
115
116                         double  p = prev_value;
117                         double  pt = prev_set_time;
118
119                         if (p == AltosLib.MISSING) {
120                                 p = 0;
121                                 pt = in.time() - 0.01;
122                         }
123
124                         /* Clip changes to reduce noise */
125                         double  ddt = in.time() - pt;
126                         double  ddv = (n - p) / ddt;
127
128                         final double max = 100000;
129
130                         /* 100gs */
131                         if (Math.abs(ddv) > max) {
132                                 if (n > p)
133                                         n = p + ddt * max;
134                                 else
135                                         n = p - ddt * max;
136                         }
137
138                         double filter_len;
139
140                         if (ascent)
141                                 filter_len = ascent_filter_len;
142                         else
143                                 filter_len = descent_filter_len;
144
145                         double f = 1/Math.exp(ddt/ filter_len);
146                         n = p * f + n * (1-f);
147
148                         set(n, in.time());
149                 }
150
151                 void set_integral(AltosValue in) {
152                         double  change = in.integrate();
153
154                         if (change != AltosLib.MISSING) {
155                                 double  prev = prev_value;
156                                 if (prev == AltosLib.MISSING)
157                                         prev = 0;
158                                 set(prev + change, in.time());
159                         }
160                 }
161
162                 void copy(AltosValue old) {
163                         value = old.value;
164                         set_time = old.set_time;
165                         prev_value = old.value;
166                         prev_set_time = old.set_time;
167                         max_value = old.max_value;
168                 }
169
170                 void finish_update() {
171                         prev_value = value;
172                         prev_set_time = set_time;
173                 }
174
175                 AltosValue() {
176                         value = AltosLib.MISSING;
177                         prev_value = AltosLib.MISSING;
178                         max_value = AltosLib.MISSING;
179                 }
180         }
181
182         class AltosCValue {
183                 AltosValue      measured;
184                 AltosValue      computed;
185
186                 double value() {
187                         double v = measured.value();
188                         if (v != AltosLib.MISSING)
189                                 return v;
190                         return computed.value();
191                 }
192
193                 boolean is_measured() {
194                         return measured.value() != AltosLib.MISSING;
195                 }
196
197                 double max() {
198                         double m = measured.max();
199
200                         if (m != AltosLib.MISSING)
201                                 return m;
202                         return computed.max();
203                 }
204
205                 double prev_value() {
206                         if (measured.value != AltosLib.MISSING && measured.prev_value != AltosLib.MISSING)
207                                 return measured.prev_value;
208                         return computed.prev_value;
209                 }
210
211                 AltosValue altos_value() {
212                         if (measured.value() != AltosLib.MISSING)
213                                 return measured;
214                         return computed;
215                 }
216
217                 double change() {
218                         double c = measured.change();
219                         if (c == AltosLib.MISSING)
220                                 c = computed.change();
221                         return c;
222                 }
223
224                 double rate() {
225                         double r = measured.rate();
226                         if (r == AltosLib.MISSING)
227                                 r = computed.rate();
228                         return r;
229                 }
230
231                 void set_measured(double new_value, double time) {
232                         measured.set(new_value, time);
233                 }
234
235                 void set_computed(double new_value, double time) {
236                         computed.set(new_value, time);
237                 }
238
239                 void set_derivative(AltosValue in) {
240                         computed.set_derivative(in);
241                 }
242
243                 void set_derivative(AltosCValue in) {
244                         set_derivative(in.altos_value());
245                 }
246
247                 void set_integral(AltosValue in) {
248                         computed.set_integral(in);
249                 }
250
251                 void set_integral(AltosCValue in) {
252                         set_integral(in.altos_value());
253                 }
254
255                 void copy(AltosCValue old) {
256                         measured.copy(old.measured);
257                         computed.copy(old.computed);
258                 }
259
260                 void finish_update() {
261                         measured.finish_update();
262                         computed.finish_update();
263                 }
264
265                 AltosCValue() {
266                         measured = new AltosValue();
267                         computed = new AltosValue();
268                 }
269         }
270
271         public int      state;
272         public int      flight;
273         public int      serial;
274         public int      altitude_32;
275         public int      receiver_serial;
276         public boolean  landed;
277         public boolean  ascent; /* going up? */
278         public boolean  boost;  /* under power */
279         public int      rssi;
280         public int      status;
281         public int      device_type;
282         public int      config_major;
283         public int      config_minor;
284         public int      apogee_delay;
285         public int      main_deploy;
286         public int      flight_log_max;
287
288         private double pressure_to_altitude(double p) {
289                 if (p == AltosLib.MISSING)
290                         return AltosLib.MISSING;
291                 return AltosConvert.pressure_to_altitude(p);
292         }
293
294         private AltosCValue ground_altitude;
295
296         public double ground_altitude() {
297                 return ground_altitude.value();
298         }
299
300         public void set_ground_altitude(double a) {
301                 ground_altitude.set_measured(a, time);
302         }
303
304         class AltosGpsGroundAltitude extends AltosValue {
305                 void set(double a, double t) {
306                         super.set(a, t);
307                         pad_alt = value();
308                         gps_altitude.set_gps_height();
309                 }
310
311                 void set_filtered(double a, double t) {
312                         super.set_filtered(a, t);
313                         pad_alt = value();
314                         gps_altitude.set_gps_height();
315                 }
316         }
317
318         private AltosGpsGroundAltitude gps_ground_altitude;
319
320         public double gps_ground_altitude() {
321                 return gps_ground_altitude.value();
322         }
323
324         public void set_gps_ground_altitude(double a) {
325                 gps_ground_altitude.set(a, time);
326         }
327
328         class AltosGroundPressure extends AltosCValue {
329                 void set_filtered(double p, double time) {
330                         computed.set_filtered(p, time);
331                         if (!is_measured())
332                                 ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
333                 }
334
335                 void set_measured(double p, double time) {
336                         super.set_measured(p, time);
337                         ground_altitude.set_computed(pressure_to_altitude(p), time);
338                 }
339         }
340
341         private AltosGroundPressure ground_pressure;
342
343         public double ground_pressure() {
344                 return ground_pressure.value();
345         }
346
347         public void set_ground_pressure (double pressure) {
348                 ground_pressure.set_measured(pressure, time);
349         }
350
351         class AltosAltitude extends AltosCValue {
352
353                 private void set_speed(AltosValue v) {
354                         if (!acceleration.is_measured() || !ascent)
355                                 speed.set_derivative(this);
356                 }
357
358                 void set_computed(double a, double time) {
359                         super.set_computed(a,time);
360                         set_speed(computed);
361                         set |= set_position;
362                 }
363
364                 void set_measured(double a, double time) {
365                         super.set_measured(a,time);
366                         set_speed(measured);
367                         set |= set_position;
368                 }
369         }
370
371         private AltosAltitude   altitude;
372
373         class AltosGpsAltitude extends AltosValue {
374
375                 private void set_gps_height() {
376                         double  a = value();
377                         double  g = gps_ground_altitude.value();
378
379                         if (a != AltosLib.MISSING && g != AltosLib.MISSING)
380                                 gps_height = a - g;
381                         else
382                                 gps_height = AltosLib.MISSING;
383                 }
384
385                 void set(double a, double t) {
386                         super.set(a, t);
387                         set_gps_height();
388                 }
389         }
390
391         private AltosGpsAltitude        gps_altitude;
392
393         private AltosValue              gps_ground_speed;
394         private AltosValue              gps_ascent_rate;
395         private AltosValue              gps_course;
396         private AltosValue              gps_speed;
397
398         public double altitude() {
399                 double a = altitude.value();
400                 if (a != AltosLib.MISSING)
401                         return a;
402                 return gps_altitude.value();
403         }
404
405         public double max_altitude() {
406                 double a = altitude.max();
407                 if (a != AltosLib.MISSING)
408                         return a;
409                 return gps_altitude.max();
410         }
411
412         public void set_altitude(double new_altitude) {
413                 altitude.set_measured(new_altitude, time);
414         }
415
416         public double gps_altitude() {
417                 return gps_altitude.value();
418         }
419
420         public double max_gps_altitude() {
421                 return gps_altitude.max();
422         }
423
424         public void set_gps_altitude(double new_gps_altitude) {
425                 gps_altitude.set(new_gps_altitude, time);
426         }
427
428         public double gps_ground_speed() {
429                 return gps_ground_speed.value();
430         }
431
432         public double max_gps_ground_speed() {
433                 return gps_ground_speed.max();
434         }
435
436         public double gps_ascent_rate() {
437                 return gps_ascent_rate.value();
438         }
439
440         public double max_gps_ascent_rate() {
441                 return gps_ascent_rate.max();
442         }
443
444         public double gps_course() {
445                 return gps_course.value();
446         }
447
448         public double gps_speed() {
449                 return gps_speed.value();
450         }
451
452         public double max_gps_speed() {
453                 return gps_speed.max();
454         }
455
456         class AltosPressure extends AltosValue {
457                 void set(double p, double time) {
458                         super.set(p, time);
459                         if (state == AltosLib.ao_flight_pad)
460                                 ground_pressure.set_filtered(p, time);
461                         double a = pressure_to_altitude(p);
462                         altitude.set_computed(a, time);
463                 }
464         }
465
466         private AltosPressure   pressure;
467
468         public double pressure() {
469                 return pressure.value();
470         }
471
472         public void set_pressure(double p) {
473                 pressure.set(p, time);
474         }
475
476         public double baro_height() {
477                 double a = altitude();
478                 double g = ground_altitude();
479                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
480                         return a - g;
481                 return AltosLib.MISSING;
482         }
483
484         public double height() {
485                 double k = kalman_height.value();
486                 if (k != AltosLib.MISSING)
487                         return k;
488
489                 double b = baro_height();
490                 if (b != AltosLib.MISSING)
491                         return b;
492
493                 return gps_height();
494         }
495
496         public double max_height() {
497                 double  k = kalman_height.max();
498                 if (k != AltosLib.MISSING)
499                         return k;
500
501                 double a = altitude.max();
502                 double g = ground_altitude();
503                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
504                         return a - g;
505                 return max_gps_height();
506         }
507
508         public double gps_height() {
509                 double a = gps_altitude();
510                 double g = gps_ground_altitude();
511
512                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
513                         return a - g;
514                 return AltosLib.MISSING;
515         }
516
517         public double max_gps_height() {
518                 double a = gps_altitude.max();
519                 double g = gps_ground_altitude();
520
521                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
522                         return a - g;
523                 return AltosLib.MISSING;
524         }
525
526         class AltosSpeed extends AltosCValue {
527
528                 boolean can_max() {
529                         return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
530                 }
531
532                 void set_accel() {
533                         acceleration.set_derivative(this);
534                 }
535
536                 void set_derivative(AltosCValue in) {
537                         super.set_derivative(in);
538                         set_accel();
539                 }
540
541                 void set_computed(double new_value, double time) {
542                         super.set_computed(new_value, time);
543                         set_accel();
544                 }
545
546                 void set_measured(double new_value, double time) {
547                         super.set_measured(new_value, time);
548                         set_accel();
549                 }
550         }
551
552         private AltosSpeed speed;
553
554         public double speed() {
555                 double v = kalman_speed.value();
556                 if (v != AltosLib.MISSING)
557                         return v;
558                 v = speed.value();
559                 if (v != AltosLib.MISSING)
560                         return v;
561                 v = gps_speed();
562                 if (v != AltosLib.MISSING)
563                         return v;
564                 return AltosLib.MISSING;
565         }
566
567         public double max_speed() {
568                 double v = kalman_speed.max();
569                 if (v != AltosLib.MISSING)
570                         return v;
571                 v = speed.max();
572                 if (v != AltosLib.MISSING)
573                         return v;
574                 v = max_gps_speed();
575                 if (v != AltosLib.MISSING)
576                         return v;
577                 return AltosLib.MISSING;
578         }
579
580         class AltosAccel extends AltosCValue {
581
582                 boolean can_max() {
583                         return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
584                 }
585
586                 void set_measured(double a, double time) {
587                         super.set_measured(a, time);
588                         if (ascent)
589                                 speed.set_integral(this.measured);
590                 }
591         }
592
593         AltosAccel acceleration;
594
595         public double acceleration() {
596                 return acceleration.value();
597         }
598
599         public double max_acceleration() {
600                 return acceleration.max();
601         }
602
603         public AltosValue       orient;
604
605         public void set_orient(double new_orient) {
606                 orient.set(new_orient, time);
607         }
608
609         public double orient() {
610                 return orient.value();
611         }
612
613         public double max_orient() {
614                 return orient.max();
615         }
616
617         public AltosValue       kalman_height, kalman_speed, kalman_acceleration;
618
619         public void set_kalman(double height, double speed, double acceleration) {
620                 kalman_height.set(height, time);
621                 kalman_speed.set(speed, time);
622                 kalman_acceleration.set(acceleration, time);
623         }
624
625         public double   battery_voltage;
626         public double   pyro_voltage;
627         public double   temperature;
628         public double   apogee_voltage;
629         public double   main_voltage;
630
631         public double   ignitor_voltage[];
632
633         public AltosGPS gps;
634         public AltosGPS temp_gps;
635         public int temp_gps_sat_tick;
636         public boolean  gps_pending;
637         public int gps_sequence;
638
639         public AltosIMU imu;
640         public AltosMag mag;
641
642         public static final int MIN_PAD_SAMPLES = 10;
643
644         public int      npad;
645         public int      gps_waiting;
646         public boolean  gps_ready;
647
648         public int      ngps;
649
650         public AltosGreatCircle from_pad;
651         public double   elevation;      /* from pad */
652         public double   range;          /* total distance */
653
654         public double   gps_height;
655
656         public double pad_lat, pad_lon, pad_alt;
657
658         public int      speak_tick;
659         public double   speak_altitude;
660
661         public String   callsign;
662         public String   firmware_version;
663
664         public double   accel_plus_g;
665         public double   accel_minus_g;
666         public double   accel;
667         public double   ground_accel;
668         public double   ground_accel_avg;
669
670         public int      log_format;
671         public String   product;
672
673         public AltosMs5607      baro;
674
675         public AltosCompanion   companion;
676
677         public int      pyro_fired;
678
679         public void set_npad(int npad) {
680                 this.npad = npad;
681                 gps_waiting = MIN_PAD_SAMPLES - npad;
682                 if (this.gps_waiting < 0)
683                         gps_waiting = 0;
684                 gps_ready = gps_waiting == 0;
685         }
686
687         public void init() {
688                 set = 0;
689
690                 received_time = System.currentTimeMillis();
691                 time = AltosLib.MISSING;
692                 time_change = AltosLib.MISSING;
693                 prev_time = AltosLib.MISSING;
694                 tick = AltosLib.MISSING;
695                 prev_tick = AltosLib.MISSING;
696                 boost_tick = AltosLib.MISSING;
697                 state = AltosLib.ao_flight_invalid;
698                 flight = AltosLib.MISSING;
699                 landed = false;
700                 boost = false;
701                 rssi = AltosLib.MISSING;
702                 status = 0;
703                 device_type = AltosLib.MISSING;
704                 config_major = AltosLib.MISSING;
705                 config_minor = AltosLib.MISSING;
706                 apogee_delay = AltosLib.MISSING;
707                 main_deploy = AltosLib.MISSING;
708                 flight_log_max = AltosLib.MISSING;
709
710                 ground_altitude = new AltosCValue();
711                 ground_pressure = new AltosGroundPressure();
712                 altitude = new AltosAltitude();
713                 pressure = new AltosPressure();
714                 speed = new AltosSpeed();
715                 acceleration = new AltosAccel();
716                 orient = new AltosValue();
717
718                 temperature = AltosLib.MISSING;
719                 battery_voltage = AltosLib.MISSING;
720                 pyro_voltage = AltosLib.MISSING;
721                 apogee_voltage = AltosLib.MISSING;
722                 main_voltage = AltosLib.MISSING;
723                 ignitor_voltage = null;
724
725                 kalman_height = new AltosValue();
726                 kalman_speed = new AltosValue();
727                 kalman_acceleration = new AltosValue();
728
729                 gps = null;
730                 temp_gps = null;
731                 temp_gps_sat_tick = 0;
732                 gps_sequence = 0;
733                 gps_pending = false;
734
735                 imu = null;
736                 mag = null;
737
738                 set_npad(0);
739                 ngps = 0;
740
741                 from_pad = null;
742                 elevation = AltosLib.MISSING;
743                 range = AltosLib.MISSING;
744                 gps_height = AltosLib.MISSING;
745
746                 pad_lat = AltosLib.MISSING;
747                 pad_lon = AltosLib.MISSING;
748                 pad_alt = AltosLib.MISSING;
749
750                 gps_altitude = new AltosGpsAltitude();
751                 gps_ground_altitude = new AltosGpsGroundAltitude();
752                 gps_ground_speed = new AltosValue();
753                 gps_speed = new AltosValue();
754                 gps_ascent_rate = new AltosValue();
755                 gps_course = new AltosValue();
756
757                 speak_tick = AltosLib.MISSING;
758                 speak_altitude = AltosLib.MISSING;
759
760                 callsign = null;
761                 firmware_version = null;
762
763                 accel_plus_g = AltosLib.MISSING;
764                 accel_minus_g = AltosLib.MISSING;
765                 accel = AltosLib.MISSING;
766
767                 ground_accel = AltosLib.MISSING;
768                 ground_accel_avg = AltosLib.MISSING;
769
770                 log_format = AltosLib.MISSING;
771                 product = null;
772                 serial = AltosLib.MISSING;
773                 receiver_serial = AltosLib.MISSING;
774                 altitude_32 = AltosLib.MISSING;
775
776                 baro = null;
777                 companion = null;
778
779                 pyro_fired = 0;
780         }
781
782         void finish_update() {
783                 prev_tick = tick;
784
785                 ground_altitude.finish_update();
786                 altitude.finish_update();
787                 pressure.finish_update();
788                 speed.finish_update();
789                 acceleration.finish_update();
790                 orient.finish_update();
791
792                 kalman_height.finish_update();
793                 kalman_speed.finish_update();
794                 kalman_acceleration.finish_update();
795         }
796
797         void copy(AltosState old) {
798
799                 if (old == null) {
800                         init();
801                         return;
802                 }
803
804                 received_time = old.received_time;
805                 time = old.time;
806                 time_change = old.time_change;
807                 prev_time = old.time;
808
809                 tick = old.tick;
810                 prev_tick = old.tick;
811                 boost_tick = old.boost_tick;
812
813                 state = old.state;
814                 flight = old.flight;
815                 landed = old.landed;
816                 ascent = old.ascent;
817                 boost = old.boost;
818                 rssi = old.rssi;
819                 status = old.status;
820                 device_type = old.device_type;
821                 config_major = old.config_major;
822                 config_minor = old.config_minor;
823                 apogee_delay = old.apogee_delay;
824                 main_deploy = old.main_deploy;
825                 flight_log_max = old.flight_log_max;
826
827                 set = 0;
828
829                 ground_pressure.copy(old.ground_pressure);
830                 ground_altitude.copy(old.ground_altitude);
831                 altitude.copy(old.altitude);
832                 pressure.copy(old.pressure);
833                 speed.copy(old.speed);
834                 acceleration.copy(old.acceleration);
835                 orient.copy(old.orient);
836
837                 battery_voltage = old.battery_voltage;
838                 pyro_voltage = old.pyro_voltage;
839                 temperature = old.temperature;
840                 apogee_voltage = old.apogee_voltage;
841                 main_voltage = old.main_voltage;
842                 ignitor_voltage = old.ignitor_voltage;
843
844                 kalman_height.copy(old.kalman_height);
845                 kalman_speed.copy(old.kalman_speed);
846                 kalman_acceleration.copy(old.kalman_acceleration);
847
848                 if (old.gps != null)
849                         gps = old.gps.clone();
850                 else
851                         gps = null;
852                 if (old.temp_gps != null)
853                         temp_gps = old.temp_gps.clone();
854                 else
855                         temp_gps = null;
856                 temp_gps_sat_tick = old.temp_gps_sat_tick;
857                 gps_sequence = old.gps_sequence;
858                 gps_pending = old.gps_pending;
859
860                 if (old.imu != null)
861                         imu = old.imu.clone();
862                 else
863                         imu = null;
864
865                 if (old.mag != null)
866                         mag = old.mag.clone();
867                 else
868                         mag = null;
869
870                 npad = old.npad;
871                 gps_waiting = old.gps_waiting;
872                 gps_ready = old.gps_ready;
873                 ngps = old.ngps;
874
875                 if (old.from_pad != null)
876                         from_pad = old.from_pad.clone();
877                 else
878                         from_pad = null;
879
880                 elevation = old.elevation;
881                 range = old.range;
882
883                 gps_height = old.gps_height;
884
885                 gps_altitude.copy(old.gps_altitude);
886                 gps_ground_altitude.copy(old.gps_ground_altitude);
887                 gps_ground_speed.copy(old.gps_ground_speed);
888                 gps_ascent_rate.copy(old.gps_ascent_rate);
889                 gps_course.copy(old.gps_course);
890                 gps_speed.copy(old.gps_speed);
891
892                 pad_lat = old.pad_lat;
893                 pad_lon = old.pad_lon;
894                 pad_alt = old.pad_alt;
895
896                 speak_tick = old.speak_tick;
897                 speak_altitude = old.speak_altitude;
898
899                 callsign = old.callsign;
900                 firmware_version = old.firmware_version;
901
902                 accel_plus_g = old.accel_plus_g;
903                 accel_minus_g = old.accel_minus_g;
904                 accel = old.accel;
905                 ground_accel = old.ground_accel;
906                 ground_accel_avg = old.ground_accel_avg;
907
908                 log_format = old.log_format;
909                 product = old.product;
910                 serial = old.serial;
911                 receiver_serial = old.receiver_serial;
912                 altitude_32 = old.altitude_32;
913
914                 baro = old.baro;
915                 companion = old.companion;
916
917                 pyro_fired = old.pyro_fired;
918         }
919
920         void update_time() {
921         }
922
923         void update_gps() {
924                 elevation = AltosLib.MISSING;
925                 range = AltosLib.MISSING;
926
927                 if (gps == null)
928                         return;
929
930                 if (gps.locked && gps.nsat >= 4) {
931                         /* Track consecutive 'good' gps reports, waiting for 10 of them */
932                         if (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_stateless) {
933                                 set_npad(npad+1);
934                                 if (pad_lat != AltosLib.MISSING && (npad < 10 || state == AltosLib.ao_flight_pad)) {
935                                         pad_lat = (pad_lat * 31 + gps.lat) / 32;
936                                         pad_lon = (pad_lon * 31 + gps.lon) / 32;
937                                         gps_ground_altitude.set_filtered(gps.alt, time);
938                                 }
939                         }
940                         if (pad_lat == AltosLib.MISSING) {
941                                 pad_lat = gps.lat;
942                                 pad_lon = gps.lon;
943                                 gps_ground_altitude.set(gps.alt, time);
944                         }
945                         gps_altitude.set(gps.alt, time);
946                         if (gps.climb_rate != AltosLib.MISSING)
947                                 gps_ascent_rate.set(gps.climb_rate, time);
948                         if (gps.ground_speed != AltosLib.MISSING)
949                                 gps_ground_speed.set(gps.ground_speed, time);
950                         if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
951                                 gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
952                                                         gps.climb_rate * gps.climb_rate), time);
953                         if (gps.course != AltosLib.MISSING)
954                                 gps_course.set(gps.course, time);
955                 }
956                 if (gps.lat != 0 && gps.lon != 0 &&
957                     pad_lat != AltosLib.MISSING &&
958                     pad_lon != AltosLib.MISSING)
959                 {
960                         double h = height();
961
962                         if (h == AltosLib.MISSING)
963                                 h = 0;
964                         from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
965                         elevation = from_pad.elevation;
966                         range = from_pad.range;
967                 }
968         }
969
970         public void set_tick(int new_tick) {
971                 if (new_tick != AltosLib.MISSING) {
972                         if (prev_tick != AltosLib.MISSING) {
973                                 while (new_tick < prev_tick - 1000) {
974                                         new_tick += 65536;
975                                 }
976                         }
977                         tick = new_tick;
978                         time = tick / 100.0;
979                         time_change = time - prev_time;
980                 }
981         }
982
983         public void set_boost_tick(int boost_tick) {
984                 if (boost_tick != AltosLib.MISSING)
985                         this.boost_tick = boost_tick;
986         }
987
988         public String state_name() {
989                 return AltosLib.state_name(state);
990         }
991
992         public void set_state(int state) {
993                 if (state != AltosLib.ao_flight_invalid) {
994                         this.state = state;
995                         ascent = (AltosLib.ao_flight_boost <= state &&
996                                   state <= AltosLib.ao_flight_coast);
997                         boost = (AltosLib.ao_flight_boost == state);
998                 }
999         }
1000
1001         public void set_device_type(int device_type) {
1002                 this.device_type = device_type;
1003                 switch (device_type) {
1004                 case AltosLib.product_telegps:
1005                         this.state = AltosLib.ao_flight_stateless;
1006                         break;
1007                 }
1008         }
1009
1010         public void set_log_format(int log_format) {
1011                 this.log_format = log_format;
1012                 switch (log_format) {
1013                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
1014                         this.state = AltosLib.ao_flight_stateless;
1015                         break;
1016                 }
1017         }
1018
1019         public void set_flight_params(int apogee_delay, int main_deploy) {
1020                 this.apogee_delay = apogee_delay;
1021                 this.main_deploy = main_deploy;
1022         }
1023
1024         public void set_config(int major, int minor, int flight_log_max) {
1025                 config_major = major;
1026                 config_minor = minor;
1027                 this.flight_log_max = flight_log_max;
1028         }
1029
1030         public void set_callsign(String callsign) {
1031                 this.callsign = callsign;
1032         }
1033
1034         public void set_firmware_version(String version) {
1035                 firmware_version = version;
1036         }
1037
1038         public int compare_version(String other_version) {
1039                 if (firmware_version == null)
1040                         return AltosLib.MISSING;
1041                 return AltosLib.compare_version(firmware_version, other_version);
1042         }
1043
1044         private void re_init() {
1045                 int bt = boost_tick;
1046                 int rs = receiver_serial;
1047                 init();
1048                 boost_tick = bt;
1049                 receiver_serial = rs;
1050         }
1051
1052         public void set_flight(int flight) {
1053
1054                 /* When the flight changes, reset the state */
1055                 if (flight != AltosLib.MISSING) {
1056                         if (this.flight != AltosLib.MISSING &&
1057                             this.flight != flight) {
1058                                 re_init();
1059                         }
1060                         this.flight = flight;
1061                 }
1062         }
1063
1064         public void set_serial(int serial) {
1065                 /* When the serial changes, reset the state */
1066                 if (serial != AltosLib.MISSING) {
1067                         if (this.serial != AltosLib.MISSING &&
1068                             this.serial != serial) {
1069                                 re_init();
1070                         }
1071                         this.serial = serial;
1072                 }
1073         }
1074
1075         public void set_receiver_serial(int serial) {
1076                 if (serial != AltosLib.MISSING)
1077                         receiver_serial = serial;
1078         }
1079
1080         public boolean altitude_32() {
1081                 return altitude_32 == 1;
1082         }
1083
1084         public void set_altitude_32(int altitude_32) {
1085                 if (altitude_32 != AltosLib.MISSING)
1086                         this.altitude_32 = altitude_32;
1087         }
1088
1089         public int rssi() {
1090                 if (rssi == AltosLib.MISSING)
1091                         return 0;
1092                 return rssi;
1093         }
1094
1095         public void set_rssi(int rssi, int status) {
1096                 if (rssi != AltosLib.MISSING) {
1097                         this.rssi = rssi;
1098                         this.status = status;
1099                 }
1100         }
1101
1102         public void set_received_time(long ms) {
1103                 received_time = ms;
1104         }
1105
1106         public void set_gps(AltosGPS gps, int sequence) {
1107                 if (gps != null) {
1108                         this.gps = gps.clone();
1109                         gps_sequence = sequence;
1110                         update_gps();
1111                         set |= set_gps;
1112                 }
1113         }
1114
1115         public void set_imu(AltosIMU imu) {
1116                 if (imu != null)
1117                         imu = imu.clone();
1118                 this.imu = imu;
1119         }
1120
1121         public void set_mag(AltosMag mag) {
1122                 this.mag = mag.clone();
1123         }
1124
1125         public AltosMs5607 make_baro() {
1126                 if (baro == null)
1127                         baro = new AltosMs5607();
1128                 return baro;
1129         }
1130
1131         public void set_ms5607(AltosMs5607 ms5607) {
1132                 baro = ms5607;
1133
1134                 if (baro != null) {
1135                         set_pressure(baro.pa);
1136                         set_temperature(baro.cc / 100.0);
1137                 }
1138         }
1139
1140         public void set_ms5607(int pres, int temp) {
1141                 if (baro != null) {
1142                         baro.set(pres, temp);
1143
1144                         set_pressure(baro.pa);
1145                         set_temperature(baro.cc / 100.0);
1146                 }
1147         }
1148
1149         public void make_companion (int nchannels) {
1150                 if (companion == null)
1151                         companion = new AltosCompanion(nchannels);
1152         }
1153
1154         public void set_companion(AltosCompanion companion) {
1155                 this.companion = companion;
1156         }
1157
1158         void update_accel() {
1159                 if (accel == AltosLib.MISSING)
1160                         return;
1161                 if (accel_plus_g == AltosLib.MISSING)
1162                         return;
1163                 if (accel_minus_g == AltosLib.MISSING)
1164                         return;
1165
1166                 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
1167                 double counts_per_mss = counts_per_g / 9.80665;
1168                 acceleration.set_measured((accel_plus_g - accel) / counts_per_mss, time);
1169         }
1170
1171         public void set_accel_g(double accel_plus_g, double accel_minus_g) {
1172                 if (accel_plus_g != AltosLib.MISSING) {
1173                         this.accel_plus_g = accel_plus_g;
1174                         this.accel_minus_g = accel_minus_g;
1175                         update_accel();
1176                 }
1177         }
1178
1179         public void set_ground_accel(double ground_accel) {
1180                 if (ground_accel != AltosLib.MISSING)
1181                         this.ground_accel = ground_accel;
1182         }
1183
1184         public void set_accel(double accel) {
1185                 if (accel != AltosLib.MISSING) {
1186                         this.accel = accel;
1187                         if (state == AltosLib.ao_flight_pad) {
1188                                 if (ground_accel_avg == AltosLib.MISSING)
1189                                         ground_accel_avg = accel;
1190                                 else
1191                                         ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
1192                         }
1193                 }
1194                 update_accel();
1195         }
1196
1197         public void set_temperature(double temperature) {
1198                 if (temperature != AltosLib.MISSING) {
1199                         this.temperature = temperature;
1200                         set |= set_data;
1201                 }
1202         }
1203
1204         public void set_battery_voltage(double battery_voltage) {
1205                 if (battery_voltage != AltosLib.MISSING) {
1206                         this.battery_voltage = battery_voltage;
1207                         set |= set_data;
1208                 }
1209         }
1210
1211         public void set_pyro_voltage(double pyro_voltage) {
1212                 if (pyro_voltage != AltosLib.MISSING) {
1213                         this.pyro_voltage = pyro_voltage;
1214                         set |= set_data;
1215                 }
1216         }
1217
1218         public void set_apogee_voltage(double apogee_voltage) {
1219                 if (apogee_voltage != AltosLib.MISSING) {
1220                         this.apogee_voltage = apogee_voltage;
1221                         set |= set_data;
1222                 }
1223         }
1224
1225         public void set_main_voltage(double main_voltage) {
1226                 if (main_voltage != AltosLib.MISSING) {
1227                         this.main_voltage = main_voltage;
1228                         set |= set_data;
1229                 }
1230         }
1231
1232         public void set_ignitor_voltage(double[] voltage) {
1233                 this.ignitor_voltage = voltage;
1234         }
1235
1236         public void set_pyro_fired(int fired) {
1237                 this.pyro_fired = fired;
1238         }
1239
1240         public double time_since_boost() {
1241                 if (tick == AltosLib.MISSING)
1242                         return 0.0;
1243
1244                 if (boost_tick == AltosLib.MISSING)
1245                         return tick / 100.0;
1246                 return (tick - boost_tick) / 100.0;
1247         }
1248
1249         public boolean valid() {
1250                 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1251         }
1252
1253         public AltosGPS make_temp_gps(boolean sats) {
1254                 if (temp_gps == null) {
1255                         temp_gps = new AltosGPS(gps);
1256                 }
1257                 gps_pending = true;
1258                 if (sats) {
1259                         if (tick != temp_gps_sat_tick)
1260                                 temp_gps.cc_gps_sat = null;
1261                         temp_gps_sat_tick = tick;
1262                 }
1263                 return temp_gps;
1264         }
1265
1266         public void set_temp_gps() {
1267                 set_gps(temp_gps, gps_sequence + 1);
1268                 gps_pending = false;
1269                 temp_gps = null;
1270         }
1271
1272         public AltosState clone() {
1273                 AltosState s = new AltosState();
1274                 s.copy(this);
1275                 return s;
1276         }
1277
1278         public AltosState () {
1279                 init();
1280         }
1281 }