altoslib: Finish AltosState changes. Update version number.
[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 = 0;
636                 tick = old.tick;
637                 prev_tick = old.tick;
638                 boost_tick = old.boost_tick;
639
640                 state = old.state;
641                 flight = old.flight;
642                 landed = old.landed;
643                 ascent = old.ascent;
644                 boost = old.boost;
645                 rssi = old.rssi;
646                 status = old.status;
647                 device_type = old.device_type;
648                 config_major = old.config_major;
649                 config_minor = old.config_minor;
650                 apogee_delay = old.apogee_delay;
651                 main_deploy = old.main_deploy;
652                 flight_log_max = old.flight_log_max;
653                 
654                 set = 0;
655
656                 ground_altitude.copy(old.ground_altitude);
657                 altitude.copy(old.altitude);
658                 pressure.copy(old.pressure);
659                 speed.copy(old.speed);
660                 acceleration.copy(old.acceleration);
661
662                 battery_voltage = old.battery_voltage;
663                 pyro_voltage = old.pyro_voltage;
664                 temperature = old.temperature;
665                 apogee_voltage = old.apogee_voltage;
666                 main_voltage = old.main_voltage;
667                 ignitor_voltage = old.ignitor_voltage;
668
669                 kalman_height.copy(old.kalman_height);
670                 kalman_speed.copy(old.kalman_speed);
671                 kalman_acceleration.copy(old.kalman_acceleration);
672
673                 if (old.gps != null)
674                         gps = old.gps.clone();
675                 else
676                         gps = null;
677                 if (old.temp_gps != null)
678                         temp_gps = old.temp_gps.clone();
679                 else
680                         temp_gps = null;
681                 temp_gps_sat_tick = old.temp_gps_sat_tick;
682                 gps_sequence = old.gps_sequence;
683                 gps_pending = old.gps_pending;
684
685                 if (old.imu != null)
686                         imu = old.imu.clone();
687                 else
688                         imu = null;
689
690                 if (old.mag != null)
691                         mag = old.mag.clone();
692                 else
693                         mag = null;
694
695                 npad = old.npad;
696                 gps_waiting = old.gps_waiting;
697                 gps_ready = old.gps_ready;
698                 ngps = old.ngps;
699
700                 if (old.from_pad != null)
701                         from_pad = old.from_pad.clone();
702                 else
703                         from_pad = null;
704
705                 elevation = old.elevation;
706                 range = old.range;
707
708                 gps_height = old.gps_height;
709                 pad_lat = old.pad_lat;
710                 pad_lon = old.pad_lon;
711                 pad_alt = old.pad_alt;
712
713                 speak_tick = old.speak_tick;
714                 speak_altitude = old.speak_altitude;
715
716                 callsign = old.callsign;
717
718                 accel_plus_g = old.accel_plus_g;
719                 accel_minus_g = old.accel_minus_g;
720                 accel = old.accel;
721                 ground_accel = old.ground_accel;
722                 ground_accel_avg = old.ground_accel_avg;
723
724                 log_format = old.log_format;
725                 serial = old.serial;
726
727                 baro = old.baro;
728                 companion = old.companion;
729         }
730         
731         void update_time() {
732         }
733
734         void update_gps() {
735                 elevation = 0;
736                 range = -1;
737                 gps_height = 0;
738
739                 if (gps == null)
740                         return;
741
742                 if (gps.locked && gps.nsat >= 4) {
743                         /* Track consecutive 'good' gps reports, waiting for 10 of them */
744                         if (state == AltosLib.ao_flight_pad) {
745                                 set_npad(npad+1);
746                                 if (pad_lat != AltosLib.MISSING) {
747                                         pad_lat = (pad_lat * 31 + gps.lat) / 32;
748                                         pad_lon = (pad_lon * 31 + gps.lon) / 32;
749                                         pad_alt = (pad_alt * 31 + gps.alt) / 32;
750                                 }
751                         }
752                         if (pad_lat == AltosLib.MISSING) {
753                                 pad_lat = gps.lat;
754                                 pad_lon = gps.lon;
755                                 pad_alt = gps.alt;
756                         }
757                 }
758                 if (gps.lat != 0 && gps.lon != 0 &&
759                     pad_lat != AltosLib.MISSING &&
760                     pad_lon != AltosLib.MISSING)
761                 {
762                         double h = height();
763
764                         if (h == AltosLib.MISSING)
765                                 h = 0;
766                         from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
767                         elevation = from_pad.elevation;
768                         range = from_pad.range;
769                         gps_height = gps.alt - pad_alt;
770                 }
771         }
772
773         public void set_tick(int new_tick) {
774                 if (new_tick != AltosLib.MISSING) {
775                         if (prev_tick != AltosLib.MISSING) {
776                                 while (new_tick < prev_tick - 1000) {
777                                         new_tick += 65536;
778                                 }
779                         }
780                         tick = new_tick;
781                         time = tick / 100.0;
782                 }
783         }
784
785         public void set_boost_tick(int boost_tick) {
786                 if (boost_tick != AltosLib.MISSING)
787                         this.boost_tick = boost_tick;
788         }
789
790         public String state_name() {
791                 return AltosLib.state_name(state);
792         }
793
794         public void set_state(int state) {
795                 if (state != AltosLib.ao_flight_invalid) {
796                         this.state = state;
797                         ascent = (AltosLib.ao_flight_boost <= state &&
798                                   state <= AltosLib.ao_flight_coast);
799                         boost = (AltosLib.ao_flight_boost == state);
800                 }
801
802         }
803
804         public void set_device_type(int device_type) {
805                 this.device_type = device_type;
806         }
807
808         public void set_config(int major, int minor, int apogee_delay, int main_deploy, int flight_log_max) {
809                 config_major = major;
810                 config_minor = minor;
811                 this.apogee_delay = apogee_delay;
812                 this.main_deploy = main_deploy;
813                 this.flight_log_max = flight_log_max;
814         }
815
816         public void set_callsign(String callsign) {
817                 this.callsign = callsign;
818         }
819
820         public void set_firmware_version(String version) {
821                 firmware_version = version;
822         }
823
824         public void set_flight(int flight) {
825
826                 /* When the flight changes, reset the state */
827                 if (flight != AltosLib.MISSING && flight != 0) {
828                         if (this.flight != AltosLib.MISSING &&
829                             this.flight != flight) {
830                                 init();
831                         }
832                         this.flight = flight;
833                 }
834         }
835
836         public void set_serial(int serial) {
837                 /* When the serial changes, reset the state */
838                 if (serial != AltosLib.MISSING) {
839                         if (this.serial != AltosLib.MISSING &&
840                             this.serial != serial) {
841                                 init();
842                         }
843                         this.serial = serial;
844                 }
845         }
846
847         public int rssi() {
848                 if (rssi == AltosLib.MISSING)
849                         return 0;
850                 return rssi;
851         }
852
853         public void set_rssi(int rssi, int status) {
854                 if (rssi != AltosLib.MISSING) {
855                         this.rssi = rssi;
856                         this.status = status;
857                 }
858         }
859
860         public void set_received_time(long ms) {
861                 received_time = ms;
862         }
863
864         public void set_gps(AltosGPS gps, int sequence) {
865                 if (gps != null) {
866                         this.gps = gps.clone();
867                         gps_sequence = sequence;
868                         update_gps();
869                         set |= set_gps;
870                 }
871         }
872
873         public void set_imu(AltosIMU imu) {
874                 if (imu != null)
875                         imu = imu.clone();
876                 this.imu = imu;
877         }
878
879         public void set_mag(AltosMag mag) {
880                 this.mag = mag.clone();
881         }
882
883         public AltosMs5607 make_baro() {
884                 if (baro == null)
885                         baro = new AltosMs5607();
886                 return baro;
887         }
888
889         public void set_ms5607(AltosMs5607 ms5607) {
890                 baro = ms5607;
891
892                 if (baro != null) {
893                         set_pressure(baro.pa);
894                         set_temperature(baro.cc / 100.0);
895                 }
896         }
897
898         public void set_ms5607(int pres, int temp) {
899                 if (baro != null) {
900                         baro.set(pres, temp);
901
902                         set_pressure(baro.pa);
903                         set_temperature(baro.cc / 100.0);
904                 }
905         }
906
907         public void make_companion (int nchannels) {
908                 if (companion == null)
909                         companion = new AltosCompanion(nchannels);
910         }
911
912         public void set_companion(AltosCompanion companion) {
913                 this.companion = companion;
914         }
915
916         void update_accel() {
917                 double  ground = ground_accel;
918
919                 if (ground == AltosLib.MISSING)
920                         ground = ground_accel_avg;
921                 if (accel == AltosLib.MISSING)
922                         return;
923                 if (ground == AltosLib.MISSING)
924                         return;
925                 if (accel_plus_g == AltosLib.MISSING)
926                         return;
927                 if (accel_minus_g == AltosLib.MISSING)
928                         return;
929
930                 double counts_per_g = (accel_minus_g - accel_plus_g) / 2.0;
931                 double counts_per_mss = counts_per_g / 9.80665;
932                 acceleration.set_measured((ground - accel) / counts_per_mss, time);
933         }
934
935         public void set_accel_g(double accel_plus_g, double accel_minus_g) {
936                 if (accel_plus_g != AltosLib.MISSING) {
937                         this.accel_plus_g = accel_plus_g;
938                         this.accel_minus_g = accel_minus_g;
939                         update_accel();
940                 }
941         }
942
943         public void set_ground_accel(double ground_accel) {
944                 if (ground_accel != AltosLib.MISSING) {
945                         this.ground_accel = ground_accel;
946                         update_accel();
947                 }
948         }
949
950         public void set_accel(double accel) {
951                 if (accel != AltosLib.MISSING) {
952                         this.accel = accel;
953                         if (state == AltosLib.ao_flight_pad) {
954                                 if (ground_accel_avg == AltosLib.MISSING)
955                                         ground_accel_avg = accel;
956                                 else
957                                         ground_accel_avg = (ground_accel_avg * 7 + accel) / 8;
958                         }
959                 }
960                 update_accel();
961         }
962
963         public void set_temperature(double temperature) {
964                 if (temperature != AltosLib.MISSING) {
965                         this.temperature = temperature;
966                         set |= set_data;
967                 }
968         }
969
970         public void set_battery_voltage(double battery_voltage) {
971                 if (battery_voltage != AltosLib.MISSING) {
972                         this.battery_voltage = battery_voltage;
973                         set |= set_data;
974                 }
975         }
976
977         public void set_pyro_voltage(double pyro_voltage) {
978                 if (pyro_voltage != AltosLib.MISSING) {
979                         this.pyro_voltage = pyro_voltage;
980                         set |= set_data;
981                 }
982         }
983
984         public void set_apogee_voltage(double apogee_voltage) {
985                 if (apogee_voltage != AltosLib.MISSING) {
986                         this.apogee_voltage = apogee_voltage;
987                         set |= set_data;
988                 }
989         }
990
991         public void set_main_voltage(double main_voltage) {
992                 if (main_voltage != AltosLib.MISSING) {
993                         this.main_voltage = main_voltage;
994                         set |= set_data;
995                 }
996         }
997
998         public void set_ignitor_voltage(double[] voltage) {
999                 this.ignitor_voltage = voltage;
1000         }
1001
1002         public double time_since_boost() {
1003                 if (tick == AltosLib.MISSING)
1004                         return 0.0;
1005
1006                 if (boost_tick != AltosLib.MISSING) {
1007                         return (tick - boost_tick) / 100.0;
1008                 }
1009                 return tick / 100.0;
1010         }
1011
1012         public boolean valid() {
1013                 return tick != AltosLib.MISSING && serial != AltosLib.MISSING;
1014         }
1015
1016         public AltosGPS make_temp_gps(boolean sats) {
1017                 if (temp_gps == null) {
1018                         temp_gps = new AltosGPS(gps);
1019                 }
1020                 gps_pending = true;
1021                 if (sats) {
1022                         if (tick != temp_gps_sat_tick)
1023                                 temp_gps.cc_gps_sat = null;
1024                         temp_gps_sat_tick = tick;
1025                 }
1026                 return temp_gps;
1027         }
1028
1029         public void set_temp_gps() {
1030                 set_gps(temp_gps, gps_sequence + 1);
1031                 gps_pending = false;
1032                 temp_gps = null;
1033         }
1034
1035         public AltosState clone() {
1036                 AltosState s = new AltosState();
1037                 s.copy(this);
1038                 return s;
1039         }
1040
1041         public AltosState () {
1042                 init();
1043         }
1044 }