update load cell channel based on repaired board and 1T load cell
[fw/altos] / teststand / TestStatsTable.java
1 /*
2  * Copyright © 2018 Bdale Garbee <bdale@gag.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 3 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 teststand;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import java.util.*;
24 import org.altusmetrum.altoslib_12.*;
25 import org.altusmetrum.altosuilib_12.*;
26
27 public class TestStatsTable extends JComponent implements AltosFontListener {
28         GridBagLayout   layout;
29
30         LinkedList<FlightStat> flight_stats = new LinkedList<FlightStat>();
31
32         class FlightStat implements AltosFontListener {
33                 JLabel          label;
34                 JTextField[]    value;
35
36                 public void font_size_changed(int font_size) {
37                         label.setFont(AltosUILib.label_font);
38                         for (int i = 0; i < value.length; i++)
39                                 value[i].setFont(AltosUILib.value_font);
40                 }
41
42                 public void set(String ... values) {
43                         for (int j = 0; j < values.length; j++)
44                                 value[j].setText(values[j]);
45                 }
46
47                 public FlightStat(GridBagLayout layout, int y, String label_text, String ... values) {
48                         GridBagConstraints      c = new GridBagConstraints();
49                         c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
50                         c.weighty = 1;
51
52                         label = new JLabel(label_text);
53                         label.setFont(AltosUILib.label_font);
54                         label.setHorizontalAlignment(SwingConstants.LEFT);
55                         c.gridx = 0; c.gridy = y;
56                         c.anchor = GridBagConstraints.WEST;
57                         c.fill = GridBagConstraints.VERTICAL;
58                         c.weightx = 0;
59                         layout.setConstraints(label, c);
60                         add(label);
61
62                         value = new JTextField[values.length];
63                         for (int j = 0; j < values.length; j++) {
64                                 value[j] = new JTextField(values[j]);
65                                 value[j].setEditable(false);
66                                 value[j].setFont(AltosUILib.value_font);
67                                 value[j].setHorizontalAlignment(SwingConstants.RIGHT);
68                                 c.gridx = j+1; c.gridy = y;
69                                 c.anchor = GridBagConstraints.EAST;
70                                 c.fill = GridBagConstraints.BOTH;
71                                 c.weightx = 1;
72                                 layout.setConstraints(value[j], c);
73                                 add(value[j]);
74                         }
75                         flight_stats.add(this);
76                 }
77
78         }
79
80         public void font_size_changed(int font_size) {
81                 for (FlightStat f : flight_stats)
82                         f.font_size_changed(font_size);
83         }
84
85         static String pos(double p, String pos, String neg) {
86                 String  h = pos;
87                 if (p < 0) {
88                         h = neg;
89                         p = -p;
90                 }
91                 int deg = (int) Math.floor(p);
92                 double min = (p - Math.floor(p)) * 60.0;
93                 return String.format("%s %4d° %9.6f'", h, deg, min);
94         }
95
96         private FlightStat      max_pressure_stat;
97         private FlightStat      max_thrust_stat;
98
99         private FlightStat      max_height_stat;
100         private FlightStat      max_speed_stat;
101         private FlightStat      max_accel_stat;
102         private FlightStat      boost_accel_stat;
103         private FlightStat      drogue_descent_stat;
104         private FlightStat      main_descent_stat;
105
106         public void set_values(TestStats stats) {
107                 if (max_thrust_stat != null && stats.max_thrust != AltosLib.MISSING) {
108                         max_thrust_stat.set(String.format("%6.1f N", stats.max_thrust),
109                                             String.format("%5.0f lbs", AltosConvert.n_to_lb(stats.max_thrust)));
110                 }
111                 if (max_height_stat != null && stats.max_height != AltosLib.MISSING) {
112                         max_height_stat.set(String.format("%6.1f m", stats.max_height),
113                                             String.format("%5.0f ft", AltosConvert.meters_to_feet(stats.max_height)));
114                 }
115                 if (max_speed_stat != null && stats.max_speed != AltosLib.MISSING) {
116                         max_speed_stat.set(String.format("%6.1f m/s", stats.max_speed),
117                                            String.format("%5.0f fps", AltosConvert.mps_to_fps(stats.max_speed)),
118                                            String.format("Mach %4.1f", AltosConvert.meters_to_mach(stats.max_speed)));
119                 }
120                 if (max_accel_stat != null && stats.max_acceleration != AltosLib.MISSING) {
121                         max_accel_stat.set(String.format("%6.1f m/s²", stats.max_acceleration),
122                                            String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.max_acceleration)),
123                                            String.format("%6.2f G", AltosConvert.meters_to_g(stats.max_acceleration)));
124                 }
125                 if (boost_accel_stat != null && stats.state_accel[AltosLib.ao_flight_boost] != AltosLib.MISSING) {
126                         boost_accel_stat.set(String.format("%6.1f m/s²", stats.state_accel[AltosLib.ao_flight_boost]),
127                                              String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.state_accel[AltosLib.ao_flight_boost])),
128                                              String.format("%6.2f G", AltosConvert.meters_to_g(stats.state_accel[AltosLib.ao_flight_boost])));
129                 }
130                 if (drogue_descent_stat != null && stats.state_speed[AltosLib.ao_flight_drogue] != AltosLib.MISSING) {
131                         drogue_descent_stat.set(String.format("%6.1f m/s", -stats.state_speed[AltosLib.ao_flight_drogue]),
132                                                 String.format("%5.0f ft/s", -AltosConvert.meters_to_feet(stats.state_speed[AltosLib.ao_flight_drogue])));
133                 }
134                 if (main_descent_stat != null && stats.state_speed[AltosLib.ao_flight_main] != AltosLib.MISSING) {
135                                 main_descent_stat.set(String.format("%6.1f m/s", -stats.state_speed[AltosLib.ao_flight_main]),
136                                                       String.format("%5.0f ft/s", -AltosConvert.meters_to_feet(stats.state_speed[AltosLib.ao_flight_main])));
137                 }
138         }
139
140         public void set_stats(TestStats stats) {
141                 int y = 0;
142                 if (stats.serial != AltosLib.MISSING) {
143                         if (stats.product != null && stats.firmware_version != null)
144                                 new FlightStat(layout, y++, "Device",
145                                                stats.product,
146                                                String.format("version %s", stats.firmware_version),
147                                                String.format("serial %d", stats.serial));
148                         else
149                                 new FlightStat(layout, y++, "Serial", String.format("%d", stats.serial));
150                 }
151                 if (stats.flight != AltosLib.MISSING)
152                         new FlightStat(layout, y++, "Test", String.format("%d", stats.flight));
153                 if (stats.year != AltosLib.MISSING && stats.hour != AltosLib.MISSING)
154                         new FlightStat(layout, y++, "Date/Time",
155                                        String.format("%04d-%02d-%02d", stats.year, stats.month, stats.day),
156                                        String.format("%02d:%02d:%02d UTC", stats.hour, stats.minute, stats.second));
157                 else {
158                         if (stats.year != AltosLib.MISSING)
159                                 new FlightStat(layout, y++, "Date",
160                                                String.format("%04d-%02d-%02d", stats.year, stats.month, stats.day));
161                         if (stats.hour != AltosLib.MISSING)
162                                 new FlightStat(layout, y++, "Time",
163                                                String.format("%02d:%02d:%02d UTC", stats.hour, stats.minute, stats.second));
164                 }
165                 if (stats.max_pressure != AltosLib.MISSING) {
166                         max_pressure_stat = new FlightStat(layout, y++, "Maximum pressure",
167                                                          String.format("%6.1f kpa", stats.max_pressure / 1000.0),
168                                                          String.format("%5.0f psi", AltosConvert.pa_to_psi(stats.max_pressure)));
169                 }
170                 if (stats.max_thrust != AltosLib.MISSING) {
171                         max_thrust_stat = new FlightStat(layout, y++, "Maximum thrust",
172                                                          String.format("%6.1f N", stats.max_thrust),
173                                                          String.format("%5.0f lbs", AltosConvert.n_to_lb(stats.max_thrust)));
174                 }
175                 if (stats.landed_time > stats.boost_time)
176                         new FlightStat(layout, y++, "Flight time",
177                                        String.format("%6.1f s", stats.landed_time - stats.boost_time));
178         }
179
180         public void tell_closing() {
181                 AltosUIPreferences.unregister_font_listener(this);
182         }
183
184         public void filter_changed(TestStats stats) {
185                 set_values(stats);
186         }
187
188         public TestStatsTable() {
189                 layout = new GridBagLayout();
190
191                 setLayout(layout);
192
193                 AltosUIPreferences.register_font_listener(this);
194         }
195
196         public TestStatsTable(TestStats stats) {
197                 this();
198                 set_stats(stats);
199         }
200 }