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;
31 import altosui.AltosSerial;
32 import altosui.AltosSerialMonitor;
33 import altosui.AltosTelemetry;
34 import altosui.AltosState;
35 import altosui.AltosDeviceDialog;
36 import altosui.AltosPreferences;
37 import altosui.AltosLog;
38 import altosui.AltosVoice;
42 class AltosFlightStatusTableModel extends AbstractTableModel {
43 private String[] columnNames = {"Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
44 private Object[] data = { 0, "idle", 0, 0 };
46 public int getColumnCount() { return columnNames.length; }
47 public int getRowCount() { return 2; }
48 public Object getValueAt(int row, int col) {
50 return columnNames[col];
54 public void setValueAt(Object value, int col) {
56 fireTableCellUpdated(1, col);
59 public void setValueAt(Object value, int row, int col) {
60 setValueAt(value, col);
63 public void set(AltosState state) {
64 setValueAt(String.format("%1.0f", state.height), 0);
65 setValueAt(state.data.state, 1);
66 setValueAt(state.data.rssi, 2);
67 double speed = state.baro_speed;
70 setValueAt(String.format("%1.0f", speed), 3);
74 class AltosFlightInfoTableModel extends AbstractTableModel {
75 private String[] columnNames = {"Field", "Value"};
81 public InfoLine(String n, String v) {
87 private ArrayList<InfoLine> rows = new ArrayList<InfoLine>();
89 public int getColumnCount() { return columnNames.length; }
90 public String getColumnName(int col) { return columnNames[col]; }
92 public int getRowCount() { return 20; }
94 public Object getValueAt(int row, int col) {
95 if (row >= rows.size())
98 return rows.get(row).name;
100 return rows.get(row).value;
104 int prev_num_rows = 0;
106 public void resetRow() {
109 public void addRow(String name, String value) {
110 if (current_row >= rows.size())
111 rows.add(current_row, new InfoLine(name, value));
113 rows.set(current_row, new InfoLine(name, value));
116 public void finish() {
117 if (current_row > prev_num_rows) {
118 fireTableRowsInserted(prev_num_rows, current_row - 1);
119 prev_num_rows = current_row;
121 fireTableDataChanged();
125 public class AltosUI extends JFrame {
126 private int channel = -1;
128 private AltosFlightStatusTableModel flightStatusModel;
129 private JTable flightStatus;
131 static final int info_columns = 3;
133 private AltosFlightInfoTableModel[] flightInfoModel;
134 private JTable[] flightInfo;
135 private AltosSerial serial_line;
136 private AltosLog altos_log;
141 private Font statusFont = new Font("SansSerif", Font.BOLD, 24);
142 private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
143 private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
145 public AltosVoice voice = new AltosVoice();
149 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
150 Object[][] statusData = { { "0", "pad", "-50", "0" } };
152 AltosPreferences.init(this);
154 vbox = Box.createVerticalBox();
157 flightStatusModel = new AltosFlightStatusTableModel();
158 flightStatus = new JTable(flightStatusModel);
159 flightStatus.setFont(statusFont);
160 TableColumnModel tcm = flightStatus.getColumnModel();
161 for (int i = 0; i < flightStatusModel.getColumnCount(); i++) {
162 DefaultTableCellRenderer r = new DefaultTableCellRenderer();
163 r.setFont(statusFont);
164 r.setHorizontalAlignment(SwingConstants.CENTER);
165 tcm.getColumn(i).setCellRenderer(r);
168 FontMetrics statusMetrics = flightStatus.getFontMetrics(statusFont);
169 int statusHeight = (statusMetrics.getHeight() + statusMetrics.getLeading()) * 15 / 10;
170 flightStatus.setRowHeight(statusHeight);
171 flightStatus.setShowGrid(false);
173 vbox.add(flightStatus);
175 hbox = Box.createHorizontalBox();
178 flightInfo = new JTable[3];
179 flightInfoModel = new AltosFlightInfoTableModel[3];
181 FontMetrics infoValueMetrics = flightStatus.getFontMetrics(infoValueFont);
182 int infoHeight = (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 20 / 10;
184 for (int i = 0; i < info_columns; i++) {
185 ibox[i] = Box.createVerticalBox();
186 flightInfoModel[i] = new AltosFlightInfoTableModel();
187 flightInfo[i] = new JTable(flightInfoModel[i]);
188 flightInfo[i].setFont(infoValueFont);
189 flightInfo[i].setRowHeight(infoHeight);
190 flightInfo[i].setShowGrid(true);
191 ibox[i].add(flightInfo[i].getTableHeader());
192 ibox[i].add(flightInfo[i]);
200 serial_line = new AltosSerial();
201 altos_log = new AltosLog(serial_line);
202 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
203 this.setSize(new Dimension (infoValueMetrics.charWidth('0') * 6 * 20,
204 statusHeight * 4 + infoHeight * 17));
206 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
207 addWindowListener(new WindowAdapter() {
209 public void windowClosing(WindowEvent e) {
213 voice.speak("Rocket flight monitor ready.");
216 public void info_reset() {
217 for (int i = 0; i < info_columns; i++)
218 flightInfoModel[i].resetRow();
221 public void info_add_row(int col, String name, String value) {
222 flightInfoModel[col].addRow(name, value);
225 public void info_add_row(int col, String name, String format, Object... parameters) {
226 flightInfoModel[col].addRow(name, String.format(format, parameters));
229 public void info_add_deg(int col, String name, double v, int pos, int neg) {
235 double deg = Math.floor(v);
236 double min = (v - deg) * 60;
238 flightInfoModel[col].addRow(name, String.format("%3.0f°%08.5f'", deg, min));
241 public void info_finish() {
242 for (int i = 0; i < info_columns; i++)
243 flightInfoModel[i].finish();
246 public void show(AltosState state) {
247 flightStatusModel.set(state);
251 info_add_row(0, "Ground state", "%s", "ready");
253 info_add_row(0, "Ground state", "wait (%d)",
255 info_add_row(0, "Rocket state", "%s", state.data.state);
256 info_add_row(0, "Callsign", "%s", state.data.callsign);
257 info_add_row(0, "Rocket serial", "%6d", state.data.serial);
258 info_add_row(0, "Rocket flight", "%6d", state.data.flight);
260 info_add_row(0, "RSSI", "%6d dBm", state.data.rssi);
261 info_add_row(0, "Height", "%6.0f m", state.height);
262 info_add_row(0, "Max height", "%6.0f m", state.max_height);
263 info_add_row(0, "Acceleration", "%8.1f m/s²", state.acceleration);
264 info_add_row(0, "Max acceleration", "%8.1f m/s²", state.max_acceleration);
265 info_add_row(0, "Speed", "%8.1f m/s", state.ascent ? state.speed : state.baro_speed);
266 info_add_row(0, "Max Speed", "%8.1f m/s", state.max_speed);
267 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
268 info_add_row(0, "Battery", "%9.2f V", state.battery);
269 info_add_row(0, "Drogue", "%9.2f V", state.drogue_sense);
270 info_add_row(0, "Main", "%9.2f V", state.main_sense);
271 info_add_row(0, "Pad altitude", "%6.0f m", state.ground_altitude);
272 if (state.gps == null) {
273 info_add_row(1, "GPS", "not available");
275 if (state.data.gps.gps_locked)
276 info_add_row(1, "GPS", " locked");
277 else if (state.data.gps.gps_connected)
278 info_add_row(1, "GPS", " unlocked");
280 info_add_row(1, "GPS", " missing");
281 info_add_row(1, "Satellites", "%6d", state.data.gps.nsat);
282 info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
283 info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
284 info_add_row(1, "GPS altitude", "%6d", state.gps.alt);
285 info_add_row(1, "GPS height", "%6.0f", state.gps_height);
287 /* The SkyTraq GPS doesn't report these values */
289 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
290 state.gps.ground_speed,
292 info_add_row(1, "GPS climb rate", "%8.1f m/s",
293 state.gps.climb_rate);
294 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
295 state.gps.h_error, state.gps.v_error);
297 info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
299 if (state.npad > 0) {
300 if (state.from_pad != null) {
301 info_add_row(1, "Distance from pad", "%6.0f m", state.from_pad.distance);
302 info_add_row(1, "Direction from pad", "%6.0f°", state.from_pad.bearing);
304 info_add_row(1, "Distance from pad", "unknown");
305 info_add_row(1, "Direction from pad", "unknown");
307 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
308 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
309 info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
311 info_add_row(1, "GPS date", "%04d-%02d-%02d",
312 state.gps.gps_time.year,
313 state.gps.gps_time.month,
314 state.gps.gps_time.day);
315 info_add_row(1, "GPS time", " %02d:%02d:%02d",
316 state.gps.gps_time.hour,
317 state.gps.gps_time.minute,
318 state.gps.gps_time.second);
322 if (state.gps.cc_gps_sat == null)
323 info_add_row(2, "Satellites Visible", "%4d", 0);
325 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
326 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
327 info_add_row(2, "Satellite id,C/N0",
329 state.gps.cc_gps_sat[c].svid,
330 state.gps.cc_gps_sat[c].c_n0);
337 class IdleThread extends Thread {
339 private AltosState state;
340 int reported_landing;
342 public void report(boolean last) {
346 /* reset the landing count once we hear about a new flight */
347 if (state.state < AltosTelemetry.ao_flight_drogue)
348 reported_landing = 0;
350 /* Shut up once the rocket is on the ground */
351 if (reported_landing > 2) {
355 /* If the rocket isn't on the pad, then report height */
356 if (state.state > AltosTelemetry.ao_flight_pad) {
357 voice.speak("%d meters", (int) (state.height + 0.5));
359 reported_landing = 0;
362 /* If the rocket is coming down, check to see if it has landed;
363 * either we've got a landed report or we haven't heard from it in
368 System.currentTimeMillis() - state.report_time >= 15000 ||
369 state.state == AltosTelemetry.ao_flight_landed))
371 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
372 voice.speak("rocket landed safely");
374 voice.speak("rocket may have crashed");
375 if (state.from_pad != null)
376 voice.speak("bearing %d degrees, range %d meters",
377 (int) (state.from_pad.bearing + 0.5),
378 (int) (state.from_pad.distance + 0.5));
385 reported_landing = 0;
392 } catch (InterruptedException ie) {
396 public void notice(AltosState new_state) {
401 private void tell(AltosState state, AltosState old_state) {
402 if (old_state == null || old_state.state != state.state) {
403 voice.speak(state.data.state);
404 if ((old_state == null || old_state.state <= AltosTelemetry.ao_flight_boost) &&
405 state.state > AltosTelemetry.ao_flight_boost) {
406 voice.speak("max speed: %d meters per second.",
407 (int) (state.max_speed + 0.5));
408 } else if ((old_state == null || old_state.state < AltosTelemetry.ao_flight_drogue) &&
409 state.state >= AltosTelemetry.ao_flight_drogue) {
410 voice.speak("max height: %d meters.",
411 (int) (state.max_height + 0.5));
414 if (old_state == null || old_state.gps_ready != state.gps_ready) {
416 voice.speak("GPS ready");
417 else if (old_state != null)
418 voice.speak("GPS lost");
423 class DisplayThread extends Thread {
424 IdleThread idle_thread;
426 String read() throws InterruptedException { return null; }
430 void update(AltosState state) throws InterruptedException { }
434 AltosState state = null;
435 AltosState old_state = null;
437 idle_thread = new IdleThread();
443 while ((line = read()) != null) {
445 AltosTelemetry t = new AltosTelemetry(line);
447 state = new AltosState(t, state);
450 tell(state, old_state);
451 idle_thread.notice(state);
452 } catch (ParseException pp) {
453 System.out.printf("Parse error on %s\n", line);
454 System.out.println("exception " + pp);
457 } catch (InterruptedException ee) {
460 idle_thread.interrupt();
464 public void report() {
465 if (idle_thread != null)
466 idle_thread.report(true);
470 class DeviceThread extends DisplayThread {
472 LinkedBlockingQueue<String> telem;
474 String read() throws InterruptedException {
480 serial.remove_monitor(telem);
483 public DeviceThread(AltosSerial s) {
485 telem = new LinkedBlockingQueue<String>();
486 serial.add_monitor(telem);
490 private void ConnectToDevice() {
491 altos_device device = AltosDeviceDialog.show(AltosUI.this, "TeleDongle");
493 if (device != null) {
495 serial_line.open(device);
496 DeviceThread thread = new DeviceThread(serial_line);
497 serial_line.set_channel(AltosPreferences.channel());
499 } catch (FileNotFoundException ee) {
500 JOptionPane.showMessageDialog(AltosUI.this,
501 String.format("Cannot open device \"%s\"",
503 "Cannot open target device",
504 JOptionPane.ERROR_MESSAGE);
505 } catch (IOException ee) {
506 JOptionPane.showMessageDialog(AltosUI.this,
509 JOptionPane.ERROR_MESSAGE);
514 void DisconnectFromDevice () {
518 String readline(FileInputStream s) throws IOException {
522 while ((c = s.read()) != -1) {
528 line = line + (char) c;
534 * Open an existing telemetry file and replay it in realtime
537 class ReplayThread extends DisplayThread {
538 FileInputStream replay;
541 ReplayThread(FileInputStream in, String name) {
548 return readline(replay);
549 } catch (IOException ee) {
550 JOptionPane.showMessageDialog(AltosUI.this,
553 JOptionPane.ERROR_MESSAGE);
561 } catch (IOException ee) {
566 void update(AltosState state) throws InterruptedException {
567 /* Make it run in realtime after the rocket leaves the pad */
568 if (state.state > AltosTelemetry.ao_flight_pad)
569 Thread.sleep((int) (Math.min(state.time_change,10) * 1000));
573 Thread display_thread;
575 private void stop_display() {
576 if (display_thread != null && display_thread.isAlive())
577 display_thread.interrupt();
578 display_thread = null;
581 private void run_display(Thread thread) {
583 display_thread = thread;
584 display_thread.start();
588 * Replay a flight from telemetry data
590 private void Replay() {
591 JFileChooser logfile_chooser = new JFileChooser();
593 logfile_chooser.setDialogTitle("Select Telemetry File");
594 logfile_chooser.setFileFilter(new FileNameExtensionFilter("Telemetry file", "telem"));
595 logfile_chooser.setCurrentDirectory(AltosPreferences.logdir());
596 int returnVal = logfile_chooser.showOpenDialog(AltosUI.this);
598 if (returnVal == JFileChooser.APPROVE_OPTION) {
599 File file = logfile_chooser.getSelectedFile();
601 System.out.println("No file selected?");
602 String filename = file.getName();
604 FileInputStream replay = new FileInputStream(file);
605 ReplayThread thread = new ReplayThread(replay, filename);
607 } catch (FileNotFoundException ee) {
608 JOptionPane.showMessageDialog(AltosUI.this,
610 "Cannot open telemetry file",
611 JOptionPane.ERROR_MESSAGE);
616 /* Connect to TeleMetrum, either directly or through
617 * a TeleDongle over the packet link
619 private void SaveFlightData() {
620 new AltosEeprom(AltosUI.this);
623 /* Create the AltosUI menus
625 private void createMenu() {
626 JMenuBar menubar = new JMenuBar();
629 JRadioButtonMenuItem radioitem;
633 menu = new JMenu("File");
634 menu.setMnemonic(KeyEvent.VK_F);
637 item = new JMenuItem("Quit",KeyEvent.VK_Q);
638 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
639 ActionEvent.CTRL_MASK));
640 item.addActionListener(new ActionListener() {
641 public void actionPerformed(ActionEvent e) {
650 menu = new JMenu("Device");
651 menu.setMnemonic(KeyEvent.VK_D);
654 item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
655 item.addActionListener(new ActionListener() {
656 public void actionPerformed(ActionEvent e) {
662 item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
663 item.addActionListener(new ActionListener() {
664 public void actionPerformed(ActionEvent e) {
665 DisconnectFromDevice();
672 item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
673 item.addActionListener(new ActionListener() {
674 public void actionPerformed(ActionEvent e) {
680 item = new JMenuItem("Replay",KeyEvent.VK_R);
681 item.addActionListener(new ActionListener() {
682 public void actionPerformed(ActionEvent e) {
690 menu = new JMenu("Log");
691 menu.setMnemonic(KeyEvent.VK_L);
694 item = new JMenuItem("New Log",KeyEvent.VK_N);
695 item.addActionListener(new ActionListener() {
696 public void actionPerformed(ActionEvent e) {
701 item = new JMenuItem("Configure Log",KeyEvent.VK_C);
702 item.addActionListener(new ActionListener() {
703 public void actionPerformed(ActionEvent e) {
704 AltosPreferences.ConfigureLog();
711 menu = new JMenu("Voice", true);
712 menu.setMnemonic(KeyEvent.VK_V);
715 radioitem = new JRadioButtonMenuItem("Enable Voice", AltosPreferences.voice());
716 radioitem.addActionListener(new ActionListener() {
717 public void actionPerformed(ActionEvent e) {
718 JRadioButtonMenuItem item = (JRadioButtonMenuItem) e.getSource();
719 boolean enabled = item.isSelected();
720 AltosPreferences.set_voice(enabled);
722 voice.speak_always("Enable voice.");
724 voice.speak_always("Disable voice.");
728 item = new JMenuItem("Test Voice",KeyEvent.VK_T);
729 item.addActionListener(new ActionListener() {
730 public void actionPerformed(ActionEvent e) {
731 voice.speak("That's one small step for man; one giant leap for mankind.");
739 menu = new JMenu("Channel", true);
740 menu.setMnemonic(KeyEvent.VK_C);
742 ButtonGroup group = new ButtonGroup();
744 for (int c = 0; c <= 9; c++) {
745 radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
747 c == AltosPreferences.channel());
748 radioitem.setActionCommand(String.format("%d", c));
749 radioitem.addActionListener(new ActionListener() {
750 public void actionPerformed(ActionEvent e) {
751 int new_channel = Integer.parseInt(e.getActionCommand());
752 AltosPreferences.set_channel(new_channel);
753 serial_line.set_channel(new_channel);
757 group.add(radioitem);
761 this.setJMenuBar(menubar);
764 public static void main(final String[] args) {
765 AltosUI altosui = new AltosUI();
766 altosui.setVisible(true);