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