altoslib: Make AltosReplayReader start synchronously enough to track states
[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_11;
24
25 import java.io.*;
26
27 public class AltosState extends AltosDataListener {
28
29         public static final int set_position = 1;
30         public static final int set_gps = 2;
31         public static final int set_data = 4;
32
33         public int set;
34
35         static final double filter_len = 2.0;
36         static final double ascent_filter_len = 0.5;
37         static final double descent_filter_len = 5.0;
38
39         /* derived data */
40
41         public long     received_time;
42
43         public int      rssi;
44         public int      status;
45
46         public double   time;
47         public double   prev_time;
48         public double   time_change;
49
50         class AltosValue {
51                 double  value;
52                 double  prev_value;
53                 private double  max_value;
54                 private double  set_time;
55                 private double  prev_set_time;
56
57                 boolean can_max() { return true; }
58
59                 void set(double new_value, double time) {
60                         if (new_value != AltosLib.MISSING) {
61                                 value = new_value;
62                                 if (can_max() && (max_value == AltosLib.MISSING || value > max_value))
63                                         max_value = value;
64                                 set_time = time;
65                         }
66                 }
67
68                 void set_filtered(double new_value, double time) {
69                         if (prev_value != AltosLib.MISSING) {
70                                 double f = 1/Math.exp((time - prev_set_time) / filter_len);
71                                 new_value = f * new_value + (1-f) * prev_value;
72                         }
73                         set(new_value, time);
74                 }
75
76                 double value() {
77                         return value;
78                 }
79
80                 double max() {
81                         return max_value;
82                 }
83
84                 double prev() {
85                         return prev_value;
86                 }
87
88                 double change() {
89                         if (value != AltosLib.MISSING && prev_value != AltosLib.MISSING)
90                                 return value - prev_value;
91                         return AltosLib.MISSING;
92                 }
93
94                 double rate() {
95                         double c = change();
96                         double t = set_time - prev_set_time;
97
98                         if (c != AltosLib.MISSING && t != 0)
99                                 return c / t;
100                         return AltosLib.MISSING;
101                 }
102
103                 double integrate() {
104                         if (value == AltosLib.MISSING)
105                                 return AltosLib.MISSING;
106                         if (prev_value == AltosLib.MISSING)
107                                 return AltosLib.MISSING;
108
109                         return (value + prev_value) / 2 * (set_time - prev_set_time);
110                 }
111
112                 double time() {
113                         return set_time;
114                 }
115
116                 void set_derivative(AltosValue in) {
117                         double  n = in.rate();
118
119                         if (n == AltosLib.MISSING)
120                                 return;
121
122                         double  p = prev_value;
123                         double  pt = prev_set_time;
124
125                         if (p == AltosLib.MISSING) {
126                                 p = 0;
127                                 pt = in.time() - 0.01;
128                         }
129
130                         /* Clip changes to reduce noise */
131                         double  ddt = in.time() - pt;
132                         double  ddv = (n - p) / ddt;
133
134                         final double max = 100000;
135
136                         /* 100gs */
137                         if (Math.abs(ddv) > max) {
138                                 if (n > p)
139                                         n = p + ddt * max;
140                                 else
141                                         n = p - ddt * max;
142                         }
143
144                         double filter_len;
145
146                         if (ascent)
147                                 filter_len = ascent_filter_len;
148                         else
149                                 filter_len = descent_filter_len;
150
151                         double f = 1/Math.exp(ddt/ filter_len);
152                         n = p * f + n * (1-f);
153
154                         set(n, in.time());
155                 }
156
157                 void set_integral(AltosValue in) {
158                         double  change = in.integrate();
159
160                         if (change != AltosLib.MISSING) {
161                                 double  prev = prev_value;
162                                 if (prev == AltosLib.MISSING)
163                                         prev = 0;
164                                 set(prev + change, in.time());
165                         }
166                 }
167
168                 void copy(AltosValue old) {
169                         value = old.value;
170                         set_time = old.set_time;
171                         prev_value = old.value;
172                         prev_set_time = old.set_time;
173                         max_value = old.max_value;
174                 }
175
176                 void finish_update() {
177                         prev_value = value;
178                         prev_set_time = set_time;
179                 }
180
181                 AltosValue() {
182                         value = AltosLib.MISSING;
183                         prev_value = AltosLib.MISSING;
184                         max_value = AltosLib.MISSING;
185                 }
186
187         }
188
189         class AltosCValue {
190
191                 class AltosIValue extends AltosValue {
192                         boolean can_max() {
193                                 return c_can_max();
194                         }
195
196                         AltosIValue() {
197                                 super();
198                         }
199                 };
200
201                 public AltosIValue      measured;
202                 public AltosIValue      computed;
203
204                 boolean can_max() { return true; }
205
206                 boolean c_can_max() { return can_max(); }
207
208                 double value() {
209                         double v = measured.value();
210                         if (v != AltosLib.MISSING)
211                                 return v;
212                         return computed.value();
213                 }
214
215                 boolean is_measured() {
216                         return measured.value() != AltosLib.MISSING;
217                 }
218
219                 double max() {
220                         double m = measured.max();
221
222                         if (m != AltosLib.MISSING)
223                                 return m;
224                         return computed.max();
225                 }
226
227                 double prev_value() {
228                         if (measured.value != AltosLib.MISSING && measured.prev_value != AltosLib.MISSING)
229                                 return measured.prev_value;
230                         return computed.prev_value;
231                 }
232
233                 AltosValue altos_value() {
234                         if (measured.value() != AltosLib.MISSING)
235                                 return measured;
236                         return computed;
237                 }
238
239                 double change() {
240                         double c = measured.change();
241                         if (c == AltosLib.MISSING)
242                                 c = computed.change();
243                         return c;
244                 }
245
246                 double rate() {
247                         double r = measured.rate();
248                         if (r == AltosLib.MISSING)
249                                 r = computed.rate();
250                         return r;
251                 }
252
253                 void set_measured(double new_value, double time) {
254                         measured.set(new_value, time);
255                 }
256
257                 void set_computed(double new_value, double time) {
258                         computed.set(new_value, time);
259                 }
260
261                 void set_derivative(AltosValue in) {
262                         computed.set_derivative(in);
263                 }
264
265                 void set_derivative(AltosCValue in) {
266                         set_derivative(in.altos_value());
267                 }
268
269                 void set_integral(AltosValue in) {
270                         computed.set_integral(in);
271                 }
272
273                 void set_integral(AltosCValue in) {
274                         set_integral(in.altos_value());
275                 }
276
277                 void copy(AltosCValue old) {
278                         measured.copy(old.measured);
279                         computed.copy(old.computed);
280                 }
281
282                 void finish_update() {
283                         measured.finish_update();
284                         computed.finish_update();
285                 }
286
287                 public AltosCValue() {
288                         measured = new AltosIValue();
289                         computed = new AltosIValue();
290                 }
291         }
292
293         private int     state;
294         public int      altitude_32;
295         public int      receiver_serial;
296         public boolean  landed;
297         public boolean  ascent; /* going up? */
298         public boolean  boost;  /* under power */
299         public int      config_major;
300         public int      config_minor;
301         public int      apogee_delay;
302         public int      main_deploy;
303         public int      flight_log_max;
304
305         private double pressure_to_altitude(double p) {
306                 if (p == AltosLib.MISSING)
307                         return AltosLib.MISSING;
308                 return AltosConvert.pressure_to_altitude(p);
309         }
310
311         private AltosCValue ground_altitude;
312
313         public double ground_altitude() {
314                 return ground_altitude.value();
315         }
316
317         public void set_ground_altitude(double a) {
318                 ground_altitude.set_measured(a, time);
319         }
320
321         class AltosGpsGroundAltitude extends AltosValue {
322                 void set(double a, double t) {
323                         super.set(a, t);
324                         pad_alt = value();
325                         gps_altitude.set_gps_height();
326                 }
327
328                 void set_filtered(double a, double t) {
329                         super.set_filtered(a, t);
330                         pad_alt = value();
331                         gps_altitude.set_gps_height();
332                 }
333
334                 AltosGpsGroundAltitude() {
335                         super();
336                 }
337         }
338
339         private AltosGpsGroundAltitude gps_ground_altitude;
340
341         public double gps_ground_altitude() {
342                 return gps_ground_altitude.value();
343         }
344
345         public void set_gps_ground_altitude(double a) {
346                 gps_ground_altitude.set(a, time);
347         }
348
349         class AltosGroundPressure extends AltosCValue {
350                 void set_filtered(double p, double time) {
351                         computed.set_filtered(p, time);
352                         if (!is_measured())
353                                 ground_altitude.set_computed(pressure_to_altitude(computed.value()), time);
354                 }
355
356                 void set_measured(double p, double time) {
357                         super.set_measured(p, time);
358                         ground_altitude.set_computed(pressure_to_altitude(p), time);
359                 }
360
361                 AltosGroundPressure () {
362                         super();
363                 }
364         }
365
366         private AltosGroundPressure ground_pressure;
367
368         public double ground_pressure() {
369                 return ground_pressure.value();
370         }
371
372         public void set_ground_pressure (double pressure) {
373                 ground_pressure.set_measured(pressure, time);
374         }
375
376         class AltosAltitude extends AltosCValue {
377
378                 private void set_speed(AltosValue v) {
379                         if (!acceleration.is_measured() || !ascent)
380                                 speed.set_derivative(this);
381                 }
382
383                 void set_computed(double a, double time) {
384                         super.set_computed(a,time);
385                         set_speed(computed);
386                         set |= set_position;
387                 }
388
389                 void set_measured(double a, double time) {
390                         super.set_measured(a,time);
391                         set_speed(measured);
392                         set |= set_position;
393                 }
394
395                 AltosAltitude() {
396                         super();
397                 }
398         }
399
400         private AltosAltitude   altitude;
401
402         class AltosGpsAltitude extends AltosValue {
403
404                 private void set_gps_height() {
405                         double  a = value();
406                         double  g = gps_ground_altitude.value();
407
408                         if (a != AltosLib.MISSING && g != AltosLib.MISSING)
409                                 gps_height = a - g;
410                         else
411                                 gps_height = AltosLib.MISSING;
412                 }
413
414                 void set(double a, double t) {
415                         super.set(a, t);
416                         set_gps_height();
417                 }
418
419                 AltosGpsAltitude() {
420                         super();
421                 }
422         }
423
424         private AltosGpsAltitude        gps_altitude;
425
426         private AltosValue              gps_ground_speed;
427         private AltosValue              gps_ascent_rate;
428         private AltosValue              gps_course;
429         private AltosValue              gps_speed;
430
431         public double altitude() {
432                 double a = altitude.value();
433                 if (a != AltosLib.MISSING)
434                         return a;
435                 return gps_altitude.value();
436         }
437
438         public double max_altitude() {
439                 double a = altitude.max();
440                 if (a != AltosLib.MISSING)
441                         return a;
442                 return gps_altitude.max();
443         }
444
445         public void set_altitude(double new_altitude) {
446                 double old_altitude = altitude.value();
447                 if (old_altitude != AltosLib.MISSING) {
448                         while (old_altitude - new_altitude > 32000)
449                                 new_altitude += 65536.0;
450                 }
451                 altitude.set_measured(new_altitude, time);
452         }
453
454         public double gps_altitude() {
455                 return gps_altitude.value();
456         }
457
458         public double max_gps_altitude() {
459                 return gps_altitude.max();
460         }
461
462         public void set_gps_altitude(double new_gps_altitude) {
463                 gps_altitude.set(new_gps_altitude, time);
464         }
465
466         public double gps_ground_speed() {
467                 return gps_ground_speed.value();
468         }
469
470         public double max_gps_ground_speed() {
471                 return gps_ground_speed.max();
472         }
473
474         public double gps_ascent_rate() {
475                 return gps_ascent_rate.value();
476         }
477
478         public double max_gps_ascent_rate() {
479                 return gps_ascent_rate.max();
480         }
481
482         public double gps_course() {
483                 return gps_course.value();
484         }
485
486         public double gps_speed() {
487                 return gps_speed.value();
488         }
489
490         public double max_gps_speed() {
491                 return gps_speed.max();
492         }
493
494         class AltosPressure extends AltosValue {
495                 void set(double p, double time) {
496                         super.set(p, time);
497                         if (state == AltosLib.ao_flight_pad)
498                                 ground_pressure.set_filtered(p, time);
499                         double a = pressure_to_altitude(p);
500                         altitude.set_computed(a, time);
501                 }
502
503                 AltosPressure() {
504                         super();
505                 }
506         }
507
508         private AltosPressure   pressure;
509
510         public double pressure() {
511                 return pressure.value();
512         }
513
514         public void set_pressure(double p) {
515                 pressure.set(p, time);
516         }
517
518         class AltosForce extends AltosValue {
519                 void set(double p, double time) {
520                         super.set(p, time);
521                 }
522
523                 AltosForce() {
524                         super();
525                 }
526         }
527         private AltosForce      thrust;
528
529         public double thrust() {
530                 return thrust.value();
531         }
532
533         public void set_thrust(double N) {
534                 thrust.set(N, time);
535         }
536
537         public double baro_height() {
538                 double a = altitude();
539                 double g = ground_altitude();
540                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
541                         return a - g;
542                 return AltosLib.MISSING;
543         }
544
545         public double height() {
546                 double k = kalman_height.value();
547                 if (k != AltosLib.MISSING)
548                         return k;
549
550                 double b = baro_height();
551                 if (b != AltosLib.MISSING)
552                         return b;
553
554                 return gps_height();
555         }
556
557         public double max_height() {
558                 double  k = kalman_height.max();
559                 if (k != AltosLib.MISSING)
560                         return k;
561
562                 double a = altitude.max();
563                 double g = ground_altitude();
564                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
565                         return a - g;
566                 return max_gps_height();
567         }
568
569         public double gps_height() {
570                 double a = gps_altitude();
571                 double g = gps_ground_altitude();
572
573                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
574                         return a - g;
575                 return AltosLib.MISSING;
576         }
577
578         public double max_gps_height() {
579                 double a = gps_altitude.max();
580                 double g = gps_ground_altitude();
581
582                 if (a != AltosLib.MISSING && g != AltosLib.MISSING)
583                         return a - g;
584                 return AltosLib.MISSING;
585         }
586
587         class AltosSpeed extends AltosCValue {
588
589                 boolean can_max() {
590                         return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
591                 }
592
593                 void set_accel() {
594                         acceleration.set_derivative(this);
595                 }
596
597                 void set_derivative(AltosCValue in) {
598                         super.set_derivative(in);
599                         set_accel();
600                 }
601
602                 void set_computed(double new_value, double time) {
603                         super.set_computed(new_value, time);
604                         set_accel();
605                 }
606
607                 void set_measured(double new_value, double time) {
608                         super.set_measured(new_value, time);
609                         set_accel();
610                 }
611
612                 AltosSpeed() {
613                         super();
614                 }
615         }
616
617         private AltosSpeed speed;
618
619         public double speed() {
620                 double v = kalman_speed.value();
621                 if (v != AltosLib.MISSING)
622                         return v;
623                 v = speed.value();
624                 if (v != AltosLib.MISSING)
625                         return v;
626                 v = gps_speed();
627                 if (v != AltosLib.MISSING)
628                         return v;
629                 return AltosLib.MISSING;
630         }
631
632         public double max_speed() {
633                 double v = kalman_speed.max();
634                 if (v != AltosLib.MISSING)
635                         return v;
636                 v = speed.max();
637                 if (v != AltosLib.MISSING)
638                         return v;
639                 v = max_gps_speed();
640                 if (v != AltosLib.MISSING)
641                         return v;
642                 return AltosLib.MISSING;
643         }
644
645         class AltosAccel extends AltosCValue {
646
647                 boolean can_max() {
648                         return state < AltosLib.ao_flight_fast || state == AltosLib.ao_flight_stateless;
649                 }
650
651                 void set_measured(double a, double time) {
652                         super.set_measured(a, time);
653                         if (ascent)
654                                 speed.set_integral(this.measured);
655                 }
656
657                 AltosAccel() {
658                         super();
659                 }
660         }
661
662         AltosAccel acceleration;
663
664         public double acceleration() {
665                 return acceleration.value();
666         }
667
668         public double max_acceleration() {
669                 return acceleration.max();
670         }
671
672         public AltosCValue      orient;
673
674         public void set_orient(double new_orient) {
675                 orient.set_measured(new_orient, time);
676         }
677
678         public double orient() {
679                 return orient.value();
680         }
681
682         public double max_orient() {
683                 return orient.max();
684         }
685
686         public AltosValue       kalman_height, kalman_speed, kalman_acceleration;
687
688         public void set_kalman(double height, double speed, double acceleration) {
689                 double old_height = kalman_height.value();
690                 if (old_height != AltosLib.MISSING) {
691                         while (old_height - height > 32000)
692                                 height += 65536;
693                 }
694                 kalman_height.set(height, time);
695                 kalman_speed.set(speed, time);
696                 kalman_acceleration.set(acceleration, time);
697         }
698
699         public double   battery_voltage;
700         public double   pyro_voltage;
701         public double   temperature;
702         public double   apogee_voltage;
703         public double   main_voltage;
704
705         public double   igniter_voltage[];
706
707         public AltosGPS gps;
708         public boolean  gps_pending;
709
710         public AltosIMU imu;
711         public AltosMag mag;
712
713         public static final int MIN_PAD_SAMPLES = 10;
714
715         public int      npad;
716         public int      gps_waiting;
717         public boolean  gps_ready;
718
719         public int      ngps;
720
721         public AltosGreatCircle from_pad;
722         public double   elevation;      /* from pad */
723         public double   range;          /* total distance */
724
725         public double   gps_height;
726
727         public double pad_lat, pad_lon, pad_alt;
728
729         public int      speak_tick;
730         public double   speak_altitude;
731
732         public String   callsign;
733         public String   firmware_version;
734
735         public double   ground_accel;
736
737         public int      log_format;
738         public int      log_space;
739         public String   product;
740
741         public AltosMs5607      baro;
742
743         public AltosCompanion   companion;
744
745         public int      pyro_fired;
746
747         public void set_npad(int npad) {
748                 this.npad = npad;
749                 gps_waiting = MIN_PAD_SAMPLES - npad;
750                 if (this.gps_waiting < 0)
751                         gps_waiting = 0;
752                 gps_ready = gps_waiting == 0;
753         }
754
755         public void init() {
756                 set = 0;
757
758                 System.out.printf("state init\n");
759                 received_time = System.currentTimeMillis();
760                 time = AltosLib.MISSING;
761                 time_change = AltosLib.MISSING;
762                 prev_time = AltosLib.MISSING;
763                 state = AltosLib.ao_flight_invalid;
764                 landed = false;
765                 boost = false;
766                 rssi = AltosLib.MISSING;
767                 status = 0;
768                 config_major = AltosLib.MISSING;
769                 config_minor = AltosLib.MISSING;
770                 apogee_delay = AltosLib.MISSING;
771                 main_deploy = AltosLib.MISSING;
772                 flight_log_max = AltosLib.MISSING;
773
774                 ground_altitude = new AltosCValue();
775                 ground_pressure = new AltosGroundPressure();
776                 altitude = new AltosAltitude();
777                 pressure = new AltosPressure();
778                 thrust = new AltosForce();
779                 speed = new AltosSpeed();
780                 acceleration = new AltosAccel();
781                 orient = new AltosCValue();
782
783                 temperature = AltosLib.MISSING;
784                 battery_voltage = AltosLib.MISSING;
785                 pyro_voltage = AltosLib.MISSING;
786                 apogee_voltage = AltosLib.MISSING;
787                 main_voltage = AltosLib.MISSING;
788                 igniter_voltage = null;
789
790                 kalman_height = new AltosValue();
791                 kalman_speed = new AltosValue();
792                 kalman_acceleration = new AltosValue();
793
794                 gps = null;
795                 gps_pending = false;
796
797                 imu = null;
798                 last_imu_time = AltosLib.MISSING;
799                 rotation = null;
800                 ground_rotation = null;
801
802                 mag = null;
803                 accel_zero_along = AltosLib.MISSING;
804                 accel_zero_across = AltosLib.MISSING;
805                 accel_zero_through = AltosLib.MISSING;
806
807                 accel_ground_along = AltosLib.MISSING;
808                 accel_ground_across = AltosLib.MISSING;
809                 accel_ground_through = AltosLib.MISSING;
810
811                 pad_orientation = AltosLib.MISSING;
812
813                 set_npad(0);
814                 ngps = 0;
815
816                 from_pad = null;
817                 elevation = AltosLib.MISSING;
818                 range = AltosLib.MISSING;
819                 gps_height = AltosLib.MISSING;
820
821                 pad_lat = AltosLib.MISSING;
822                 pad_lon = AltosLib.MISSING;
823                 pad_alt = AltosLib.MISSING;
824
825                 gps_altitude = new AltosGpsAltitude();
826                 gps_ground_altitude = new AltosGpsGroundAltitude();
827                 gps_ground_speed = new AltosValue();
828                 gps_speed = new AltosValue();
829                 gps_ascent_rate = new AltosValue();
830                 gps_course = new AltosValue();
831
832                 speak_tick = AltosLib.MISSING;
833                 speak_altitude = AltosLib.MISSING;
834
835                 callsign = null;
836                 firmware_version = null;
837
838                 ground_accel = AltosLib.MISSING;
839
840                 log_format = AltosLib.MISSING;
841                 log_space = AltosLib.MISSING;
842                 product = null;
843                 receiver_serial = AltosLib.MISSING;
844                 altitude_32 = AltosLib.MISSING;
845
846                 baro = null;
847                 companion = null;
848
849                 pyro_fired = 0;
850         }
851
852         void finish_update() {
853                 ground_altitude.finish_update();
854                 altitude.finish_update();
855                 pressure.finish_update();
856                 speed.finish_update();
857                 acceleration.finish_update();
858                 orient.finish_update();
859
860                 kalman_height.finish_update();
861                 kalman_speed.finish_update();
862                 kalman_acceleration.finish_update();
863         }
864
865         void update_time() {
866         }
867
868         void update_gps() {
869                 elevation = AltosLib.MISSING;
870                 range = AltosLib.MISSING;
871
872                 if (gps == null)
873                         return;
874
875                 if (gps.locked && gps.nsat >= 4) {
876                         /* Track consecutive 'good' gps reports, waiting for 10 of them */
877                         if (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_stateless) {
878                                 set_npad(npad+1);
879                                 if (pad_lat != AltosLib.MISSING && (npad < 10 || state == AltosLib.ao_flight_pad)) {
880                                         pad_lat = (pad_lat * 31 + gps.lat) / 32;
881                                         pad_lon = (pad_lon * 31 + gps.lon) / 32;
882                                         gps_ground_altitude.set_filtered(gps.alt, time);
883                                 }
884                         }
885                         if (pad_lat == AltosLib.MISSING) {
886                                 pad_lat = gps.lat;
887                                 pad_lon = gps.lon;
888                                 gps_ground_altitude.set(gps.alt, time);
889                         }
890                         gps_altitude.set(gps.alt, time);
891                         if (gps.climb_rate != AltosLib.MISSING)
892                                 gps_ascent_rate.set(gps.climb_rate, time);
893                         if (gps.ground_speed != AltosLib.MISSING)
894                                 gps_ground_speed.set(gps.ground_speed, time);
895                         if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
896                                 gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
897                                                         gps.climb_rate * gps.climb_rate), time);
898                         if (gps.course != AltosLib.MISSING)
899                                 gps_course.set(gps.course, time);
900                 }
901                 if (gps.lat != 0 && gps.lon != 0 &&
902                     pad_lat != AltosLib.MISSING &&
903                     pad_lon != AltosLib.MISSING)
904                 {
905                         double h = height();
906
907                         if (h == AltosLib.MISSING)
908                                 h = 0;
909                         from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
910                         elevation = from_pad.elevation;
911                         range = from_pad.range;
912                 }
913         }
914
915         public String state_name() {
916                 return AltosLib.state_name(state);
917         }
918
919         public void set_product(String product) {
920                 this.product = product;
921         }
922
923         public void set_state(int state) {
924                 if (state != AltosLib.ao_flight_invalid) {
925                         this.state = state;
926                         ascent = (AltosLib.ao_flight_boost <= state &&
927                                   state <= AltosLib.ao_flight_coast);
928                         boost = (AltosLib.ao_flight_boost == state);
929                 }
930         }
931
932         public int state() {
933                 return state;
934         }
935
936         public void set_log_format(int log_format) {
937                 this.log_format = log_format;
938                 switch (log_format) {
939                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
940                         this.state = AltosLib.ao_flight_stateless;
941                         break;
942                 }
943         }
944
945         public void set_log_space(int log_space) {
946                 this.log_space = log_space;
947         }
948
949         public void set_flight_params(int apogee_delay, int main_deploy) {
950                 this.apogee_delay = apogee_delay;
951                 this.main_deploy = main_deploy;
952         }
953
954         public void set_config(int major, int minor, int flight_log_max) {
955                 config_major = major;
956                 config_minor = minor;
957                 this.flight_log_max = flight_log_max;
958         }
959
960         public void set_callsign(String callsign) {
961                 this.callsign = callsign;
962         }
963
964         public void set_firmware_version(String version) {
965                 firmware_version = version;
966         }
967
968         public int compare_version(String other_version) {
969                 if (firmware_version == null)
970                         return AltosLib.MISSING;
971                 return AltosLib.compare_version(firmware_version, other_version);
972         }
973
974         private void re_init() {
975                 int rs = receiver_serial;
976                 init();
977                 receiver_serial = rs;
978         }
979
980 //      public void set_flight(int flight) {
981 //
982 //              /* When the flight changes, reset the state */
983 //              if (flight != AltosLib.MISSING) {
984 //                      if (this.flight != AltosLib.MISSING &&
985 //                          this.flight != flight) {
986 //                              re_init();
987 //                      }
988 //                      this.flight = flight;
989 //              }
990 //      }
991 //
992 //      public void set_serial(int serial) {
993 //              /* When the serial changes, reset the state */
994 //              if (serial != AltosLib.MISSING) {
995 //                      if (this.serial != AltosLib.MISSING &&
996 //                          this.serial != serial) {
997 //                              re_init();
998 //                      }
999 //                      this.serial = serial;
1000 //              }
1001 //      }
1002 //
1003 //      public void set_receiver_serial(int serial) {
1004 //              if (serial != AltosLib.MISSING)
1005 //                      receiver_serial = serial;
1006 //      }
1007
1008         public boolean altitude_32() {
1009                 return altitude_32 == 1;
1010         }
1011
1012         public void set_altitude_32(int altitude_32) {
1013                 if (altitude_32 != AltosLib.MISSING)
1014                         this.altitude_32 = altitude_32;
1015         }
1016
1017         public int rssi() {
1018                 if (rssi == AltosLib.MISSING)
1019                         return 0;
1020                 return rssi;
1021         }
1022
1023         public void set_rssi(int rssi, int status) {
1024                 if (rssi != AltosLib.MISSING) {
1025                         this.rssi = rssi;
1026                         this.status = status;
1027                 }
1028         }
1029
1030         public void set_received_time(long ms) {
1031                 received_time = ms;
1032         }
1033
1034         public void set_gps(AltosGPS gps) {
1035                 if (gps != null) {
1036                         this.gps = gps;
1037                         update_gps();
1038                         set |= set_gps;
1039                 }
1040         }
1041
1042
1043         public double   accel_zero_along;
1044         public double   accel_zero_across;
1045         public double   accel_zero_through;
1046
1047         public AltosRotation    rotation;
1048         public AltosRotation    ground_rotation;
1049
1050         public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
1051                 if (zero_along != AltosLib.MISSING) {
1052                         accel_zero_along = zero_along;
1053                         accel_zero_across = zero_across;
1054                         accel_zero_through = zero_through;
1055                 }
1056         }
1057
1058         public int pad_orientation;
1059
1060         public double   accel_ground_along, accel_ground_across, accel_ground_through;
1061
1062         void update_pad_rotation() {
1063                 if (pad_orientation != AltosLib.MISSING && accel_ground_along != AltosLib.MISSING) {
1064                         rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - accel_zero_across),
1065                                                      AltosIMU.convert_accel(accel_ground_through - accel_zero_through),
1066                                                      AltosIMU.convert_accel(accel_ground_along - accel_zero_along),
1067                                                      pad_orientation);
1068                         ground_rotation = rotation;
1069                         orient.set_computed(rotation.tilt(), time);
1070                 }
1071         }
1072
1073         public void set_accel_ground(double ground_along, double ground_across, double ground_through) {
1074                 accel_ground_along = ground_along;
1075                 accel_ground_across = ground_across;
1076                 accel_ground_through = ground_through;
1077                 update_pad_rotation();
1078         }
1079
1080         public void set_pad_orientation(int pad_orientation) {
1081                 this.pad_orientation = pad_orientation;
1082                 update_pad_rotation();
1083         }
1084
1085         public double   last_imu_time;
1086
1087         private double radians(double degrees) {
1088                 if (degrees == AltosLib.MISSING)
1089                         return AltosLib.MISSING;
1090                 return degrees * Math.PI / 180.0;
1091         }
1092
1093         private void update_orient() {
1094                 if (last_imu_time != AltosLib.MISSING) {
1095                         double  t = time - last_imu_time;
1096
1097                         double  pitch = radians(gyro_pitch());
1098                         double  yaw = radians(gyro_yaw());
1099                         double  roll = radians(gyro_roll());
1100
1101                         if (t > 0 & pitch != AltosLib.MISSING && rotation != null) {
1102                                 rotation.rotate(t, pitch, yaw, roll);
1103                                 orient.set_computed(rotation.tilt(), time);
1104                         }
1105                 }
1106                 last_imu_time = time;
1107         }
1108
1109         private double  gyro_roll, gyro_pitch, gyro_yaw;
1110
1111         public void set_gyro(double roll, double pitch, double yaw) {
1112                 gyro_roll = roll;
1113                 gyro_pitch = roll;
1114                 gyro_roll = roll;
1115                 update_orient();
1116         }
1117
1118         private double accel_along, accel_across, accel_through;
1119
1120         public void set_accel(double along, double across, double through) {
1121                 accel_along = along;
1122                 accel_across = across;
1123                 accel_through = through;
1124                 update_orient();
1125         }
1126
1127         public double accel_along() {
1128                 return accel_along;
1129         }
1130
1131         public double accel_across() {
1132                 return accel_across;
1133         }
1134
1135         public double accel_through() {
1136                 return accel_through;
1137         }
1138
1139         public double gyro_roll() {
1140                 return gyro_roll;
1141         }
1142
1143         public double gyro_pitch() {
1144                 return gyro_pitch;
1145         }
1146
1147         public double gyro_yaw() {
1148                 return gyro_yaw;
1149         }
1150
1151         private double mag_along, mag_across, mag_through;
1152
1153         public void set_mag(double along, double across, double through) {
1154                 mag_along = along;
1155                 mag_across = across;
1156                 mag_through = through;
1157         }
1158
1159         public double mag_along() {
1160                 return mag_along;
1161         }
1162
1163         public double mag_across() {
1164                 if (mag != null)
1165                         return AltosMag.convert_gauss(mag.across);
1166                 return AltosLib.MISSING;
1167         }
1168
1169         public double mag_through() {
1170                 if (mag != null)
1171                         return AltosMag.convert_gauss(mag.through);
1172                 return AltosLib.MISSING;
1173         }
1174
1175         public void set_companion(AltosCompanion companion) {
1176                 this.companion = companion;
1177         }
1178
1179         public void set_acceleration(double acceleration) {
1180                 if (acceleration != AltosLib.MISSING) {
1181                         this.acceleration.set_measured(acceleration, time);
1182                         set |= set_data;
1183                 }
1184         }
1185
1186         public void set_temperature(double temperature) {
1187                 if (temperature != AltosLib.MISSING) {
1188                         this.temperature = temperature;
1189                         set |= set_data;
1190                 }
1191         }
1192
1193         public void set_battery_voltage(double battery_voltage) {
1194                 if (battery_voltage != AltosLib.MISSING) {
1195                         this.battery_voltage = battery_voltage;
1196                         set |= set_data;
1197                 }
1198         }
1199
1200         public void set_pyro_voltage(double pyro_voltage) {
1201                 if (pyro_voltage != AltosLib.MISSING) {
1202                         this.pyro_voltage = pyro_voltage;
1203                         set |= set_data;
1204                 }
1205         }
1206
1207         public void set_apogee_voltage(double apogee_voltage) {
1208                 if (apogee_voltage != AltosLib.MISSING) {
1209                         this.apogee_voltage = apogee_voltage;
1210                         set |= set_data;
1211                 }
1212         }
1213
1214         public void set_main_voltage(double main_voltage) {
1215                 if (main_voltage != AltosLib.MISSING) {
1216                         this.main_voltage = main_voltage;
1217                         set |= set_data;
1218                 }
1219         }
1220
1221         public void set_igniter_voltage(double[] voltage) {
1222                 this.igniter_voltage = voltage;
1223         }
1224
1225         public void set_pyro_fired(int fired) {
1226                 this.pyro_fired = fired;
1227         }
1228
1229         public AltosState (AltosCalData cal_data) {
1230                 super(cal_data);
1231                 init();
1232         }
1233 }