altos/test: Adjust CRC error rate after FEC fix
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 /*
20  * Track flight state from telemetry or eeprom data stream
21  */
22
23 package org.altusmetrum.altoslib_14;
24
25 public class AltosState extends AltosDataListener {
26
27         public static final int set_position = 1;
28         public static final int set_gps = 2;
29         public static final int set_data = 4;
30
31         public int set;
32
33         static final double filter_len = 2.0;
34         static final double ascent_filter_len = 0.5;
35         static final double descent_filter_len = 5.0;
36
37         /* derived data */
38
39         public long     received_time;
40
41         public int      rssi;
42         public int      status;
43
44         class AltosValue {
45                 double  value;
46                 double  prev_value;
47                 private double  max_value;
48                 private double  set_time;
49                 private double  prev_set_time;
50
51                 boolean can_max() { return true; }
52
53                 void set(double new_value, double time) {
54                         if (new_value != AltosLib.MISSING) {
55                                 value = new_value;
56                                 if (can_max() && (max_value == AltosLib.MISSING || value > max_value))
57                                         max_value = value;
58                                 set_time = time;
59                         }
60                 }
61
62                 void set_filtered(double new_value, double time) {
63                         if (prev_value != AltosLib.MISSING) {
64                                 double f = 1/Math.exp((time - prev_set_time) / filter_len);
65                                 new_value = f * new_value + (1-f) * prev_value;
66                         }
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
183         class AltosCValue {
184
185                 class AltosIValue extends AltosValue {
186                         boolean can_max() {
187                                 return c_can_max();
188                         }
189
190                         AltosIValue() {
191                                 super();
192                         }
193                 };
194
195                 public AltosIValue      measured;
196                 public AltosIValue      computed;
197
198                 boolean can_max() { return true; }
199
200                 boolean c_can_max() { return can_max(); }
201
202                 double value() {
203                         double v = measured.value();
204                         if (v != AltosLib.MISSING)
205                                 return v;
206                         return computed.value();
207                 }
208
209                 boolean is_measured() {
210                         return measured.value() != AltosLib.MISSING;
211                 }
212
213                 double max() {
214                         double m = measured.max();
215
216                         if (m != AltosLib.MISSING)
217                                 return m;
218                         return computed.max();
219                 }
220
221                 double prev_value() {
222                         if (measured.value != AltosLib.MISSING && measured.prev_value != AltosLib.MISSING)
223                                 return measured.prev_value;
224                         return computed.prev_value;
225                 }
226
227                 AltosValue altos_value() {
228                         if (measured.value() != AltosLib.MISSING)
229                                 return measured;
230                         return computed;
231                 }
232
233                 double change() {
234                         double c = measured.change();
235                         if (c == AltosLib.MISSING)
236                                 c = computed.change();
237                         return c;
238                 }
239
240                 double rate() {
241                         double r = measured.rate();
242                         if (r == AltosLib.MISSING)
243                                 r = computed.rate();
244                         return r;
245                 }
246
247                 void set_measured(double new_value, double time) {
248                         measured.set(new_value, time);
249                 }
250
251                 void set_computed(double new_value, double time) {
252                         computed.set(new_value, time);
253                 }
254
255                 void set_derivative(AltosValue in) {
256                         computed.set_derivative(in);
257                 }
258
259                 void set_derivative(AltosCValue in) {
260                         set_derivative(in.altos_value());
261                 }
262
263                 void set_integral(AltosValue in) {
264                         computed.set_integral(in);
265                 }
266
267                 void set_integral(AltosCValue in) {
268                         set_integral(in.altos_value());
269                 }
270
271                 void copy(AltosCValue old) {
272                         measured.copy(old.measured);
273                         computed.copy(old.computed);
274                 }
275
276                 void finish_update() {
277                         measured.finish_update();
278                         computed.finish_update();
279                 }
280
281                 public AltosCValue() {
282                         measured = new AltosIValue();
283                         computed = new AltosIValue();
284                 }
285         }
286
287         public boolean  landed;
288         public boolean  ascent; /* going up? */
289         public boolean  boost;  /* under power */
290
291         private double pressure_to_altitude(double p) {
292                 if (p == AltosLib.MISSING)
293                         return AltosLib.MISSING;
294                 return AltosConvert.pressure_to_altitude(p);
295         }
296
297         private AltosCValue ground_altitude;
298
299         public double ground_altitude() {
300                 return ground_altitude.value();
301         }
302
303         public void set_ground_altitude(double a) {
304                 ground_altitude.set_measured(a, time);
305         }
306
307         class AltosGpsGroundAltitude extends AltosValue {
308                 void set(double a, double t) {
309                         super.set(a, t);
310
311                         gps_altitude.set_gps_height();
312                 }
313
314                 void set_filtered(double a, double t) {
315                         super.set_filtered(a, t);
316                         gps_altitude.set_gps_height();
317                 }
318
319                 AltosGpsGroundAltitude() {
320                         super();
321                 }
322         }
323
324         private AltosGpsGroundAltitude gps_ground_altitude;
325
326         public double gps_ground_altitude() {
327                 return gps_ground_altitude.value();
328         }
329
330         public void set_gps_ground_altitude(double a) {
331                 gps_ground_altitude.set(a, time);
332         }
333
334         class AltosGroundPressure extends AltosCValue {
335                 void set_filtered(double p, double time) {
336                         computed.set_filtered(p, time);
337                         if (!is_measured())
338                                 ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
339                 }
340
341                 void set_measured(double p, double time) {
342                         super.set_measured(p, time);
343                         ground_altitude.set_computed(pressure_to_altitude(p), time);
344                 }
345
346                 AltosGroundPressure () {
347                         super();
348                 }
349         }
350
351         private AltosGroundPressure ground_pressure;
352
353         public double ground_pressure() {
354                 return ground_pressure.value();
355         }
356
357         public void set_ground_pressure (double pressure) {
358                 ground_pressure.set_measured(pressure, time);
359         }
360
361         class AltosAltitude extends AltosCValue {
362
363                 private void set_speed(AltosValue v) {
364                         if (!acceleration.is_measured() || !ascent)
365                                 speed.set_derivative(this);
366                 }
367
368                 void set_computed(double a, double time) {
369                         super.set_computed(a,time);
370                         set_speed(computed);
371                         set |= set_position;
372                 }
373
374                 void set_measured(double a, double time) {
375                         super.set_measured(a,time);
376                         set_speed(measured);
377                         set |= set_position;
378                 }
379
380                 AltosAltitude() {
381                         super();
382                 }
383         }
384
385         private AltosAltitude   altitude;
386
387         class AltosGpsAltitude extends AltosValue {
388
389                 private void set_gps_height() {
390                         double  a = value();
391                         double  g = gps_ground_altitude.value();
392
393                         if (a != AltosLib.MISSING && g != AltosLib.MISSING)
394                                 gps_height = a - g;
395                         else
396                                 gps_height = AltosLib.MISSING;
397                 }
398
399                 void set(double a, double t) {
400                         super.set(a, t);
401                         set_gps_height();
402                 }
403
404                 AltosGpsAltitude() {
405                         super();
406                 }
407         }
408
409         private AltosGpsAltitude        gps_altitude;
410
411         private AltosValue              gps_ground_speed;
412         private AltosValue              gps_ascent_rate;
413         private AltosValue              gps_course;
414         private AltosValue              gps_speed;
415
416         public double altitude() {
417                 double a = altitude.value();
418                 if (a != AltosLib.MISSING)
419                         return a;
420                 return gps_altitude.value();
421         }
422
423         public double max_altitude() {
424                 double a = altitude.max();
425                 if (a != AltosLib.MISSING)
426                         return a;
427                 return gps_altitude.max();
428         }
429
430         public void set_altitude(double new_altitude) {
431                 double old_altitude = altitude.value();
432                 if (old_altitude != AltosLib.MISSING) {
433                         while (old_altitude - new_altitude > 32000)
434                                 new_altitude += 65536.0;
435                 }
436                 altitude.set_measured(new_altitude, time);
437         }
438
439         public double gps_altitude() {
440                 return gps_altitude.value();
441         }
442
443         public double max_gps_altitude() {
444                 return gps_altitude.max();
445         }
446
447         public void set_gps_altitude(double new_gps_altitude) {
448                 gps_altitude.set(new_gps_altitude, time);
449         }
450
451         public double gps_ground_speed() {
452                 return gps_ground_speed.value();
453         }
454
455         public double max_gps_ground_speed() {
456                 return gps_ground_speed.max();
457         }
458
459         public double gps_ascent_rate() {
460                 return gps_ascent_rate.value();
461         }
462
463         public double max_gps_ascent_rate() {
464                 return gps_ascent_rate.max();
465         }
466
467         public double gps_course() {
468                 return gps_course.value();
469         }
470
471         public double gps_speed() {
472                 return gps_speed.value();
473         }
474
475         public double max_gps_speed() {
476                 return gps_speed.max();
477         }
478
479         class AltosPressure extends AltosValue {
480                 void set(double p, double time) {
481                         super.set(p, time);
482                         if (state() == AltosLib.ao_flight_pad)
483                                 ground_pressure.set_filtered(p, time);
484                         double a = pressure_to_altitude(p);
485                         altitude.set_computed(a, time);
486                 }
487
488                 AltosPressure() {
489                         super();
490                 }
491         }
492
493         private AltosPressure   pressure;
494
495         public double pressure() {
496                 return pressure.value();
497         }
498
499         public void set_pressure(double p) {
500                 pressure.set(p, time);
501         }
502
503         public void set_thrust(double N) {
504         }
505
506         public double baro_height() {
507                 double a = altitude();
508                 double g = ground_altitude();
509                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
510                         return a - g;
511                 return AltosLib.MISSING;
512         }
513
514         public double height() {
515                 double b = baro_height();
516                 if (b != AltosLib.MISSING)
517                         return b;
518
519                 double k = kalman_height.value();
520                 if (k != AltosLib.MISSING)
521                         return k;
522
523                 return gps_height();
524         }
525
526         public double max_height() {
527                 double a = altitude.max();
528                 double g = ground_altitude();
529                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
530                         return a - g;
531
532                 double  k = kalman_height.max();
533                 if (k != AltosLib.MISSING)
534                         return k;
535
536                 return max_gps_height();
537         }
538
539         public double gps_height() {
540                 double a = gps_altitude();
541                 double g = gps_ground_altitude();
542
543                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
544                         return a - g;
545                 return AltosLib.MISSING;
546         }
547
548         public double max_gps_height() {
549                 double a = gps_altitude.max();
550                 double g = gps_ground_altitude();
551
552                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
553                         return a - g;
554                 return AltosLib.MISSING;
555         }
556
557         class AltosSpeed extends AltosCValue {
558
559                 boolean can_max() {
560                         return state() < AltosLib.ao_flight_fast || state() == AltosLib.ao_flight_stateless;
561                 }
562
563                 void set_accel() {
564                         acceleration.set_derivative(this);
565                 }
566
567                 void set_derivative(AltosCValue in) {
568                         super.set_derivative(in);
569                         set_accel();
570                 }
571
572                 void set_computed(double new_value, double time) {
573                         super.set_computed(new_value, time);
574                         set_accel();
575                 }
576
577                 void set_measured(double new_value, double time) {
578                         super.set_measured(new_value, time);
579                         set_accel();
580                 }
581
582                 AltosSpeed() {
583                         super();
584                 }
585         }
586
587         private AltosSpeed speed;
588
589         public double speed() {
590                 double v = kalman_speed.value();
591                 if (v != AltosLib.MISSING)
592                         return v;
593                 v = speed.value();
594                 if (v != AltosLib.MISSING)
595                         return v;
596                 v = gps_speed();
597                 if (v != AltosLib.MISSING)
598                         return v;
599                 return AltosLib.MISSING;
600         }
601
602         public double max_speed() {
603                 double v = kalman_speed.max();
604                 if (v != AltosLib.MISSING)
605                         return v;
606                 v = speed.max();
607                 if (v != AltosLib.MISSING)
608                         return v;
609                 v = max_gps_speed();
610                 if (v != AltosLib.MISSING)
611                         return v;
612                 return AltosLib.MISSING;
613         }
614
615         class AltosAccel extends AltosCValue {
616
617                 boolean can_max() {
618                         return state() < AltosLib.ao_flight_fast || state() == AltosLib.ao_flight_stateless;
619                 }
620
621                 void set_measured(double a, double time) {
622                         super.set_measured(a, time);
623                         if (ascent)
624                                 speed.set_integral(this.measured);
625                 }
626
627                 AltosAccel() {
628                         super();
629                 }
630         }
631
632         AltosAccel acceleration;
633
634         public double acceleration() {
635                 return acceleration.value();
636         }
637
638         public double max_acceleration() {
639                 return acceleration.max();
640         }
641
642         public AltosCValue      orient;
643
644         public void set_orient(double new_orient) {
645                 orient.set_measured(new_orient, time);
646         }
647
648         public double orient() {
649                 return orient.value();
650         }
651
652         public double max_orient() {
653                 return orient.max();
654         }
655
656         public AltosValue       kalman_height, kalman_speed, kalman_acceleration;
657
658         public void set_kalman(double height, double speed, double acceleration) {
659                 double old_height = kalman_height.value();
660                 if (old_height != AltosLib.MISSING) {
661                         while (old_height - height > 32000)
662                                 height += 65536;
663                 }
664                 kalman_height.set(height, time);
665                 kalman_speed.set(speed, time);
666                 kalman_acceleration.set(acceleration, time);
667         }
668
669         public double   battery_voltage;
670         public double   pyro_voltage;
671         public double   temperature;
672         public double   apogee_voltage;
673         public double   main_voltage;
674
675         public double   igniter_voltage[];
676
677         public AltosGPS gps;
678         public boolean  gps_pending;
679
680         public static final int MIN_PAD_SAMPLES = 10;
681
682         public int      npad;
683         public int      gps_waiting;
684         public boolean  gps_ready;
685
686         public int      ngps;
687
688         public AltosGreatCircle from_pad;
689         public double   elevation;      /* from pad */
690         public double   distance;       /* distance along ground */
691         public double   range;          /* total distance */
692
693         public double   gps_height;
694
695         public double pad_lat, pad_lon;
696
697         public int      speak_tick;
698         public double   speak_altitude;
699
700         public double   ground_accel;
701
702         public AltosCompanion   companion;
703
704         public int      pyro_fired;
705
706         public double   motor_pressure;
707
708         public void set_npad(int npad) {
709                 this.npad = npad;
710                 gps_waiting = MIN_PAD_SAMPLES - npad;
711                 if (this.gps_waiting < 0)
712                         gps_waiting = 0;
713                 gps_ready = gps_waiting == 0;
714         }
715
716         public void init() {
717                 super.init();
718
719                 set = 0;
720
721                 received_time = System.currentTimeMillis();
722                 landed = false;
723                 boost = false;
724                 rssi = AltosLib.MISSING;
725                 status = 0;
726
727                 ground_altitude = new AltosCValue();
728                 ground_pressure = new AltosGroundPressure();
729                 altitude = new AltosAltitude();
730                 pressure = new AltosPressure();
731                 speed = new AltosSpeed();
732                 acceleration = new AltosAccel();
733                 orient = new AltosCValue();
734
735                 temperature = AltosLib.MISSING;
736                 battery_voltage = AltosLib.MISSING;
737                 pyro_voltage = AltosLib.MISSING;
738                 apogee_voltage = AltosLib.MISSING;
739                 main_voltage = AltosLib.MISSING;
740                 igniter_voltage = null;
741
742                 kalman_height = new AltosValue();
743                 kalman_speed = new AltosValue();
744                 kalman_acceleration = new AltosValue();
745
746                 gps = null;
747                 gps_pending = false;
748
749                 last_imu_time = AltosLib.MISSING;
750                 rotation = null;
751
752                 accel_ground_along = AltosLib.MISSING;
753                 accel_ground_across = AltosLib.MISSING;
754                 accel_ground_through = AltosLib.MISSING;
755
756                 accel_along = AltosLib.MISSING;
757                 accel_across = AltosLib.MISSING;
758                 accel_through = AltosLib.MISSING;
759
760                 gyro_roll = AltosLib.MISSING;
761                 gyro_pitch = AltosLib.MISSING;
762                 gyro_yaw = AltosLib.MISSING;
763
764                 mag_along = AltosLib.MISSING;
765                 mag_across = AltosLib.MISSING;
766                 mag_through = AltosLib.MISSING;
767
768                 set_npad(0);
769                 ngps = 0;
770
771                 from_pad = null;
772                 elevation = AltosLib.MISSING;
773                 distance = AltosLib.MISSING;
774                 range = AltosLib.MISSING;
775                 gps_height = AltosLib.MISSING;
776
777                 pad_lat = AltosLib.MISSING;
778                 pad_lon = AltosLib.MISSING;
779
780                 gps_altitude = new AltosGpsAltitude();
781                 gps_ground_altitude = new AltosGpsGroundAltitude();
782                 gps_ground_speed = new AltosValue();
783                 gps_speed = new AltosValue();
784                 gps_ascent_rate = new AltosValue();
785                 gps_course = new AltosValue();
786
787                 speak_tick = AltosLib.MISSING;
788                 speak_altitude = AltosLib.MISSING;
789
790                 ground_accel = AltosLib.MISSING;
791
792                 companion = null;
793
794                 pyro_fired = 0;
795         }
796
797         void finish_update() {
798                 ground_altitude.finish_update();
799                 altitude.finish_update();
800                 pressure.finish_update();
801                 speed.finish_update();
802                 acceleration.finish_update();
803                 orient.finish_update();
804
805                 kalman_height.finish_update();
806                 kalman_speed.finish_update();
807                 kalman_acceleration.finish_update();
808         }
809
810         void update_time() {
811         }
812
813         void update_gps() {
814                 elevation = AltosLib.MISSING;
815                 distance = AltosLib.MISSING;
816                 range = AltosLib.MISSING;
817
818                 if (gps == null)
819                         return;
820
821                 if (gps.locked && gps.nsat >= 4) {
822                         /* Track consecutive 'good' gps reports, waiting for 10 of them */
823                         if (state() == AltosLib.ao_flight_pad || state() == AltosLib.ao_flight_stateless) {
824                                 set_npad(npad+1);
825                                 if (pad_lat != AltosLib.MISSING && (npad < 10 || state() == AltosLib.ao_flight_pad)) {
826                                         pad_lat = (pad_lat * 31 + gps.lat) / 32;
827                                         pad_lon = (pad_lon * 31 + gps.lon) / 32;
828                                         gps_ground_altitude.set_filtered(gps.alt, time);
829                                 }
830                         }
831                         if (pad_lat == AltosLib.MISSING) {
832                                 pad_lat = gps.lat;
833                                 pad_lon = gps.lon;
834                                 gps_ground_altitude.set(gps.alt, time);
835                         }
836                         gps_altitude.set(gps.alt, time);
837                         if (gps.climb_rate != AltosLib.MISSING)
838                                 gps_ascent_rate.set(gps.climb_rate, time);
839                         if (gps.ground_speed != AltosLib.MISSING)
840                                 gps_ground_speed.set(gps.ground_speed, time);
841                         if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
842                                 gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
843                                                         gps.climb_rate * gps.climb_rate), time);
844                         if (gps.course != AltosLib.MISSING)
845                                 gps_course.set(gps.course, time);
846                 } else if (state() == AltosLib.ao_flight_pad || state() == AltosLib.ao_flight_stateless) {
847                         set_npad(0);
848                 }
849                 if (gps.lat != 0 && gps.lon != 0 &&
850                     pad_lat != AltosLib.MISSING &&
851                     pad_lon != AltosLib.MISSING)
852                 {
853                         double h = height();
854
855                         if (h == AltosLib.MISSING)
856                                 h = 0;
857                         from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
858                         elevation = from_pad.elevation;
859                         distance = from_pad.distance;
860                         range = from_pad.range;
861                 }
862         }
863
864         public void set_state(int state) {
865                 super.set_state(state);
866                 ascent = (AltosLib.ao_flight_boost <= state() &&
867                           state() <= AltosLib.ao_flight_coast);
868                 boost = (AltosLib.ao_flight_boost == state());
869         }
870
871         public int rssi() {
872                 if (rssi == AltosLib.MISSING)
873                         return 0;
874                 return rssi;
875         }
876
877         public void set_rssi(int rssi, int status) {
878                 if (rssi != AltosLib.MISSING) {
879                         this.rssi = rssi;
880                         this.status = status;
881                 }
882         }
883
884         public void set_received_time(long ms) {
885                 received_time = ms;
886         }
887
888         public void set_gps(AltosGPS gps, boolean set_location, boolean set_sats) {
889                 super.set_gps(gps, set_location, set_sats);
890                 if (gps != null) {
891                         this.gps = gps;
892                         update_gps();
893                         set |= set_gps;
894                 }
895         }
896
897         public AltosRotation    rotation;
898
899         public double   accel_ground_along, accel_ground_across, accel_ground_through;
900
901         void update_pad_rotation() {
902                 if (cal_data().pad_orientation != AltosLib.MISSING && accel_ground_along != AltosLib.MISSING) {
903                         rotation = new AltosRotation(accel_ground_across,
904                                                      accel_ground_through,
905                                                      accel_ground_along,
906                                                      cal_data().pad_orientation);
907                         orient.set_computed(rotation.tilt(), time);
908                 }
909         }
910
911         public void set_accel_ground(double ground_along, double ground_across, double ground_through) {
912                 accel_ground_along = ground_along;
913                 accel_ground_across = ground_across;
914                 accel_ground_through = ground_through;
915                 update_pad_rotation();
916         }
917
918         public double   last_imu_time;
919
920         private void update_orient() {
921                 if (last_imu_time != AltosLib.MISSING) {
922                         double  t = time - last_imu_time;
923
924                         if (t > 0 && gyro_pitch != AltosLib.MISSING && rotation != null) {
925                                 double  pitch = AltosConvert.degrees_to_radians(gyro_pitch) * t;
926                                 double  yaw = AltosConvert.degrees_to_radians(gyro_yaw) * t;
927                                 double  roll = AltosConvert.degrees_to_radians(gyro_roll) * t;
928
929                                 rotation.rotate(pitch, yaw, roll);
930                                 orient.set_computed(rotation.tilt(), time);
931                         }
932                 }
933                 last_imu_time = time;
934         }
935
936         private double  gyro_roll, gyro_pitch, gyro_yaw;
937
938         public void set_gyro(double roll, double pitch, double yaw) {
939                 gyro_roll = roll;
940                 gyro_pitch = pitch;
941                 gyro_yaw = yaw;
942                 update_orient();
943         }
944
945         private double accel_along, accel_across, accel_through;
946
947         public void set_accel(double along, double across, double through) {
948                 accel_along = along;
949                 accel_across = across;
950                 accel_through = through;
951                 update_orient();
952         }
953
954         public double accel_along() {
955                 return accel_along;
956         }
957
958         public double accel_across() {
959                 return accel_across;
960         }
961
962         public double accel_through() {
963                 return accel_through;
964         }
965
966         public double gyro_roll() {
967                 return gyro_roll;
968         }
969
970         public double gyro_pitch() {
971                 return gyro_pitch;
972         }
973
974         public double gyro_yaw() {
975                 return gyro_yaw;
976         }
977
978         private double mag_along, mag_across, mag_through;
979
980         public void set_mag(double along, double across, double through) {
981                 mag_along = along;
982                 mag_across = across;
983                 mag_through = through;
984         }
985
986         public double mag_along() {
987                 return mag_along;
988         }
989
990         public double mag_across() {
991                 return mag_across;
992         }
993
994         public double mag_through() {
995                 return mag_through;
996         }
997
998         public void set_companion(AltosCompanion companion) {
999                 this.companion = companion;
1000         }
1001
1002         public void set_acceleration(double acceleration) {
1003                 if (acceleration != AltosLib.MISSING) {
1004                         this.acceleration.set_measured(acceleration, time);
1005                         set |= set_data;
1006                 }
1007         }
1008
1009         public void set_temperature(double temperature) {
1010                 if (temperature != AltosLib.MISSING) {
1011                         this.temperature = temperature;
1012                         set |= set_data;
1013                 }
1014         }
1015
1016         public void set_battery_voltage(double battery_voltage) {
1017                 if (battery_voltage != AltosLib.MISSING) {
1018                         this.battery_voltage = battery_voltage;
1019                         set |= set_data;
1020                 }
1021         }
1022
1023         public void set_pyro_voltage(double pyro_voltage) {
1024                 if (pyro_voltage != AltosLib.MISSING) {
1025                         this.pyro_voltage = pyro_voltage;
1026                         set |= set_data;
1027                 }
1028         }
1029
1030         public void set_apogee_voltage(double apogee_voltage) {
1031                 if (apogee_voltage != AltosLib.MISSING) {
1032                         this.apogee_voltage = apogee_voltage;
1033                         set |= set_data;
1034                 }
1035         }
1036
1037         public void set_main_voltage(double main_voltage) {
1038                 if (main_voltage != AltosLib.MISSING) {
1039                         this.main_voltage = main_voltage;
1040                         set |= set_data;
1041                 }
1042         }
1043
1044         public void set_igniter_voltage(double[] voltage) {
1045                 this.igniter_voltage = voltage;
1046         }
1047
1048         public void set_pyro_fired(int fired) {
1049                 this.pyro_fired = fired;
1050         }
1051
1052         public void set_motor_pressure(double motor_pressure) {
1053                 this.motor_pressure = motor_pressure;
1054         }
1055
1056         public AltosState() {
1057                 init();
1058         }
1059
1060         public AltosState (AltosCalData cal_data) {
1061                 super(cal_data);
1062                 init();
1063         }
1064 }