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