b05cd3586061ad111d99084733f84865c360339e
[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_4;
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      receiver_serial;
275         public boolean  landed;
276         public boolean  ascent; /* going up? */
277         public boolean  boost;  /* under power */
278         public int      rssi;
279         public int      status;
280         public int      device_type;
281         public int      config_major;
282         public int      config_minor;
283         public int      apogee_delay;
284         public int      main_deploy;
285         public int      flight_log_max;
286
287         private double pressure_to_altitude(double p) {
288                 if (p == AltosLib.MISSING)
289                         return AltosLib.MISSING;
290                 return AltosConvert.pressure_to_altitude(p);
291         }
292
293         private AltosCValue ground_altitude;
294
295         public double ground_altitude() {
296                 return ground_altitude.value();
297         }
298
299         public void set_ground_altitude(double a) {
300                 ground_altitude.set_measured(a, time);
301         }
302
303         class AltosGpsGroundAltitude extends AltosValue {
304                 void set(double a, double t) {
305                         super.set(a, t);
306                         pad_alt = value();
307                         gps_altitude.set_gps_height();
308                 }
309
310                 void set_filtered(double a, double t) {
311                         super.set_filtered(a, t);
312                         pad_alt = value();
313                         gps_altitude.set_gps_height();
314                 }
315         }
316
317         private AltosGpsGroundAltitude gps_ground_altitude;
318
319         public double gps_ground_altitude() {
320                 return gps_ground_altitude.value();
321         }
322
323         public void set_gps_ground_altitude(double a) {
324                 gps_ground_altitude.set(a, time);
325         }
326
327         class AltosGroundPressure extends AltosCValue {
328                 void set_filtered(double p, double time) {
329                         computed.set_filtered(p, time);
330                         if (!is_measured())
331                                 ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
332                 }
333
334                 void set_measured(double p, double time) {
335                         super.set_measured(p, time);
336                         ground_altitude.set_computed(pressure_to_altitude(p), time);
337                 }
338         }
339
340         private AltosGroundPressure ground_pressure;
341
342         public double ground_pressure() {
343                 return ground_pressure.value();
344         }
345
346         public void set_ground_pressure (double pressure) {
347                 ground_pressure.set_measured(pressure, time);
348         }
349
350         class AltosAltitude extends AltosCValue {
351
352                 private void set_speed(AltosValue v) {
353                         if (!acceleration.is_measured() || !ascent)
354                                 speed.set_derivative(this);
355                 }
356
357                 void set_computed(double a, double time) {
358                         super.set_computed(a,time);
359                         set_speed(computed);
360                         set |= set_position;
361                 }
362
363                 void set_measured(double a, double time) {
364                         super.set_measured(a,time);
365                         set_speed(measured);
366                         set |= set_position;
367                 }
368         }
369
370         private AltosAltitude   altitude;
371
372         class AltosGpsAltitude extends AltosValue {
373
374                 private void set_gps_height() {
375                         double  a = value();
376                         double  g = gps_ground_altitude.value();
377
378                         if (a != AltosLib.MISSING && g != AltosLib.MISSING)
379                                 gps_height = a - g;
380                         else
381                                 gps_height = AltosLib.MISSING;
382                 }
383
384                 void set(double a, double t) {
385                         super.set(a, t);
386                         set_gps_height();
387                 }
388         }
389
390         private AltosGpsAltitude        gps_altitude;
391
392         private AltosValue              gps_ground_speed;
393         private AltosValue              gps_ascent_rate;
394         private AltosValue              gps_course;
395         private AltosValue              gps_speed;
396
397         public double altitude() {
398                 double a = altitude.value();
399                 if (a != AltosLib.MISSING)
400                         return a;
401                 return gps_altitude.value();
402         }
403
404         public double max_altitude() {
405                 double a = altitude.max();
406                 if (a != AltosLib.MISSING)
407                         return a;
408                 return gps_altitude.max();
409         }
410
411         public void set_altitude(double new_altitude) {
412                 altitude.set_measured(new_altitude, time);
413         }
414
415         public double gps_altitude() {
416                 return gps_altitude.value();
417         }
418
419         public double max_gps_altitude() {
420                 return gps_altitude.max();
421         }
422
423         public void set_gps_altitude(double new_gps_altitude) {
424                 gps_altitude.set(new_gps_altitude, time);
425         }
426
427         public double gps_ground_speed() {
428                 return gps_ground_speed.value();
429         }
430
431         public double max_gps_ground_speed() {
432                 return gps_ground_speed.max();
433         }
434
435         public double gps_ascent_rate() {
436                 return gps_ascent_rate.value();
437         }
438
439         public double max_gps_ascent_rate() {
440                 return gps_ascent_rate.max();
441         }
442
443         public double gps_course() {
444                 return gps_course.value();
445         }
446
447         public double gps_speed() {
448                 return gps_speed.value();
449         }
450
451         public double max_gps_speed() {
452                 return gps_speed.max();
453         }
454
455         class AltosPressure extends AltosValue {
456                 void set(double p, double time) {
457                         super.set(p, time);
458                         if (state == AltosLib.ao_flight_pad)
459                                 ground_pressure.set_filtered(p, time);
460                         double a = pressure_to_altitude(p);
461                         altitude.set_computed(a, time);
462                 }
463         }
464
465         private AltosPressure   pressure;
466
467         public double pressure() {
468                 return pressure.value();
469         }
470
471         public void set_pressure(double p) {
472                 pressure.set(p, time);
473         }
474
475         public double height() {
476                 double k = kalman_height.value();
477                 if (k != AltosLib.MISSING)
478                         return k;
479
480                 double a = altitude();
481                 double g = ground_altitude();
482                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
483                         return a - g;
484                 return gps_height();
485         }
486
487         public double max_height() {
488                 double  k = kalman_height.max();
489                 if (k != AltosLib.MISSING)
490                         return k;
491
492                 double a = altitude.max();
493                 double g = ground_altitude();
494                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
495                         return a - g;
496                 return max_gps_height();
497         }
498
499         public double gps_height() {
500                 double a = gps_altitude();
501                 double g = gps_ground_altitude();
502
503                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
504                         return a - g;
505                 return AltosLib.MISSING;
506         }
507
508         public double max_gps_height() {
509                 double a = gps_altitude.max();
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         class AltosSpeed extends AltosCValue {
518
519                 boolean can_max() {
520                         return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
521                 }
522
523                 void set_accel() {
524                         acceleration.set_derivative(this);
525                 }
526
527                 void set_derivative(AltosCValue in) {
528                         super.set_derivative(in);
529                         set_accel();
530                 }
531
532                 void set_computed(double new_value, double time) {
533                         super.set_computed(new_value, time);
534                         set_accel();
535                 }
536
537                 void set_measured(double new_value, double time) {
538                         super.set_measured(new_value, time);
539                         set_accel();
540                 }
541         }
542
543         private AltosSpeed speed;
544
545         public double speed() {
546                 double v = kalman_speed.value();
547                 if (v != AltosLib.MISSING)
548                         return v;
549                 v = speed.value();
550                 if (v != AltosLib.MISSING)
551                         return v;
552                 v = gps_speed();
553                 if (v != AltosLib.MISSING)
554                         return v;
555                 return AltosLib.MISSING;
556         }
557
558         public double max_speed() {
559                 double v = kalman_speed.max();
560                 if (v != AltosLib.MISSING)
561                         return v;
562                 v = speed.max();
563                 if (v != AltosLib.MISSING)
564                         return v;
565                 v = max_gps_speed();
566                 if (v != AltosLib.MISSING)
567                         return v;
568                 return AltosLib.MISSING;
569         }
570
571         class AltosAccel extends AltosCValue {
572
573                 boolean can_max() {
574                         return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
575                 }
576
577                 void set_measured(double a, double time) {
578                         super.set_measured(a, time);
579                         if (ascent)
580                                 speed.set_integral(this.measured);
581                 }
582         }
583
584         AltosAccel acceleration;
585
586         public double acceleration() {
587                 return acceleration.value();
588         }
589
590         public double max_acceleration() {
591                 return acceleration.max();
592         }
593
594         public AltosValue       orient;
595
596         public void set_orient(double new_orient) {
597                 orient.set(new_orient, time);
598         }
599
600         public double orient() {
601                 return orient.value();
602         }
603
604         public double max_orient() {
605                 return orient.max();
606         }
607
608         public AltosValue       kalman_height, kalman_speed, kalman_acceleration;
609
610         public void set_kalman(double height, double speed, double acceleration) {
611                 kalman_height.set(height, time);
612                 kalman_speed.set(speed, time);
613                 kalman_acceleration.set(acceleration, time);
614         }
615
616         public double   battery_voltage;
617         public double   pyro_voltage;
618         public double   temperature;
619         public double   apogee_voltage;
620         public double   main_voltage;
621
622         public double   ignitor_voltage[];
623
624         public AltosGPS gps;
625         public AltosGPS temp_gps;
626         public int temp_gps_sat_tick;
627         public boolean  gps_pending;
628         public int gps_sequence;
629
630         public AltosIMU imu;
631         public AltosMag mag;
632
633         public static final int MIN_PAD_SAMPLES = 10;
634
635         public int      npad;
636         public int      gps_waiting;
637         public boolean  gps_ready;
638
639         public int      ngps;
640
641         public AltosGreatCircle from_pad;
642         public double   elevation;      /* from pad */
643         public double   range;          /* total distance */
644
645         public double   gps_height;
646
647         public double pad_lat, pad_lon, pad_alt;
648
649         public int      speak_tick;
650         public double   speak_altitude;
651
652         public String   callsign;
653         public String   firmware_version;
654
655         public double   accel_plus_g;
656         public double   accel_minus_g;
657         public double   accel;
658         public double   ground_accel;
659         public double   ground_accel_avg;
660
661         public int      log_format;
662         public String   product;
663
664         public AltosMs5607      baro;
665
666         public AltosCompanion   companion;
667
668         public int      pyro_fired;
669
670         public void set_npad(int npad) {
671                 this.npad = npad;
672                 gps_waiting = MIN_PAD_SAMPLES - npad;
673                 if (this.gps_waiting < 0)
674                         gps_waiting = 0;
675                 gps_ready = gps_waiting == 0;
676         }
677
678         public void init() {
679                 set = 0;
680
681                 received_time = System.currentTimeMillis();
682                 time = AltosLib.MISSING;
683                 time_change = AltosLib.MISSING;
684                 prev_time = AltosLib.MISSING;
685                 tick = AltosLib.MISSING;
686                 prev_tick = AltosLib.MISSING;
687                 boost_tick = AltosLib.MISSING;
688                 state = AltosLib.ao_flight_invalid;
689                 flight = AltosLib.MISSING;
690                 landed = false;
691                 boost = false;
692                 rssi = AltosLib.MISSING;
693                 status = 0;
694                 device_type = AltosLib.MISSING;
695                 config_major = AltosLib.MISSING;
696                 config_minor = AltosLib.MISSING;
697                 apogee_delay = AltosLib.MISSING;
698                 main_deploy = AltosLib.MISSING;
699                 flight_log_max = AltosLib.MISSING;
700
701                 ground_altitude = new AltosCValue();
702                 ground_pressure = new AltosGroundPressure();
703                 altitude = new AltosAltitude();
704                 pressure = new AltosPressure();
705                 speed = new AltosSpeed();
706                 acceleration = new AltosAccel();
707                 orient = new AltosValue();
708
709                 temperature = AltosLib.MISSING;
710                 battery_voltage = AltosLib.MISSING;
711                 pyro_voltage = AltosLib.MISSING;
712                 apogee_voltage = AltosLib.MISSING;
713                 main_voltage = AltosLib.MISSING;
714                 ignitor_voltage = null;
715
716                 kalman_height = new AltosValue();
717                 kalman_speed = new AltosValue();
718                 kalman_acceleration = new AltosValue();
719
720                 gps = null;
721                 temp_gps = null;
722                 temp_gps_sat_tick = 0;
723                 gps_sequence = 0;
724                 gps_pending = false;
725
726                 imu = null;
727                 mag = null;
728
729                 set_npad(0);
730                 ngps = 0;
731
732                 from_pad = null;
733                 elevation = AltosLib.MISSING;
734                 range = AltosLib.MISSING;
735                 gps_height = AltosLib.MISSING;
736
737                 pad_lat = AltosLib.MISSING;
738                 pad_lon = AltosLib.MISSING;
739                 pad_alt = AltosLib.MISSING;
740
741                 gps_altitude = new AltosGpsAltitude();
742                 gps_ground_altitude = new AltosGpsGroundAltitude();
743                 gps_ground_speed = new AltosValue();
744                 gps_speed = new AltosValue();
745                 gps_ascent_rate = new AltosValue();
746                 gps_course = new AltosValue();
747
748                 speak_tick = AltosLib.MISSING;
749                 speak_altitude = AltosLib.MISSING;
750
751                 callsign = null;
752                 firmware_version = null;
753
754                 accel_plus_g = AltosLib.MISSING;
755                 accel_minus_g = AltosLib.MISSING;
756                 accel = AltosLib.MISSING;
757
758                 ground_accel = AltosLib.MISSING;
759                 ground_accel_avg = AltosLib.MISSING;
760
761                 log_format = AltosLib.MISSING;
762                 product = null;
763                 serial = AltosLib.MISSING;
764                 receiver_serial = AltosLib.MISSING;
765
766                 baro = null;
767                 companion = null;
768
769                 pyro_fired = 0;
770         }
771
772         void finish_update() {
773                 prev_tick = tick;
774
775                 ground_altitude.finish_update();
776                 altitude.finish_update();
777                 pressure.finish_update();
778                 speed.finish_update();
779                 acceleration.finish_update();
780                 orient.finish_update();
781
782                 kalman_height.finish_update();
783                 kalman_speed.finish_update();
784                 kalman_acceleration.finish_update();
785         }
786
787         void copy(AltosState old) {
788
789                 if (old == null) {
790                         init();
791                         return;
792                 }
793
794                 received_time = old.received_time;
795                 time = old.time;
796                 time_change = old.time_change;
797                 prev_time = old.time;
798
799                 tick = old.tick;
800                 prev_tick = old.tick;
801                 boost_tick = old.boost_tick;
802
803                 state = old.state;
804                 flight = old.flight;
805                 landed = old.landed;
806                 ascent = old.ascent;
807                 boost = old.boost;
808                 rssi = old.rssi;
809                 status = old.status;
810                 device_type = old.device_type;
811                 config_major = old.config_major;
812                 config_minor = old.config_minor;
813                 apogee_delay = old.apogee_delay;
814                 main_deploy = old.main_deploy;
815                 flight_log_max = old.flight_log_max;
816
817                 set = 0;
818
819                 ground_pressure.copy(old.ground_pressure);
820                 ground_altitude.copy(old.ground_altitude);
821                 altitude.copy(old.altitude);
822                 pressure.copy(old.pressure);
823                 speed.copy(old.speed);
824                 acceleration.copy(old.acceleration);
825                 orient.copy(old.orient);
826
827                 battery_voltage = old.battery_voltage;
828                 pyro_voltage = old.pyro_voltage;
829                 temperature = old.temperature;
830                 apogee_voltage = old.apogee_voltage;
831                 main_voltage = old.main_voltage;
832                 ignitor_voltage = old.ignitor_voltage;
833
834                 kalman_height.copy(old.kalman_height);
835                 kalman_speed.copy(old.kalman_speed);
836                 kalman_acceleration.copy(old.kalman_acceleration);
837
838                 if (old.gps != null)
839                         gps = old.gps.clone();
840                 else
841                         gps = null;
842                 if (old.temp_gps != null)
843                         temp_gps = old.temp_gps.clone();
844                 else
845                         temp_gps = null;
846                 temp_gps_sat_tick = old.temp_gps_sat_tick;
847                 gps_sequence = old.gps_sequence;
848                 gps_pending = old.gps_pending;
849
850                 if (old.imu != null)
851                         imu = old.imu.clone();
852                 else
853                         imu = null;
854
855                 if (old.mag != null)
856                         mag = old.mag.clone();
857                 else
858                         mag = null;
859
860                 npad = old.npad;
861                 gps_waiting = old.gps_waiting;
862                 gps_ready = old.gps_ready;
863                 ngps = old.ngps;
864
865                 if (old.from_pad != null)
866                         from_pad = old.from_pad.clone();
867                 else
868                         from_pad = null;
869
870                 elevation = old.elevation;
871                 range = old.range;
872
873                 gps_height = old.gps_height;
874
875                 gps_altitude.copy(old.gps_altitude);
876                 gps_ground_altitude.copy(old.gps_ground_altitude);
877                 gps_ground_speed.copy(old.gps_ground_speed);
878                 gps_ascent_rate.copy(old.gps_ascent_rate);
879                 gps_course.copy(old.gps_course);
880                 gps_speed.copy(old.gps_speed);
881
882                 pad_lat = old.pad_lat;
883                 pad_lon = old.pad_lon;
884                 pad_alt = old.pad_alt;
885
886                 speak_tick = old.speak_tick;
887                 speak_altitude = old.speak_altitude;
888
889                 callsign = old.callsign;
890                 firmware_version = old.firmware_version;
891
892                 accel_plus_g = old.accel_plus_g;
893                 accel_minus_g = old.accel_minus_g;
894                 accel = old.accel;
895                 ground_accel = old.ground_accel;
896                 ground_accel_avg = old.ground_accel_avg;
897
898                 log_format = old.log_format;
899                 product = old.product;
900                 serial = old.serial;
901                 receiver_serial = old.receiver_serial;
902
903                 baro = old.baro;
904                 companion = old.companion;
905
906                 pyro_fired = old.pyro_fired;
907         }
908
909         void update_time() {
910         }
911
912         void update_gps() {
913                 elevation = AltosLib.MISSING;
914                 range = AltosLib.MISSING;
915
916                 if (gps == null)
917                         return;
918
919                 if (gps.locked && gps.nsat >= 4) {
920                         /* Track consecutive 'good' gps reports, waiting for 10 of them */
921                         if (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_stateless) {
922                                 set_npad(npad+1);
923                                 if (pad_lat != AltosLib.MISSING && (npad < 10 || state == AltosLib.ao_flight_pad)) {
924                                         pad_lat = (pad_lat * 31 + gps.lat) / 32;
925                                         pad_lon = (pad_lon * 31 + gps.lon) / 32;
926                                         gps_ground_altitude.set_filtered(gps.alt, time);
927                                 }
928                         }
929                         if (pad_lat == AltosLib.MISSING) {
930                                 pad_lat = gps.lat;
931                                 pad_lon = gps.lon;
932                                 gps_ground_altitude.set(gps.alt, time);
933                         }
934                         gps_altitude.set(gps.alt, time);
935                         if (gps.climb_rate != AltosLib.MISSING)
936                                 gps_ascent_rate.set(gps.climb_rate, time);
937                         if (gps.ground_speed != AltosLib.MISSING)
938                                 gps_ground_speed.set(gps.ground_speed, time);
939                         if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
940                                 gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
941                                                         gps.climb_rate * gps.climb_rate), time);
942                         if (gps.course != AltosLib.MISSING)
943                                 gps_course.set(gps.course, time);
944                 }
945                 if (gps.lat != 0 && gps.lon != 0 &&
946                     pad_lat != AltosLib.MISSING &&
947                     pad_lon != AltosLib.MISSING)
948                 {
949                         double h = height();
950
951                         if (h == AltosLib.MISSING)
952                                 h = 0;
953                         from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
954                         elevation = from_pad.elevation;
955                         range = from_pad.range;
956                 }
957         }
958
959         public void set_tick(int new_tick) {
960                 if (new_tick != AltosLib.MISSING) {
961                         if (prev_tick != AltosLib.MISSING) {
962                                 while (new_tick < prev_tick - 1000) {
963                                         new_tick += 65536;
964                                 }
965                         }
966                         tick = new_tick;
967                         time = tick / 100.0;
968                         time_change = time - prev_time;
969                 }
970         }
971
972         public void set_boost_tick(int boost_tick) {
973                 if (boost_tick != AltosLib.MISSING)
974                         this.boost_tick = boost_tick;
975         }
976
977         public String state_name() {
978                 return AltosLib.state_name(state);
979         }
980
981         public void set_state(int state) {
982                 if (state != AltosLib.ao_flight_invalid) {
983                         this.state = state;
984                         ascent = (AltosLib.ao_flight_boost <= state &&
985                                   state <= AltosLib.ao_flight_coast);
986                         boost = (AltosLib.ao_flight_boost == state);
987                 }
988         }
989
990         public void set_device_type(int device_type) {
991                 this.device_type = device_type;
992                 switch (device_type) {
993                 case AltosLib.product_telegps:
994                         this.state = AltosLib.ao_flight_stateless;
995                         break;
996                 }
997         }
998
999         public void set_log_format(int log_format) {
1000                 this.log_format = log_format;
1001                 switch (log_format) {
1002                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
1003                         this.state = AltosLib.ao_flight_stateless;
1004                         break;
1005                 }
1006         }
1007
1008         public void set_flight_params(int apogee_delay, int main_deploy) {
1009                 this.apogee_delay = apogee_delay;
1010                 this.main_deploy = main_deploy;
1011         }
1012
1013         public void set_config(int major, int minor, int flight_log_max) {
1014                 config_major = major;
1015                 config_minor = minor;
1016                 this.flight_log_max = flight_log_max;
1017         }
1018
1019         public void set_callsign(String callsign) {
1020                 this.callsign = callsign;
1021         }
1022
1023         public void set_firmware_version(String version) {
1024                 firmware_version = version;
1025         }
1026
1027         public void set_flight(int flight) {
1028
1029                 /* When the flight changes, reset the state */
1030                 if (flight != AltosLib.MISSING && flight != 0) {
1031                         if (this.flight != AltosLib.MISSING &&
1032                             this.flight != flight) {
1033                                 int bt = boost_tick;
1034                                 init();
1035                                 boost_tick = bt;
1036                         }
1037                         this.flight = flight;
1038                 }
1039         }
1040
1041         public void set_serial(int serial) {
1042                 /* When the serial changes, reset the state */
1043                 if (serial != AltosLib.MISSING) {
1044                         if (this.serial != AltosLib.MISSING &&
1045                             this.serial != serial) {
1046                                 int bt = boost_tick;
1047                                 init();
1048                                 boost_tick = bt;
1049                         }
1050                         this.serial = serial;
1051                 }
1052         }
1053
1054         public void set_receiver_serial(int serial) {
1055                 if (serial != AltosLib.MISSING)
1056                         receiver_serial = serial;
1057         }
1058
1059         public int rssi() {
1060                 if (rssi == AltosLib.MISSING)
1061                         return 0;
1062                 return rssi;
1063         }
1064
1065         public void set_rssi(int rssi, int status) {
1066                 if (rssi != AltosLib.MISSING) {
1067                         this.rssi = rssi;
1068                         this.status = status;
1069                 }
1070         }
1071
1072         public void set_received_time(long ms) {
1073                 received_time = ms;
1074         }
1075
1076         public void set_gps(AltosGPS gps, int sequence) {
1077                 if (gps != null) {
1078                         this.gps = gps.clone();
1079                         gps_sequence = sequence;
1080                         update_gps();
1081                         set |= set_gps;
1082                 }
1083         }
1084
1085         public void set_imu(AltosIMU imu) {
1086                 if (imu != null)
1087                         imu = imu.clone();
1088                 this.imu = imu;
1089         }
1090
1091         public void set_mag(AltosMag mag) {
1092                 this.mag = mag.clone();
1093         }
1094
1095         public AltosMs5607 make_baro() {
1096                 if (baro == null)
1097                         baro = new AltosMs5607();
1098                 return baro;
1099         }
1100
1101         public void set_ms5607(AltosMs5607 ms5607) {
1102                 baro = ms5607;
1103
1104                 if (baro != null) {
1105                         set_pressure(baro.pa);
1106                         set_temperature(baro.cc / 100.0);
1107                 }
1108         }
1109
1110         public void set_ms5607(int pres, int temp) {
1111                 if (baro != null) {
1112                         baro.set(pres, temp);
1113
1114                         set_pressure(baro.pa);
1115                         set_temperature(baro.cc / 100.0);
1116                 }
1117         }
1118
1119         public void make_companion (int nchannels) {
1120                 if (companion == null)
1121                         companion = new AltosCompanion(nchannels);
1122         }
1123
1124         public void set_companion(AltosCompanion companion) {
1125                 this.companion = companion;
1126         }
1127
1128         void update_accel() {
1129                 if (accel == AltosLib.MISSING)
1130                         return;
1131                 if (accel_plus_g == AltosLib.MISSING)
1132                         return;
1133                 if (accel_minus_g == AltosLib.MISSING)
1134                         return;
1135
1136                 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
1137                 double counts_per_mss = counts_per_g / 9.80665;
1138                 acceleration.set_measured((accel_plus_g - accel) / counts_per_mss, time);
1139         }
1140
1141         public void set_accel_g(double accel_plus_g, double accel_minus_g) {
1142                 if (accel_plus_g != AltosLib.MISSING) {
1143                         this.accel_plus_g = accel_plus_g;
1144                         this.accel_minus_g = accel_minus_g;
1145                         update_accel();
1146                 }
1147         }
1148
1149         public void set_ground_accel(double ground_accel) {
1150                 if (ground_accel != AltosLib.MISSING)
1151                         this.ground_accel = ground_accel;
1152         }
1153
1154         public void set_accel(double accel) {
1155                 if (accel != AltosLib.MISSING) {
1156                         this.accel = accel;
1157                         if (state == AltosLib.ao_flight_pad) {
1158                                 if (ground_accel_avg == AltosLib.MISSING)
1159                                         ground_accel_avg = accel;
1160                                 else
1161                                         ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
1162                         }
1163                 }
1164                 update_accel();
1165         }
1166
1167         public void set_temperature(double temperature) {
1168                 if (temperature != AltosLib.MISSING) {
1169                         this.temperature = temperature;
1170                         set |= set_data;
1171                 }
1172         }
1173
1174         public void set_battery_voltage(double battery_voltage) {
1175                 if (battery_voltage != AltosLib.MISSING) {
1176                         this.battery_voltage = battery_voltage;
1177                         set |= set_data;
1178                 }
1179         }
1180
1181         public void set_pyro_voltage(double pyro_voltage) {
1182                 if (pyro_voltage != AltosLib.MISSING) {
1183                         this.pyro_voltage = pyro_voltage;
1184                         set |= set_data;
1185                 }
1186         }
1187
1188         public void set_apogee_voltage(double apogee_voltage) {
1189                 if (apogee_voltage != AltosLib.MISSING) {
1190                         this.apogee_voltage = apogee_voltage;
1191                         set |= set_data;
1192                 }
1193         }
1194
1195         public void set_main_voltage(double main_voltage) {
1196                 if (main_voltage != AltosLib.MISSING) {
1197                         this.main_voltage = main_voltage;
1198                         set |= set_data;
1199                 }
1200         }
1201
1202         public void set_ignitor_voltage(double[] voltage) {
1203                 this.ignitor_voltage = voltage;
1204         }
1205
1206         public void set_pyro_fired(int fired) {
1207                 this.pyro_fired = fired;
1208         }
1209
1210         public double time_since_boost() {
1211                 if (tick == AltosLib.MISSING)
1212                         return 0.0;
1213
1214                 if (boost_tick == AltosLib.MISSING)
1215                         return tick / 100.0;
1216                 return (tick - boost_tick) / 100.0;
1217         }
1218
1219         public boolean valid() {
1220                 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1221         }
1222
1223         public AltosGPS make_temp_gps(boolean sats) {
1224                 if (temp_gps == null) {
1225                         temp_gps = new AltosGPS(gps);
1226                 }
1227                 gps_pending = true;
1228                 if (sats) {
1229                         if (tick != temp_gps_sat_tick)
1230                                 temp_gps.cc_gps_sat = null;
1231                         temp_gps_sat_tick = tick;
1232                 }
1233                 return temp_gps;
1234         }
1235
1236         public void set_temp_gps() {
1237                 set_gps(temp_gps, gps_sequence + 1);
1238                 gps_pending = false;
1239                 temp_gps = null;
1240         }
1241
1242         public AltosState clone() {
1243                 AltosState s = new AltosState();
1244                 s.copy(this);
1245                 return s;
1246         }
1247
1248         public AltosState () {
1249                 init();
1250         }
1251 }