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