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