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.*;
29 import java.util.concurrent.LinkedBlockingQueue;
32 import altosui.AltosSerial;
33 import altosui.AltosSerialMonitor;
34 import altosui.AltosTelemetry;
35 import altosui.AltosState;
36 import altosui.AltosDeviceDialog;
37 import altosui.AltosPreferences;
38 import altosui.AltosLog;
39 import altosui.AltosVoice;
41 class AltosFlightStatusTableModel extends AbstractTableModel {
42 private String[] columnNames = {"Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
43 private Object[] data = { 0, "idle", 0, 0 };
45 public int getColumnCount() { return columnNames.length; }
46 public int getRowCount() { return 2; }
47 public Object getValueAt(int row, int col) {
49 return columnNames[col];
53 public void setValueAt(Object value, int col) {
55 fireTableCellUpdated(1, col);
58 public void setValueAt(Object value, int row, int col) {
59 setValueAt(value, col);
62 public void set(AltosState state) {
63 setValueAt(String.format("%1.0f", state.height), 0);
64 setValueAt(state.data.state, 1);
65 setValueAt(state.data.rssi, 2);
66 double speed = state.baro_speed;
69 setValueAt(String.format("%1.0f", speed), 3);
73 class AltosFlightInfoTableModel extends AbstractTableModel {
74 private String[] columnNames = {"Field", "Value"};
80 public InfoLine(String n, String v) {
86 private ArrayList<InfoLine> rows = new ArrayList<InfoLine>();
88 public int getColumnCount() { return columnNames.length; }
89 public String getColumnName(int col) { return columnNames[col]; }
91 public int getRowCount() { return 20; }
93 public Object getValueAt(int row, int col) {
94 if (row >= rows.size())
97 return rows.get(row).name;
99 return rows.get(row).value;
103 int prev_num_rows = 0;
105 public void resetRow() {
108 public void addRow(String name, String value) {
109 if (current_row >= rows.size())
110 rows.add(current_row, new InfoLine(name, value));
112 rows.set(current_row, new InfoLine(name, value));
115 public void finish() {
116 if (current_row > prev_num_rows) {
117 fireTableRowsInserted(prev_num_rows, current_row - 1);
118 prev_num_rows = current_row;
120 fireTableDataChanged();
124 public class AltosUI extends JFrame {
125 private int channel = -1;
127 private AltosFlightStatusTableModel flightStatusModel;
128 private JTable flightStatus;
130 static final int info_columns = 3;
132 private AltosFlightInfoTableModel[] flightInfoModel;
133 private JTable[] flightInfo;
134 private AltosSerial serial_line;
135 private AltosLog altos_log;
140 private Font statusFont = new Font("SansSerif", Font.BOLD, 24);
141 private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
142 private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
144 public AltosVoice voice = new AltosVoice();
148 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
149 Object[][] statusData = { { "0", "pad", "-50", "0" } };
151 AltosPreferences.init(this);
153 vbox = Box.createVerticalBox();
156 flightStatusModel = new AltosFlightStatusTableModel();
157 flightStatus = new JTable(flightStatusModel);
158 flightStatus.setFont(statusFont);
159 TableColumnModel tcm = flightStatus.getColumnModel();
160 for (int i = 0; i < flightStatusModel.getColumnCount(); i++) {
161 DefaultTableCellRenderer r = new DefaultTableCellRenderer();
162 r.setFont(statusFont);
163 r.setHorizontalAlignment(SwingConstants.CENTER);
164 tcm.getColumn(i).setCellRenderer(r);
167 FontMetrics statusMetrics = flightStatus.getFontMetrics(statusFont);
168 int statusHeight = (statusMetrics.getHeight() + statusMetrics.getLeading()) * 15 / 10;
169 flightStatus.setRowHeight(statusHeight);
170 flightStatus.setShowGrid(false);
172 vbox.add(flightStatus);
174 hbox = Box.createHorizontalBox();
177 flightInfo = new JTable[3];
178 flightInfoModel = new AltosFlightInfoTableModel[3];
180 FontMetrics infoValueMetrics = flightStatus.getFontMetrics(infoValueFont);
181 int infoHeight = (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 20 / 10;
183 for (int i = 0; i < info_columns; i++) {
184 ibox[i] = Box.createVerticalBox();
185 flightInfoModel[i] = new AltosFlightInfoTableModel();
186 flightInfo[i] = new JTable(flightInfoModel[i]);
187 flightInfo[i].setFont(infoValueFont);
188 flightInfo[i].setRowHeight(infoHeight);
189 flightInfo[i].setShowGrid(true);
190 ibox[i].add(flightInfo[i].getTableHeader());
191 ibox[i].add(flightInfo[i]);
199 serial_line = new AltosSerial();
200 altos_log = new AltosLog(serial_line);
201 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
202 this.setSize(new Dimension (infoValueMetrics.charWidth('0') * 6 * 20,
203 statusHeight * 4 + infoHeight * 17));
205 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
206 addWindowListener(new WindowAdapter() {
208 public void windowClosing(WindowEvent e) {
214 public void info_reset() {
215 for (int i = 0; i < info_columns; i++)
216 flightInfoModel[i].resetRow();
219 public void info_add_row(int col, String name, String value) {
220 flightInfoModel[col].addRow(name, value);
223 public void info_add_row(int col, String name, String format, Object value) {
224 flightInfoModel[col].addRow(name, String.format(format, value));
227 public void info_add_row(int col, String name, String format, Object v1, Object v2) {
228 flightInfoModel[col].addRow(name, String.format(format, v1, v2));
231 public void info_add_row(int col, String name, String format, Object v1, Object v2, Object v3) {
232 flightInfoModel[col].addRow(name, String.format(format, v1, v2, v3));
235 public void info_add_deg(int col, String name, double v, int pos, int neg) {
241 double deg = Math.floor(v);
242 double min = (v - deg) * 60;
244 flightInfoModel[col].addRow(name, String.format("%3.0f°%08.5f'", deg, min));
247 public void info_finish() {
248 for (int i = 0; i < info_columns; i++)
249 flightInfoModel[i].finish();
252 static final int MIN_PAD_SAMPLES = 10;
254 public void show(AltosState state) {
255 flightStatusModel.set(state);
258 if (state.npad >= MIN_PAD_SAMPLES)
259 info_add_row(0, "Ground state", "%s", "ready");
261 info_add_row(0, "Ground state", "wait (%d)",
262 MIN_PAD_SAMPLES - state.npad);
263 info_add_row(0, "Rocket state", "%s", state.data.state);
264 info_add_row(0, "Callsign", "%s", state.data.callsign);
265 info_add_row(0, "Rocket serial", "%6d", state.data.serial);
266 info_add_row(0, "Rocket flight", "%6d", state.data.flight);
268 info_add_row(0, "RSSI", "%6d dBm", state.data.rssi);
269 info_add_row(0, "Height", "%6.0f m", state.height);
270 info_add_row(0, "Max height", "%6.0f m", state.max_height);
271 info_add_row(0, "Acceleration", "%8.1f m/s²", state.acceleration);
272 info_add_row(0, "Max acceleration", "%8.1f m/s²", state.max_acceleration);
273 info_add_row(0, "Speed", "%8.1f m/s", state.ascent ? state.speed : state.baro_speed);
274 info_add_row(0, "Max Speed", "%8.1f m/s", state.max_speed);
275 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
276 info_add_row(0, "Battery", "%9.2f V", state.battery);
277 info_add_row(0, "Drogue", "%9.2f V", state.drogue_sense);
278 info_add_row(0, "Main", "%9.2f V", state.main_sense);
279 info_add_row(0, "Pad altitude", "%6.0f m", state.ground_altitude);
280 if (state.gps == null) {
281 info_add_row(1, "GPS", "not available");
283 if (state.data.gps.gps_locked)
284 info_add_row(1, "GPS", " locked");
285 else if (state.data.gps.gps_connected)
286 info_add_row(1, "GPS", " unlocked");
288 info_add_row(1, "GPS", " missing");
289 info_add_row(1, "Satellites", "%6d", state.data.gps.nsat);
290 info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
291 info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
292 info_add_row(1, "GPS altitude", "%6d", state.gps.alt);
293 info_add_row(1, "GPS height", "%6.0f", state.gps_height);
295 /* The SkyTraq GPS doesn't report these values */
297 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
298 state.gps.ground_speed,
300 info_add_row(1, "GPS climb rate", "%8.1f m/s",
301 state.gps.climb_rate);
302 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
303 state.gps.h_error, state.gps.v_error);
305 info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
307 if (state.npad > 0) {
308 if (state.from_pad != null) {
309 info_add_row(1, "Distance from pad", "%6.0f m", state.from_pad.distance);
310 info_add_row(1, "Direction from pad", "%6.0f°", state.from_pad.bearing);
312 info_add_row(1, "Distance from pad", "unknown");
313 info_add_row(1, "Direction from pad", "unknown");
315 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
316 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
317 info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
319 info_add_row(1, "GPS date", "%04d-%02d-%02d",
320 state.gps.gps_time.year,
321 state.gps.gps_time.month,
322 state.gps.gps_time.day);
323 info_add_row(1, "GPS time", " %02d:%02d:%02d",
324 state.gps.gps_time.hour,
325 state.gps.gps_time.minute,
326 state.gps.gps_time.second);
330 if (state.gps.cc_gps_sat == null)
331 info_add_row(2, "Satellites Visible", "%4d", 0);
333 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
334 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
335 info_add_row(2, "Satellite id,C/N0",
337 state.gps.cc_gps_sat[c].svid,
338 state.gps.cc_gps_sat[c].c_n0);
345 class IdleThread extends Thread {
347 private AltosState state;
350 int reported_landing = 0;
359 /* reset the landing count once we hear about a new flight */
360 if (state.state < AltosTelemetry.ao_flight_drogue)
361 reported_landing = 0;
363 /* Shut up once the rocket is on the ground */
364 if (reported_landing > 2)
367 /* If the rocket isn't on the pad, then report height */
368 if (state.state > AltosTelemetry.ao_flight_pad) {
369 voice.speak(String.format("%d meters", (int) (state.height + 0.5)));
372 /* If the rocket is coming down, check to see if it has landed;
373 * either we've got a landed report or we haven't heard from it in
377 (System.currentTimeMillis() - state.report_time > 10000 ||
378 state.state == AltosTelemetry.ao_flight_landed))
380 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
381 voice.speak("rocket landed safely");
383 voice.speak("rocket may have crashed");
384 if (state.gps != null)
385 voice.speak(String.format("bearing %d degrees, range %d meters",
386 (int) (state.from_pad.bearing + 0.5),
387 (int) (state.from_pad.distance + 0.5)));
391 } catch (InterruptedException ie) {
395 public void notice(AltosState new_state) {
400 private void tell(AltosState state, AltosState old_state) {
401 if (old_state == null || old_state.state != state.state) {
402 voice.speak(state.data.state);
403 switch (state.state) {
404 case AltosTelemetry.ao_flight_fast:
405 voice.speak(String.format("max speed %d meters per second",
406 (int) (state.max_speed + 0.5)));
408 case AltosTelemetry.ao_flight_drogue:
409 voice.speak(String.format("max height %d meters",
410 (int) (state.max_height + 0.5)));
417 class DisplayThread extends Thread {
418 String read() throws InterruptedException { return null; }
422 void update(AltosState state) throws InterruptedException { }
426 AltosState state = null;
427 AltosState old_state = null;
428 IdleThread idle_thread = new IdleThread();
434 while ((line = read()) != null) {
436 AltosTelemetry t = new AltosTelemetry(line);
438 state = new AltosState(t, state);
441 tell(state, old_state);
442 idle_thread.notice(state);
443 } catch (ParseException pp) {
444 System.out.printf("Parse error on %s\n", line);
445 System.out.println("exception " + pp);
448 } catch (InterruptedException ee) {
451 idle_thread.interrupt();
456 class DeviceThread extends DisplayThread {
458 LinkedBlockingQueue<String> telem;
460 String read() throws InterruptedException {
466 serial.remove_monitor(telem);
467 System.out.println("DisplayThread done");
470 public DeviceThread(AltosSerial s) {
472 telem = new LinkedBlockingQueue<String>();
473 serial.add_monitor(telem);
477 private void ConnectToDevice() {
478 AltosDevice device = AltosDeviceDialog.show(AltosUI.this, "TeleDongle");
480 if (device != null) {
482 serial_line.connect(device.tty);
483 DeviceThread thread = new DeviceThread(serial_line);
485 } catch (FileNotFoundException ee) {
486 JOptionPane.showMessageDialog(AltosUI.this,
488 "Cannot open serial port",
489 JOptionPane.ERROR_MESSAGE);
490 } catch (NoSuchPortException ee) {
491 JOptionPane.showMessageDialog(AltosUI.this,
493 "No such serial port",
494 JOptionPane.ERROR_MESSAGE);
495 } catch (PortInUseException ee) {
496 JOptionPane.showMessageDialog(AltosUI.this,
499 JOptionPane.ERROR_MESSAGE);
500 } catch (IOException ee) {
501 JOptionPane.showMessageDialog(AltosUI.this,
504 JOptionPane.ERROR_MESSAGE);
509 void DisconnectFromDevice () {
513 String readline(FileInputStream s) throws IOException {
517 while ((c = s.read()) != -1) {
522 line = line + (char) c;
528 * Open an existing telemetry file and replay it in realtime
531 class ReplayThread extends DisplayThread {
532 FileInputStream replay;
535 ReplayThread(FileInputStream in, String name) {
542 return readline(replay);
543 } catch (IOException ee) {
544 JOptionPane.showMessageDialog(AltosUI.this,
547 JOptionPane.ERROR_MESSAGE);
555 } catch (IOException ee) {
559 void update(AltosState state) throws InterruptedException {
560 /* Make it run in realtime after the rocket leaves the pad */
561 if (state.state > AltosTelemetry.ao_flight_pad)
562 Thread.sleep((int) (state.time_change * 1000));
566 Thread display_thread;
568 private void stop_display() {
569 if (display_thread != null && display_thread.isAlive())
570 display_thread.interrupt();
571 display_thread = null;
574 private void run_display(Thread thread) {
576 display_thread = thread;
577 display_thread.start();
581 * Replay a flight from telemetry data
583 private void Replay() {
584 JFileChooser logfile_chooser = new JFileChooser();
586 logfile_chooser.setDialogTitle("Select Telemetry File");
587 logfile_chooser.setFileFilter(new FileNameExtensionFilter("Telemetry file", "telem"));
588 logfile_chooser.setCurrentDirectory(AltosPreferences.logdir());
589 int returnVal = logfile_chooser.showOpenDialog(AltosUI.this);
591 if (returnVal == JFileChooser.APPROVE_OPTION) {
592 File file = logfile_chooser.getSelectedFile();
594 System.out.println("No file selected?");
595 String filename = file.getName();
597 FileInputStream replay = new FileInputStream(file);
598 ReplayThread thread = new ReplayThread(replay, filename);
600 } catch (FileNotFoundException ee) {
601 JOptionPane.showMessageDialog(AltosUI.this,
603 "Cannot open serial port",
604 JOptionPane.ERROR_MESSAGE);
609 /* Connect to TeleMetrum, either directly or through
610 * a TeleDongle over the packet link
612 private void SaveFlightData() {
615 /* Create the AltosUI menus
617 private void createMenu() {
618 JMenuBar menubar = new JMenuBar();
621 JRadioButtonMenuItem radioitem;
625 menu = new JMenu("File");
626 menu.setMnemonic(KeyEvent.VK_F);
629 item = new JMenuItem("Quit",KeyEvent.VK_Q);
630 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
631 ActionEvent.CTRL_MASK));
632 item.addActionListener(new ActionListener() {
633 public void actionPerformed(ActionEvent e) {
642 menu = new JMenu("Device");
643 menu.setMnemonic(KeyEvent.VK_D);
646 item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
647 item.addActionListener(new ActionListener() {
648 public void actionPerformed(ActionEvent e) {
654 item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
655 item.addActionListener(new ActionListener() {
656 public void actionPerformed(ActionEvent e) {
657 DisconnectFromDevice();
664 item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
665 item.addActionListener(new ActionListener() {
666 public void actionPerformed(ActionEvent e) {
672 item = new JMenuItem("Replay",KeyEvent.VK_R);
673 item.addActionListener(new ActionListener() {
674 public void actionPerformed(ActionEvent e) {
682 menu = new JMenu("Log");
683 menu.setMnemonic(KeyEvent.VK_L);
686 item = new JMenuItem("New Log",KeyEvent.VK_N);
687 item.addActionListener(new ActionListener() {
688 public void actionPerformed(ActionEvent e) {
693 item = new JMenuItem("Configure Log",KeyEvent.VK_C);
694 item.addActionListener(new ActionListener() {
695 public void actionPerformed(ActionEvent e) {
696 AltosPreferences.ConfigureLog();
703 menu = new JMenu("Voice", true);
704 menu.setMnemonic(KeyEvent.VK_V);
707 radioitem = new JRadioButtonMenuItem("Enable Voice");
708 radioitem.addActionListener(new ActionListener() {
709 public void actionPerformed(ActionEvent e) {
717 menu = new JMenu("Channel", true);
718 menu.setMnemonic(KeyEvent.VK_C);
720 ButtonGroup group = new ButtonGroup();
722 for (int c = 0; c <= 9; c++) {
723 radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
726 radioitem.setActionCommand(String.format("%d", c));
727 radioitem.addActionListener(new ActionListener() {
728 public void actionPerformed(ActionEvent e) {
729 System.out.println("Command: " + e.getActionCommand() + " param: " +
734 group.add(radioitem);
738 this.setJMenuBar(menubar);
741 public static void main(final String[] args) {
742 AltosUI altosui = new AltosUI();
743 altosui.setVisible(true);