lose the --kml option, a few UI text tweaks
[fw/altos] / teststand / TestStand.java
1 /*
2  * Copyright © 2010 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 teststand;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import java.io.*;
25 import java.util.concurrent.*;
26 import org.altusmetrum.altoslib_12.*;
27 import org.altusmetrum.altosuilib_12.*;
28
29 public class TestStand extends AltosUIFrame implements AltosEepromGrapher {
30         public AltosVoice voice = new AltosVoice();
31
32         public static boolean load_library(Frame frame) {
33                 if (!Altos.load_library()) {
34                         JOptionPane.showMessageDialog(frame,
35                                                       String.format("No AltOS library in \"%s\"",
36                                                                     System.getProperty("java.library.path","<undefined>")),
37                                                       "Cannot load device access library",
38                                                       JOptionPane.ERROR_MESSAGE);
39                         return false;
40                 }
41                 return true;
42         }
43
44         void telemetry_window(AltosDevice device) {
45                 try {
46                         AltosFlightReader reader = new AltosTelemetryReader(new AltosSerial(device));
47                         if (reader != null)
48                                 new AltosFlightUI(voice, reader, device.getSerial());
49                 } catch (FileNotFoundException ee) {
50                         JOptionPane.showMessageDialog(TestStand.this,
51                                                       ee.getMessage(),
52                                                       String.format ("Cannot open %s", device.toShortString()),
53                                                       JOptionPane.ERROR_MESSAGE);
54                 } catch (AltosSerialInUseException si) {
55                         JOptionPane.showMessageDialog(TestStand.this,
56                                                       String.format("Device \"%s\" already in use",
57                                                                     device.toShortString()),
58                                                       "Device in use",
59                                                       JOptionPane.ERROR_MESSAGE);
60                 } catch (IOException ee) {
61                         JOptionPane.showMessageDialog(TestStand.this,
62                                                       String.format ("Unknown I/O error on %s", device.toShortString()),
63                                                       "Unknown I/O error",
64                                                       JOptionPane.ERROR_MESSAGE);
65                 } catch (TimeoutException te) {
66                         JOptionPane.showMessageDialog(this,
67                                                       String.format ("Timeout on %s", device.toShortString()),
68                                                       "Timeout error",
69                                                       JOptionPane.ERROR_MESSAGE);
70                 } catch (InterruptedException ie) {
71                         JOptionPane.showMessageDialog(this,
72                                                       String.format("Interrupted %s", device.toShortString()),
73                                                       "Interrupted exception",
74                                                       JOptionPane.ERROR_MESSAGE);
75                 }
76         }
77
78         public void scan_device_selected(AltosDevice device) {
79                 telemetry_window(device);
80         }
81
82         Container       pane;
83         GridBagLayout   gridbag;
84
85         JButton addButton(int x, int y, String label) {
86                 GridBagConstraints      c;
87                 JButton                 b;
88
89                 c = new GridBagConstraints();
90                 c.gridx = x; c.gridy = y;
91                 c.fill = GridBagConstraints.BOTH;
92                 c.weightx = 1;
93                 c.weighty = 1;
94                 b = new JButton(label);
95
96                 //Dimension ps = b.getPreferredSize();
97
98                 gridbag.setConstraints(b, c);
99                 add(b, c);
100                 return b;
101         }
102
103         /* OSXAdapter interfaces */
104         public void macosx_file_handler(String path) {
105                 process_graph(new File(path));
106         }
107
108         public void macosx_quit_handler() {
109                 System.exit(0);
110         }
111
112         public void macosx_preferences_handler() {
113                 ConfigureTestStand();
114         }
115
116         public TestStand() {
117
118                 load_library(null);
119
120                 register_for_macosx_events();
121
122                 AltosUIPreferences.set_component(this);
123
124                 pane = getContentPane();
125                 gridbag = new GridBagLayout();
126                 pane.setLayout(gridbag);
127
128                 JButton b;
129
130                 b = addButton(0, 0, "Monitor Flight");
131                 b.addActionListener(new ActionListener() {
132                                         public void actionPerformed(ActionEvent e) {
133                                                 ConnectToDevice();
134                                         }
135                                 });
136                 b.setToolTipText("Connect to TeleDongle and monitor telemetry");
137                 b = addButton(1, 0, "Save Data");
138                 b.addActionListener(new ActionListener() {
139                                         public void actionPerformed(ActionEvent e) {
140                                                 SaveFlightData();
141                                         }
142                                 });
143                 b.setToolTipText("Download and/or delete flight data from an altimeter");
144                 b = addButton(3, 0, "Graph Data");
145                 b.addActionListener(new ActionListener() {
146                                         public void actionPerformed(ActionEvent e) {
147                                                 GraphData();
148                                         }
149                                 });
150                 b.setToolTipText("Present flight data in a graph and table of statistics");
151                 b = addButton(4, 0, "Export Data");
152                 b.addActionListener(new ActionListener() {
153                                         public void actionPerformed(ActionEvent e) {
154                                                 ExportData();
155                                         }
156                                 });
157                 b.setToolTipText("Convert flight data for a spreadsheet or GoogleEarth");
158                 b = addButton(0, 1, "Configure TeleFire");
159                 b.addActionListener(new ActionListener() {
160                                         public void actionPerformed(ActionEvent e) {
161                                                 ConfigureTeleMetrum();
162                                         }
163                                 });
164                 b.setToolTipText("Set flight, storage and communication parameters");
165                 b = addButton(1, 1, "Configure TestStand");
166                 b.addActionListener(new ActionListener() {
167                                 public void actionPerformed(ActionEvent e) {
168                                         ConfigureTestStand();
169                                 }
170                         });
171                 b.setToolTipText("Global TestStand settings");
172
173                 b = addButton(2, 1, "Configure Ground Station");
174                 b.addActionListener(new ActionListener() {
175                                 public void actionPerformed(ActionEvent e) {
176                                         ConfigureTeleDongle();
177                                 }
178                         });
179
180                 b = addButton(3, 1, "Flash Image");
181                 b.addActionListener(new ActionListener() {
182                                 public void actionPerformed(ActionEvent e) {
183                                         FlashImage();
184                                 }
185                         });
186                 b.setToolTipText("Replace the firmware in any AltusMetrum product");
187
188                 b = addButton(0, 2, "Scan Channels");
189                 b.addActionListener(new ActionListener() {
190                                 public void actionPerformed(ActionEvent e) {
191                                         ScanChannels();
192                                 }
193                         });
194                 b.setToolTipText("Find what channel an altimeter is sending telemetry on");
195
196                 b = addButton(4, 2, "Quit");
197                 b.addActionListener(new ActionListener() {
198                                 public void actionPerformed(ActionEvent e) {
199                                         System.exit(0);
200                                 }
201                         });
202                 b.setToolTipText("Close all active windows and terminate TestStand");
203
204                 setTitle("Altus Metrum Test Stand");
205
206                 pane.doLayout();
207                 pane.validate();
208
209                 doLayout();
210                 validate();
211
212                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
213                 addWindowListener(new WindowAdapter() {
214                         @Override
215                         public void windowClosing(WindowEvent e) {
216                                 System.exit(0);
217                         }
218                 });
219
220                 setLocationByPlatform(false);
221
222                 /* Insets aren't set before the window is visible */
223                 setVisible(true);
224         }
225
226         private void ConnectToDevice() {
227                 AltosDevice     device = AltosDeviceUIDialog.show(TestStand.this,
228                                                                 Altos.product_basestation);
229
230                 if (device != null)
231                         telemetry_window(device);
232         }
233
234         void ConfigureCallsign() {
235                 String  result;
236                 result = JOptionPane.showInputDialog(TestStand.this,
237                                                      "Configure Callsign",
238                                                      AltosUIPreferences.callsign());
239                 if (result != null)
240                         AltosUIPreferences.set_callsign(result);
241         }
242
243         void ConfigureTeleMetrum() {
244                 new AltosConfigFC(TestStand.this);
245         }
246
247         void ConfigureTeleDongle() {
248                 new AltosConfigTD(TestStand.this);
249         }
250
251         void FlashImage() {
252                 AltosFlashUI.show(TestStand.this);
253         }
254
255         void ScanChannels() {
256                 new AltosScanUI(TestStand.this, true);
257         }
258
259         /* Connect to TeleMetrum, either directly or through
260          * a TeleDongle over the packet link
261          */
262
263         public void graph_flights(AltosEepromList flights) {
264                 for (AltosEepromLog flight : flights) {
265                         if (flight.graph_selected && flight.file != null) {
266                                 process_graph(flight.file);
267                         }
268                 }
269         }
270
271         private void SaveFlightData() {
272                 new AltosEepromManage(this, this, AltosLib.product_any);
273         }
274
275         private static AltosFlightSeries make_series(AltosRecordSet set) {
276                 AltosFlightSeries series = new AltosFlightSeries(set.cal_data());
277                 set.capture_series(series);
278                 series.finish();
279                 return series;
280         }
281
282         /* Load a flight log file and write out a CSV file containing
283          * all of the data in standard units
284          */
285
286         private void ExportData() {
287                 AltosDataChooser chooser;
288                 chooser = new AltosDataChooser(this);
289                 AltosRecordSet set = chooser.runDialog();
290                 if (set == null)
291                         return;
292                 AltosFlightSeries series = make_series(set);
293                 new AltosCSVUI(TestStand.this, series, chooser.file());
294         }
295
296         /* Load a flight log CSV file and display a pretty graph.
297          */
298
299         private void GraphData() {
300                 AltosDataChooser chooser;
301                 chooser = new AltosDataChooser(this);
302                 AltosRecordSet set = chooser.runDialog();
303                 if (set == null)
304                         return;
305                 try {
306                         new AltosGraphUI(set, chooser.file());
307                 } catch (InterruptedException ie) {
308                 } catch (IOException ie) {
309                 }
310         }
311
312         private void ConfigureTestStand() {
313                 new AltosConfigureUI(TestStand.this, voice);
314         }
315
316         static AltosWriter open_csv(File file) {
317                 try {
318                         return new AltosCSV(file);
319                 } catch (FileNotFoundException fe) {
320                         System.out.printf("%s\n", fe.getMessage());
321                         return null;
322                 }
323         }
324
325         static AltosRecordSet record_set(File input) {
326                 try {
327                         return AltosLib.record_set(input);
328                 } catch (IOException ie) {
329                         String message = ie.getMessage();
330                         if (message == null)
331                                 message = String.format("%s (I/O error)", input.toString());
332                         System.err.printf("%s: %s\n", input.toString(), message);
333                 }
334                 return null;
335         }
336
337         static final int process_none = 0;
338         static final int process_csv = 1;
339         static final int process_graph = 3;
340         static final int process_summary = 4;
341         static final int process_oneline = 5;
342
343         static boolean process_csv(File input) {
344                 AltosRecordSet set = record_set(input);
345                 if (set == null)
346                         return false;
347
348                 File output = Altos.replace_extension(input,".csv");
349                 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
350                 if (input.equals(output)) {
351                         System.out.printf("Not processing '%s'\n", input);
352                         return false;
353                 } else {
354                         AltosWriter writer = open_csv(output);
355                         if (writer == null)
356                                 return false;
357                         AltosFlightSeries series = make_series(set);
358                         writer.write(series);
359                         writer.close();
360                 }
361                 return true;
362         }
363
364         static boolean process_graph(File file) {
365                 AltosRecordSet set = record_set(file);
366                 if (set == null)
367                         return false;
368                 try {
369                         new AltosGraphUI(set, file);
370                         return true;
371                 } catch (InterruptedException ie) {
372                 } catch (IOException ie) {
373                 }
374                 return false;
375         }
376
377         static boolean process_summary(File file) {
378                 AltosRecordSet set = record_set(file);
379                 if (set == null)
380                         return false;
381                 System.out.printf("%s:\n", file.toString());
382                 AltosFlightSeries series = make_series(set);
383                 TestStats stats = new TestStats(series);
384                 if (stats.serial != AltosLib.MISSING)
385                         System.out.printf("Serial:       %5d\n", stats.serial);
386                 if (stats.flight != AltosLib.MISSING)
387                         System.out.printf("Flight:       %5d\n", stats.flight);
388                 if (stats.year != AltosLib.MISSING)
389                         System.out.printf("Date:    %04d-%02d-%02d\n",
390                                           stats.year, stats.month, stats.day);
391                 if (stats.hour != AltosLib.MISSING)
392                         System.out.printf("Time:      %02d:%02d:%02d UTC\n",
393                                           stats.hour, stats.minute, stats.second);
394                 if (stats.max_height != AltosLib.MISSING)
395                         System.out.printf("Max height:  %6.0f m    %6.0f ft\n",
396                                           stats.max_height,
397                                           AltosConvert.meters_to_feet(stats.max_height));
398                 if (stats.max_speed != AltosLib.MISSING)
399                         System.out.printf("Max speed:   %6.0f m/s  %6.0f ft/s  %6.4f Mach\n",
400                                           stats.max_speed,
401                                           AltosConvert.meters_to_feet(stats.max_speed),
402                                           AltosConvert.meters_to_mach(stats.max_speed));
403                 if (stats.max_acceleration != AltosLib.MISSING) {
404                         System.out.printf("Max accel:   %6.0f m/s² %6.0f ft/s² %6.2f g\n",
405                                           stats.max_acceleration,
406                                           AltosConvert.meters_to_feet(stats.max_acceleration),
407                                           AltosConvert.meters_to_g(stats.max_acceleration));
408                 }
409                 if (stats.state_speed[Altos.ao_flight_drogue] != AltosLib.MISSING)
410                         System.out.printf("Drogue rate: %6.0f m/s  %6.0f ft/s\n",
411                                           stats.state_speed[Altos.ao_flight_drogue],
412                                           AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_drogue]));
413                 if (stats.state_speed[Altos.ao_flight_main] != AltosLib.MISSING)
414                         System.out.printf("Main rate:   %6.0f m/s  %6.0f ft/s\n",
415                                           stats.state_speed[Altos.ao_flight_main],
416                                           AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_main]));
417                 if (stats.landed_time != AltosLib.MISSING &&
418                     stats.boost_time != AltosLib.MISSING &&
419                     stats.landed_time > stats.boost_time)
420                         System.out.printf("Flight time: %6.0f s\n",
421                                           stats.landed_time -
422                                           stats.boost_time);
423                 System.out.printf("\n");
424                 return true;
425         }
426
427         static boolean process_oneline(File file) {
428                 AltosRecordSet set = record_set(file);
429                 if (set == null)
430                         return false;
431                 System.out.printf("%s", file.toString());
432                 AltosFlightSeries series = make_series(set);
433                 TestStats stats = new TestStats(series);
434                 if (stats.max_height != AltosLib.MISSING)
435                         System.out.printf(" height  %6.0f m", stats.max_height);
436                 if (stats.max_speed != AltosLib.MISSING)
437                         System.out.printf(" speed   %6.0f m/s", stats.max_speed);
438                 if (stats.state_enter_speed[AltosLib.ao_flight_drogue] != AltosLib.MISSING)
439                         System.out.printf(" drogue-deploy   %6.0f m/s", stats.state_enter_speed[AltosLib.ao_flight_drogue]);
440                 if (stats.max_acceleration != AltosLib.MISSING)
441                         System.out.printf(" accel   %6.0f m/s²", stats.max_acceleration);
442                 System.out.printf("\n");
443                 return true;
444         }
445
446         public static void help(int code) {
447                 System.out.printf("Usage: teststand [OPTION]... [FILE]...\n");
448                 System.out.printf("  Options:\n");
449                 System.out.printf("    --graph <filename>\t\tgraph a flight\n");
450                 System.out.printf("    --summary <filename>\t\tText summary of a flight\n");
451                 System.out.printf("    --oneline <filename>\t\tOne line summary of a flight\n");
452                 System.out.printf("    --csv\tgenerate comma separated output for spreadsheets, etc\n");
453                 System.exit(code);
454         }
455
456         public static void main(final String[] args) {
457                 int     errors = 0;
458                 load_library(null);
459                 try {
460                         UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
461                 } catch (Exception e) {
462                 }
463                 TestStand teststand = null;
464
465                 /* Handle batch-mode */
466                 if (args.length == 0) {
467                         teststand = new TestStand();
468                         java.util.List<AltosDevice> devices = AltosUSBDevice.list(Altos.product_basestation);
469                         if (devices != null)
470                                 for (AltosDevice device : devices)
471                                         teststand.telemetry_window(device);
472                 } else {
473                         int process = process_none;
474                         for (int i = 0; i < args.length; i++) {
475                                 if (args[i].equals("--help"))
476                                         help(0);
477                                 else if (args[i].equals("--csv"))
478                                         process = process_csv;
479                                 else if (args[i].equals("--graph"))
480                                         process = process_graph;
481                                 else if (args[i].equals("--summary"))
482                                         process = process_summary;
483                                 else if (args[i].equals("--oneline"))
484                                         process = process_oneline;
485                                 else if (args[i].startsWith("--"))
486                                         help(1);
487                                 else {
488                                         File file = new File(args[i]);
489                                         switch (process) {
490                                         case process_none:
491                                                 if (teststand == null)
492                                                         teststand = new TestStand();
493                                         case process_graph:
494                                                 if (!process_graph(file))
495                                                         ++errors;
496                                                 break;
497                                         case process_csv:
498                                                 if (!process_csv(file))
499                                                         ++errors;
500                                                 break;
501                                         case process_summary:
502                                                 if (!process_summary(file))
503                                                         ++errors;
504                                                 break;
505                                         case process_oneline:
506                                                 if (!process_oneline(file))
507                                                         ++errors;
508                                                 break;
509                                         }
510                                 }
511                         }
512                 }
513                 if (errors != 0)
514                         System.exit(errors);
515         }
516 }