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