altosui: Add imu and mag sensor values to plots
[fw/altos] / altosui / AltosGraphDataPoint.java
1 /*
2  * Copyright © 2013 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 altosui;
19
20 import org.altusmetrum.altosuilib_1.*;
21 import org.altusmetrum.altoslib_2.*;
22
23 public class AltosGraphDataPoint implements AltosUIDataPoint {
24
25         AltosState      state;
26
27         public static final int data_height = 0;
28         public static final int data_speed = 1;
29         public static final int data_accel = 2;
30         public static final int data_temp = 3;
31         public static final int data_battery_voltage = 4;
32         public static final int data_drogue_voltage = 5;
33         public static final int data_main_voltage = 6;
34         public static final int data_rssi = 7;
35         public static final int data_state = 8;
36         public static final int data_gps_height = 9;
37         public static final int data_gps_nsat_solution = 10;
38         public static final int data_gps_nsat_view = 11;
39         public static final int data_temperature = 12;
40         public static final int data_range = 13;
41         public static final int data_distance = 14;
42         public static final int data_pressure = 15;
43         public static final int data_accel_x = 16;
44         public static final int data_accel_y = 17;
45         public static final int data_accel_z = 18;
46         public static final int data_gyro_x = 19;
47         public static final int data_gyro_y = 20;
48         public static final int data_gyro_z = 21;
49         public static final int data_mag_x = 22;
50         public static final int data_mag_y = 23;
51         public static final int data_mag_z = 24;
52         public static final int data_orient = 25;
53
54         public double x() throws AltosUIDataMissing {
55                 double  time = state.time_since_boost();
56                 if (time < -2)
57                         throw new AltosUIDataMissing(-1);
58                 return time;
59         }
60
61         public double y(int index) throws AltosUIDataMissing {
62                 double y = AltosLib.MISSING;
63                 switch (index) {
64                 case data_height:
65                         y = state.height();
66                         break;
67                 case data_speed:
68                         y = state.speed();
69                         break;
70                 case data_accel:
71                         y = state.acceleration();
72                         break;
73                 case data_temp:
74                         y = state.temperature;
75                         break;
76                 case data_battery_voltage:
77                         y = state.battery_voltage;
78                         break;
79                 case data_drogue_voltage:
80                         y = state.apogee_voltage;
81                         break;
82                 case data_main_voltage:
83                         y = state.main_voltage;
84                         break;
85                 case data_rssi:
86                         y = state.rssi;
87                         break;
88                 case data_gps_height:
89                         y = state.gps_height;
90                         break;  
91                 case data_gps_nsat_solution:
92                         if (state.gps != null)
93                                 y = state.gps.nsat;
94                         break;
95                 case data_gps_nsat_view:
96                         if (state.gps != null && state.gps.cc_gps_sat != null)
97                                 y = state.gps.cc_gps_sat.length;
98                         break;
99                 case data_temperature:
100                         y = state.temperature;
101                         break;
102                 case data_range:
103                         y = state.range;
104                         break;
105                 case data_distance:
106                         if (state.from_pad != null)
107                                 y = state.from_pad.distance;
108                         break;
109                 case data_pressure:
110                         y = state.pressure();
111                         break;
112                         
113                 case data_accel_x:
114                 case data_accel_y:
115                 case data_accel_z:
116                 case data_gyro_x:
117                 case data_gyro_y:
118                 case data_gyro_z:
119                         AltosIMU        imu = state.imu;
120                         if (imu == null)
121                                 break;
122                         switch (index) {
123                         case data_accel_x:
124                                 y = imu.accel_x;
125                                 break;
126                         case data_accel_y:
127                                 y = imu.accel_y;
128                                 break;
129                         case data_accel_z:
130                                 y = imu.accel_z;
131                                 break;
132                         case data_gyro_x:
133                                 y = imu.gyro_x;
134                                 break;
135                         case data_gyro_y:
136                                 y = imu.gyro_y;
137                                 break;
138                         case data_gyro_z:
139                                 y = imu.gyro_z;
140                                 break;
141                         }
142                         break;
143                 case data_mag_x:
144                 case data_mag_y:
145                 case data_mag_z:
146                         AltosMag        mag = state.mag;
147                         if (mag == null)
148                                 break;
149                         switch (index) {
150                         case data_mag_x:
151                                 y = mag.x;
152                                 break;
153                         case data_mag_y:
154                                 y = mag.y;
155                                 break;
156                         case data_mag_z:
157                                 y = mag.z;
158                                 break;
159                         }
160                         break;
161                 case data_orient:
162                         y = state.orient();
163                         break;
164                 }
165                 if (y == AltosLib.MISSING)
166                         throw new AltosUIDataMissing(index);
167                 return y;
168         }
169
170         public int id(int index) {
171                 if (index == data_state) {
172                         int s = state.state;
173                         if (s < Altos.ao_flight_boost || s > Altos.ao_flight_landed)
174                                 return -1;
175                         return s;
176                 }
177                 return 0;
178         }
179
180         public String id_name(int index) {
181                 if (index == data_state)
182                         return state.state_name();
183                 return "";
184         }
185
186         public AltosGraphDataPoint (AltosState state) {
187                 this.state = state;
188         }
189 }