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