2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
22 import java.awt.event.*;
25 import java.util.concurrent.*;
26 import org.altusmetrum.altoslib_12.*;
27 import org.altusmetrum.altosuilib_12.*;
29 public class TestStand extends AltosUIFrame implements AltosEepromGrapher {
30 public AltosVoice voice = new AltosVoice();
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);
44 void telemetry_window(AltosDevice device) {
46 AltosFlightReader reader = new AltosTelemetryReader(new AltosSerial(device));
48 new AltosFlightUI(voice, reader, device.getSerial());
49 } catch (FileNotFoundException ee) {
50 JOptionPane.showMessageDialog(TestStand.this,
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()),
59 JOptionPane.ERROR_MESSAGE);
60 } catch (IOException ee) {
61 JOptionPane.showMessageDialog(TestStand.this,
62 String.format ("Unknown I/O error on %s", device.toShortString()),
64 JOptionPane.ERROR_MESSAGE);
65 } catch (TimeoutException te) {
66 JOptionPane.showMessageDialog(this,
67 String.format ("Timeout on %s", device.toShortString()),
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);
78 public void scan_device_selected(AltosDevice device) {
79 telemetry_window(device);
83 GridBagLayout gridbag;
85 JButton addButton(int x, int y, String label) {
89 c = new GridBagConstraints();
90 c.gridx = x; c.gridy = y;
91 c.fill = GridBagConstraints.BOTH;
94 b = new JButton(label);
96 //Dimension ps = b.getPreferredSize();
98 gridbag.setConstraints(b, c);
103 /* OSXAdapter interfaces */
104 public void macosx_file_handler(String path) {
105 process_graph(new File(path));
108 public void macosx_quit_handler() {
112 public void macosx_preferences_handler() {
113 ConfigureTestStand();
120 register_for_macosx_events();
122 AltosUIPreferences.set_component(this);
124 pane = getContentPane();
125 gridbag = new GridBagLayout();
126 pane.setLayout(gridbag);
130 b = addButton(0, 0, "Monitor Flight");
131 b.addActionListener(new ActionListener() {
132 public void actionPerformed(ActionEvent e) {
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) {
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) {
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) {
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();
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();
171 b.setToolTipText("Global TestStand settings");
173 b = addButton(2, 1, "Configure Ground Station");
174 b.addActionListener(new ActionListener() {
175 public void actionPerformed(ActionEvent e) {
176 ConfigureTeleDongle();
180 b = addButton(3, 1, "Flash Image");
181 b.addActionListener(new ActionListener() {
182 public void actionPerformed(ActionEvent e) {
186 b.setToolTipText("Replace the firmware in any AltusMetrum product");
188 b = addButton(0, 2, "Scan Channels");
189 b.addActionListener(new ActionListener() {
190 public void actionPerformed(ActionEvent e) {
194 b.setToolTipText("Find what channel an altimeter is sending telemetry on");
196 b = addButton(4, 2, "Quit");
197 b.addActionListener(new ActionListener() {
198 public void actionPerformed(ActionEvent e) {
202 b.setToolTipText("Close all active windows and terminate TestStand");
212 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
213 addWindowListener(new WindowAdapter() {
215 public void windowClosing(WindowEvent e) {
220 setLocationByPlatform(false);
222 /* Insets aren't set before the window is visible */
226 private void ConnectToDevice() {
227 AltosDevice device = AltosDeviceUIDialog.show(TestStand.this,
228 Altos.product_basestation);
231 telemetry_window(device);
234 void ConfigureCallsign() {
236 result = JOptionPane.showInputDialog(TestStand.this,
237 "Configure Callsign",
238 AltosUIPreferences.callsign());
240 AltosUIPreferences.set_callsign(result);
243 void ConfigureTeleMetrum() {
244 new AltosConfigFC(TestStand.this);
247 void ConfigureTeleDongle() {
248 new AltosConfigTD(TestStand.this);
252 AltosFlashUI.show(TestStand.this);
255 void ScanChannels() {
256 new AltosScanUI(TestStand.this, true);
259 void LaunchController() {
260 new AltosLaunchUI(TestStand.this);
263 /* Connect to TeleMetrum, either directly or through
264 * a TeleDongle over the packet link
267 public void graph_flights(AltosEepromList flights) {
268 for (AltosEepromLog flight : flights) {
269 if (flight.graph_selected && flight.file != null) {
270 process_graph(flight.file);
275 private void SaveFlightData() {
276 new AltosEepromManage(this, this, AltosLib.product_any);
279 private static AltosFlightSeries make_series(AltosRecordSet set) {
280 AltosFlightSeries series = new AltosFlightSeries(set.cal_data());
281 set.capture_series(series);
286 /* Load a flight log file and write out a CSV file containing
287 * all of the data in standard units
290 private void ExportData() {
291 AltosDataChooser chooser;
292 chooser = new AltosDataChooser(this);
293 AltosRecordSet set = chooser.runDialog();
296 AltosFlightSeries series = make_series(set);
297 new AltosCSVUI(TestStand.this, series, chooser.file());
300 /* Load a flight log CSV file and display a pretty graph.
303 private void GraphData() {
304 AltosDataChooser chooser;
305 chooser = new AltosDataChooser(this);
306 AltosRecordSet set = chooser.runDialog();
310 new AltosGraphUI(set, chooser.file());
311 } catch (InterruptedException ie) {
312 } catch (IOException ie) {
316 private void ConfigureTestStand() {
317 new AltosConfigureUI(TestStand.this, voice);
320 static AltosWriter open_csv(File file) {
322 return new AltosCSV(file);
323 } catch (FileNotFoundException fe) {
324 System.out.printf("%s\n", fe.getMessage());
329 static AltosWriter open_kml(File file) {
331 return new AltosKML(file);
332 } catch (FileNotFoundException fe) {
333 System.out.printf("%s\n", fe.getMessage());
338 static AltosRecordSet record_set(File input) {
340 return AltosLib.record_set(input);
341 } catch (IOException ie) {
342 String message = ie.getMessage();
344 message = String.format("%s (I/O error)", input.toString());
345 System.err.printf("%s: %s\n", input.toString(), message);
350 static final int process_none = 0;
351 static final int process_csv = 1;
352 static final int process_kml = 2;
353 static final int process_graph = 3;
354 static final int process_summary = 4;
355 static final int process_oneline = 5;
357 static boolean process_csv(File input) {
358 AltosRecordSet set = record_set(input);
362 File output = Altos.replace_extension(input,".csv");
363 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
364 if (input.equals(output)) {
365 System.out.printf("Not processing '%s'\n", input);
368 AltosWriter writer = open_csv(output);
371 AltosFlightSeries series = make_series(set);
372 writer.write(series);
378 static boolean process_kml(File input) {
379 AltosRecordSet set = record_set(input);
383 File output = Altos.replace_extension(input,".kml");
384 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
385 if (input.equals(output)) {
386 System.out.printf("Not processing '%s'\n", input);
389 AltosWriter writer = open_kml(output);
392 AltosFlightSeries series = make_series(set);
394 writer.write(series);
400 static boolean process_graph(File file) {
401 AltosRecordSet set = record_set(file);
405 new AltosGraphUI(set, file);
407 } catch (InterruptedException ie) {
408 } catch (IOException ie) {
413 static boolean process_summary(File file) {
414 AltosRecordSet set = record_set(file);
417 System.out.printf("%s:\n", file.toString());
418 AltosFlightSeries series = make_series(set);
419 AltosFlightStats stats = new AltosFlightStats(series);
420 if (stats.serial != AltosLib.MISSING)
421 System.out.printf("Serial: %5d\n", stats.serial);
422 if (stats.flight != AltosLib.MISSING)
423 System.out.printf("Flight: %5d\n", stats.flight);
424 if (stats.year != AltosLib.MISSING)
425 System.out.printf("Date: %04d-%02d-%02d\n",
426 stats.year, stats.month, stats.day);
427 if (stats.hour != AltosLib.MISSING)
428 System.out.printf("Time: %02d:%02d:%02d UTC\n",
429 stats.hour, stats.minute, stats.second);
430 if (stats.max_height != AltosLib.MISSING)
431 System.out.printf("Max height: %6.0f m %6.0f ft\n",
433 AltosConvert.meters_to_feet(stats.max_height));
434 if (stats.max_speed != AltosLib.MISSING)
435 System.out.printf("Max speed: %6.0f m/s %6.0f ft/s %6.4f Mach\n",
437 AltosConvert.meters_to_feet(stats.max_speed),
438 AltosConvert.meters_to_mach(stats.max_speed));
439 if (stats.max_acceleration != AltosLib.MISSING) {
440 System.out.printf("Max accel: %6.0f m/s² %6.0f ft/s² %6.2f g\n",
441 stats.max_acceleration,
442 AltosConvert.meters_to_feet(stats.max_acceleration),
443 AltosConvert.meters_to_g(stats.max_acceleration));
445 if (stats.state_speed[Altos.ao_flight_drogue] != AltosLib.MISSING)
446 System.out.printf("Drogue rate: %6.0f m/s %6.0f ft/s\n",
447 stats.state_speed[Altos.ao_flight_drogue],
448 AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_drogue]));
449 if (stats.state_speed[Altos.ao_flight_main] != AltosLib.MISSING)
450 System.out.printf("Main rate: %6.0f m/s %6.0f ft/s\n",
451 stats.state_speed[Altos.ao_flight_main],
452 AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_main]));
453 if (stats.landed_time != AltosLib.MISSING &&
454 stats.boost_time != AltosLib.MISSING &&
455 stats.landed_time > stats.boost_time)
456 System.out.printf("Flight time: %6.0f s\n",
459 System.out.printf("\n");
463 static boolean process_oneline(File file) {
464 AltosRecordSet set = record_set(file);
467 System.out.printf("%s", file.toString());
468 AltosFlightSeries series = make_series(set);
469 AltosFlightStats stats = new AltosFlightStats(series);
470 if (stats.max_height != AltosLib.MISSING)
471 System.out.printf(" height %6.0f m", stats.max_height);
472 if (stats.max_speed != AltosLib.MISSING)
473 System.out.printf(" speed %6.0f m/s", stats.max_speed);
474 if (stats.state_enter_speed[AltosLib.ao_flight_drogue] != AltosLib.MISSING)
475 System.out.printf(" drogue-deploy %6.0f m/s", stats.state_enter_speed[AltosLib.ao_flight_drogue]);
476 if (stats.max_acceleration != AltosLib.MISSING)
477 System.out.printf(" accel %6.0f m/s²", stats.max_acceleration);
478 System.out.printf("\n");
482 public static void help(int code) {
483 System.out.printf("Usage: teststand [OPTION]... [FILE]...\n");
484 System.out.printf(" Options:\n");
485 System.out.printf(" --graph <filename>\t\tgraph a flight\n");
486 System.out.printf(" --summary <filename>\t\tText summary of a flight\n");
487 System.out.printf(" --oneline <filename>\t\tOne line summary of a flight\n");
488 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
489 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
493 public static void main(final String[] args) {
497 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
498 } catch (Exception e) {
500 TestStand teststand = null;
502 /* Handle batch-mode */
503 if (args.length == 0) {
504 teststand = new TestStand();
505 java.util.List<AltosDevice> devices = AltosUSBDevice.list(Altos.product_basestation);
507 for (AltosDevice device : devices)
508 teststand.telemetry_window(device);
510 int process = process_none;
511 for (int i = 0; i < args.length; i++) {
512 if (args[i].equals("--help"))
514 else if (args[i].equals("--kml"))
515 process = process_kml;
516 else if (args[i].equals("--csv"))
517 process = process_csv;
518 else if (args[i].equals("--graph"))
519 process = process_graph;
520 else if (args[i].equals("--summary"))
521 process = process_summary;
522 else if (args[i].equals("--oneline"))
523 process = process_oneline;
524 else if (args[i].startsWith("--"))
527 File file = new File(args[i]);
530 if (teststand == null)
531 teststand = new TestStand();
533 if (!process_graph(file))
537 if (!process_kml(file))
541 if (!process_csv(file))
544 case process_summary:
545 if (!process_summary(file))
548 case process_oneline:
549 if (!process_oneline(file))