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;
43 import altosui.AltosChannelMenu;
44 import altosui.AltosFlash;
48 public class AltosUI extends JFrame {
49 private int channel = -1;
51 private AltosFlightStatusTableModel flightStatusModel;
52 private JTable flightStatus;
54 static final int info_columns = 3;
56 private AltosFlightInfoTableModel[] flightInfoModel;
57 private JTable[] flightInfo;
58 private AltosSerial serial_line;
59 private AltosLog altos_log;
64 private Font statusFont = new Font("SansSerif", Font.BOLD, 24);
65 private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
66 private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
68 public AltosVoice voice = new AltosVoice();
72 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
73 Object[][] statusData = { { "0", "pad", "-50", "0" } };
75 AltosPreferences.init(this);
77 vbox = Box.createVerticalBox();
80 flightStatusModel = new AltosFlightStatusTableModel();
81 flightStatus = new JTable(flightStatusModel);
82 flightStatus.setFont(statusFont);
83 TableColumnModel tcm = flightStatus.getColumnModel();
84 for (int i = 0; i < flightStatusModel.getColumnCount(); i++) {
85 DefaultTableCellRenderer r = new DefaultTableCellRenderer();
86 r.setFont(statusFont);
87 r.setHorizontalAlignment(SwingConstants.CENTER);
88 tcm.getColumn(i).setCellRenderer(r);
91 FontMetrics statusMetrics = flightStatus.getFontMetrics(statusFont);
92 int statusHeight = (statusMetrics.getHeight() + statusMetrics.getLeading()) * 15 / 10;
93 flightStatus.setRowHeight(statusHeight);
94 flightStatus.setShowGrid(false);
96 vbox.add(flightStatus);
98 hbox = Box.createHorizontalBox();
101 flightInfo = new JTable[3];
102 flightInfoModel = new AltosFlightInfoTableModel[3];
104 FontMetrics infoValueMetrics = flightStatus.getFontMetrics(infoValueFont);
105 int infoHeight = (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 20 / 10;
107 for (int i = 0; i < info_columns; i++) {
108 ibox[i] = Box.createVerticalBox();
109 flightInfoModel[i] = new AltosFlightInfoTableModel();
110 flightInfo[i] = new JTable(flightInfoModel[i]);
111 flightInfo[i].setFont(infoValueFont);
112 flightInfo[i].setRowHeight(infoHeight);
113 flightInfo[i].setShowGrid(true);
114 ibox[i].add(flightInfo[i].getTableHeader());
115 ibox[i].add(flightInfo[i]);
123 serial_line = new AltosSerial();
124 altos_log = new AltosLog(serial_line);
125 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
126 this.setSize(new Dimension (infoValueMetrics.charWidth('0') * 6 * 20,
127 statusHeight * 4 + infoHeight * 17));
129 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
130 addWindowListener(new WindowAdapter() {
132 public void windowClosing(WindowEvent e) {
136 voice.speak("Rocket flight monitor ready.");
139 public void info_reset() {
140 for (int i = 0; i < info_columns; i++)
141 flightInfoModel[i].resetRow();
144 public void info_add_row(int col, String name, String value) {
145 flightInfoModel[col].addRow(name, value);
148 public void info_add_row(int col, String name, String format, Object... parameters) {
149 flightInfoModel[col].addRow(name, String.format(format, parameters));
152 public void info_add_deg(int col, String name, double v, int pos, int neg) {
158 double deg = Math.floor(v);
159 double min = (v - deg) * 60;
161 flightInfoModel[col].addRow(name, String.format("%3.0f°%08.5f'", deg, min));
164 public void info_finish() {
165 for (int i = 0; i < info_columns; i++)
166 flightInfoModel[i].finish();
169 public void show(AltosState state) {
170 flightStatusModel.set(state);
174 info_add_row(0, "Ground state", "%s", "ready");
176 info_add_row(0, "Ground state", "wait (%d)",
178 info_add_row(0, "Rocket state", "%s", state.data.state());
179 info_add_row(0, "Callsign", "%s", state.data.callsign);
180 info_add_row(0, "Rocket serial", "%6d", state.data.serial);
181 info_add_row(0, "Rocket flight", "%6d", state.data.flight);
183 info_add_row(0, "RSSI", "%6d dBm", state.data.rssi);
184 info_add_row(0, "Height", "%6.0f m", state.height);
185 info_add_row(0, "Max height", "%6.0f m", state.max_height);
186 info_add_row(0, "Acceleration", "%8.1f m/s²", state.acceleration);
187 info_add_row(0, "Max acceleration", "%8.1f m/s²", state.max_acceleration);
188 info_add_row(0, "Speed", "%8.1f m/s", state.ascent ? state.speed : state.baro_speed);
189 info_add_row(0, "Max Speed", "%8.1f m/s", state.max_speed);
190 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
191 info_add_row(0, "Battery", "%9.2f V", state.battery);
192 info_add_row(0, "Drogue", "%9.2f V", state.drogue_sense);
193 info_add_row(0, "Main", "%9.2f V", state.main_sense);
194 info_add_row(0, "Pad altitude", "%6.0f m", state.ground_altitude);
195 if (state.gps == null) {
196 info_add_row(1, "GPS", "not available");
198 if (state.data.gps.locked)
199 info_add_row(1, "GPS", " locked");
200 else if (state.data.gps.connected)
201 info_add_row(1, "GPS", " unlocked");
203 info_add_row(1, "GPS", " missing");
204 info_add_row(1, "Satellites", "%6d", state.data.gps.nsat);
205 info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
206 info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
207 info_add_row(1, "GPS altitude", "%6d", state.gps.alt);
208 info_add_row(1, "GPS height", "%6.0f", state.gps_height);
210 /* The SkyTraq GPS doesn't report these values */
212 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
213 state.gps.ground_speed,
215 info_add_row(1, "GPS climb rate", "%8.1f m/s",
216 state.gps.climb_rate);
217 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
218 state.gps.h_error, state.gps.v_error);
220 info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
222 if (state.npad > 0) {
223 if (state.from_pad != null) {
224 info_add_row(1, "Distance from pad", "%6.0f m", state.from_pad.distance);
225 info_add_row(1, "Direction from pad", "%6.0f°", state.from_pad.bearing);
227 info_add_row(1, "Distance from pad", "unknown");
228 info_add_row(1, "Direction from pad", "unknown");
230 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
231 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
232 info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
234 info_add_row(1, "GPS date", "%04d-%02d-%02d",
238 info_add_row(1, "GPS time", " %02d:%02d:%02d",
245 if (state.gps.cc_gps_sat == null)
246 info_add_row(2, "Satellites Visible", "%4d", 0);
248 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
249 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
250 info_add_row(2, "Satellite id,C/N0",
252 state.gps.cc_gps_sat[c].svid,
253 state.gps.cc_gps_sat[c].c_n0);
260 class IdleThread extends Thread {
262 private AltosState state;
263 int reported_landing;
265 public void report(boolean last) {
269 /* reset the landing count once we hear about a new flight */
270 if (state.state < Altos.ao_flight_drogue)
271 reported_landing = 0;
273 /* Shut up once the rocket is on the ground */
274 if (reported_landing > 2) {
278 /* If the rocket isn't on the pad, then report height */
279 if (state.state > Altos.ao_flight_pad) {
280 voice.speak("%d meters", (int) (state.height + 0.5));
282 reported_landing = 0;
285 /* If the rocket is coming down, check to see if it has landed;
286 * either we've got a landed report or we haven't heard from it in
291 System.currentTimeMillis() - state.report_time >= 15000 ||
292 state.state == Altos.ao_flight_landed))
294 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
295 voice.speak("rocket landed safely");
297 voice.speak("rocket may have crashed");
298 if (state.from_pad != null)
299 voice.speak("bearing %d degrees, range %d meters",
300 (int) (state.from_pad.bearing + 0.5),
301 (int) (state.from_pad.distance + 0.5));
308 reported_landing = 0;
315 } catch (InterruptedException ie) {
319 public void notice(AltosState new_state) {
324 private void tell(AltosState state, AltosState old_state) {
325 if (old_state == null || old_state.state != state.state) {
326 voice.speak(state.data.state());
327 if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
328 state.state > Altos.ao_flight_boost) {
329 voice.speak("max speed: %d meters per second.",
330 (int) (state.max_speed + 0.5));
331 } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
332 state.state >= Altos.ao_flight_drogue) {
333 voice.speak("max height: %d meters.",
334 (int) (state.max_height + 0.5));
337 if (old_state == null || old_state.gps_ready != state.gps_ready) {
339 voice.speak("GPS ready");
340 else if (old_state != null)
341 voice.speak("GPS lost");
346 class DisplayThread extends Thread {
347 IdleThread idle_thread;
351 AltosRecord read() throws InterruptedException, ParseException { return null; }
355 void update(AltosState state) throws InterruptedException { }
359 AltosState state = null;
360 AltosState old_state = null;
362 idle_thread = new IdleThread();
370 AltosRecord record = read();
374 state = new AltosState(record, state);
377 tell(state, old_state);
378 idle_thread.notice(state);
379 } catch (ParseException pp) {
380 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
383 } catch (InterruptedException ee) {
386 idle_thread.interrupt();
390 public void report() {
391 if (idle_thread != null)
392 idle_thread.report(true);
396 class DeviceThread extends DisplayThread {
398 LinkedBlockingQueue<String> telem;
400 AltosRecord read() throws InterruptedException, ParseException {
401 return new AltosTelemetry(telem.take());
406 serial.remove_monitor(telem);
409 public DeviceThread(AltosSerial s) {
411 telem = new LinkedBlockingQueue<String>();
412 serial.add_monitor(telem);
417 private void ConnectToDevice() {
418 AltosDevice device = AltosDeviceDialog.show(AltosUI.this, AltosDevice.BaseStation);
420 if (device != null) {
422 serial_line.open(device);
423 DeviceThread thread = new DeviceThread(serial_line);
424 serial_line.set_channel(AltosPreferences.channel());
425 serial_line.set_callsign(AltosPreferences.callsign());
427 } catch (FileNotFoundException ee) {
428 JOptionPane.showMessageDialog(AltosUI.this,
429 String.format("Cannot open device \"%s\"",
431 "Cannot open target device",
432 JOptionPane.ERROR_MESSAGE);
433 } catch (IOException ee) {
434 JOptionPane.showMessageDialog(AltosUI.this,
437 JOptionPane.ERROR_MESSAGE);
442 void DisconnectFromDevice () {
446 void ConfigureCallsign() {
448 result = JOptionPane.showInputDialog(AltosUI.this,
449 "Configure Callsign",
450 AltosPreferences.callsign());
451 if (result != null) {
452 AltosPreferences.set_callsign(result);
453 if (serial_line != null)
454 serial_line.set_callsign(result);
458 void ConfigureTeleMetrum() {
459 new AltosConfig(AltosUI.this);
463 new AltosFlash(AltosUI.this);
467 * Open an existing telemetry file and replay it in realtime
470 class ReplayThread extends DisplayThread {
474 public AltosRecord read() {
476 return reader.read();
477 } catch (IOException ie) {
478 JOptionPane.showMessageDialog(AltosUI.this,
481 JOptionPane.ERROR_MESSAGE);
482 } catch (ParseException pe) {
487 public void close () {
491 public ReplayThread(AltosReader in_reader, String in_name) {
494 void update(AltosState state) throws InterruptedException {
495 /* Make it run in realtime after the rocket leaves the pad */
496 if (state.state > Altos.ao_flight_pad)
497 Thread.sleep((int) (Math.min(state.time_change,10) * 1000));
501 class ReplayTelemetryThread extends ReplayThread {
502 ReplayTelemetryThread(FileInputStream in, String in_name) {
503 super(new AltosTelemetryReader(in), in_name);
508 class ReplayEepromThread extends ReplayThread {
509 ReplayEepromThread(FileInputStream in, String in_name) {
510 super(new AltosEepromReader(in), 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("Replay File",KeyEvent.VK_R);
583 item.addActionListener(new ActionListener() {
584 public void actionPerformed(ActionEvent e) {
590 item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
591 item.addActionListener(new ActionListener() {
592 public void actionPerformed(ActionEvent e) {
598 item = new JMenuItem("Flash Image",KeyEvent.VK_F);
599 item.addActionListener(new ActionListener() {
600 public void actionPerformed(ActionEvent e) {
606 item = new JMenuItem("Quit",KeyEvent.VK_Q);
607 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
608 ActionEvent.CTRL_MASK));
609 item.addActionListener(new ActionListener() {
610 public void actionPerformed(ActionEvent e) {
619 menu = new JMenu("Device");
620 menu.setMnemonic(KeyEvent.VK_D);
623 item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
624 item.addActionListener(new ActionListener() {
625 public void actionPerformed(ActionEvent e) {
631 item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
632 item.addActionListener(new ActionListener() {
633 public void actionPerformed(ActionEvent e) {
634 DisconnectFromDevice();
641 item = new JMenuItem("Set Callsign",KeyEvent.VK_S);
642 item.addActionListener(new ActionListener() {
643 public void actionPerformed(ActionEvent e) {
650 item = new JMenuItem("Configure TeleMetrum device",KeyEvent.VK_T);
651 item.addActionListener(new ActionListener() {
652 public void actionPerformed(ActionEvent e) {
653 ConfigureTeleMetrum();
661 menu = new JMenu("Log");
662 menu.setMnemonic(KeyEvent.VK_L);
665 item = new JMenuItem("New Log",KeyEvent.VK_N);
666 item.addActionListener(new ActionListener() {
667 public void actionPerformed(ActionEvent e) {
672 item = new JMenuItem("Configure Log",KeyEvent.VK_C);
673 item.addActionListener(new ActionListener() {
674 public void actionPerformed(ActionEvent e) {
675 AltosPreferences.ConfigureLog();
682 menu = new JMenu("Voice", true);
683 menu.setMnemonic(KeyEvent.VK_V);
686 radioitem = new JRadioButtonMenuItem("Enable Voice", AltosPreferences.voice());
687 radioitem.addActionListener(new ActionListener() {
688 public void actionPerformed(ActionEvent e) {
689 JRadioButtonMenuItem item = (JRadioButtonMenuItem) e.getSource();
690 boolean enabled = item.isSelected();
691 AltosPreferences.set_voice(enabled);
693 voice.speak_always("Enable voice.");
695 voice.speak_always("Disable voice.");
699 item = new JMenuItem("Test Voice",KeyEvent.VK_T);
700 item.addActionListener(new ActionListener() {
701 public void actionPerformed(ActionEvent e) {
702 voice.speak("That's one small step for man; one giant leap for mankind.");
710 menu = new AltosChannelMenu(AltosPreferences.channel());
711 menu.addActionListener(new ActionListener() {
712 public void actionPerformed(ActionEvent e) {
713 int new_channel = Integer.parseInt(e.getActionCommand());
714 AltosPreferences.set_channel(new_channel);
715 serial_line.set_channel(new_channel);
718 menu.setMnemonic(KeyEvent.VK_C);
722 this.setJMenuBar(menubar);
725 public static void main(final String[] args) {
726 AltosUI altosui = new AltosUI();
727 altosui.setVisible(true);