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