altoslib: Check for valid pad alt before computing GPS height series
[fw/altos] / altosuilib / AltosGraph.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; 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 package org.altusmetrum.altosuilib_11;
20
21 import java.io.*;
22 import java.util.ArrayList;
23
24 import java.awt.*;
25 import javax.swing.*;
26 import org.altusmetrum.altoslib_11.*;
27
28 import org.jfree.ui.*;
29 import org.jfree.chart.*;
30 import org.jfree.chart.plot.*;
31 import org.jfree.chart.axis.*;
32 import org.jfree.chart.renderer.*;
33 import org.jfree.chart.renderer.xy.*;
34 import org.jfree.chart.labels.*;
35 import org.jfree.data.xy.*;
36 import org.jfree.data.*;
37
38 class AltosNsat extends AltosUnits {
39
40         public double value(double v, boolean imperial_units) {
41                 return v;
42         }
43
44         public double inverse(double v, boolean imperial_units) {
45                 return v;
46         }
47
48         public String show_units(boolean imperial_units) {
49                 return "Sats";
50         }
51
52         public String say_units(boolean imperial_units) {
53                 return "Satellites";
54         }
55
56         public int show_fraction(int width, boolean imperial_units) {
57                 return 0;
58         }
59 }
60
61 class AltosDbm extends AltosUnits {
62
63         public double value(double d, boolean imperial_units) {
64                 return d;
65         }
66
67         public double inverse(double d, boolean imperial_units) {
68                 return d;
69         }
70
71         public String show_units(boolean imperial_units) {
72                 return "dBm";
73         }
74
75         public String say_units(boolean imperial_units) {
76                 return "D B M";
77         }
78
79         public int show_fraction(int width, boolean imperial_units) {
80                 return 0;
81         }
82 }
83
84 class AltosMagUnits extends AltosUnits {
85
86         public double value(double p, boolean imperial_units) {
87                 return p;
88         }
89
90         public double inverse(double p, boolean imperial_units) {
91                 return p;
92         }
93
94         public String show_units(boolean imperial_units) {
95                 return "Ga";
96         }
97
98         public String say_units(boolean imperial_units) {
99                 return "gauss";
100         }
101
102         public int show_fraction(int width, boolean imperial_units) {
103                 return 2;
104         }
105 }
106
107 class AltosDopUnits extends AltosUnits {
108
109         public double value(double p, boolean imperial_units) {
110                 return p;
111         }
112
113         public double inverse(double p, boolean imperial_units) {
114                 return p;
115         }
116
117         public String show_units(boolean imperial_units) {
118                 return null;
119         }
120
121         public String say_units(boolean imperial_units) {
122                 return null;
123         }
124
125         public int show_fraction(int width, boolean imperial_units) {
126                 return 1;
127         }
128 }
129
130 public class AltosGraph extends AltosUIGraph {
131
132         static final private Color height_color = new Color(194,31,31);
133         static final private Color gps_height_color = new Color(150,31,31);
134         static final private Color pressure_color = new Color (225,31,31);
135         static final private Color range_color = new Color(100, 31, 31);
136         static final private Color distance_color = new Color(100, 31, 194);
137         static final private Color speed_color = new Color(31,194,31);
138         static final private Color accel_color = new Color(31,31,194);
139         static final private Color voltage_color = new Color(194, 194, 31);
140         static final private Color battery_voltage_color = new Color(194, 194, 31);
141         static final private Color drogue_voltage_color = new Color(150, 150, 31);
142         static final private Color main_voltage_color = new Color(100, 100, 31);
143         static final private Color gps_nsat_color = new Color (194, 31, 194);
144         static final private Color gps_nsat_solution_color = new Color (194, 31, 194);
145         static final private Color gps_nsat_view_color = new Color (150, 31, 150);
146         static final private Color gps_course_color = new Color (100, 31, 112);
147         static final private Color gps_ground_speed_color = new Color (31, 112, 100);
148         static final private Color gps_climb_rate_color = new Color (31, 31, 112);
149         static final private Color gps_pdop_color = new Color(50, 194, 0);
150         static final private Color gps_hdop_color = new Color(50, 0, 194);
151         static final private Color gps_vdop_color = new Color(194, 0, 50);
152         static final private Color temperature_color = new Color (31, 194, 194);
153         static final private Color dbm_color = new Color(31, 100, 100);
154         static final private Color state_color = new Color(0,0,0);
155         static final private Color accel_x_color = new Color(255, 0, 0);
156         static final private Color accel_y_color = new Color(0, 255, 0);
157         static final private Color accel_z_color = new Color(0, 0, 255);
158         static final private Color gyro_x_color = new Color(192, 0, 0);
159         static final private Color gyro_y_color = new Color(0, 192, 0);
160         static final private Color gyro_z_color = new Color(0, 0, 192);
161         static final private Color mag_x_color = new Color(128, 0, 0);
162         static final private Color mag_y_color = new Color(0, 128, 0);
163         static final private Color mag_z_color = new Color(0, 0, 128);
164         static final private Color orient_color = new Color(31, 31, 31);
165
166         static AltosNsat nsat_units = new AltosNsat();
167         static AltosDbm dbm_units = new AltosDbm();
168         static AltosRotationRate gyro_units = new AltosRotationRate();
169         static AltosOrient orient_units = new AltosOrient();
170         static AltosMagUnits mag_units = new AltosMagUnits();
171         static AltosDopUnits dop_units = new AltosDopUnits();
172
173         AltosUIAxis     height_axis, speed_axis, accel_axis, voltage_axis, temperature_axis, nsat_axis, dbm_axis;
174         AltosUIAxis     distance_axis, pressure_axis;
175         AltosUIAxis     gyro_axis, orient_axis, mag_axis;
176         AltosUIAxis     course_axis, dop_axis;
177
178         public AltosGraph(AltosUIEnable enable, AltosFlightStats stats, AltosGraphDataSet dataSet) {
179                 super(enable);
180
181                 height_axis = newAxis("Height", AltosConvert.height, height_color);
182                 pressure_axis = newAxis("Pressure", AltosConvert.pressure, pressure_color, 0);
183                 speed_axis = newAxis("Speed", AltosConvert.speed, speed_color);
184                 accel_axis = newAxis("Acceleration", AltosConvert.accel, accel_color);
185                 voltage_axis = newAxis("Voltage", AltosConvert.voltage, voltage_color);
186                 temperature_axis = newAxis("Temperature", AltosConvert.temperature, temperature_color, 0);
187                 nsat_axis = newAxis("Satellites", nsat_units, gps_nsat_color,
188                                     AltosUIAxis.axis_include_zero | AltosUIAxis.axis_integer);
189                 dbm_axis = newAxis("Signal Strength", dbm_units, dbm_color, 0);
190                 distance_axis = newAxis("Distance", AltosConvert.distance, range_color);
191
192                 gyro_axis = newAxis("Rotation Rate", gyro_units, gyro_z_color, 0);
193                 orient_axis = newAxis("Tilt Angle", orient_units, orient_color, 0);
194                 mag_axis = newAxis("Magnetic Field", mag_units, mag_x_color, 0);
195                 course_axis = newAxis("Course", orient_units, gps_course_color, 0);
196                 dop_axis = newAxis("Dilution of Precision", dop_units, gps_pdop_color, 0);
197
198                 addMarker("State", AltosGraphDataPoint.data_state, state_color);
199
200                 if (stats.has_flight_data) {
201                         addSeries("Height",
202                                   AltosGraphDataPoint.data_height,
203                                   AltosConvert.height,
204                                   height_color,
205                                   true,
206                                   height_axis);
207                         addSeries("Pressure",
208                                   AltosGraphDataPoint.data_pressure,
209                                   AltosConvert.pressure,
210                                   pressure_color,
211                                   false,
212                                   pressure_axis);
213                         addSeries("Speed",
214                                   AltosGraphDataPoint.data_speed,
215                                   AltosConvert.speed,
216                                   speed_color,
217                                   true,
218                                   speed_axis);
219                         addSeries("Acceleration",
220                                   AltosGraphDataPoint.data_accel,
221                                   AltosConvert.accel,
222                                   accel_color,
223                                   true,
224                                   accel_axis);
225                 }
226                 if (stats.has_gps) {
227                         boolean enable_gps = false;
228
229                         if (!stats.has_flight_data)
230                                 enable_gps = true;
231
232                         addSeries("Range",
233                                   AltosGraphDataPoint.data_range,
234                                   AltosConvert.distance,
235                                   range_color,
236                                   false,
237                                   distance_axis);
238                         addSeries("Distance",
239                                   AltosGraphDataPoint.data_distance,
240                                   AltosConvert.distance,
241                                   distance_color,
242                                   enable_gps,
243                                   distance_axis);
244                         addSeries("GPS Height",
245                                   AltosGraphDataPoint.data_gps_height,
246                                   AltosConvert.height,
247                                   gps_height_color,
248                                   enable_gps,
249                                   height_axis);
250                         addSeries("GPS Altitude",
251                                   AltosGraphDataPoint.data_gps_altitude,
252                                   AltosConvert.height,
253                                   gps_height_color,
254                                   false,
255                                   height_axis);
256                         addSeries("GPS Satellites in Solution",
257                                   AltosGraphDataPoint.data_gps_nsat_solution,
258                                   nsat_units,
259                                   gps_nsat_solution_color,
260                                   false,
261                                   nsat_axis);
262                         if (stats.has_gps_sats) {
263                                 addSeries("GPS Satellites in View",
264                                           AltosGraphDataPoint.data_gps_nsat_view,
265                                           nsat_units,
266                                           gps_nsat_view_color,
267                                           false,
268                                           nsat_axis);
269                         }
270                         if (stats.has_gps_detail) {
271                                 addSeries("GPS Course",
272                                           AltosGraphDataPoint.data_gps_course,
273                                           orient_units,
274                                           gps_course_color,
275                                           false,
276                                           course_axis);
277                                 addSeries("GPS Ground Speed",
278                                           AltosGraphDataPoint.data_gps_ground_speed,
279                                           AltosConvert.speed,
280                                           gps_ground_speed_color,
281                                           enable_gps,
282                                           speed_axis);
283                                 addSeries("GPS Climb Rate",
284                                           AltosGraphDataPoint.data_gps_climb_rate,
285                                           AltosConvert.speed,
286                                           gps_climb_rate_color,
287                                           enable_gps,
288                                           speed_axis);
289                         }
290                         addSeries("GPS Position DOP",
291                                   AltosGraphDataPoint.data_gps_pdop,
292                                   dop_units,
293                                   gps_pdop_color,
294                                   false,
295                                   dop_axis);
296                         if (stats.has_gps_detail) {
297                                 addSeries("GPS Horizontal DOP",
298                                           AltosGraphDataPoint.data_gps_hdop,
299                                           dop_units,
300                                           gps_hdop_color,
301                                           false,
302                                           dop_axis);
303                                 addSeries("GPS Vertical DOP",
304                                           AltosGraphDataPoint.data_gps_vdop,
305                                           dop_units,
306                                           gps_vdop_color,
307                                           false,
308                                           dop_axis);
309                         }
310                 }
311                 if (stats.has_rssi)
312                         addSeries("Received Signal Strength",
313                                   AltosGraphDataPoint.data_rssi,
314                                   dbm_units,
315                                   dbm_color,
316                                   false,
317                                   dbm_axis);
318
319                 if (stats.has_battery)
320                         addSeries("Battery Voltage",
321                                   AltosGraphDataPoint.data_battery_voltage,
322                                   AltosConvert.voltage,
323                                   battery_voltage_color,
324                                   false,
325                                   voltage_axis);
326
327                 if (stats.has_flight_adc) {
328                         addSeries("Temperature",
329                                   AltosGraphDataPoint.data_temperature,
330                                   AltosConvert.temperature,
331                                   temperature_color,
332                                   false,
333                                   temperature_axis);
334                         addSeries("Drogue Voltage",
335                                   AltosGraphDataPoint.data_drogue_voltage,
336                                   AltosConvert.voltage,
337                                   drogue_voltage_color,
338                                   false,
339                                   voltage_axis);
340                         addSeries("Main Voltage",
341                                   AltosGraphDataPoint.data_main_voltage,
342                                   AltosConvert.voltage,
343                                   main_voltage_color,
344                                   false,
345                                   voltage_axis);
346                 }
347
348                 if (stats.has_imu) {
349                         addSeries("Acceleration Along",
350                                   AltosGraphDataPoint.data_accel_along,
351                                   AltosConvert.accel,
352                                   accel_x_color,
353                                   false,
354                                   accel_axis);
355                         addSeries("Acceleration Across",
356                                   AltosGraphDataPoint.data_accel_across,
357                                   AltosConvert.accel,
358                                   accel_y_color,
359                                   false,
360                                   accel_axis);
361                         addSeries("Acceleration Through",
362                                   AltosGraphDataPoint.data_accel_through,
363                                   AltosConvert.accel,
364                                   accel_z_color,
365                                   false,
366                                   accel_axis);
367                         addSeries("Roll Rate",
368                                   AltosGraphDataPoint.data_gyro_roll,
369                                   gyro_units,
370                                   gyro_x_color,
371                                   false,
372                                   gyro_axis);
373                         addSeries("Pitch Rate",
374                                   AltosGraphDataPoint.data_gyro_pitch,
375                                   gyro_units,
376                                   gyro_y_color,
377                                   false,
378                                   gyro_axis);
379                         addSeries("Yaw Rate",
380                                   AltosGraphDataPoint.data_gyro_yaw,
381                                   gyro_units,
382                                   gyro_z_color,
383                                   false,
384                                   gyro_axis);
385                 }
386                 if (stats.has_mag) {
387                         addSeries("Magnetometer Along",
388                                   AltosGraphDataPoint.data_mag_along,
389                                   mag_units,
390                                   mag_x_color,
391                                   false,
392                                   mag_axis);
393                         addSeries("Magnetometer Across",
394                                   AltosGraphDataPoint.data_mag_across,
395                                   mag_units,
396                                   mag_y_color,
397                                   false,
398                                   mag_axis);
399                         addSeries("Magnetometer Through",
400                                   AltosGraphDataPoint.data_mag_through,
401                                   mag_units,
402                                   mag_z_color,
403                                   false,
404                                   mag_axis);
405                 }
406                 if (stats.has_orient)
407                         addSeries("Tilt Angle",
408                                   AltosGraphDataPoint.data_orient,
409                                   orient_units,
410                                   orient_color,
411                                   false,
412                                   orient_axis);
413                 if (stats.num_ignitor > 0) {
414                         for (int i = 0; i < stats.num_ignitor; i++)
415                                 addSeries(AltosLib.ignitor_name(i),
416                                           AltosGraphDataPoint.data_ignitor_0 + i,
417                                           AltosConvert.voltage,
418                                           main_voltage_color,
419                                           false,
420                                           voltage_axis);
421                         for (int i = 0; i < stats.num_ignitor; i++)
422                                 addMarker(AltosLib.ignitor_name(i), AltosGraphDataPoint.data_ignitor_fired_0 + i, state_color);
423                 }
424
425                 setDataSet(dataSet);
426         }
427 }