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