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