422590573459b3829366f6a37dc3e88601bd132d
[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 boolean  landed;
273         public boolean  ascent; /* going up? */
274         public boolean  boost;  /* under power */
275         public int      rssi;
276         public int      status;
277         public int      device_type;
278         public int      config_major;
279         public int      config_minor;
280         public int      apogee_delay;
281         public int      main_deploy;
282         public int      flight_log_max;
283
284         private double pressure_to_altitude(double p) {
285                 if (p == AltosLib.MISSING)
286                         return AltosLib.MISSING;
287                 return AltosConvert.pressure_to_altitude(p);
288         }
289
290         private AltosCValue ground_altitude;
291
292         public double ground_altitude() {
293                 return ground_altitude.value();
294         }
295
296         public void set_ground_altitude(double a) {
297                 ground_altitude.set_measured(a, time);
298         }
299
300         class AltosGroundPressure extends AltosCValue {
301                 void set_filtered(double p, double time) {
302                         computed.set_filtered(p, time);
303                         ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
304                 }
305
306                 void set_measured(double p, double time) {
307                         super.set_measured(p, time);
308                         ground_altitude.set_computed(pressure_to_altitude(p), time);
309                 }
310         }
311
312         private AltosGroundPressure ground_pressure;
313                 
314         public double ground_pressure() {
315                 return ground_pressure.value();
316         }
317
318         public void set_ground_pressure (double pressure) {
319                 ground_pressure.set_measured(pressure, time);
320         }
321
322         class AltosAltitude extends AltosCValue {
323
324                 private void set_speed(AltosValue v) {
325                         if (!acceleration.is_measured() || !ascent)
326                                 speed.set_derivative(this);
327                 }
328
329                 void set_computed(double a, double time) {
330                         super.set_computed(a,time);
331                         set_speed(computed);
332                         set |= set_position;
333                 }
334
335                 void set_measured(double a, double time) {
336                         super.set_measured(a,time);
337                         set_speed(measured);
338                         set |= set_position;
339                 }
340         }
341
342         private AltosAltitude   altitude;
343
344         public double altitude() {
345                 double a = altitude.value();
346                 if (a != AltosLib.MISSING)
347                         return a;
348                 if (gps != null)
349                         return gps.alt;
350                 return AltosLib.MISSING;
351         }
352
353         public double max_altitude() {
354                 double a = altitude.max();
355                 if (a != AltosLib.MISSING)
356                         return a;
357                 return AltosLib.MISSING;
358         }
359
360         public void set_altitude(double new_altitude) {
361                 altitude.set_measured(new_altitude, time);
362         }
363
364         class AltosPressure extends AltosValue {
365                 void set(double p, double time) {
366                         super.set(p, time);
367                         if (state == AltosLib.ao_flight_pad)
368                                 ground_pressure.set_filtered(p, time);
369                         double a = pressure_to_altitude(p);
370                         altitude.set_computed(a, time);
371                 }
372         }
373
374         private AltosPressure   pressure;
375
376         public double pressure() {
377                 return pressure.value();
378         }
379
380         public void set_pressure(double p) {
381                 pressure.set(p, time);
382         }
383
384         public double height() {
385                 double k = kalman_height.value();
386                 if (k != AltosLib.MISSING)
387                         return k;
388
389                 double a = altitude();
390                 double g = ground_altitude();
391                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
392                         return a - g;
393                 return AltosLib.MISSING;
394         }
395
396         public double max_height() {
397                 double  k = kalman_height.max();
398                 if (k != AltosLib.MISSING)
399                         return k;
400
401                 double a = altitude.max();
402                 double g = ground_altitude();
403                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
404                         return a - g;
405                 return AltosLib.MISSING;
406         }
407
408         class AltosSpeed extends AltosCValue {
409                 
410                 void set_accel() {
411                         acceleration.set_derivative(this);
412                 }
413
414                 void set_derivative(AltosCValue in) {
415                         super.set_derivative(in);
416                         set_accel();
417                 }
418
419                 void set_computed(double new_value, double time) {
420                         super.set_computed(new_value, time);
421                         set_accel();
422                 }
423
424                 void set_measured(double new_value, double time) {
425                         super.set_measured(new_value, time);
426                         set_accel();
427                 }
428         }
429
430         private AltosSpeed speed;
431
432         public double speed() {
433                 double v = kalman_speed.value();
434                 if (v != AltosLib.MISSING)
435                         return v;
436                 return speed.value();
437         }
438
439         public double max_speed() {
440                 double v = kalman_speed.max();
441                 if (v != AltosLib.MISSING)
442                         return v;
443                 return speed.max();
444         }
445
446         class AltosAccel extends AltosCValue {
447                 void set_measured(double a, double time) {
448                         super.set_measured(a, time);
449                         if (ascent)
450                                 speed.set_integral(this.measured);
451                 }
452         }
453
454         AltosAccel acceleration;
455
456         public double acceleration() {
457                 return acceleration.value();
458         }
459
460         public double max_acceleration() {
461                 return acceleration.max();
462         }
463
464         public AltosValue       kalman_height, kalman_speed, kalman_acceleration;
465
466         public void set_kalman(double height, double speed, double acceleration) {
467                 kalman_height.set(height, time);
468                 kalman_speed.set(speed, time);
469                 kalman_acceleration.set(acceleration, time);
470         }
471
472         public double   battery_voltage;
473         public double   pyro_voltage;
474         public double   temperature;
475         public double   apogee_voltage;
476         public double   main_voltage;
477
478         public double   ignitor_voltage[];
479
480         public AltosGPS gps;
481         public AltosGPS temp_gps;
482         public int temp_gps_sat_tick;
483         public boolean  gps_pending;
484         public int gps_sequence;
485
486         public AltosIMU imu;
487         public AltosMag mag;
488
489         public static final int MIN_PAD_SAMPLES = 10;
490
491         public int      npad;
492         public int      gps_waiting;
493         public boolean  gps_ready;
494
495         public int      ngps;
496
497         public AltosGreatCircle from_pad;
498         public double   elevation;      /* from pad */
499         public double   range;          /* total distance */
500
501         public double   gps_height;
502
503         public double pad_lat, pad_lon, pad_alt;
504
505         public int      speak_tick;
506         public double   speak_altitude;
507
508         public String   callsign;
509         public String   firmware_version;
510
511         public double   accel_plus_g;
512         public double   accel_minus_g;
513         public double   accel;
514         public double   ground_accel;
515         public double   ground_accel_avg;
516
517         public int      log_format;
518
519         public AltosMs5607      baro;
520
521         public AltosCompanion   companion;
522
523         public void set_npad(int npad) {
524                 this.npad = npad;
525                 gps_waiting = MIN_PAD_SAMPLES - npad;
526                 if (this.gps_waiting < 0)
527                         gps_waiting = 0;
528                 gps_ready = gps_waiting == 0;
529         }
530
531         public void init() {
532                 set = 0;
533
534                 received_time = System.currentTimeMillis();
535                 time = AltosLib.MISSING;
536                 time_change = AltosLib.MISSING;
537                 prev_time = AltosLib.MISSING;
538                 tick = AltosLib.MISSING;
539                 prev_tick = AltosLib.MISSING;
540                 boost_tick = AltosLib.MISSING;
541                 state = AltosLib.ao_flight_invalid;
542                 flight = AltosLib.MISSING;
543                 landed = false;
544                 boost = false;
545                 rssi = AltosLib.MISSING;
546                 status = 0;
547                 device_type = AltosLib.MISSING;
548                 config_major = AltosLib.MISSING;
549                 config_minor = AltosLib.MISSING;
550                 apogee_delay = AltosLib.MISSING;
551                 main_deploy = AltosLib.MISSING;
552                 flight_log_max = AltosLib.MISSING;
553
554                 ground_altitude = new AltosCValue();
555                 ground_pressure = new AltosGroundPressure();
556                 altitude = new AltosAltitude();
557                 pressure = new AltosPressure();
558                 speed = new AltosSpeed();
559                 acceleration = new AltosAccel();
560
561                 temperature = AltosLib.MISSING;
562                 battery_voltage = AltosLib.MISSING;
563                 pyro_voltage = AltosLib.MISSING;
564                 apogee_voltage = AltosLib.MISSING;
565                 main_voltage = AltosLib.MISSING;
566                 ignitor_voltage = null;
567
568                 kalman_height = new AltosValue();
569                 kalman_speed = new AltosValue();
570                 kalman_acceleration = new AltosValue();
571
572                 gps = null;
573                 temp_gps = null;
574                 temp_gps_sat_tick = 0;
575                 gps_sequence = 0;
576                 gps_pending = false;
577
578                 imu = null;
579                 mag = null;
580
581                 set_npad(0);
582                 ngps = 0;
583
584                 from_pad = null;
585                 elevation = AltosLib.MISSING;
586                 range = AltosLib.MISSING;
587                 gps_height = AltosLib.MISSING;
588
589                 pad_lat = AltosLib.MISSING;
590                 pad_lon = AltosLib.MISSING;
591                 pad_alt = AltosLib.MISSING;
592
593                 speak_tick = AltosLib.MISSING;
594                 speak_altitude = AltosLib.MISSING;
595
596                 callsign = null;
597
598                 accel_plus_g = AltosLib.MISSING;
599                 accel_minus_g = AltosLib.MISSING;
600                 accel = AltosLib.MISSING;
601
602                 ground_accel = AltosLib.MISSING;
603                 ground_accel_avg = AltosLib.MISSING;
604
605                 log_format = AltosLib.MISSING;
606                 serial = AltosLib.MISSING;
607
608                 baro = null;
609                 companion = null;
610         }
611
612         void finish_update() {
613                 prev_tick = tick;
614
615                 ground_altitude.finish_update();
616                 altitude.finish_update();
617                 pressure.finish_update();
618                 speed.finish_update();
619                 acceleration.finish_update();
620
621                 kalman_height.finish_update();
622                 kalman_speed.finish_update();
623                 kalman_acceleration.finish_update();
624         }
625
626         void copy(AltosState old) {
627
628                 if (old == null) {
629                         init();
630                         return;
631                 }
632
633                 received_time = old.received_time;
634                 time = old.time;
635                 time_change = old.time_change;
636                 prev_time = old.time;
637                 
638                 tick = old.tick;
639                 prev_tick = old.tick;
640                 boost_tick = old.boost_tick;
641
642                 state = old.state;
643                 flight = old.flight;
644                 landed = old.landed;
645                 ascent = old.ascent;
646                 boost = old.boost;
647                 rssi = old.rssi;
648                 status = old.status;
649                 device_type = old.device_type;
650                 config_major = old.config_major;
651                 config_minor = old.config_minor;
652                 apogee_delay = old.apogee_delay;
653                 main_deploy = old.main_deploy;
654                 flight_log_max = old.flight_log_max;
655                 
656                 set = 0;
657
658                 ground_altitude.copy(old.ground_altitude);
659                 altitude.copy(old.altitude);
660                 pressure.copy(old.pressure);
661                 speed.copy(old.speed);
662                 acceleration.copy(old.acceleration);
663
664                 battery_voltage = old.battery_voltage;
665                 pyro_voltage = old.pyro_voltage;
666                 temperature = old.temperature;
667                 apogee_voltage = old.apogee_voltage;
668                 main_voltage = old.main_voltage;
669                 ignitor_voltage = old.ignitor_voltage;
670
671                 kalman_height.copy(old.kalman_height);
672                 kalman_speed.copy(old.kalman_speed);
673                 kalman_acceleration.copy(old.kalman_acceleration);
674
675                 if (old.gps != null)
676                         gps = old.gps.clone();
677                 else
678                         gps = null;
679                 if (old.temp_gps != null)
680                         temp_gps = old.temp_gps.clone();
681                 else
682                         temp_gps = null;
683                 temp_gps_sat_tick = old.temp_gps_sat_tick;
684                 gps_sequence = old.gps_sequence;
685                 gps_pending = old.gps_pending;
686
687                 if (old.imu != null)
688                         imu = old.imu.clone();
689                 else
690                         imu = null;
691
692                 if (old.mag != null)
693                         mag = old.mag.clone();
694                 else
695                         mag = null;
696
697                 npad = old.npad;
698                 gps_waiting = old.gps_waiting;
699                 gps_ready = old.gps_ready;
700                 ngps = old.ngps;
701
702                 if (old.from_pad != null)
703                         from_pad = old.from_pad.clone();
704                 else
705                         from_pad = null;
706
707                 elevation = old.elevation;
708                 range = old.range;
709
710                 gps_height = old.gps_height;
711                 pad_lat = old.pad_lat;
712                 pad_lon = old.pad_lon;
713                 pad_alt = old.pad_alt;
714
715                 speak_tick = old.speak_tick;
716                 speak_altitude = old.speak_altitude;
717
718                 callsign = old.callsign;
719
720                 accel_plus_g = old.accel_plus_g;
721                 accel_minus_g = old.accel_minus_g;
722                 accel = old.accel;
723                 ground_accel = old.ground_accel;
724                 ground_accel_avg = old.ground_accel_avg;
725
726                 log_format = old.log_format;
727                 serial = old.serial;
728
729                 baro = old.baro;
730                 companion = old.companion;
731         }
732         
733         void update_time() {
734         }
735
736         void update_gps() {
737                 elevation = 0;
738                 range = -1;
739                 gps_height = 0;
740
741                 if (gps == null)
742                         return;
743
744                 if (gps.locked && gps.nsat >= 4) {
745                         /* Track consecutive 'good' gps reports, waiting for 10 of them */
746                         if (state == AltosLib.ao_flight_pad) {
747                                 set_npad(npad+1);
748                                 if (pad_lat != AltosLib.MISSING) {
749                                         pad_lat = (pad_lat * 31 + gps.lat) / 32;
750                                         pad_lon = (pad_lon * 31 + gps.lon) / 32;
751                                         pad_alt = (pad_alt * 31 + gps.alt) / 32;
752                                 }
753                         }
754                         if (pad_lat == AltosLib.MISSING) {
755                                 pad_lat = gps.lat;
756                                 pad_lon = gps.lon;
757                                 pad_alt = gps.alt;
758                         }
759                 }
760                 if (gps.lat != 0 && gps.lon != 0 &&
761                     pad_lat != AltosLib.MISSING &&
762                     pad_lon != AltosLib.MISSING)
763                 {
764                         double h = height();
765
766                         if (h == AltosLib.MISSING)
767                                 h = 0;
768                         from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
769                         elevation = from_pad.elevation;
770                         range = from_pad.range;
771                         gps_height = gps.alt - pad_alt;
772                 }
773         }
774
775         public void set_tick(int new_tick) {
776                 if (new_tick != AltosLib.MISSING) {
777                         if (prev_tick != AltosLib.MISSING) {
778                                 while (new_tick < prev_tick - 1000) {
779                                         new_tick += 65536;
780                                 }
781                         }
782                         tick = new_tick;
783                         time = tick / 100.0;
784                         time_change = time - prev_time;
785                 }
786         }
787
788         public void set_boost_tick(int boost_tick) {
789                 if (boost_tick != AltosLib.MISSING)
790                         this.boost_tick = boost_tick;
791         }
792
793         public String state_name() {
794                 return AltosLib.state_name(state);
795         }
796
797         public void set_state(int state) {
798                 if (state != AltosLib.ao_flight_invalid) {
799                         this.state = state;
800                         ascent = (AltosLib.ao_flight_boost <= state &&
801                                   state <= AltosLib.ao_flight_coast);
802                         boost = (AltosLib.ao_flight_boost == state);
803                 }
804
805         }
806
807         public void set_device_type(int device_type) {
808                 this.device_type = device_type;
809         }
810
811         public void set_config(int major, int minor, int apogee_delay, int main_deploy, int flight_log_max) {
812                 config_major = major;
813                 config_minor = minor;
814                 this.apogee_delay = apogee_delay;
815                 this.main_deploy = main_deploy;
816                 this.flight_log_max = flight_log_max;
817         }
818
819         public void set_callsign(String callsign) {
820                 this.callsign = callsign;
821         }
822
823         public void set_firmware_version(String version) {
824                 firmware_version = version;
825         }
826
827         public void set_flight(int flight) {
828
829                 /* When the flight changes, reset the state */
830                 if (flight != AltosLib.MISSING && flight != 0) {
831                         if (this.flight != AltosLib.MISSING &&
832                             this.flight != flight) {
833                                 init();
834                         }
835                         this.flight = flight;
836                 }
837         }
838
839         public void set_serial(int serial) {
840                 /* When the serial changes, reset the state */
841                 if (serial != AltosLib.MISSING) {
842                         if (this.serial != AltosLib.MISSING &&
843                             this.serial != serial) {
844                                 init();
845                         }
846                         this.serial = serial;
847                 }
848         }
849
850         public int rssi() {
851                 if (rssi == AltosLib.MISSING)
852                         return 0;
853                 return rssi;
854         }
855
856         public void set_rssi(int rssi, int status) {
857                 if (rssi != AltosLib.MISSING) {
858                         this.rssi = rssi;
859                         this.status = status;
860                 }
861         }
862
863         public void set_received_time(long ms) {
864                 received_time = ms;
865         }
866
867         public void set_gps(AltosGPS gps, int sequence) {
868                 if (gps != null) {
869                         this.gps = gps.clone();
870                         gps_sequence = sequence;
871                         update_gps();
872                         set |= set_gps;
873                 }
874         }
875
876         public void set_imu(AltosIMU imu) {
877                 if (imu != null)
878                         imu = imu.clone();
879                 this.imu = imu;
880         }
881
882         public void set_mag(AltosMag mag) {
883                 this.mag = mag.clone();
884         }
885
886         public AltosMs5607 make_baro() {
887                 if (baro == null)
888                         baro = new AltosMs5607();
889                 return baro;
890         }
891
892         public void set_ms5607(AltosMs5607 ms5607) {
893                 baro = ms5607;
894
895                 if (baro != null) {
896                         set_pressure(baro.pa);
897                         set_temperature(baro.cc / 100.0);
898                 }
899         }
900
901         public void set_ms5607(int pres, int temp) {
902                 if (baro != null) {
903                         baro.set(pres, temp);
904
905                         set_pressure(baro.pa);
906                         set_temperature(baro.cc / 100.0);
907                 }
908         }
909
910         public void make_companion (int nchannels) {
911                 if (companion == null)
912                         companion = new AltosCompanion(nchannels);
913         }
914
915         public void set_companion(AltosCompanion companion) {
916                 this.companion = companion;
917         }
918
919         void update_accel() {
920                 double  ground = ground_accel;
921
922                 if (ground == AltosLib.MISSING)
923                         ground = ground_accel_avg;
924                 if (accel == AltosLib.MISSING)
925                         return;
926                 if (ground == AltosLib.MISSING)
927                         return;
928                 if (accel_plus_g == AltosLib.MISSING)
929                         return;
930                 if (accel_minus_g == AltosLib.MISSING)
931                         return;
932
933                 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
934                 double counts_per_mss = counts_per_g / 9.80665;
935                 acceleration.set_measured((ground - accel) / counts_per_mss, time);
936         }
937
938         public void set_accel_g(double accel_plus_g, double accel_minus_g) {
939                 if (accel_plus_g != AltosLib.MISSING) {
940                         this.accel_plus_g = accel_plus_g;
941                         this.accel_minus_g = accel_minus_g;
942                         update_accel();
943                 }
944         }
945
946         public void set_ground_accel(double ground_accel) {
947                 if (ground_accel != AltosLib.MISSING) {
948                         this.ground_accel = ground_accel;
949                         update_accel();
950                 }
951         }
952
953         public void set_accel(double accel) {
954                 if (accel != AltosLib.MISSING) {
955                         this.accel = accel;
956                         if (state == AltosLib.ao_flight_pad) {
957                                 if (ground_accel_avg == AltosLib.MISSING)
958                                         ground_accel_avg = accel;
959                                 else
960                                         ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
961                         }
962                 }
963                 update_accel();
964         }
965
966         public void set_temperature(double temperature) {
967                 if (temperature != AltosLib.MISSING) {
968                         this.temperature = temperature;
969                         set |= set_data;
970                 }
971         }
972
973         public void set_battery_voltage(double battery_voltage) {
974                 if (battery_voltage != AltosLib.MISSING) {
975                         this.battery_voltage = battery_voltage;
976                         set |= set_data;
977                 }
978         }
979
980         public void set_pyro_voltage(double pyro_voltage) {
981                 if (pyro_voltage != AltosLib.MISSING) {
982                         this.pyro_voltage = pyro_voltage;
983                         set |= set_data;
984                 }
985         }
986
987         public void set_apogee_voltage(double apogee_voltage) {
988                 if (apogee_voltage != AltosLib.MISSING) {
989                         this.apogee_voltage = apogee_voltage;
990                         set |= set_data;
991                 }
992         }
993
994         public void set_main_voltage(double main_voltage) {
995                 if (main_voltage != AltosLib.MISSING) {
996                         this.main_voltage = main_voltage;
997                         set |= set_data;
998                 }
999         }
1000
1001         public void set_ignitor_voltage(double[] voltage) {
1002                 this.ignitor_voltage = voltage;
1003         }
1004
1005         public double time_since_boost() {
1006                 if (tick == AltosLib.MISSING)
1007                         return 0.0;
1008
1009                 if (boost_tick != AltosLib.MISSING) {
1010                         return (tick - boost_tick) / 100.0;
1011                 }
1012                 return tick / 100.0;
1013         }
1014
1015         public boolean valid() {
1016                 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1017         }
1018
1019         public AltosGPS make_temp_gps(boolean sats) {
1020                 if (temp_gps == null) {
1021                         temp_gps = new AltosGPS(gps);
1022                 }
1023                 gps_pending = true;
1024                 if (sats) {
1025                         if (tick != temp_gps_sat_tick)
1026                                 temp_gps.cc_gps_sat = null;
1027                         temp_gps_sat_tick = tick;
1028                 }
1029                 return temp_gps;
1030         }
1031
1032         public void set_temp_gps() {
1033                 set_gps(temp_gps, gps_sequence + 1);
1034                 gps_pending = false;
1035                 temp_gps = null;
1036         }
1037
1038         public AltosState clone() {
1039                 AltosState s = new AltosState();
1040                 s.copy(this);
1041                 return s;
1042         }
1043
1044         public AltosState () {
1045                 init();
1046         }
1047 }