f46b12eaa7b00b3973f4b1c6f4995515093e1409
[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   ignitor_voltage[];
706
707         public AltosGPS gps;
708         public AltosGPS temp_gps;
709         public int temp_gps_sat_tick;
710         public boolean  gps_pending;
711
712         public AltosIMU imu;
713         public AltosMag mag;
714
715         public static final int MIN_PAD_SAMPLES = 10;
716
717         public int      npad;
718         public int      gps_waiting;
719         public boolean  gps_ready;
720
721         public int      ngps;
722
723         public AltosGreatCircle from_pad;
724         public double   elevation;      /* from pad */
725         public double   range;          /* total distance */
726
727         public double   gps_height;
728
729         public double pad_lat, pad_lon, pad_alt;
730
731         public int      speak_tick;
732         public double   speak_altitude;
733
734         public String   callsign;
735         public String   firmware_version;
736
737         public double   ground_accel;
738
739         public int      log_format;
740         public int      log_space;
741         public String   product;
742
743         public AltosMs5607      baro;
744
745         public AltosCompanion   companion;
746
747         public int      pyro_fired;
748
749         public void set_npad(int npad) {
750                 this.npad = npad;
751                 gps_waiting = MIN_PAD_SAMPLES - npad;
752                 if (this.gps_waiting < 0)
753                         gps_waiting = 0;
754                 gps_ready = gps_waiting == 0;
755         }
756
757         public void init() {
758                 set = 0;
759
760                 received_time = System.currentTimeMillis();
761                 time = AltosLib.MISSING;
762                 time_change = AltosLib.MISSING;
763                 prev_time = AltosLib.MISSING;
764                 state = AltosLib.ao_flight_invalid;
765                 landed = false;
766                 boost = false;
767                 rssi = AltosLib.MISSING;
768                 status = 0;
769                 config_major = AltosLib.MISSING;
770                 config_minor = AltosLib.MISSING;
771                 apogee_delay = AltosLib.MISSING;
772                 main_deploy = AltosLib.MISSING;
773                 flight_log_max = AltosLib.MISSING;
774
775                 ground_altitude = new AltosCValue();
776                 ground_pressure = new AltosGroundPressure();
777                 altitude = new AltosAltitude();
778                 pressure = new AltosPressure();
779                 thrust = new AltosForce();
780                 speed = new AltosSpeed();
781                 acceleration = new AltosAccel();
782                 orient = new AltosCValue();
783
784                 temperature = AltosLib.MISSING;
785                 battery_voltage = AltosLib.MISSING;
786                 pyro_voltage = AltosLib.MISSING;
787                 apogee_voltage = AltosLib.MISSING;
788                 main_voltage = AltosLib.MISSING;
789                 ignitor_voltage = null;
790
791                 kalman_height = new AltosValue();
792                 kalman_speed = new AltosValue();
793                 kalman_acceleration = new AltosValue();
794
795                 gps = null;
796                 temp_gps = null;
797                 temp_gps_sat_tick = 0;
798                 gps_pending = false;
799
800                 imu = null;
801                 last_imu_time = AltosLib.MISSING;
802                 rotation = null;
803                 ground_rotation = null;
804
805                 mag = null;
806                 accel_zero_along = AltosLib.MISSING;
807                 accel_zero_across = AltosLib.MISSING;
808                 accel_zero_through = AltosLib.MISSING;
809
810                 accel_ground_along = AltosLib.MISSING;
811                 accel_ground_across = AltosLib.MISSING;
812                 accel_ground_through = AltosLib.MISSING;
813
814                 pad_orientation = AltosLib.MISSING;
815
816                 set_npad(0);
817                 ngps = 0;
818
819                 from_pad = null;
820                 elevation = AltosLib.MISSING;
821                 range = AltosLib.MISSING;
822                 gps_height = AltosLib.MISSING;
823
824                 pad_lat = AltosLib.MISSING;
825                 pad_lon = AltosLib.MISSING;
826                 pad_alt = AltosLib.MISSING;
827
828                 gps_altitude = new AltosGpsAltitude();
829                 gps_ground_altitude = new AltosGpsGroundAltitude();
830                 gps_ground_speed = new AltosValue();
831                 gps_speed = new AltosValue();
832                 gps_ascent_rate = new AltosValue();
833                 gps_course = new AltosValue();
834
835                 speak_tick = AltosLib.MISSING;
836                 speak_altitude = AltosLib.MISSING;
837
838                 callsign = null;
839                 firmware_version = null;
840
841                 ground_accel = AltosLib.MISSING;
842
843                 log_format = AltosLib.MISSING;
844                 log_space = AltosLib.MISSING;
845                 product = null;
846                 receiver_serial = AltosLib.MISSING;
847                 altitude_32 = AltosLib.MISSING;
848
849                 baro = null;
850                 companion = null;
851
852                 pyro_fired = 0;
853         }
854
855         void finish_update() {
856                 ground_altitude.finish_update();
857                 altitude.finish_update();
858                 pressure.finish_update();
859                 speed.finish_update();
860                 acceleration.finish_update();
861                 orient.finish_update();
862
863                 kalman_height.finish_update();
864                 kalman_speed.finish_update();
865                 kalman_acceleration.finish_update();
866         }
867
868         void update_time() {
869         }
870
871         void update_gps() {
872                 elevation = AltosLib.MISSING;
873                 range = AltosLib.MISSING;
874
875                 if (gps == null)
876                         return;
877
878                 if (gps.locked && gps.nsat >= 4) {
879                         /* Track consecutive 'good' gps reports, waiting for 10 of them */
880                         if (state == AltosLib.ao_flight_pad || state == AltosLib.ao_flight_stateless) {
881                                 set_npad(npad+1);
882                                 if (pad_lat != AltosLib.MISSING && (npad < 10 || state == AltosLib.ao_flight_pad)) {
883                                         pad_lat = (pad_lat * 31 + gps.lat) / 32;
884                                         pad_lon = (pad_lon * 31 + gps.lon) / 32;
885                                         gps_ground_altitude.set_filtered(gps.alt, time);
886                                 }
887                         }
888                         if (pad_lat == AltosLib.MISSING) {
889                                 pad_lat = gps.lat;
890                                 pad_lon = gps.lon;
891                                 gps_ground_altitude.set(gps.alt, time);
892                         }
893                         gps_altitude.set(gps.alt, time);
894                         if (gps.climb_rate != AltosLib.MISSING)
895                                 gps_ascent_rate.set(gps.climb_rate, time);
896                         if (gps.ground_speed != AltosLib.MISSING)
897                                 gps_ground_speed.set(gps.ground_speed, time);
898                         if (gps.climb_rate != AltosLib.MISSING && gps.ground_speed != AltosLib.MISSING)
899                                 gps_speed.set(Math.sqrt(gps.ground_speed * gps.ground_speed +
900                                                         gps.climb_rate * gps.climb_rate), time);
901                         if (gps.course != AltosLib.MISSING)
902                                 gps_course.set(gps.course, time);
903                 }
904                 if (gps.lat != 0 && gps.lon != 0 &&
905                     pad_lat != AltosLib.MISSING &&
906                     pad_lon != AltosLib.MISSING)
907                 {
908                         double h = height();
909
910                         if (h == AltosLib.MISSING)
911                                 h = 0;
912                         from_pad = new AltosGreatCircle(pad_lat, pad_lon, 0, gps.lat, gps.lon, h);
913                         elevation = from_pad.elevation;
914                         range = from_pad.range;
915                 }
916         }
917
918         public String state_name() {
919                 return AltosLib.state_name(state);
920         }
921
922         public void set_product(String product) {
923                 this.product = product;
924         }
925
926         public void set_state(int state) {
927                 if (state != AltosLib.ao_flight_invalid) {
928                         this.state = state;
929                         ascent = (AltosLib.ao_flight_boost <= state &&
930                                   state <= AltosLib.ao_flight_coast);
931                         boost = (AltosLib.ao_flight_boost == state);
932                 }
933         }
934
935         public int state() {
936                 return state;
937         }
938
939         public void set_log_format(int log_format) {
940                 this.log_format = log_format;
941                 switch (log_format) {
942                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
943                         this.state = AltosLib.ao_flight_stateless;
944                         break;
945                 }
946         }
947
948         public void set_log_space(int log_space) {
949                 this.log_space = log_space;
950         }
951
952         public void set_flight_params(int apogee_delay, int main_deploy) {
953                 this.apogee_delay = apogee_delay;
954                 this.main_deploy = main_deploy;
955         }
956
957         public void set_config(int major, int minor, int flight_log_max) {
958                 config_major = major;
959                 config_minor = minor;
960                 this.flight_log_max = flight_log_max;
961         }
962
963         public void set_callsign(String callsign) {
964                 this.callsign = callsign;
965         }
966
967         public void set_firmware_version(String version) {
968                 firmware_version = version;
969         }
970
971         public int compare_version(String other_version) {
972                 if (firmware_version == null)
973                         return AltosLib.MISSING;
974                 return AltosLib.compare_version(firmware_version, other_version);
975         }
976
977         private void re_init() {
978                 int rs = receiver_serial;
979                 init();
980                 receiver_serial = rs;
981         }
982
983 //      public void set_flight(int flight) {
984 //
985 //              /* When the flight changes, reset the state */
986 //              if (flight != AltosLib.MISSING) {
987 //                      if (this.flight != AltosLib.MISSING &&
988 //                          this.flight != flight) {
989 //                              re_init();
990 //                      }
991 //                      this.flight = flight;
992 //              }
993 //      }
994 //
995 //      public void set_serial(int serial) {
996 //              /* When the serial changes, reset the state */
997 //              if (serial != AltosLib.MISSING) {
998 //                      if (this.serial != AltosLib.MISSING &&
999 //                          this.serial != serial) {
1000 //                              re_init();
1001 //                      }
1002 //                      this.serial = serial;
1003 //              }
1004 //      }
1005 //
1006 //      public void set_receiver_serial(int serial) {
1007 //              if (serial != AltosLib.MISSING)
1008 //                      receiver_serial = serial;
1009 //      }
1010
1011         public boolean altitude_32() {
1012                 return altitude_32 == 1;
1013         }
1014
1015         public void set_altitude_32(int altitude_32) {
1016                 if (altitude_32 != AltosLib.MISSING)
1017                         this.altitude_32 = altitude_32;
1018         }
1019
1020         public int rssi() {
1021                 if (rssi == AltosLib.MISSING)
1022                         return 0;
1023                 return rssi;
1024         }
1025
1026         public void set_rssi(int rssi, int status) {
1027                 if (rssi != AltosLib.MISSING) {
1028                         this.rssi = rssi;
1029                         this.status = status;
1030                 }
1031         }
1032
1033         public void set_received_time(long ms) {
1034                 received_time = ms;
1035         }
1036
1037         public void set_gps(AltosGPS gps) {
1038                 if (gps != null) {
1039                         this.gps = gps;
1040                         update_gps();
1041                         set |= set_gps;
1042                 }
1043         }
1044
1045
1046         public double   accel_zero_along;
1047         public double   accel_zero_across;
1048         public double   accel_zero_through;
1049
1050         public AltosRotation    rotation;
1051         public AltosRotation    ground_rotation;
1052
1053         public void set_accel_zero(double zero_along, double zero_across, double zero_through) {
1054                 if (zero_along != AltosLib.MISSING) {
1055                         accel_zero_along = zero_along;
1056                         accel_zero_across = zero_across;
1057                         accel_zero_through = zero_through;
1058                 }
1059         }
1060
1061         public int pad_orientation;
1062
1063         public double   accel_ground_along, accel_ground_across, accel_ground_through;
1064
1065         void update_pad_rotation() {
1066                 if (pad_orientation != AltosLib.MISSING && accel_ground_along != AltosLib.MISSING) {
1067                         rotation = new AltosRotation(AltosIMU.convert_accel(accel_ground_across - accel_zero_across),
1068                                                      AltosIMU.convert_accel(accel_ground_through - accel_zero_through),
1069                                                      AltosIMU.convert_accel(accel_ground_along - accel_zero_along),
1070                                                      pad_orientation);
1071                         ground_rotation = rotation;
1072                         orient.set_computed(rotation.tilt(), time);
1073                 }
1074         }
1075
1076         public void set_accel_ground(double ground_along, double ground_across, double ground_through) {
1077                 accel_ground_along = ground_along;
1078                 accel_ground_across = ground_across;
1079                 accel_ground_through = ground_through;
1080                 update_pad_rotation();
1081         }
1082
1083         public void set_pad_orientation(int pad_orientation) {
1084                 this.pad_orientation = pad_orientation;
1085                 update_pad_rotation();
1086         }
1087
1088         public double   last_imu_time;
1089
1090         private double radians(double degrees) {
1091                 if (degrees == AltosLib.MISSING)
1092                         return AltosLib.MISSING;
1093                 return degrees * Math.PI / 180.0;
1094         }
1095
1096         private void update_orient() {
1097                 if (last_imu_time != AltosLib.MISSING) {
1098                         double  t = time - last_imu_time;
1099
1100                         double  pitch = radians(gyro_pitch());
1101                         double  yaw = radians(gyro_yaw());
1102                         double  roll = radians(gyro_roll());
1103
1104                         if (t > 0 & pitch != AltosLib.MISSING && rotation != null) {
1105                                 rotation.rotate(t, pitch, yaw, roll);
1106                                 orient.set_computed(rotation.tilt(), time);
1107                         }
1108                 }
1109                 last_imu_time = time;
1110         }
1111
1112         private double  gyro_roll, gyro_pitch, gyro_yaw;
1113
1114         public void set_gyro(double roll, double pitch, double yaw) {
1115                 gyro_roll = roll;
1116                 gyro_pitch = roll;
1117                 gyro_roll = roll;
1118                 update_orient();
1119         }
1120
1121         private double accel_along, accel_across, accel_through;
1122
1123         public void set_accel(double along, double across, double through) {
1124                 accel_along = along;
1125                 accel_across = across;
1126                 accel_through = through;
1127                 update_orient();
1128         }
1129
1130         public double accel_along() {
1131                 return accel_along;
1132         }
1133
1134         public double accel_across() {
1135                 return accel_across;
1136         }
1137
1138         public double accel_through() {
1139                 return accel_through;
1140         }
1141
1142         public double gyro_roll() {
1143                 return gyro_roll;
1144         }
1145
1146         public double gyro_pitch() {
1147                 return gyro_pitch;
1148         }
1149
1150         public double gyro_yaw() {
1151                 return gyro_yaw;
1152         }
1153
1154         private double mag_along, mag_across, mag_through;
1155
1156         public void set_mag(double along, double across, double through) {
1157                 mag_along = along;
1158                 mag_across = across;
1159                 mag_through = through;
1160         }
1161
1162         public double mag_along() {
1163                 return mag_along;
1164         }
1165
1166         public double mag_across() {
1167                 if (mag != null)
1168                         return AltosMag.convert_gauss(mag.across);
1169                 return AltosLib.MISSING;
1170         }
1171
1172         public double mag_through() {
1173                 if (mag != null)
1174                         return AltosMag.convert_gauss(mag.through);
1175                 return AltosLib.MISSING;
1176         }
1177
1178         public void set_companion(AltosCompanion companion) {
1179                 this.companion = companion;
1180         }
1181
1182         public void set_acceleration(double acceleration) {
1183                 if (acceleration != AltosLib.MISSING) {
1184                         this.acceleration.set_measured(acceleration, time);
1185                         set |= set_data;
1186                 }
1187         }
1188
1189         public void set_temperature(double temperature) {
1190                 if (temperature != AltosLib.MISSING) {
1191                         this.temperature = temperature;
1192                         set |= set_data;
1193                 }
1194         }
1195
1196         public void set_battery_voltage(double battery_voltage) {
1197                 if (battery_voltage != AltosLib.MISSING) {
1198                         this.battery_voltage = battery_voltage;
1199                         set |= set_data;
1200                 }
1201         }
1202
1203         public void set_pyro_voltage(double pyro_voltage) {
1204                 if (pyro_voltage != AltosLib.MISSING) {
1205                         this.pyro_voltage = pyro_voltage;
1206                         set |= set_data;
1207                 }
1208         }
1209
1210         public void set_apogee_voltage(double apogee_voltage) {
1211                 if (apogee_voltage != AltosLib.MISSING) {
1212                         this.apogee_voltage = apogee_voltage;
1213                         set |= set_data;
1214                 }
1215         }
1216
1217         public void set_main_voltage(double main_voltage) {
1218                 if (main_voltage != AltosLib.MISSING) {
1219                         this.main_voltage = main_voltage;
1220                         set |= set_data;
1221                 }
1222         }
1223
1224         public void set_ignitor_voltage(double[] voltage) {
1225                 this.ignitor_voltage = voltage;
1226         }
1227
1228         public void set_pyro_fired(int fired) {
1229                 this.pyro_fired = fired;
1230         }
1231
1232         public AltosState (AltosCalData cal_data) {
1233                 super(cal_data);
1234                 init();
1235         }
1236 }