telegps: Add graph display
[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                 addSeries("Height",
242                           AltosGraphDataPoint.data_height,
243                           AltosConvert.height,
244                           height_color,
245                           true,
246                           height_axis);
247                 addSeries("Pressure",
248                           AltosGraphDataPoint.data_pressure,
249                           pressure_units,
250                           pressure_color,
251                           false,
252                           pressure_axis);
253                 addSeries("Speed",
254                           AltosGraphDataPoint.data_speed,
255                           AltosConvert.speed,
256                           speed_color,
257                           true,
258                           speed_axis);
259                 addSeries("Acceleration",
260                           AltosGraphDataPoint.data_accel,
261                           AltosConvert.accel,
262                           accel_color,
263                           true,
264                           accel_axis);
265                 if (stats.has_gps) {
266                         addSeries("Range",
267                                   AltosGraphDataPoint.data_range,
268                                   AltosConvert.distance,
269                                   range_color,
270                                   false,
271                                   distance_axis);
272                         addSeries("Distance",
273                                   AltosGraphDataPoint.data_distance,
274                                   AltosConvert.distance,
275                                   distance_color,
276                                   false,
277                                   distance_axis);
278                         addSeries("GPS Height",
279                                   AltosGraphDataPoint.data_gps_height,
280                                   AltosConvert.height,
281                                   gps_height_color,
282                                   false,
283                                   height_axis);
284                         addSeries("GPS Satellites in Solution",
285                                   AltosGraphDataPoint.data_gps_nsat_solution,
286                                   nsat_units,
287                                   gps_nsat_solution_color,
288                                   false,
289                                   nsat_axis);
290                         addSeries("GPS Satellites in View",
291                                   AltosGraphDataPoint.data_gps_nsat_view,
292                                   nsat_units,
293                                   gps_nsat_view_color,
294                                   false,
295                                   nsat_axis);
296                         addSeries("GPS Course",
297                                   AltosGraphDataPoint.data_gps_course,
298                                   orient_units,
299                                   gps_course_color,
300                                   false,
301                                   course_axis);
302                         addSeries("GPS Ground Speed",
303                                   AltosGraphDataPoint.data_gps_ground_speed,
304                                   AltosConvert.speed,
305                                   gps_ground_speed_color,
306                                   false,
307                                   speed_axis);
308                         addSeries("GPS Climb Rate",
309                                   AltosGraphDataPoint.data_gps_climb_rate,
310                                   AltosConvert.speed,
311                                   gps_climb_rate_color,
312                                   false,
313                                   speed_axis);
314                 }
315                 if (stats.has_rssi)
316                         addSeries("Received Signal Strength",
317                                   AltosGraphDataPoint.data_rssi,
318                                   dbm_units,
319                                   dbm_color,
320                                   false,
321                                   dbm_axis);
322                 if (stats.has_other_adc) {
323                         addSeries("Temperature",
324                                   AltosGraphDataPoint.data_temperature,
325                                   AltosConvert.temperature,
326                                   temperature_color,
327                                   false,
328                                   temperature_axis);
329                         addSeries("Battery Voltage",
330                                   AltosGraphDataPoint.data_battery_voltage,
331                                   voltage_units,
332                                   battery_voltage_color,
333                                   false,
334                                   voltage_axis);
335                         addSeries("Drogue Voltage",
336                                   AltosGraphDataPoint.data_drogue_voltage,
337                                   voltage_units,
338                                   drogue_voltage_color,
339                                   false,
340                                   voltage_axis);
341                         addSeries("Main Voltage",
342                                   AltosGraphDataPoint.data_main_voltage,
343                                   voltage_units,
344                                   main_voltage_color,
345                                   false,
346                                   voltage_axis);
347                 }
348
349                 if (stats.has_imu) {
350                         addSeries("Acceleration X",
351                                   AltosGraphDataPoint.data_accel_x,
352                                   AltosConvert.accel,
353                                   accel_x_color,
354                                   false,
355                                   accel_axis);
356                         addSeries("Acceleration Y",
357                                   AltosGraphDataPoint.data_accel_y,
358                                   AltosConvert.accel,
359                                   accel_y_color,
360                                   false,
361                                   accel_axis);
362                         addSeries("Acceleration Z",
363                                   AltosGraphDataPoint.data_accel_z,
364                                   AltosConvert.accel,
365                                   accel_z_color,
366                                   false,
367                                   accel_axis);
368                         addSeries("Rotation Rate X",
369                                   AltosGraphDataPoint.data_gyro_x,
370                                   gyro_units,
371                                   gyro_x_color,
372                                   false,
373                                   gyro_axis);
374                         addSeries("Rotation Rate Y",
375                                   AltosGraphDataPoint.data_gyro_y,
376                                   gyro_units,
377                                   gyro_y_color,
378                                   false,
379                                   gyro_axis);
380                         addSeries("Rotation Rate Z",
381                                   AltosGraphDataPoint.data_gyro_z,
382                                   gyro_units,
383                                   gyro_z_color,
384                                   false,
385                                   gyro_axis);
386                 }
387                 if (stats.has_mag) {
388                         addSeries("Magnetometer X",
389                                   AltosGraphDataPoint.data_mag_x,
390                                   mag_units,
391                                   mag_x_color,
392                                   false,
393                                   mag_axis);
394                         addSeries("Magnetometer Y",
395                                   AltosGraphDataPoint.data_mag_y,
396                                   mag_units,
397                                   mag_y_color,
398                                   false,
399                                   mag_axis);
400                         addSeries("Magnetometer Z",
401                                   AltosGraphDataPoint.data_mag_z,
402                                   mag_units,
403                                   mag_z_color,
404                                   false,
405                                   mag_axis);
406                 }
407                 if (stats.has_orient)
408                         addSeries("Tilt Angle",
409                                   AltosGraphDataPoint.data_orient,
410                                   orient_units,
411                                   orient_color,
412                                   false,
413                                   orient_axis);
414                 if (stats.num_ignitor > 0) {
415                         for (int i = 0; i < stats.num_ignitor; i++)
416                                 addSeries(AltosLib.ignitor_name(i),
417                                           AltosGraphDataPoint.data_ignitor_0 + i,
418                                           voltage_units,
419                                           main_voltage_color,
420                                           false,
421                                           voltage_axis);
422                         for (int i = 0; i < stats.num_ignitor; i++)
423                                 addMarker(AltosLib.ignitor_name(i), AltosGraphDataPoint.data_ignitor_fired_0 + i, state_color);
424                 }
425
426                 setDataSet(dataSet);
427         }
428 }