29001d9911beaef886bdca594e225aabc9c6dddc
[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 Flight 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(4, 1, "Fire Igniter");
189                 b.addActionListener(new ActionListener() {
190                                 public void actionPerformed(ActionEvent e) {
191                                         FireIgniter();
192                                 }
193                         });
194                 b.setToolTipText("Remote control of igniters for deployment testing");
195                 b = addButton(0, 2, "Scan Channels");
196                 b.addActionListener(new ActionListener() {
197                                 public void actionPerformed(ActionEvent e) {
198                                         ScanChannels();
199                                 }
200                         });
201                 b.setToolTipText("Find what channel an altimeter is sending telemetry on");
202                 b = addButton(1, 2, "Load Maps");
203                 b.addActionListener(new ActionListener() {
204                                 public void actionPerformed(ActionEvent e) {
205                                         LoadMaps();
206                                 }
207                         });
208                 b.setToolTipText("Download satellite images for off-line flight monitoring");
209
210 //              b = addButton(3, 2, "Launch Controller");
211 //              b.addActionListener(new ActionListener() {
212 //                              public void actionPerformed(ActionEvent e) {
213 //                                      LaunchController();
214 //                              }
215 //                      });
216
217                 b = addButton(4, 2, "Quit");
218                 b.addActionListener(new ActionListener() {
219                                 public void actionPerformed(ActionEvent e) {
220                                         System.exit(0);
221                                 }
222                         });
223                 b.setToolTipText("Close all active windows and terminate TestStand");
224
225                 setTitle("AltOS");
226
227                 pane.doLayout();
228                 pane.validate();
229
230                 doLayout();
231                 validate();
232
233                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
234                 addWindowListener(new WindowAdapter() {
235                         @Override
236                         public void windowClosing(WindowEvent e) {
237                                 System.exit(0);
238                         }
239                 });
240
241                 setLocationByPlatform(false);
242
243                 /* Insets aren't set before the window is visible */
244                 setVisible(true);
245         }
246
247         private void ConnectToDevice() {
248                 AltosDevice     device = AltosDeviceUIDialog.show(TestStand.this,
249                                                                 Altos.product_basestation);
250
251                 if (device != null)
252                         telemetry_window(device);
253         }
254
255         void ConfigureCallsign() {
256                 String  result;
257                 result = JOptionPane.showInputDialog(TestStand.this,
258                                                      "Configure Callsign",
259                                                      AltosUIPreferences.callsign());
260                 if (result != null)
261                         AltosUIPreferences.set_callsign(result);
262         }
263
264         void ConfigureTeleMetrum() {
265                 new AltosConfigFC(TestStand.this);
266         }
267
268         void ConfigureTeleDongle() {
269                 new AltosConfigTD(TestStand.this);
270         }
271
272         void FlashImage() {
273                 AltosFlashUI.show(TestStand.this);
274         }
275
276         void FireIgniter() {
277                 new AltosIgniteUI(TestStand.this);
278         }
279
280         void ScanChannels() {
281                 new AltosScanUI(TestStand.this, true);
282         }
283
284         void LoadMaps() {
285                 new AltosUIMapPreload(TestStand.this);
286         }
287
288         void LaunchController() {
289                 new AltosLaunchUI(TestStand.this);
290         }
291
292         /* Connect to TeleMetrum, either directly or through
293          * a TeleDongle over the packet link
294          */
295
296         public void graph_flights(AltosEepromList flights) {
297                 for (AltosEepromLog flight : flights) {
298                         if (flight.graph_selected && flight.file != null) {
299                                 process_graph(flight.file);
300                         }
301                 }
302         }
303
304         private void SaveFlightData() {
305                 new AltosEepromManage(this, this, AltosLib.product_any);
306         }
307
308         private static AltosFlightSeries make_series(AltosRecordSet set) {
309                 AltosFlightSeries series = new AltosFlightSeries(set.cal_data());
310                 set.capture_series(series);
311                 series.finish();
312                 return series;
313         }
314
315         /* Load a flight log file and write out a CSV file containing
316          * all of the data in standard units
317          */
318
319         private void ExportData() {
320                 AltosDataChooser chooser;
321                 chooser = new AltosDataChooser(this);
322                 AltosRecordSet set = chooser.runDialog();
323                 if (set == null)
324                         return;
325                 AltosFlightSeries series = make_series(set);
326                 new AltosCSVUI(TestStand.this, series, chooser.file());
327         }
328
329         /* Load a flight log CSV file and display a pretty graph.
330          */
331
332         private void GraphData() {
333                 AltosDataChooser chooser;
334                 chooser = new AltosDataChooser(this);
335                 AltosRecordSet set = chooser.runDialog();
336                 if (set == null)
337                         return;
338                 try {
339                         new AltosGraphUI(set, chooser.file());
340                 } catch (InterruptedException ie) {
341                 } catch (IOException ie) {
342                 }
343         }
344
345         private void ConfigureTestStand() {
346                 new AltosConfigureUI(TestStand.this, voice);
347         }
348
349         static AltosWriter open_csv(File file) {
350                 try {
351                         return new AltosCSV(file);
352                 } catch (FileNotFoundException fe) {
353                         System.out.printf("%s\n", fe.getMessage());
354                         return null;
355                 }
356         }
357
358         static AltosWriter open_kml(File file) {
359                 try {
360                         return new AltosKML(file);
361                 } catch (FileNotFoundException fe) {
362                         System.out.printf("%s\n", fe.getMessage());
363                         return null;
364                 }
365         }
366
367         static AltosRecordSet record_set(File input) {
368                 try {
369                         return AltosLib.record_set(input);
370                 } catch (IOException ie) {
371                         String message = ie.getMessage();
372                         if (message == null)
373                                 message = String.format("%s (I/O error)", input.toString());
374                         System.err.printf("%s: %s\n", input.toString(), message);
375                 }
376                 return null;
377         }
378
379         static final int process_none = 0;
380         static final int process_csv = 1;
381         static final int process_kml = 2;
382         static final int process_graph = 3;
383         static final int process_summary = 4;
384         static final int process_oneline = 5;
385
386         static boolean process_csv(File input) {
387                 AltosRecordSet set = record_set(input);
388                 if (set == null)
389                         return false;
390
391                 File output = Altos.replace_extension(input,".csv");
392                 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
393                 if (input.equals(output)) {
394                         System.out.printf("Not processing '%s'\n", input);
395                         return false;
396                 } else {
397                         AltosWriter writer = open_csv(output);
398                         if (writer == null)
399                                 return false;
400                         AltosFlightSeries series = make_series(set);
401                         writer.write(series);
402                         writer.close();
403                 }
404                 return true;
405         }
406
407         static boolean process_kml(File input) {
408                 AltosRecordSet set = record_set(input);
409                 if (set == null)
410                         return false;
411
412                 File output = Altos.replace_extension(input,".kml");
413                 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
414                 if (input.equals(output)) {
415                         System.out.printf("Not processing '%s'\n", input);
416                         return false;
417                 } else {
418                         AltosWriter writer = open_kml(output);
419                         if (writer == null)
420                                 return false;
421                         AltosFlightSeries series = make_series(set);
422                         series.finish();
423                         writer.write(series);
424                         writer.close();
425                         return true;
426                 }
427         }
428
429         static boolean process_graph(File file) {
430                 AltosRecordSet set = record_set(file);
431                 if (set == null)
432                         return false;
433                 try {
434                         new AltosGraphUI(set, file);
435                         return true;
436                 } catch (InterruptedException ie) {
437                 } catch (IOException ie) {
438                 }
439                 return false;
440         }
441
442         static boolean process_summary(File file) {
443                 AltosRecordSet set = record_set(file);
444                 if (set == null)
445                         return false;
446                 System.out.printf("%s:\n", file.toString());
447                 AltosFlightSeries series = make_series(set);
448                 AltosFlightStats stats = new AltosFlightStats(series);
449                 if (stats.serial != AltosLib.MISSING)
450                         System.out.printf("Serial:       %5d\n", stats.serial);
451                 if (stats.flight != AltosLib.MISSING)
452                         System.out.printf("Flight:       %5d\n", stats.flight);
453                 if (stats.year != AltosLib.MISSING)
454                         System.out.printf("Date:    %04d-%02d-%02d\n",
455                                           stats.year, stats.month, stats.day);
456                 if (stats.hour != AltosLib.MISSING)
457                         System.out.printf("Time:      %02d:%02d:%02d UTC\n",
458                                           stats.hour, stats.minute, stats.second);
459                 if (stats.max_height != AltosLib.MISSING)
460                         System.out.printf("Max height:  %6.0f m    %6.0f ft\n",
461                                           stats.max_height,
462                                           AltosConvert.meters_to_feet(stats.max_height));
463                 if (stats.max_speed != AltosLib.MISSING)
464                         System.out.printf("Max speed:   %6.0f m/s  %6.0f ft/s  %6.4f Mach\n",
465                                           stats.max_speed,
466                                           AltosConvert.meters_to_feet(stats.max_speed),
467                                           AltosConvert.meters_to_mach(stats.max_speed));
468                 if (stats.max_acceleration != AltosLib.MISSING) {
469                         System.out.printf("Max accel:   %6.0f m/s² %6.0f ft/s² %6.2f g\n",
470                                           stats.max_acceleration,
471                                           AltosConvert.meters_to_feet(stats.max_acceleration),
472                                           AltosConvert.meters_to_g(stats.max_acceleration));
473                 }
474                 if (stats.state_speed[Altos.ao_flight_drogue] != AltosLib.MISSING)
475                         System.out.printf("Drogue rate: %6.0f m/s  %6.0f ft/s\n",
476                                           stats.state_speed[Altos.ao_flight_drogue],
477                                           AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_drogue]));
478                 if (stats.state_speed[Altos.ao_flight_main] != AltosLib.MISSING)
479                         System.out.printf("Main rate:   %6.0f m/s  %6.0f ft/s\n",
480                                           stats.state_speed[Altos.ao_flight_main],
481                                           AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_main]));
482                 if (stats.landed_time != AltosLib.MISSING &&
483                     stats.boost_time != AltosLib.MISSING &&
484                     stats.landed_time > stats.boost_time)
485                         System.out.printf("Flight time: %6.0f s\n",
486                                           stats.landed_time -
487                                           stats.boost_time);
488                 System.out.printf("\n");
489                 return true;
490         }
491
492         static boolean process_oneline(File file) {
493                 AltosRecordSet set = record_set(file);
494                 if (set == null)
495                         return false;
496                 System.out.printf("%s", file.toString());
497                 AltosFlightSeries series = make_series(set);
498                 AltosFlightStats stats = new AltosFlightStats(series);
499                 if (stats.max_height != AltosLib.MISSING)
500                         System.out.printf(" height  %6.0f m", stats.max_height);
501                 if (stats.max_speed != AltosLib.MISSING)
502                         System.out.printf(" speed   %6.0f m/s", stats.max_speed);
503                 if (stats.state_enter_speed[AltosLib.ao_flight_drogue] != AltosLib.MISSING)
504                         System.out.printf(" drogue-deploy   %6.0f m/s", stats.state_enter_speed[AltosLib.ao_flight_drogue]);
505                 if (stats.max_acceleration != AltosLib.MISSING)
506                         System.out.printf(" accel   %6.0f m/s²", stats.max_acceleration);
507                 System.out.printf("\n");
508                 return true;
509         }
510
511         public static void help(int code) {
512                 System.out.printf("Usage: teststand [OPTION]... [FILE]...\n");
513                 System.out.printf("  Options:\n");
514                 System.out.printf("    --graph <filename>\t\tgraph a flight\n");
515                 System.out.printf("    --summary <filename>\t\tText summary of a flight\n");
516                 System.out.printf("    --oneline <filename>\t\tOne line summary of a flight\n");
517                 System.out.printf("    --csv\tgenerate comma separated output for spreadsheets, etc\n");
518                 System.out.printf("    --kml\tgenerate KML output for use with Google Earth\n");
519                 System.exit(code);
520         }
521
522         public static void main(final String[] args) {
523                 int     errors = 0;
524                 load_library(null);
525                 try {
526                         UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
527                 } catch (Exception e) {
528                 }
529                 TestStand teststand = null;
530
531                 /* Handle batch-mode */
532                 if (args.length == 0) {
533                         teststand = new TestStand();
534                         java.util.List<AltosDevice> devices = AltosUSBDevice.list(Altos.product_basestation);
535                         if (devices != null)
536                                 for (AltosDevice device : devices)
537                                         teststand.telemetry_window(device);
538                 } else {
539                         int process = process_none;
540                         for (int i = 0; i < args.length; i++) {
541                                 if (args[i].equals("--help"))
542                                         help(0);
543                                 else if (args[i].equals("--kml"))
544                                         process = process_kml;
545                                 else if (args[i].equals("--csv"))
546                                         process = process_csv;
547                                 else if (args[i].equals("--graph"))
548                                         process = process_graph;
549                                 else if (args[i].equals("--summary"))
550                                         process = process_summary;
551                                 else if (args[i].equals("--oneline"))
552                                         process = process_oneline;
553                                 else if (args[i].startsWith("--"))
554                                         help(1);
555                                 else {
556                                         File file = new File(args[i]);
557                                         switch (process) {
558                                         case process_none:
559                                                 if (teststand == null)
560                                                         teststand = new TestStand();
561                                         case process_graph:
562                                                 if (!process_graph(file))
563                                                         ++errors;
564                                                 break;
565                                         case process_kml:
566                                                 if (!process_kml(file))
567                                                         ++errors;
568                                                 break;
569                                         case process_csv:
570                                                 if (!process_csv(file))
571                                                         ++errors;
572                                                 break;
573                                         case process_summary:
574                                                 if (!process_summary(file))
575                                                         ++errors;
576                                                 break;
577                                         case process_oneline:
578                                                 if (!process_oneline(file))
579                                                         ++errors;
580                                                 break;
581                                         }
582                                 }
583                         }
584                 }
585                 if (errors != 0)
586                         System.exit(errors);
587         }
588 }