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.AltosRecord;
35 import altosui.AltosTelemetry;
36 import altosui.AltosState;
37 import altosui.AltosDeviceDialog;
38 import altosui.AltosPreferences;
39 import altosui.AltosLog;
40 import altosui.AltosVoice;
41 import altosui.AltosFlightStatusTableModel;
42 import altosui.AltosFlightInfoTableModel;
46 public class AltosUI extends JFrame {
47 private int channel = -1;
49 private AltosFlightStatusTableModel flightStatusModel;
50 private JTable flightStatus;
52 static final int info_columns = 3;
54 private AltosFlightInfoTableModel[] flightInfoModel;
55 private JTable[] flightInfo;
56 private AltosSerial serial_line;
57 private AltosLog altos_log;
62 private Font statusFont = new Font("SansSerif", Font.BOLD, 24);
63 private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
64 private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
66 public AltosVoice voice = new AltosVoice();
70 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
71 Object[][] statusData = { { "0", "pad", "-50", "0" } };
73 AltosPreferences.init(this);
75 vbox = Box.createVerticalBox();
78 flightStatusModel = new AltosFlightStatusTableModel();
79 flightStatus = new JTable(flightStatusModel);
80 flightStatus.setFont(statusFont);
81 TableColumnModel tcm = flightStatus.getColumnModel();
82 for (int i = 0; i < flightStatusModel.getColumnCount(); i++) {
83 DefaultTableCellRenderer r = new DefaultTableCellRenderer();
84 r.setFont(statusFont);
85 r.setHorizontalAlignment(SwingConstants.CENTER);
86 tcm.getColumn(i).setCellRenderer(r);
89 FontMetrics statusMetrics = flightStatus.getFontMetrics(statusFont);
90 int statusHeight = (statusMetrics.getHeight() + statusMetrics.getLeading()) * 15 / 10;
91 flightStatus.setRowHeight(statusHeight);
92 flightStatus.setShowGrid(false);
94 vbox.add(flightStatus);
96 hbox = Box.createHorizontalBox();
99 flightInfo = new JTable[3];
100 flightInfoModel = new AltosFlightInfoTableModel[3];
102 FontMetrics infoValueMetrics = flightStatus.getFontMetrics(infoValueFont);
103 int infoHeight = (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 20 / 10;
105 for (int i = 0; i < info_columns; i++) {
106 ibox[i] = Box.createVerticalBox();
107 flightInfoModel[i] = new AltosFlightInfoTableModel();
108 flightInfo[i] = new JTable(flightInfoModel[i]);
109 flightInfo[i].setFont(infoValueFont);
110 flightInfo[i].setRowHeight(infoHeight);
111 flightInfo[i].setShowGrid(true);
112 ibox[i].add(flightInfo[i].getTableHeader());
113 ibox[i].add(flightInfo[i]);
121 serial_line = new AltosSerial();
122 altos_log = new AltosLog(serial_line);
123 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
124 this.setSize(new Dimension (infoValueMetrics.charWidth('0') * 6 * 20,
125 statusHeight * 4 + infoHeight * 17));
127 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
128 addWindowListener(new WindowAdapter() {
130 public void windowClosing(WindowEvent e) {
134 voice.speak("Rocket flight monitor ready.");
137 public void info_reset() {
138 for (int i = 0; i < info_columns; i++)
139 flightInfoModel[i].resetRow();
142 public void info_add_row(int col, String name, String value) {
143 flightInfoModel[col].addRow(name, value);
146 public void info_add_row(int col, String name, String format, Object... parameters) {
147 flightInfoModel[col].addRow(name, String.format(format, parameters));
150 public void info_add_deg(int col, String name, double v, int pos, int neg) {
156 double deg = Math.floor(v);
157 double min = (v - deg) * 60;
159 flightInfoModel[col].addRow(name, String.format("%3.0f°%08.5f'", deg, min));
162 public void info_finish() {
163 for (int i = 0; i < info_columns; i++)
164 flightInfoModel[i].finish();
167 public void show(AltosState state) {
168 flightStatusModel.set(state);
172 info_add_row(0, "Ground state", "%s", "ready");
174 info_add_row(0, "Ground state", "wait (%d)",
176 info_add_row(0, "Rocket state", "%s", state.data.state);
177 info_add_row(0, "Callsign", "%s", state.data.callsign);
178 info_add_row(0, "Rocket serial", "%6d", state.data.serial);
179 info_add_row(0, "Rocket flight", "%6d", state.data.flight);
181 info_add_row(0, "RSSI", "%6d dBm", state.data.rssi);
182 info_add_row(0, "Height", "%6.0f m", state.height);
183 info_add_row(0, "Max height", "%6.0f m", state.max_height);
184 info_add_row(0, "Acceleration", "%8.1f m/s²", state.acceleration);
185 info_add_row(0, "Max acceleration", "%8.1f m/s²", state.max_acceleration);
186 info_add_row(0, "Speed", "%8.1f m/s", state.ascent ? state.speed : state.baro_speed);
187 info_add_row(0, "Max Speed", "%8.1f m/s", state.max_speed);
188 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
189 info_add_row(0, "Battery", "%9.2f V", state.battery);
190 info_add_row(0, "Drogue", "%9.2f V", state.drogue_sense);
191 info_add_row(0, "Main", "%9.2f V", state.main_sense);
192 info_add_row(0, "Pad altitude", "%6.0f m", state.ground_altitude);
193 if (state.gps == null) {
194 info_add_row(1, "GPS", "not available");
196 if (state.data.gps.gps_locked)
197 info_add_row(1, "GPS", " locked");
198 else if (state.data.gps.gps_connected)
199 info_add_row(1, "GPS", " unlocked");
201 info_add_row(1, "GPS", " missing");
202 info_add_row(1, "Satellites", "%6d", state.data.gps.nsat);
203 info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
204 info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
205 info_add_row(1, "GPS altitude", "%6d", state.gps.alt);
206 info_add_row(1, "GPS height", "%6.0f", state.gps_height);
208 /* The SkyTraq GPS doesn't report these values */
210 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
211 state.gps.ground_speed,
213 info_add_row(1, "GPS climb rate", "%8.1f m/s",
214 state.gps.climb_rate);
215 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
216 state.gps.h_error, state.gps.v_error);
218 info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
220 if (state.npad > 0) {
221 if (state.from_pad != null) {
222 info_add_row(1, "Distance from pad", "%6.0f m", state.from_pad.distance);
223 info_add_row(1, "Direction from pad", "%6.0f°", state.from_pad.bearing);
225 info_add_row(1, "Distance from pad", "unknown");
226 info_add_row(1, "Direction from pad", "unknown");
228 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
229 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
230 info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
232 info_add_row(1, "GPS date", "%04d-%02d-%02d",
236 info_add_row(1, "GPS time", " %02d:%02d:%02d",
243 if (state.gps.cc_gps_sat == null)
244 info_add_row(2, "Satellites Visible", "%4d", 0);
246 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
247 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
248 info_add_row(2, "Satellite id,C/N0",
250 state.gps.cc_gps_sat[c].svid,
251 state.gps.cc_gps_sat[c].c_n0);
258 class IdleThread extends Thread {
260 private AltosState state;
261 int reported_landing;
263 public void report(boolean last) {
267 /* reset the landing count once we hear about a new flight */
268 if (state.state < Altos.ao_flight_drogue)
269 reported_landing = 0;
271 /* Shut up once the rocket is on the ground */
272 if (reported_landing > 2) {
276 /* If the rocket isn't on the pad, then report height */
277 if (state.state > Altos.ao_flight_pad) {
278 voice.speak("%d meters", (int) (state.height + 0.5));
280 reported_landing = 0;
283 /* If the rocket is coming down, check to see if it has landed;
284 * either we've got a landed report or we haven't heard from it in
289 System.currentTimeMillis() - state.report_time >= 15000 ||
290 state.state == Altos.ao_flight_landed))
292 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
293 voice.speak("rocket landed safely");
295 voice.speak("rocket may have crashed");
296 if (state.from_pad != null)
297 voice.speak("bearing %d degrees, range %d meters",
298 (int) (state.from_pad.bearing + 0.5),
299 (int) (state.from_pad.distance + 0.5));
306 reported_landing = 0;
313 } catch (InterruptedException ie) {
317 public void notice(AltosState new_state) {
322 private void tell(AltosState state, AltosState old_state) {
323 if (old_state == null || old_state.state != state.state) {
324 voice.speak(state.data.state);
325 if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
326 state.state > Altos.ao_flight_boost) {
327 voice.speak("max speed: %d meters per second.",
328 (int) (state.max_speed + 0.5));
329 } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
330 state.state >= Altos.ao_flight_drogue) {
331 voice.speak("max height: %d meters.",
332 (int) (state.max_height + 0.5));
335 if (old_state == null || old_state.gps_ready != state.gps_ready) {
337 voice.speak("GPS ready");
338 else if (old_state != null)
339 voice.speak("GPS lost");
344 class DisplayThread extends Thread {
345 IdleThread idle_thread;
349 AltosRecord read() throws InterruptedException, ParseException { return null; }
353 void update(AltosState state) throws InterruptedException { }
357 AltosState state = null;
358 AltosState old_state = null;
360 idle_thread = new IdleThread();
368 AltosRecord record = read();
372 state = new AltosState(record, state);
375 tell(state, old_state);
376 idle_thread.notice(state);
377 } catch (ParseException pp) {
378 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
381 } catch (InterruptedException ee) {
384 idle_thread.interrupt();
388 public void report() {
389 if (idle_thread != null)
390 idle_thread.report(true);
394 class TelemetryThread extends DisplayThread {
396 String readline() throws InterruptedException { return null; }
398 AltosRecord read() throws InterruptedException, ParseException {
399 return new AltosTelemetry(readline());
403 class DeviceThread extends TelemetryThread {
405 LinkedBlockingQueue<String> telem;
407 String readline() throws InterruptedException {
413 serial.remove_monitor(telem);
416 public DeviceThread(AltosSerial s) {
418 telem = new LinkedBlockingQueue<String>();
419 serial.add_monitor(telem);
424 private void ConnectToDevice() {
425 altos_device device = AltosDeviceDialog.show(AltosUI.this, "TeleDongle");
427 if (device != null) {
429 serial_line.open(device);
430 DeviceThread thread = new DeviceThread(serial_line);
431 serial_line.set_channel(AltosPreferences.channel());
433 } catch (FileNotFoundException ee) {
434 JOptionPane.showMessageDialog(AltosUI.this,
435 String.format("Cannot open device \"%s\"",
437 "Cannot open target device",
438 JOptionPane.ERROR_MESSAGE);
439 } catch (IOException ee) {
440 JOptionPane.showMessageDialog(AltosUI.this,
443 JOptionPane.ERROR_MESSAGE);
448 void DisconnectFromDevice () {
453 * Open an existing telemetry file and replay it in realtime
456 class ReplayTelemetryThread extends TelemetryThread {
457 FileInputStream replay;
459 ReplayTelemetryThread(FileInputStream in, String in_name) {
466 String line = AltosRecord.gets(replay);
467 System.out.printf("telemetry line %s\n", line);
469 } catch (IOException ee) {
470 JOptionPane.showMessageDialog(AltosUI.this,
473 JOptionPane.ERROR_MESSAGE);
481 } catch (IOException ee) {
486 void update(AltosState state) throws InterruptedException {
487 /* Make it run in realtime after the rocket leaves the pad */
488 if (state.state > Altos.ao_flight_pad)
489 Thread.sleep((int) (Math.min(state.time_change,10) * 1000));
493 class ReplayEepromThread extends DisplayThread {
494 FileInputStream replay;
496 AltosRecord read () {
503 } catch (IOException ee) {
508 ReplayEepromThread(FileInputStream in, String in_name) {
514 Thread display_thread;
516 private void stop_display() {
517 if (display_thread != null && display_thread.isAlive())
518 display_thread.interrupt();
519 display_thread = null;
522 private void run_display(Thread thread) {
524 display_thread = thread;
525 display_thread.start();
529 * Replay a flight from telemetry data
531 private void Replay() {
532 JFileChooser logfile_chooser = new JFileChooser();
534 logfile_chooser.setDialogTitle("Select Flight Record File");
535 logfile_chooser.setFileFilter(new FileNameExtensionFilter("Flight data file", "eeprom", "telem"));
536 logfile_chooser.setCurrentDirectory(AltosPreferences.logdir());
537 int returnVal = logfile_chooser.showOpenDialog(AltosUI.this);
539 if (returnVal == JFileChooser.APPROVE_OPTION) {
540 File file = logfile_chooser.getSelectedFile();
542 System.out.println("No file selected?");
543 String filename = file.getName();
545 FileInputStream replay = new FileInputStream(file);
546 DisplayThread thread;
547 if (filename.endsWith("eeprom"))
548 thread = new ReplayEepromThread(replay, filename);
550 thread = new ReplayTelemetryThread(replay, filename);
552 } catch (FileNotFoundException ee) {
553 JOptionPane.showMessageDialog(AltosUI.this,
555 "Cannot open telemetry file",
556 JOptionPane.ERROR_MESSAGE);
561 /* Connect to TeleMetrum, either directly or through
562 * a TeleDongle over the packet link
564 private void SaveFlightData() {
565 new AltosEepromDownload(AltosUI.this);
568 /* Create the AltosUI menus
570 private void createMenu() {
571 JMenuBar menubar = new JMenuBar();
574 JRadioButtonMenuItem radioitem;
578 menu = new JMenu("File");
579 menu.setMnemonic(KeyEvent.VK_F);
582 item = new JMenuItem("Quit",KeyEvent.VK_Q);
583 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
584 ActionEvent.CTRL_MASK));
585 item.addActionListener(new ActionListener() {
586 public void actionPerformed(ActionEvent e) {
595 menu = new JMenu("Device");
596 menu.setMnemonic(KeyEvent.VK_D);
599 item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
600 item.addActionListener(new ActionListener() {
601 public void actionPerformed(ActionEvent e) {
607 item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
608 item.addActionListener(new ActionListener() {
609 public void actionPerformed(ActionEvent e) {
610 DisconnectFromDevice();
617 item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
618 item.addActionListener(new ActionListener() {
619 public void actionPerformed(ActionEvent e) {
625 item = new JMenuItem("Replay",KeyEvent.VK_R);
626 item.addActionListener(new ActionListener() {
627 public void actionPerformed(ActionEvent e) {
635 menu = new JMenu("Log");
636 menu.setMnemonic(KeyEvent.VK_L);
639 item = new JMenuItem("New Log",KeyEvent.VK_N);
640 item.addActionListener(new ActionListener() {
641 public void actionPerformed(ActionEvent e) {
646 item = new JMenuItem("Configure Log",KeyEvent.VK_C);
647 item.addActionListener(new ActionListener() {
648 public void actionPerformed(ActionEvent e) {
649 AltosPreferences.ConfigureLog();
656 menu = new JMenu("Voice", true);
657 menu.setMnemonic(KeyEvent.VK_V);
660 radioitem = new JRadioButtonMenuItem("Enable Voice", AltosPreferences.voice());
661 radioitem.addActionListener(new ActionListener() {
662 public void actionPerformed(ActionEvent e) {
663 JRadioButtonMenuItem item = (JRadioButtonMenuItem) e.getSource();
664 boolean enabled = item.isSelected();
665 AltosPreferences.set_voice(enabled);
667 voice.speak_always("Enable voice.");
669 voice.speak_always("Disable voice.");
673 item = new JMenuItem("Test Voice",KeyEvent.VK_T);
674 item.addActionListener(new ActionListener() {
675 public void actionPerformed(ActionEvent e) {
676 voice.speak("That's one small step for man; one giant leap for mankind.");
684 menu = new JMenu("Channel", true);
685 menu.setMnemonic(KeyEvent.VK_C);
687 ButtonGroup group = new ButtonGroup();
689 for (int c = 0; c <= 9; c++) {
690 radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
692 c == AltosPreferences.channel());
693 radioitem.setActionCommand(String.format("%d", c));
694 radioitem.addActionListener(new ActionListener() {
695 public void actionPerformed(ActionEvent e) {
696 int new_channel = Integer.parseInt(e.getActionCommand());
697 AltosPreferences.set_channel(new_channel);
698 serial_line.set_channel(new_channel);
702 group.add(radioitem);
706 this.setJMenuBar(menubar);
709 public static void main(final String[] args) {
710 AltosUI altosui = new AltosUI();
711 altosui.setVisible(true);