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