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.AltosFlashUI;
45 import altosui.AltosLogfileChooser;
49 public class AltosUI extends JFrame {
50 private int channel = -1;
52 private AltosFlightStatusTableModel flightStatusModel;
53 private JTable flightStatus;
55 static final int info_columns = 3;
57 private AltosFlightInfoTableModel[] flightInfoModel;
58 private JTable[] flightInfo;
59 private AltosSerial serial_line;
60 private AltosLog altos_log;
65 private Font statusFont = new Font("SansSerif", Font.BOLD, 24);
66 private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
67 private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
69 public AltosVoice voice = new AltosVoice();
73 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
74 Object[][] statusData = { { "0", "pad", "-50", "0" } };
76 AltosPreferences.init(this);
78 vbox = Box.createVerticalBox();
81 flightStatusModel = new AltosFlightStatusTableModel();
82 flightStatus = new JTable(flightStatusModel);
83 flightStatus.setFont(statusFont);
84 TableColumnModel tcm = flightStatus.getColumnModel();
85 for (int i = 0; i < flightStatusModel.getColumnCount(); i++) {
86 DefaultTableCellRenderer r = new DefaultTableCellRenderer();
87 r.setFont(statusFont);
88 r.setHorizontalAlignment(SwingConstants.CENTER);
89 tcm.getColumn(i).setCellRenderer(r);
92 FontMetrics statusMetrics = flightStatus.getFontMetrics(statusFont);
93 int statusHeight = (statusMetrics.getHeight() + statusMetrics.getLeading()) * 15 / 10;
94 flightStatus.setRowHeight(statusHeight);
95 flightStatus.setShowGrid(false);
97 vbox.add(flightStatus);
99 hbox = Box.createHorizontalBox();
102 flightInfo = new JTable[3];
103 flightInfoModel = new AltosFlightInfoTableModel[3];
105 FontMetrics infoValueMetrics = flightStatus.getFontMetrics(infoValueFont);
106 int infoHeight = (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 20 / 10;
108 for (int i = 0; i < info_columns; i++) {
109 ibox[i] = Box.createVerticalBox();
110 flightInfoModel[i] = new AltosFlightInfoTableModel();
111 flightInfo[i] = new JTable(flightInfoModel[i]);
112 flightInfo[i].setFont(infoValueFont);
113 flightInfo[i].setRowHeight(infoHeight);
114 flightInfo[i].setShowGrid(true);
115 ibox[i].add(flightInfo[i].getTableHeader());
116 ibox[i].add(flightInfo[i]);
124 serial_line = new AltosSerial();
125 altos_log = new AltosLog(serial_line);
126 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
127 this.setSize(new Dimension (infoValueMetrics.charWidth('0') * 6 * 20,
128 statusHeight * 4 + infoHeight * 17));
130 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
131 addWindowListener(new WindowAdapter() {
133 public void windowClosing(WindowEvent e) {
137 voice.speak("Rocket flight monitor ready.");
140 public void info_reset() {
141 for (int i = 0; i < info_columns; i++)
142 flightInfoModel[i].resetRow();
145 public void info_add_row(int col, String name, String value) {
146 flightInfoModel[col].addRow(name, value);
149 public void info_add_row(int col, String name, String format, Object... parameters) {
150 flightInfoModel[col].addRow(name, String.format(format, parameters));
153 public void info_add_deg(int col, String name, double v, int pos, int neg) {
159 double deg = Math.floor(v);
160 double min = (v - deg) * 60;
162 flightInfoModel[col].addRow(name, String.format("%3.0f°%08.5f'", deg, min));
165 public void info_finish() {
166 for (int i = 0; i < info_columns; i++)
167 flightInfoModel[i].finish();
170 public void show(AltosState state) {
171 flightStatusModel.set(state);
175 info_add_row(0, "Ground state", "%s", "ready");
177 info_add_row(0, "Ground state", "wait (%d)",
179 info_add_row(0, "Rocket state", "%s", state.data.state());
180 info_add_row(0, "Callsign", "%s", state.data.callsign);
181 info_add_row(0, "Rocket serial", "%6d", state.data.serial);
182 info_add_row(0, "Rocket flight", "%6d", state.data.flight);
184 info_add_row(0, "RSSI", "%6d dBm", state.data.rssi);
185 info_add_row(0, "Height", "%6.0f m", state.height);
186 info_add_row(0, "Max height", "%6.0f m", state.max_height);
187 info_add_row(0, "Acceleration", "%8.1f m/s²", state.acceleration);
188 info_add_row(0, "Max acceleration", "%8.1f m/s²", state.max_acceleration);
189 info_add_row(0, "Speed", "%8.1f m/s", state.ascent ? state.speed : state.baro_speed);
190 info_add_row(0, "Max Speed", "%8.1f m/s", state.max_speed);
191 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
192 info_add_row(0, "Battery", "%9.2f V", state.battery);
193 info_add_row(0, "Drogue", "%9.2f V", state.drogue_sense);
194 info_add_row(0, "Main", "%9.2f V", state.main_sense);
195 info_add_row(0, "Pad altitude", "%6.0f m", state.ground_altitude);
196 if (state.gps == null) {
197 info_add_row(1, "GPS", "not available");
199 if (state.data.gps.locked)
200 info_add_row(1, "GPS", " locked");
201 else if (state.data.gps.connected)
202 info_add_row(1, "GPS", " unlocked");
204 info_add_row(1, "GPS", " missing");
205 info_add_row(1, "Satellites", "%6d", state.data.gps.nsat);
206 info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
207 info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
208 info_add_row(1, "GPS altitude", "%6d", state.gps.alt);
209 info_add_row(1, "GPS height", "%6.0f", state.gps_height);
211 /* The SkyTraq GPS doesn't report these values */
213 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
214 state.gps.ground_speed,
216 info_add_row(1, "GPS climb rate", "%8.1f m/s",
217 state.gps.climb_rate);
218 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
219 state.gps.h_error, state.gps.v_error);
221 info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
223 if (state.npad > 0) {
224 if (state.from_pad != null) {
225 info_add_row(1, "Distance from pad", "%6.0f m", state.from_pad.distance);
226 info_add_row(1, "Direction from pad", "%6.0f°", state.from_pad.bearing);
228 info_add_row(1, "Distance from pad", "unknown");
229 info_add_row(1, "Direction from pad", "unknown");
231 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
232 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
233 info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
235 info_add_row(1, "GPS date", "%04d-%02d-%02d",
239 info_add_row(1, "GPS time", " %02d:%02d:%02d",
246 if (state.gps.cc_gps_sat == null)
247 info_add_row(2, "Satellites Visible", "%4d", 0);
249 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
250 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
251 info_add_row(2, "Satellite id,C/N0",
253 state.gps.cc_gps_sat[c].svid,
254 state.gps.cc_gps_sat[c].c_n0);
261 class IdleThread extends Thread {
263 private AltosState state;
264 int reported_landing;
266 public void report(boolean last) {
270 /* reset the landing count once we hear about a new flight */
271 if (state.state < Altos.ao_flight_drogue)
272 reported_landing = 0;
274 /* Shut up once the rocket is on the ground */
275 if (reported_landing > 2) {
279 /* If the rocket isn't on the pad, then report height */
280 if (state.state > Altos.ao_flight_pad) {
281 voice.speak("%d meters", (int) (state.height + 0.5));
283 reported_landing = 0;
286 /* If the rocket is coming down, check to see if it has landed;
287 * either we've got a landed report or we haven't heard from it in
292 System.currentTimeMillis() - state.report_time >= 15000 ||
293 state.state == Altos.ao_flight_landed))
295 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
296 voice.speak("rocket landed safely");
298 voice.speak("rocket may have crashed");
299 if (state.from_pad != null)
300 voice.speak("bearing %d degrees, range %d meters",
301 (int) (state.from_pad.bearing + 0.5),
302 (int) (state.from_pad.distance + 0.5));
309 reported_landing = 0;
316 } catch (InterruptedException ie) {
320 public void notice(AltosState new_state) {
325 private void tell(AltosState state, AltosState old_state) {
326 if (old_state == null || old_state.state != state.state) {
327 voice.speak(state.data.state());
328 if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
329 state.state > Altos.ao_flight_boost) {
330 voice.speak("max speed: %d meters per second.",
331 (int) (state.max_speed + 0.5));
332 } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
333 state.state >= Altos.ao_flight_drogue) {
334 voice.speak("max height: %d meters.",
335 (int) (state.max_height + 0.5));
338 if (old_state == null || old_state.gps_ready != state.gps_ready) {
340 voice.speak("GPS ready");
341 else if (old_state != null)
342 voice.speak("GPS lost");
347 class DisplayThread extends Thread {
348 IdleThread idle_thread;
352 AltosRecord read() throws InterruptedException, ParseException { return null; }
356 void update(AltosState state) throws InterruptedException { }
360 AltosState state = null;
361 AltosState old_state = null;
363 idle_thread = new IdleThread();
371 AltosRecord record = read();
375 state = new AltosState(record, state);
378 tell(state, old_state);
379 idle_thread.notice(state);
380 } catch (ParseException pp) {
381 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
384 } catch (InterruptedException ee) {
387 idle_thread.interrupt();
391 public void report() {
392 if (idle_thread != null)
393 idle_thread.report(true);
397 class DeviceThread extends DisplayThread {
399 LinkedBlockingQueue<String> telem;
401 AltosRecord read() throws InterruptedException, ParseException {
402 return new AltosTelemetry(telem.take());
407 serial.remove_monitor(telem);
410 public DeviceThread(AltosSerial s) {
412 telem = new LinkedBlockingQueue<String>();
413 serial.add_monitor(telem);
418 private void ConnectToDevice() {
419 AltosDevice device = AltosDeviceDialog.show(AltosUI.this, AltosDevice.BaseStation);
421 if (device != null) {
423 serial_line.open(device);
424 DeviceThread thread = new DeviceThread(serial_line);
425 serial_line.set_channel(AltosPreferences.channel());
426 serial_line.set_callsign(AltosPreferences.callsign());
428 } catch (FileNotFoundException ee) {
429 JOptionPane.showMessageDialog(AltosUI.this,
430 String.format("Cannot open device \"%s\"",
432 "Cannot open target device",
433 JOptionPane.ERROR_MESSAGE);
434 } catch (IOException ee) {
435 JOptionPane.showMessageDialog(AltosUI.this,
438 JOptionPane.ERROR_MESSAGE);
443 void DisconnectFromDevice () {
447 void ConfigureCallsign() {
449 result = JOptionPane.showInputDialog(AltosUI.this,
450 "Configure Callsign",
451 AltosPreferences.callsign());
452 if (result != null) {
453 AltosPreferences.set_callsign(result);
454 if (serial_line != null)
455 serial_line.set_callsign(result);
459 void ConfigureTeleMetrum() {
460 new AltosConfig(AltosUI.this);
464 new AltosFlashUI(AltosUI.this);
468 * Open an existing telemetry file and replay it in realtime
471 class ReplayThread extends DisplayThread {
475 public AltosRecord read() {
477 return reader.read();
478 } catch (IOException ie) {
479 JOptionPane.showMessageDialog(AltosUI.this,
482 JOptionPane.ERROR_MESSAGE);
483 } catch (ParseException pe) {
488 public void close () {
492 public ReplayThread(AltosReader in_reader, String in_name) {
495 void update(AltosState state) throws InterruptedException {
496 /* Make it run in realtime after the rocket leaves the pad */
497 if (state.state > Altos.ao_flight_pad)
498 Thread.sleep((int) (Math.min(state.time_change,10) * 1000));
502 class ReplayTelemetryThread extends ReplayThread {
503 ReplayTelemetryThread(FileInputStream in, String in_name) {
504 super(new AltosTelemetryReader(in), in_name);
509 class ReplayEepromThread extends ReplayThread {
510 ReplayEepromThread(FileInputStream in, String in_name) {
511 super(new AltosEepromReader(in), in_name);
515 Thread display_thread;
517 private void stop_display() {
518 if (display_thread != null && display_thread.isAlive())
519 display_thread.interrupt();
520 display_thread = null;
523 private void run_display(Thread thread) {
525 display_thread = thread;
526 display_thread.start();
530 * Replay a flight from telemetry data
532 private void Replay() {
533 AltosLogfileChooser chooser = new AltosLogfileChooser(
536 File file = chooser.runDialog();
539 String filename = file.getName();
541 FileInputStream replay = new FileInputStream(file);
542 DisplayThread thread;
543 if (filename.endsWith("eeprom"))
544 thread = new ReplayEepromThread(replay, filename);
546 thread = new ReplayTelemetryThread(replay, filename);
548 } catch (FileNotFoundException ee) {
549 JOptionPane.showMessageDialog(AltosUI.this,
551 "Cannot open telemetry file",
552 JOptionPane.ERROR_MESSAGE);
557 /* Connect to TeleMetrum, either directly or through
558 * a TeleDongle over the packet link
560 private void SaveFlightData() {
561 new AltosEepromDownload(AltosUI.this);
564 /* Create the AltosUI menus
566 private void createMenu() {
567 JMenuBar menubar = new JMenuBar();
570 JRadioButtonMenuItem radioitem;
574 menu = new JMenu("File");
575 menu.setMnemonic(KeyEvent.VK_F);
578 item = new JMenuItem("Replay File",KeyEvent.VK_R);
579 item.addActionListener(new ActionListener() {
580 public void actionPerformed(ActionEvent e) {
586 item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
587 item.addActionListener(new ActionListener() {
588 public void actionPerformed(ActionEvent e) {
594 item = new JMenuItem("Flash Image",KeyEvent.VK_F);
595 item.addActionListener(new ActionListener() {
596 public void actionPerformed(ActionEvent e) {
602 item = new JMenuItem("Quit",KeyEvent.VK_Q);
603 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
604 ActionEvent.CTRL_MASK));
605 item.addActionListener(new ActionListener() {
606 public void actionPerformed(ActionEvent e) {
615 menu = new JMenu("Device");
616 menu.setMnemonic(KeyEvent.VK_D);
619 item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
620 item.addActionListener(new ActionListener() {
621 public void actionPerformed(ActionEvent e) {
627 item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
628 item.addActionListener(new ActionListener() {
629 public void actionPerformed(ActionEvent e) {
630 DisconnectFromDevice();
637 item = new JMenuItem("Set Callsign",KeyEvent.VK_S);
638 item.addActionListener(new ActionListener() {
639 public void actionPerformed(ActionEvent e) {
646 item = new JMenuItem("Configure TeleMetrum device",KeyEvent.VK_T);
647 item.addActionListener(new ActionListener() {
648 public void actionPerformed(ActionEvent e) {
649 ConfigureTeleMetrum();
657 menu = new JMenu("Log");
658 menu.setMnemonic(KeyEvent.VK_L);
661 item = new JMenuItem("New Log",KeyEvent.VK_N);
662 item.addActionListener(new ActionListener() {
663 public void actionPerformed(ActionEvent e) {
668 item = new JMenuItem("Configure Log",KeyEvent.VK_C);
669 item.addActionListener(new ActionListener() {
670 public void actionPerformed(ActionEvent e) {
671 AltosPreferences.ConfigureLog();
678 menu = new JMenu("Voice", true);
679 menu.setMnemonic(KeyEvent.VK_V);
682 radioitem = new JRadioButtonMenuItem("Enable Voice", AltosPreferences.voice());
683 radioitem.addActionListener(new ActionListener() {
684 public void actionPerformed(ActionEvent e) {
685 JRadioButtonMenuItem item = (JRadioButtonMenuItem) e.getSource();
686 boolean enabled = item.isSelected();
687 AltosPreferences.set_voice(enabled);
689 voice.speak_always("Enable voice.");
691 voice.speak_always("Disable voice.");
695 item = new JMenuItem("Test Voice",KeyEvent.VK_T);
696 item.addActionListener(new ActionListener() {
697 public void actionPerformed(ActionEvent e) {
698 voice.speak("That's one small step for man; one giant leap for mankind.");
706 menu = new AltosChannelMenu(AltosPreferences.channel());
707 menu.addActionListener(new ActionListener() {
708 public void actionPerformed(ActionEvent e) {
709 int new_channel = Integer.parseInt(e.getActionCommand());
710 AltosPreferences.set_channel(new_channel);
711 serial_line.set_channel(new_channel);
714 menu.setMnemonic(KeyEvent.VK_C);
718 this.setJMenuBar(menubar);
721 public static void main(final String[] args) {
722 AltosUI altosui = new AltosUI();
723 altosui.setVisible(true);