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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
31 import altosui.AltosSerial;
32 import altosui.AltosSerialMonitor;
33 import altosui.AltosTelemetry;
34 import altosui.AltosState;
35 import altosui.AltosDeviceDialog;
37 class AltosFlightStatusTableModel extends AbstractTableModel {
38 private String[] columnNames = {"Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
39 private Object[] data = { 0, "idle", 0, 0 };
41 public int getColumnCount() { return columnNames.length; }
42 public int getRowCount() { return 2; }
43 public Object getValueAt(int row, int col) {
45 return columnNames[col];
49 public void setValueAt(Object value, int col) {
51 fireTableCellUpdated(1, col);
54 public void setValueAt(Object value, int row, int col) {
55 setValueAt(value, col);
58 public void set(AltosState state) {
59 setValueAt(String.format("%1.0f", state.height), 0);
60 setValueAt(state.data.state, 1);
61 setValueAt(state.data.rssi, 2);
62 double speed = state.baro_speed;
65 setValueAt(String.format("%1.0f", speed), 3);
69 class AltosFlightInfoTableModel extends AbstractTableModel {
70 private String[] columnNames = {"Field", "Value"};
76 public InfoLine(String n, String v) {
82 private ArrayList<InfoLine> rows = new ArrayList<InfoLine>();
84 public int getColumnCount() { return columnNames.length; }
85 public String getColumnName(int col) { return columnNames[col]; }
87 public int getRowCount() { return 20; }
89 public Object getValueAt(int row, int col) {
90 if (row >= rows.size())
93 return rows.get(row).name;
95 return rows.get(row).value;
99 int prev_num_rows = 0;
101 public void resetRow() {
104 public void addRow(String name, String value) {
105 if (current_row >= rows.size())
106 rows.add(current_row, new InfoLine(name, value));
108 rows.set(current_row, new InfoLine(name, value));
111 public void finish() {
112 if (current_row > prev_num_rows) {
113 fireTableRowsInserted(prev_num_rows, current_row - 1);
114 prev_num_rows = current_row;
116 fireTableDataChanged();
120 public class AltosUI extends JFrame {
121 private int channel = -1;
123 private AltosFlightStatusTableModel flightStatusModel;
124 private JTable flightStatus;
126 static final int info_columns = 3;
128 private AltosFlightInfoTableModel[] flightInfoModel;
129 private JTable[] flightInfo;
130 private AltosSerial serialLine;
135 private Font statusFont = new Font("SansSerif", Font.BOLD, 24);
136 private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
137 private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
141 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
142 Object[][] statusData = { { "0", "pad", "-50", "0" } };
144 vbox = Box.createVerticalBox();
147 flightStatusModel = new AltosFlightStatusTableModel();
148 flightStatus = new JTable(flightStatusModel);
149 flightStatus.setFont(statusFont);
150 TableColumnModel tcm = flightStatus.getColumnModel();
151 for (int i = 0; i < flightStatusModel.getColumnCount(); i++) {
152 DefaultTableCellRenderer r = new DefaultTableCellRenderer();
153 r.setFont(statusFont);
154 r.setHorizontalAlignment(SwingConstants.CENTER);
155 tcm.getColumn(i).setCellRenderer(r);
158 FontMetrics statusMetrics = flightStatus.getFontMetrics(statusFont);
159 int statusHeight = (statusMetrics.getHeight() + statusMetrics.getLeading()) * 15 / 10;
160 flightStatus.setRowHeight(statusHeight);
161 flightStatus.setShowGrid(false);
163 vbox.add(flightStatus);
165 hbox = Box.createHorizontalBox();
168 flightInfo = new JTable[3];
169 flightInfoModel = new AltosFlightInfoTableModel[3];
171 FontMetrics infoValueMetrics = flightStatus.getFontMetrics(infoValueFont);
172 int infoHeight = (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 20 / 10;
174 for (int i = 0; i < info_columns; i++) {
175 ibox[i] = Box.createVerticalBox();
176 flightInfoModel[i] = new AltosFlightInfoTableModel();
177 flightInfo[i] = new JTable(flightInfoModel[i]);
178 flightInfo[i].setFont(infoValueFont);
179 flightInfo[i].setRowHeight(infoHeight);
180 flightInfo[i].setShowGrid(true);
181 ibox[i].add(flightInfo[i].getTableHeader());
182 ibox[i].add(flightInfo[i]);
190 serialLine = new AltosSerial();
191 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
192 this.setSize(new Dimension (infoValueMetrics.charWidth('0') * 6 * 20,
193 statusHeight * 4 + infoHeight * 17));
195 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
196 addWindowListener(new WindowAdapter() {
198 public void windowClosing(WindowEvent e) {
204 public void info_reset() {
205 for (int i = 0; i < info_columns; i++)
206 flightInfoModel[i].resetRow();
209 public void info_add_row(int col, String name, String value) {
210 flightInfoModel[col].addRow(name, value);
213 public void info_add_row(int col, String name, String format, Object value) {
214 flightInfoModel[col].addRow(name, String.format(format, value));
217 public void info_add_row(int col, String name, String format, Object v1, Object v2) {
218 flightInfoModel[col].addRow(name, String.format(format, v1, v2));
221 public void info_add_row(int col, String name, String format, Object v1, Object v2, Object v3) {
222 flightInfoModel[col].addRow(name, String.format(format, v1, v2, v3));
225 public void info_add_deg(int col, String name, double v, int pos, int neg) {
231 double deg = Math.floor(v);
232 double min = (v - deg) * 60;
234 flightInfoModel[col].addRow(name, String.format("%3.0f°%08.5f'", deg, min));
237 public void info_finish() {
238 for (int i = 0; i < info_columns; i++)
239 flightInfoModel[i].finish();
242 static final int MIN_PAD_SAMPLES = 10;
244 public void show(AltosState state) {
245 flightStatusModel.set(state);
248 if (state.npad >= MIN_PAD_SAMPLES)
249 info_add_row(0, "Ground state", "%s", "ready");
251 info_add_row(0, "Ground state", "wait (%d)",
252 MIN_PAD_SAMPLES - state.npad);
253 info_add_row(0, "Rocket state", "%s", state.data.state);
254 info_add_row(0, "Callsign", "%s", state.data.callsign);
255 info_add_row(0, "Rocket serial", "%6d", state.data.serial);
256 info_add_row(0, "Rocket flight", "%6d", state.data.flight);
258 info_add_row(0, "RSSI", "%6d dBm", state.data.rssi);
259 info_add_row(0, "Height", "%6.0f m", state.height);
260 info_add_row(0, "Max height", "%6.0f m", state.max_height);
261 info_add_row(0, "Acceleration", "%8.1f m/s²", state.acceleration);
262 info_add_row(0, "Max acceleration", "%8.1f m/s²", state.max_acceleration);
263 info_add_row(0, "Speed", "%8.1f m/s", state.ascent ? state.speed : state.baro_speed);
264 info_add_row(0, "Max Speed", "%8.1f m/s", state.max_speed);
265 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
266 info_add_row(0, "Battery", "%9.2f V", state.battery);
267 info_add_row(0, "Drogue", "%9.2f V", state.drogue_sense);
268 info_add_row(0, "Main", "%9.2f V", state.main_sense);
269 info_add_row(0, "Pad altitude", "%6.0f m", state.ground_altitude);
270 if (state.gps == null) {
271 info_add_row(1, "GPS", "not available");
273 if (state.data.gps.gps_locked)
274 info_add_row(1, "GPS", " locked");
275 else if (state.data.gps.gps_connected)
276 info_add_row(1, "GPS", " unlocked");
278 info_add_row(1, "GPS", " missing");
279 info_add_row(1, "Satellites", "%6d", state.data.gps.nsat);
280 info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
281 info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
282 info_add_row(1, "GPS altitude", "%6d", state.gps.alt);
283 info_add_row(1, "GPS height", "%6.0f", state.gps_height);
285 /* The SkyTraq GPS doesn't report these values */
287 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
288 state.gps.ground_speed,
290 info_add_row(1, "GPS climb rate", "%8.1f m/s",
291 state.gps.climb_rate);
292 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
293 state.gps.h_error, state.gps.v_error);
295 info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
297 if (state.npad > 0) {
298 if (state.from_pad != null) {
299 info_add_row(1, "Distance from pad", "%6.0f m", state.from_pad.distance);
300 info_add_row(1, "Direction from pad", "%6.0f°", state.from_pad.bearing);
302 info_add_row(1, "Distance from pad", "unknown");
303 info_add_row(1, "Direction from pad", "unknown");
305 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
306 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
307 info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
309 info_add_row(1, "GPS date", "%04d-%02d-%02d",
310 state.gps.gps_time.year,
311 state.gps.gps_time.month,
312 state.gps.gps_time.day);
313 info_add_row(1, "GPS time", " %02d:%02d:%02d",
314 state.gps.gps_time.hour,
315 state.gps.gps_time.minute,
316 state.gps.gps_time.second);
320 if (state.gps.cc_gps_sat == null)
321 info_add_row(2, "Satellites Visible", "%4d", 0);
323 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
324 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
325 info_add_row(2, "Satellite id,C/N0",
327 state.gps.cc_gps_sat[c].svid,
328 state.gps.cc_gps_sat[c].c_n0);
335 /* User Preferences */
336 Preferences altosui_preferences = Preferences.userNodeForPackage(this.getClass());
339 private File logdir = null;
341 /* logdir preference name */
342 final static String logdirPreference = "LOGDIR";
344 /* Default logdir is ~/TeleMetrum */
345 final static String logdirName = "TeleMetrum";
347 /* Initialize logdir from preferences */
349 String logdir_string = altosui_preferences.get(logdirPreference, null);
350 if (logdir_string != null)
351 logdir = new File(logdir_string);
353 /* a hack -- make the file chooser tell us what the default directory
354 * would be and stick our logdir in a subdirectory of that.
356 logdir = new File(new JFileChooser().getCurrentDirectory(), logdirName);
359 private void set_logdir(File new_logdir) {
361 System.out.printf("Set logdir to %s\n", logdir.toString());
362 synchronized (altosui_preferences) {
363 altosui_preferences.put(logdirPreference, logdir.getPath());
365 altosui_preferences.flush();
366 } catch (BackingStoreException ee) {
367 JOptionPane.showMessageDialog(AltosUI.this,
368 altosui_preferences.absolutePath(),
369 "Cannot save prefernces",
370 JOptionPane.ERROR_MESSAGE);
375 private boolean check_dir(File dir) {
378 JOptionPane.showMessageDialog(AltosUI.this,
380 "Cannot create directory",
381 JOptionPane.ERROR_MESSAGE);
384 } else if (!dir.isDirectory()) {
385 JOptionPane.showMessageDialog(AltosUI.this,
387 "Is not a directory",
388 JOptionPane.ERROR_MESSAGE);
394 private void PickSerialDevice() {
395 java.util.Enumeration<CommPortIdentifier> port_list = CommPortIdentifier.getPortIdentifiers();
396 while (port_list.hasMoreElements()) {
397 CommPortIdentifier identifier = port_list.nextElement();
398 System.out.println("Serial port " + identifier.getName());
402 class DisplayThread extends Thread {
403 String read() throws InterruptedException { return null; }
407 void update(AltosState state) throws InterruptedException { }
411 AltosState state = null;
416 while ((line = read()) != null) {
418 AltosTelemetry t = new AltosTelemetry(line);
419 state = new AltosState(t, state);
422 } catch (ParseException pp) {
423 System.out.printf("Parse error on %s\n", line);
424 System.out.println("exception " + pp);
427 } catch (InterruptedException ee) {
434 class DeviceThread extends DisplayThread {
437 String read() throws InterruptedException {
438 System.out.println("Waiting for telemetry");
439 String s = serial.get_telem();
440 System.out.println("Got telemetry " + s);
446 System.out.println("DisplayThread done");
449 public DeviceThread(AltosSerial s) {
454 private void ConnectToDevice() {
455 AltosDevice device = AltosDeviceDialog.show(AltosUI.this, "TeleDongle");
457 if (device != null) {
459 serialLine.connect(device.tty);
460 DeviceThread thread = new DeviceThread(serialLine);
462 } catch (FileNotFoundException ee) {
463 JOptionPane.showMessageDialog(AltosUI.this,
465 "Cannot open serial port",
466 JOptionPane.ERROR_MESSAGE);
467 } catch (NoSuchPortException ee) {
468 JOptionPane.showMessageDialog(AltosUI.this,
470 "No such serial port",
471 JOptionPane.ERROR_MESSAGE);
472 } catch (PortInUseException ee) {
473 JOptionPane.showMessageDialog(AltosUI.this,
476 JOptionPane.ERROR_MESSAGE);
477 } catch (IOException ee) {
478 JOptionPane.showMessageDialog(AltosUI.this,
481 JOptionPane.ERROR_MESSAGE);
486 void DisconnectFromDevice () {
490 String readline(FileInputStream s) throws IOException {
494 while ((c = s.read()) != -1) {
499 line = line + (char) c;
505 * Open an existing telemetry file and replay it in realtime
508 class ReplayThread extends DisplayThread {
509 FileInputStream replay;
512 ReplayThread(FileInputStream in, String name) {
519 return readline(replay);
520 } catch (IOException ee) {
521 JOptionPane.showMessageDialog(AltosUI.this,
524 JOptionPane.ERROR_MESSAGE);
532 } catch (IOException ee) {
536 void update(AltosState state) throws InterruptedException {
537 /* Make it run in realtime after the rocket leaves the pad */
538 if (state.state > AltosTelemetry.ao_flight_pad)
539 Thread.sleep((int) (state.time_change * 1000));
543 Thread display_thread;
545 private void stop_display() {
546 if (display_thread != null && display_thread.isAlive())
547 display_thread.interrupt();
548 display_thread = null;
551 private void run_display(Thread thread) {
553 display_thread = thread;
554 display_thread.start();
558 * Replay a flight from telemetry data
560 private void Replay() {
561 JFileChooser logfile_chooser = new JFileChooser();
563 logfile_chooser.setDialogTitle("Select Telemetry File");
564 logfile_chooser.setFileFilter(new FileNameExtensionFilter("Telemetry file", "telem"));
565 logfile_chooser.setCurrentDirectory(logdir);
566 int returnVal = logfile_chooser.showOpenDialog(AltosUI.this);
568 if (returnVal == JFileChooser.APPROVE_OPTION) {
569 File file = logfile_chooser.getSelectedFile();
571 System.out.println("No file selected?");
572 String filename = file.getName();
574 FileInputStream replay = new FileInputStream(file);
575 ReplayThread thread = new ReplayThread(replay, filename);
577 } catch (FileNotFoundException ee) {
578 JOptionPane.showMessageDialog(AltosUI.this,
580 "Cannot open serial port",
581 JOptionPane.ERROR_MESSAGE);
586 /* Connect to TeleMetrum, either directly or through
587 * a TeleDongle over the packet link
589 private void SaveFlightData() {
592 /* Configure the log directory. This is where all telemetry and eeprom files
593 * will be written to, and where replay will look for telemetry files
595 private void ConfigureLog() {
596 JFileChooser logdir_chooser = new JFileChooser();
598 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
599 logdir_chooser.setCurrentDirectory(logdir.getParentFile());
600 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
602 if (logdir_chooser.showDialog(AltosUI.this, "Select Directory") == JFileChooser.APPROVE_OPTION) {
603 File dir = logdir_chooser.getSelectedFile();
609 /* Create the AltosUI menus
611 private void createMenu() {
612 JMenuBar menubar = new JMenuBar();
615 JRadioButtonMenuItem radioitem;
619 menu = new JMenu("File");
620 menu.setMnemonic(KeyEvent.VK_F);
623 item = new JMenuItem("Quit",KeyEvent.VK_Q);
624 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
625 ActionEvent.CTRL_MASK));
626 item.addActionListener(new ActionListener() {
627 public void actionPerformed(ActionEvent e) {
636 menu = new JMenu("Device");
637 menu.setMnemonic(KeyEvent.VK_D);
640 item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
641 item.addActionListener(new ActionListener() {
642 public void actionPerformed(ActionEvent e) {
648 item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
649 item.addActionListener(new ActionListener() {
650 public void actionPerformed(ActionEvent e) {
651 DisconnectFromDevice();
658 item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
659 item.addActionListener(new ActionListener() {
660 public void actionPerformed(ActionEvent e) {
666 item = new JMenuItem("Replay",KeyEvent.VK_R);
667 item.addActionListener(new ActionListener() {
668 public void actionPerformed(ActionEvent e) {
676 menu = new JMenu("Log");
677 menu.setMnemonic(KeyEvent.VK_L);
680 item = new JMenuItem("New Log",KeyEvent.VK_N);
681 item.addActionListener(new ActionListener() {
682 public void actionPerformed(ActionEvent e) {
687 item = new JMenuItem("Configure Log",KeyEvent.VK_C);
688 item.addActionListener(new ActionListener() {
689 public void actionPerformed(ActionEvent e) {
697 menu = new JMenu("Voice", true);
698 menu.setMnemonic(KeyEvent.VK_V);
701 radioitem = new JRadioButtonMenuItem("Enable Voice");
702 radioitem.addActionListener(new ActionListener() {
703 public void actionPerformed(ActionEvent e) {
711 menu = new JMenu("Channel", true);
712 menu.setMnemonic(KeyEvent.VK_C);
714 ButtonGroup group = new ButtonGroup();
716 for (int c = 0; c <= 9; c++) {
717 radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
720 radioitem.setActionCommand(String.format("%d", c));
721 radioitem.addActionListener(new ActionListener() {
722 public void actionPerformed(ActionEvent e) {
723 System.out.println("Command: " + e.getActionCommand() + " param: " +
728 group.add(radioitem);
732 this.setJMenuBar(menubar);
735 public static void main(final String[] args) {
736 AltosUI altosui = new AltosUI();
737 altosui.setVisible(true);