Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / altoslib / AltosRecord.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.AltosLib;
19
20 public abstract class AltosRecord implements Comparable <AltosRecord>, Cloneable {
21
22         public static final int seen_flight = 1;
23         public static final int seen_sensor = 2;
24         public static final int seen_temp_volt = 4;
25         public static final int seen_deploy = 8;
26         public static final int seen_gps_time = 16;
27         public static final int seen_gps_lat = 32;
28         public static final int seen_gps_lon = 64;
29         public static final int seen_companion = 128;
30
31         public int      seen;
32         
33         public final static int MISSING = 0x7fffffff;
34
35         /* Every AltosRecord implementation provides these fields */
36         
37         public int      version;
38         public String   callsign;
39         public int      serial;
40         public int      flight;
41         public int      rssi;
42         public int      status;
43         public int      state;
44         public int      tick;
45
46         public AltosGPS gps;
47         public boolean  new_gps;
48
49         public double   time;   /* seconds since boost */
50
51         public int      device_type;
52         public int      config_major;
53         public int      config_minor;
54         public int      apogee_delay;
55         public int      main_deploy;
56         public int      flight_log_max;
57         public String   firmware_version;
58
59         public AltosRecordCompanion companion;
60
61         /* Telemetry sources have these values recorded from the flight computer */
62         public double   kalman_height;
63         public double   kalman_speed;
64         public double   kalman_acceleration;
65
66         /*
67          * Abstract methods that convert record data
68          * to standard units:
69          *
70          *      pressure:       Pa
71          *      voltage:        V
72          *      acceleration:   m/s²
73          *      speed:          m/s
74          *      height:         m
75          *      temperature:    °C
76          */
77
78         abstract public double pressure();
79         abstract public double ground_pressure();
80         abstract public double acceleration();
81
82         public double altitude() {
83                 double  p = pressure();
84
85                 if (p == MISSING)
86                         return MISSING;
87                 return AltosConvert.pressure_to_altitude(p);
88         }
89
90         public double ground_altitude() {
91                 double  p = ground_pressure();
92
93                 if (p == MISSING)
94                         return MISSING;
95                 return AltosConvert.pressure_to_altitude(p);
96         }
97
98         public double height() {
99                 double  g = ground_altitude();
100                 double  a = altitude();
101
102                 if (g == MISSING)
103                         return MISSING;
104                 if (a == MISSING)
105                         return MISSING;
106                 return a - g;
107         }
108
109         public double battery_voltage() { return MISSING; }
110
111         public double main_voltage() { return MISSING; }
112
113         public double drogue_voltage() { return MISSING; }
114
115         public double temperature() { return MISSING; }
116         
117         public AltosIMU imu() { return null; }
118
119         public AltosMag mag() { return null; }
120
121         public String state() {
122                 return AltosLib.state_name(state);
123         }
124
125         public int compareTo(AltosRecord o) {
126                 return tick - o.tick;
127         }
128
129         public void copy(AltosRecord old) {
130                 seen = old.seen;
131                 version = old.version;
132                 callsign = old.callsign;
133                 serial = old.serial;
134                 flight = old.flight;
135                 rssi = old.rssi;
136                 status = old.status;
137                 state = old.state;
138                 tick = old.tick;
139                 gps = new AltosGPS(old.gps);
140                 new_gps = old.new_gps;
141                 companion = old.companion;
142                 kalman_acceleration = old.kalman_acceleration;
143                 kalman_speed = old.kalman_speed;
144                 kalman_height = old.kalman_height;
145         }
146
147         public AltosRecord clone() {
148                 try {
149                         AltosRecord n = (AltosRecord) super.clone();
150                         n.copy(this);
151                         return n;
152                 } catch (CloneNotSupportedException e) {
153                         return null;
154                 }
155         }
156
157         public AltosRecord() {
158                 seen = 0;
159                 version = 0;
160                 callsign = "N0CALL";
161                 serial = 0;
162                 flight = 0;
163                 rssi = 0;
164                 status = 0;
165                 state = AltosLib.ao_flight_startup;
166                 tick = 0;
167                 gps = new AltosGPS();
168                 new_gps = false;
169                 companion = null;
170
171                 kalman_acceleration = MISSING;
172                 kalman_speed = MISSING;
173                 kalman_height = MISSING;
174         }
175 }