altosdroid: Implement voice just like altosui
[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 import java.lang.*;
21 import java.text.*;
22 import java.util.HashMap;
23 import java.io.*;
24
25 public class AltosRecord implements Comparable <AltosRecord>, Cloneable {
26
27         public static final int seen_flight = 1;
28         public static final int seen_sensor = 2;
29         public static final int seen_temp_volt = 4;
30         public static final int seen_deploy = 8;
31         public static final int seen_gps_time = 16;
32         public static final int seen_gps_lat = 32;
33         public static final int seen_gps_lon = 64;
34         public static final int seen_companion = 128;
35
36         public int      seen;
37         
38         public final static int MISSING = 0x7fffffff;
39
40         /* Every AltosRecord implementation provides these fields */
41         
42         public int      version;
43         public String   callsign;
44         public int      serial;
45         public int      flight;
46         public int      rssi;
47         public int      status;
48         public int      state;
49         public int      tick;
50
51         /* Current flight dynamic state */
52         public double   acceleration;   /* m/s² */
53         public double   speed;          /* m/s */
54         public double   height;         /* m */
55
56         public AltosGPS gps;
57         public boolean  new_gps;
58
59         public double   time;   /* seconds since boost */
60
61         public int      device_type;
62         public int      config_major;
63         public int      config_minor;
64         public int      apogee_delay;
65         public int      main_deploy;
66         public int      flight_log_max;
67         public String   firmware_version;
68
69         public AltosRecordCompanion companion;
70
71         /*
72          * Abstract methods that convert record data
73          * to standard units:
74          *
75          *      pressure:       Pa
76          *      voltage:        V
77          *      acceleration:   m/s²
78          *      speed:          m/s
79          *      height:         m
80          *      temperature:    °C
81          */
82
83         public double raw_pressure() { return MISSING; }
84
85         public double filtered_pressure() { return MISSING; }
86
87         public double ground_pressure() { return MISSING; }
88
89         public double battery_voltage() { return MISSING; }
90
91         public double main_voltage() { return MISSING; }
92
93         public double drogue_voltage() { return MISSING; }
94
95         public double temperature() { return MISSING; }
96         
97         public double acceleration() { return MISSING; }
98
99         public double accel_speed() { return MISSING; }
100
101         public AltosIMU imu() { return null; }
102
103         public AltosMag mag() { return null; }
104
105         /*
106          * Convert various pressure values to altitude
107          */
108
109         public double raw_altitude() {
110                 double p = raw_pressure();
111                 if (p == MISSING)
112                         return MISSING;
113                 return AltosConvert.pressure_to_altitude(p);
114         }
115
116         public double ground_altitude() {
117                 double p = ground_pressure();
118                 if (p == MISSING)
119                         return MISSING;
120                 return AltosConvert.pressure_to_altitude(p);
121         }
122
123         public double filtered_altitude() {
124                 double  ga = ground_altitude();
125                 if (height != MISSING && ga != MISSING)
126                         return height + ga;
127
128                 double  p = filtered_pressure();
129                 if (p == MISSING)
130                         return MISSING;
131                 return AltosConvert.pressure_to_altitude(p);
132         }
133
134         public double filtered_height() {
135                 if (height != MISSING)
136                         return height;
137
138                 double f = filtered_altitude();
139                 double g = ground_altitude();
140                 if (f == MISSING || g == MISSING)
141                         return MISSING;
142                 return f - g;
143         }
144
145         public double raw_height() {
146                 double r = raw_altitude();
147                 double g = ground_altitude();
148
149                 if (r == MISSING || g == MISSING)
150                         return height;
151                 return r - g;
152         }
153
154         public String state() {
155                 return AltosLib.state_name(state);
156         }
157
158         public int compareTo(AltosRecord o) {
159                 return tick - o.tick;
160         }
161
162         public void copy(AltosRecord old) {
163                 seen = old.seen;
164                 version = old.version;
165                 callsign = old.callsign;
166                 serial = old.serial;
167                 flight = old.flight;
168                 rssi = old.rssi;
169                 status = old.status;
170                 state = old.state;
171                 tick = old.tick;
172                 acceleration = old.acceleration;
173                 speed = old.speed;
174                 height = old.height;
175                 gps = new AltosGPS(old.gps);
176                 new_gps = false;
177                 companion = old.companion;
178         }
179
180         public AltosRecord clone() {
181                 try {
182                         AltosRecord n = (AltosRecord) super.clone();
183                         n.copy(this);
184                         return n;
185                 } catch (CloneNotSupportedException e) {
186                         return null;
187                 }
188         }
189
190         public AltosRecord() {
191                 seen = 0;
192                 version = 0;
193                 callsign = "N0CALL";
194                 serial = 0;
195                 flight = 0;
196                 rssi = 0;
197                 status = 0;
198                 state = AltosLib.ao_flight_startup;
199                 tick = 0;
200                 acceleration = MISSING;
201                 speed = MISSING;
202                 height = MISSING;
203                 gps = new AltosGPS();
204                 new_gps = false;
205                 companion = null;
206         }
207 }