dd741716a884d2c2d191a3cb530b638886c77a06
[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 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         /* Current flight dynamic state */
47         public double   acceleration;   /* m/s² */
48         public double   speed;          /* m/s */
49         public double   height;         /* m */
50
51         public AltosGPS gps;
52         public boolean  new_gps;
53
54         public double   time;   /* seconds since boost */
55
56         public int      device_type;
57         public int      config_major;
58         public int      config_minor;
59         public int      apogee_delay;
60         public int      main_deploy;
61         public int      flight_log_max;
62         public String   firmware_version;
63
64         public AltosRecordCompanion companion;
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         public double raw_pressure() { return MISSING; }
79
80         public double filtered_pressure() { return MISSING; }
81
82         public double ground_pressure() { return MISSING; }
83
84         public double battery_voltage() { return MISSING; }
85
86         public double main_voltage() { return MISSING; }
87
88         public double drogue_voltage() { return MISSING; }
89
90         public double temperature() { return MISSING; }
91         
92         public double acceleration() { return MISSING; }
93
94         public double accel_speed() { return MISSING; }
95
96         public AltosIMU imu() { return null; }
97
98         public AltosMag mag() { return null; }
99
100         /*
101          * Convert various pressure values to altitude
102          */
103
104         public double raw_altitude() {
105                 double p = raw_pressure();
106                 if (p == MISSING)
107                         return MISSING;
108                 return AltosConvert.pressure_to_altitude(p);
109         }
110
111         public double ground_altitude() {
112                 double p = ground_pressure();
113                 if (p == MISSING)
114                         return MISSING;
115                 return AltosConvert.pressure_to_altitude(p);
116         }
117
118         public double filtered_altitude() {
119                 double  ga = ground_altitude();
120                 if (height != MISSING && ga != MISSING)
121                         return height + ga;
122
123                 double  p = filtered_pressure();
124                 if (p == MISSING)
125                         return raw_altitude();
126                 return AltosConvert.pressure_to_altitude(p);
127         }
128
129         public double filtered_height() {
130                 if (height != MISSING)
131                         return height;
132
133                 double f = filtered_altitude();
134                 double g = ground_altitude();
135                 if (f == MISSING || g == MISSING)
136                         return MISSING;
137                 return f - g;
138         }
139
140         public double raw_height() {
141                 double r = raw_altitude();
142                 double g = ground_altitude();
143
144                 if (r == MISSING || g == MISSING)
145                         return height;
146                 return r - g;
147         }
148
149         public String state() {
150                 return AltosLib.state_name(state);
151         }
152
153         public int compareTo(AltosRecord o) {
154                 return tick - o.tick;
155         }
156
157         public void copy(AltosRecord old) {
158                 seen = old.seen;
159                 version = old.version;
160                 callsign = old.callsign;
161                 serial = old.serial;
162                 flight = old.flight;
163                 rssi = old.rssi;
164                 status = old.status;
165                 state = old.state;
166                 tick = old.tick;
167                 acceleration = old.acceleration;
168                 speed = old.speed;
169                 height = old.height;
170                 gps = new AltosGPS(old.gps);
171                 new_gps = false;
172                 companion = old.companion;
173         }
174
175         public AltosRecord clone() {
176                 try {
177                         AltosRecord n = (AltosRecord) super.clone();
178                         n.copy(this);
179                         return n;
180                 } catch (CloneNotSupportedException e) {
181                         return null;
182                 }
183         }
184
185         public AltosRecord() {
186                 seen = 0;
187                 version = 0;
188                 callsign = "N0CALL";
189                 serial = 0;
190                 flight = 0;
191                 rssi = 0;
192                 status = 0;
193                 state = AltosLib.ao_flight_startup;
194                 tick = 0;
195                 acceleration = MISSING;
196                 speed = MISSING;
197                 height = MISSING;
198                 gps = new AltosGPS();
199                 new_gps = false;
200                 companion = null;
201         }
202 }