Switch from GPLv2 to GPLv2+
[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 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 AltosMagUnits 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 "Ga";
165         }
166
167         public String say_units(boolean imperial_units) {
168                 return "gauss";
169         }
170
171         public int show_fraction(int width, boolean imperial_units) {
172                 return 2;
173         }
174 }
175
176 class AltosDopUnits 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 null;
188         }
189
190         public String say_units(boolean imperial_units) {
191                 return null;
192         }
193
194         public int show_fraction(int width, boolean imperial_units) {
195                 return 1;
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 gps_course_color = new Color (100, 31, 112);
216         static final private Color gps_ground_speed_color = new Color (31, 112, 100);
217         static final private Color gps_climb_rate_color = new Color (31, 31, 112);
218         static final private Color gps_pdop_color = new Color(50, 194, 0);
219         static final private Color gps_hdop_color = new Color(50, 0, 194);
220         static final private Color gps_vdop_color = new Color(194, 0, 50);
221         static final private Color temperature_color = new Color (31, 194, 194);
222         static final private Color dbm_color = new Color(31, 100, 100);
223         static final private Color state_color = new Color(0,0,0);
224         static final private Color accel_x_color = new Color(255, 0, 0);
225         static final private Color accel_y_color = new Color(0, 255, 0);
226         static final private Color accel_z_color = new Color(0, 0, 255);
227         static final private Color gyro_x_color = new Color(192, 0, 0);
228         static final private Color gyro_y_color = new Color(0, 192, 0);
229         static final private Color gyro_z_color = new Color(0, 0, 192);
230         static final private Color mag_x_color = new Color(128, 0, 0);
231         static final private Color mag_y_color = new Color(0, 128, 0);
232         static final private Color mag_z_color = new Color(0, 0, 128);
233         static final private Color orient_color = new Color(31, 31, 31);
234
235         static AltosVoltage voltage_units = new AltosVoltage();
236         static AltosPressure pressure_units = new AltosPressure();
237         static AltosNsat nsat_units = new AltosNsat();
238         static AltosDbm dbm_units = new AltosDbm();
239         static AltosGyroUnits gyro_units = new AltosGyroUnits();
240         static AltosOrient orient_units = new AltosOrient();
241         static AltosMagUnits mag_units = new AltosMagUnits();
242         static AltosDopUnits dop_units = new AltosDopUnits();
243
244         AltosUIAxis     height_axis, speed_axis, accel_axis, voltage_axis, temperature_axis, nsat_axis, dbm_axis;
245         AltosUIAxis     distance_axis, pressure_axis;
246         AltosUIAxis     gyro_axis, orient_axis, mag_axis;
247         AltosUIAxis     course_axis, dop_axis;
248
249         public AltosGraph(AltosUIEnable enable, AltosFlightStats stats, AltosGraphDataSet dataSet) {
250                 super(enable);
251
252                 height_axis = newAxis("Height", AltosConvert.height, height_color);
253                 pressure_axis = newAxis("Pressure", pressure_units, pressure_color, 0);
254                 speed_axis = newAxis("Speed", AltosConvert.speed, speed_color);
255                 accel_axis = newAxis("Acceleration", AltosConvert.accel, accel_color);
256                 voltage_axis = newAxis("Voltage", voltage_units, voltage_color);
257                 temperature_axis = newAxis("Temperature", AltosConvert.temperature, temperature_color, 0);
258                 nsat_axis = newAxis("Satellites", nsat_units, gps_nsat_color,
259                                     AltosUIAxis.axis_include_zero | AltosUIAxis.axis_integer);
260                 dbm_axis = newAxis("Signal Strength", dbm_units, dbm_color, 0);
261                 distance_axis = newAxis("Distance", AltosConvert.distance, range_color);
262
263                 gyro_axis = newAxis("Rotation Rate", gyro_units, gyro_z_color, 0);
264                 orient_axis = newAxis("Tilt Angle", orient_units, orient_color, 0);
265                 mag_axis = newAxis("Magnetic Field", mag_units, mag_x_color, 0);
266                 course_axis = newAxis("Course", orient_units, gps_course_color, 0);
267                 dop_axis = newAxis("Dilution of Precision", dop_units, gps_pdop_color, 0);
268
269                 addMarker("State", AltosGraphDataPoint.data_state, state_color);
270
271                 if (stats.has_flight_data) {
272                         addSeries("Height",
273                                   AltosGraphDataPoint.data_height,
274                                   AltosConvert.height,
275                                   height_color,
276                                   true,
277                                   height_axis);
278                         addSeries("Pressure",
279                                   AltosGraphDataPoint.data_pressure,
280                                   pressure_units,
281                                   pressure_color,
282                                   false,
283                                   pressure_axis);
284                         addSeries("Speed",
285                                   AltosGraphDataPoint.data_speed,
286                                   AltosConvert.speed,
287                                   speed_color,
288                                   true,
289                                   speed_axis);
290                         addSeries("Acceleration",
291                                   AltosGraphDataPoint.data_accel,
292                                   AltosConvert.accel,
293                                   accel_color,
294                                   true,
295                                   accel_axis);
296                 }
297                 if (stats.has_gps) {
298                         boolean enable_gps = false;
299
300                         if (!stats.has_flight_data)
301                                 enable_gps = true;
302
303                         addSeries("Range",
304                                   AltosGraphDataPoint.data_range,
305                                   AltosConvert.distance,
306                                   range_color,
307                                   false,
308                                   distance_axis);
309                         addSeries("Distance",
310                                   AltosGraphDataPoint.data_distance,
311                                   AltosConvert.distance,
312                                   distance_color,
313                                   enable_gps,
314                                   distance_axis);
315                         addSeries("GPS Height",
316                                   AltosGraphDataPoint.data_gps_height,
317                                   AltosConvert.height,
318                                   gps_height_color,
319                                   enable_gps,
320                                   height_axis);
321                         addSeries("GPS Altitude",
322                                   AltosGraphDataPoint.data_gps_altitude,
323                                   AltosConvert.height,
324                                   gps_height_color,
325                                   false,
326                                   height_axis);
327                         addSeries("GPS Satellites in Solution",
328                                   AltosGraphDataPoint.data_gps_nsat_solution,
329                                   nsat_units,
330                                   gps_nsat_solution_color,
331                                   false,
332                                   nsat_axis);
333                         if (stats.has_gps_sats) {
334                                 addSeries("GPS Satellites in View",
335                                           AltosGraphDataPoint.data_gps_nsat_view,
336                                           nsat_units,
337                                           gps_nsat_view_color,
338                                           false,
339                                           nsat_axis);
340                         }
341                         if (stats.has_gps_detail) {
342                                 addSeries("GPS Course",
343                                           AltosGraphDataPoint.data_gps_course,
344                                           orient_units,
345                                           gps_course_color,
346                                           false,
347                                           course_axis);
348                                 addSeries("GPS Ground Speed",
349                                           AltosGraphDataPoint.data_gps_ground_speed,
350                                           AltosConvert.speed,
351                                           gps_ground_speed_color,
352                                           enable_gps,
353                                           speed_axis);
354                                 addSeries("GPS Climb Rate",
355                                           AltosGraphDataPoint.data_gps_climb_rate,
356                                           AltosConvert.speed,
357                                           gps_climb_rate_color,
358                                           enable_gps,
359                                           speed_axis);
360                         }
361                         addSeries("GPS Position DOP",
362                                   AltosGraphDataPoint.data_gps_pdop,
363                                   dop_units,
364                                   gps_pdop_color,
365                                   false,
366                                   dop_axis);
367                         if (stats.has_gps_detail) {
368                                 addSeries("GPS Horizontal DOP",
369                                           AltosGraphDataPoint.data_gps_hdop,
370                                           dop_units,
371                                           gps_hdop_color,
372                                           false,
373                                           dop_axis);
374                                 addSeries("GPS Vertical DOP",
375                                           AltosGraphDataPoint.data_gps_vdop,
376                                           dop_units,
377                                           gps_vdop_color,
378                                           false,
379                                           dop_axis);
380                         }
381                 }
382                 if (stats.has_rssi)
383                         addSeries("Received Signal Strength",
384                                   AltosGraphDataPoint.data_rssi,
385                                   dbm_units,
386                                   dbm_color,
387                                   false,
388                                   dbm_axis);
389
390                 if (stats.has_battery)
391                         addSeries("Battery Voltage",
392                                   AltosGraphDataPoint.data_battery_voltage,
393                                   voltage_units,
394                                   battery_voltage_color,
395                                   false,
396                                   voltage_axis);
397
398                 if (stats.has_flight_adc) {
399                         addSeries("Temperature",
400                                   AltosGraphDataPoint.data_temperature,
401                                   AltosConvert.temperature,
402                                   temperature_color,
403                                   false,
404                                   temperature_axis);
405                         addSeries("Drogue Voltage",
406                                   AltosGraphDataPoint.data_drogue_voltage,
407                                   voltage_units,
408                                   drogue_voltage_color,
409                                   false,
410                                   voltage_axis);
411                         addSeries("Main Voltage",
412                                   AltosGraphDataPoint.data_main_voltage,
413                                   voltage_units,
414                                   main_voltage_color,
415                                   false,
416                                   voltage_axis);
417                 }
418
419                 if (stats.has_imu) {
420                         addSeries("Acceleration Along",
421                                   AltosGraphDataPoint.data_accel_along,
422                                   AltosConvert.accel,
423                                   accel_x_color,
424                                   false,
425                                   accel_axis);
426                         addSeries("Acceleration Across",
427                                   AltosGraphDataPoint.data_accel_across,
428                                   AltosConvert.accel,
429                                   accel_y_color,
430                                   false,
431                                   accel_axis);
432                         addSeries("Acceleration Through",
433                                   AltosGraphDataPoint.data_accel_through,
434                                   AltosConvert.accel,
435                                   accel_z_color,
436                                   false,
437                                   accel_axis);
438                         addSeries("Roll Rate",
439                                   AltosGraphDataPoint.data_gyro_roll,
440                                   gyro_units,
441                                   gyro_x_color,
442                                   false,
443                                   gyro_axis);
444                         addSeries("Pitch Rate",
445                                   AltosGraphDataPoint.data_gyro_pitch,
446                                   gyro_units,
447                                   gyro_y_color,
448                                   false,
449                                   gyro_axis);
450                         addSeries("Yaw Rate",
451                                   AltosGraphDataPoint.data_gyro_yaw,
452                                   gyro_units,
453                                   gyro_z_color,
454                                   false,
455                                   gyro_axis);
456                 }
457                 if (stats.has_mag) {
458                         addSeries("Magnetometer Along",
459                                   AltosGraphDataPoint.data_mag_along,
460                                   mag_units,
461                                   mag_x_color,
462                                   false,
463                                   mag_axis);
464                         addSeries("Magnetometer Across",
465                                   AltosGraphDataPoint.data_mag_across,
466                                   mag_units,
467                                   mag_y_color,
468                                   false,
469                                   mag_axis);
470                         addSeries("Magnetometer Through",
471                                   AltosGraphDataPoint.data_mag_through,
472                                   mag_units,
473                                   mag_z_color,
474                                   false,
475                                   mag_axis);
476                 }
477                 if (stats.has_orient)
478                         addSeries("Tilt Angle",
479                                   AltosGraphDataPoint.data_orient,
480                                   orient_units,
481                                   orient_color,
482                                   false,
483                                   orient_axis);
484                 if (stats.num_ignitor > 0) {
485                         for (int i = 0; i < stats.num_ignitor; i++)
486                                 addSeries(AltosLib.ignitor_name(i),
487                                           AltosGraphDataPoint.data_ignitor_0 + i,
488                                           voltage_units,
489                                           main_voltage_color,
490                                           false,
491                                           voltage_axis);
492                         for (int i = 0; i < stats.num_ignitor; i++)
493                                 addMarker(AltosLib.ignitor_name(i), AltosGraphDataPoint.data_ignitor_fired_0 + i, state_color);
494                 }
495
496                 setDataSet(dataSet);
497         }
498 }