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;
40 import altosui.AltosFlightStatusTableModel;
41 import altosui.AltosFlightInfoTableModel;
45 public class AltosUI extends JFrame {
46 private int channel = -1;
48 private AltosFlightStatusTableModel flightStatusModel;
49 private JTable flightStatus;
51 static final int info_columns = 3;
53 private AltosFlightInfoTableModel[] flightInfoModel;
54 private JTable[] flightInfo;
55 private AltosSerial serial_line;
56 private AltosLog altos_log;
61 private Font statusFont = new Font("SansSerif", Font.BOLD, 24);
62 private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
63 private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
65 public AltosVoice voice = new AltosVoice();
69 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
70 Object[][] statusData = { { "0", "pad", "-50", "0" } };
72 AltosPreferences.init(this);
74 vbox = Box.createVerticalBox();
77 flightStatusModel = new AltosFlightStatusTableModel();
78 flightStatus = new JTable(flightStatusModel);
79 flightStatus.setFont(statusFont);
80 TableColumnModel tcm = flightStatus.getColumnModel();
81 for (int i = 0; i < flightStatusModel.getColumnCount(); i++) {
82 DefaultTableCellRenderer r = new DefaultTableCellRenderer();
83 r.setFont(statusFont);
84 r.setHorizontalAlignment(SwingConstants.CENTER);
85 tcm.getColumn(i).setCellRenderer(r);
88 FontMetrics statusMetrics = flightStatus.getFontMetrics(statusFont);
89 int statusHeight = (statusMetrics.getHeight() + statusMetrics.getLeading()) * 15 / 10;
90 flightStatus.setRowHeight(statusHeight);
91 flightStatus.setShowGrid(false);
93 vbox.add(flightStatus);
95 hbox = Box.createHorizontalBox();
98 flightInfo = new JTable[3];
99 flightInfoModel = new AltosFlightInfoTableModel[3];
101 FontMetrics infoValueMetrics = flightStatus.getFontMetrics(infoValueFont);
102 int infoHeight = (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 20 / 10;
104 for (int i = 0; i < info_columns; i++) {
105 ibox[i] = Box.createVerticalBox();
106 flightInfoModel[i] = new AltosFlightInfoTableModel();
107 flightInfo[i] = new JTable(flightInfoModel[i]);
108 flightInfo[i].setFont(infoValueFont);
109 flightInfo[i].setRowHeight(infoHeight);
110 flightInfo[i].setShowGrid(true);
111 ibox[i].add(flightInfo[i].getTableHeader());
112 ibox[i].add(flightInfo[i]);
120 serial_line = new AltosSerial();
121 altos_log = new AltosLog(serial_line);
122 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
123 this.setSize(new Dimension (infoValueMetrics.charWidth('0') * 6 * 20,
124 statusHeight * 4 + infoHeight * 17));
126 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
127 addWindowListener(new WindowAdapter() {
129 public void windowClosing(WindowEvent e) {
133 voice.speak("Rocket flight monitor ready.");
136 public void info_reset() {
137 for (int i = 0; i < info_columns; i++)
138 flightInfoModel[i].resetRow();
141 public void info_add_row(int col, String name, String value) {
142 flightInfoModel[col].addRow(name, value);
145 public void info_add_row(int col, String name, String format, Object... parameters) {
146 flightInfoModel[col].addRow(name, String.format(format, parameters));
149 public void info_add_deg(int col, String name, double v, int pos, int neg) {
155 double deg = Math.floor(v);
156 double min = (v - deg) * 60;
158 flightInfoModel[col].addRow(name, String.format("%3.0f°%08.5f'", deg, min));
161 public void info_finish() {
162 for (int i = 0; i < info_columns; i++)
163 flightInfoModel[i].finish();
166 public void show(AltosState state) {
167 flightStatusModel.set(state);
171 info_add_row(0, "Ground state", "%s", "ready");
173 info_add_row(0, "Ground state", "wait (%d)",
175 info_add_row(0, "Rocket state", "%s", state.data.state);
176 info_add_row(0, "Callsign", "%s", state.data.callsign);
177 info_add_row(0, "Rocket serial", "%6d", state.data.serial);
178 info_add_row(0, "Rocket flight", "%6d", state.data.flight);
180 info_add_row(0, "RSSI", "%6d dBm", state.data.rssi);
181 info_add_row(0, "Height", "%6.0f m", state.height);
182 info_add_row(0, "Max height", "%6.0f m", state.max_height);
183 info_add_row(0, "Acceleration", "%8.1f m/s²", state.acceleration);
184 info_add_row(0, "Max acceleration", "%8.1f m/s²", state.max_acceleration);
185 info_add_row(0, "Speed", "%8.1f m/s", state.ascent ? state.speed : state.baro_speed);
186 info_add_row(0, "Max Speed", "%8.1f m/s", state.max_speed);
187 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
188 info_add_row(0, "Battery", "%9.2f V", state.battery);
189 info_add_row(0, "Drogue", "%9.2f V", state.drogue_sense);
190 info_add_row(0, "Main", "%9.2f V", state.main_sense);
191 info_add_row(0, "Pad altitude", "%6.0f m", state.ground_altitude);
192 if (state.gps == null) {
193 info_add_row(1, "GPS", "not available");
195 if (state.data.gps.gps_locked)
196 info_add_row(1, "GPS", " locked");
197 else if (state.data.gps.gps_connected)
198 info_add_row(1, "GPS", " unlocked");
200 info_add_row(1, "GPS", " missing");
201 info_add_row(1, "Satellites", "%6d", state.data.gps.nsat);
202 info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
203 info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
204 info_add_row(1, "GPS altitude", "%6d", state.gps.alt);
205 info_add_row(1, "GPS height", "%6.0f", state.gps_height);
207 /* The SkyTraq GPS doesn't report these values */
209 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
210 state.gps.ground_speed,
212 info_add_row(1, "GPS climb rate", "%8.1f m/s",
213 state.gps.climb_rate);
214 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
215 state.gps.h_error, state.gps.v_error);
217 info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
219 if (state.npad > 0) {
220 if (state.from_pad != null) {
221 info_add_row(1, "Distance from pad", "%6.0f m", state.from_pad.distance);
222 info_add_row(1, "Direction from pad", "%6.0f°", state.from_pad.bearing);
224 info_add_row(1, "Distance from pad", "unknown");
225 info_add_row(1, "Direction from pad", "unknown");
227 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
228 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
229 info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
231 info_add_row(1, "GPS date", "%04d-%02d-%02d",
235 info_add_row(1, "GPS time", " %02d:%02d:%02d",
242 if (state.gps.cc_gps_sat == null)
243 info_add_row(2, "Satellites Visible", "%4d", 0);
245 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
246 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
247 info_add_row(2, "Satellite id,C/N0",
249 state.gps.cc_gps_sat[c].svid,
250 state.gps.cc_gps_sat[c].c_n0);
257 class IdleThread extends Thread {
259 private AltosState state;
260 int reported_landing;
262 public void report(boolean last) {
266 /* reset the landing count once we hear about a new flight */
267 if (state.state < Altos.ao_flight_drogue)
268 reported_landing = 0;
270 /* Shut up once the rocket is on the ground */
271 if (reported_landing > 2) {
275 /* If the rocket isn't on the pad, then report height */
276 if (state.state > Altos.ao_flight_pad) {
277 voice.speak("%d meters", (int) (state.height + 0.5));
279 reported_landing = 0;
282 /* If the rocket is coming down, check to see if it has landed;
283 * either we've got a landed report or we haven't heard from it in
288 System.currentTimeMillis() - state.report_time >= 15000 ||
289 state.state == Altos.ao_flight_landed))
291 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
292 voice.speak("rocket landed safely");
294 voice.speak("rocket may have crashed");
295 if (state.from_pad != null)
296 voice.speak("bearing %d degrees, range %d meters",
297 (int) (state.from_pad.bearing + 0.5),
298 (int) (state.from_pad.distance + 0.5));
305 reported_landing = 0;
312 } catch (InterruptedException ie) {
316 public void notice(AltosState new_state) {
321 private void tell(AltosState state, AltosState old_state) {
322 if (old_state == null || old_state.state != state.state) {
323 voice.speak(state.data.state);
324 if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
325 state.state > Altos.ao_flight_boost) {
326 voice.speak("max speed: %d meters per second.",
327 (int) (state.max_speed + 0.5));
328 } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
329 state.state >= Altos.ao_flight_drogue) {
330 voice.speak("max height: %d meters.",
331 (int) (state.max_height + 0.5));
334 if (old_state == null || old_state.gps_ready != state.gps_ready) {
336 voice.speak("GPS ready");
337 else if (old_state != null)
338 voice.speak("GPS lost");
343 class DisplayThread extends Thread {
344 IdleThread idle_thread;
346 String read() throws InterruptedException { return null; }
350 void update(AltosState state) throws InterruptedException { }
354 AltosState state = null;
355 AltosState old_state = null;
357 idle_thread = new IdleThread();
363 while ((line = read()) != null) {
365 AltosTelemetry t = new AltosTelemetry(line);
367 state = new AltosState(t, state);
370 tell(state, old_state);
371 idle_thread.notice(state);
372 } catch (ParseException pp) {
373 System.out.printf("Parse error on %s\n", line);
374 System.out.println("exception " + pp);
377 } catch (InterruptedException ee) {
380 idle_thread.interrupt();
384 public void report() {
385 if (idle_thread != null)
386 idle_thread.report(true);
390 class DeviceThread extends DisplayThread {
392 LinkedBlockingQueue<String> telem;
394 String read() throws InterruptedException {
400 serial.remove_monitor(telem);
403 public DeviceThread(AltosSerial s) {
405 telem = new LinkedBlockingQueue<String>();
406 serial.add_monitor(telem);
410 private void ConnectToDevice() {
411 altos_device device = AltosDeviceDialog.show(AltosUI.this, "TeleDongle");
413 if (device != null) {
415 serial_line.open(device);
416 DeviceThread thread = new DeviceThread(serial_line);
417 serial_line.set_channel(AltosPreferences.channel());
419 } catch (FileNotFoundException ee) {
420 JOptionPane.showMessageDialog(AltosUI.this,
421 String.format("Cannot open device \"%s\"",
423 "Cannot open target device",
424 JOptionPane.ERROR_MESSAGE);
425 } catch (IOException ee) {
426 JOptionPane.showMessageDialog(AltosUI.this,
429 JOptionPane.ERROR_MESSAGE);
434 void DisconnectFromDevice () {
438 String readline(FileInputStream s) throws IOException {
442 while ((c = s.read()) != -1) {
448 line = line + (char) c;
454 * Open an existing telemetry file and replay it in realtime
457 class ReplayThread extends DisplayThread {
458 FileInputStream replay;
461 ReplayThread(FileInputStream in, String name) {
468 return readline(replay);
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 > AltosTelemetry.ao_flight_pad)
489 Thread.sleep((int) (Math.min(state.time_change,10) * 1000));
493 Thread display_thread;
495 private void stop_display() {
496 if (display_thread != null && display_thread.isAlive())
497 display_thread.interrupt();
498 display_thread = null;
501 private void run_display(Thread thread) {
503 display_thread = thread;
504 display_thread.start();
508 * Replay a flight from telemetry data
510 private void Replay() {
511 JFileChooser logfile_chooser = new JFileChooser();
513 logfile_chooser.setDialogTitle("Select Telemetry File");
514 logfile_chooser.setFileFilter(new FileNameExtensionFilter("Telemetry file", "telem"));
515 logfile_chooser.setCurrentDirectory(AltosPreferences.logdir());
516 int returnVal = logfile_chooser.showOpenDialog(AltosUI.this);
518 if (returnVal == JFileChooser.APPROVE_OPTION) {
519 File file = logfile_chooser.getSelectedFile();
521 System.out.println("No file selected?");
522 String filename = file.getName();
524 FileInputStream replay = new FileInputStream(file);
525 ReplayThread thread = new ReplayThread(replay, filename);
527 } catch (FileNotFoundException ee) {
528 JOptionPane.showMessageDialog(AltosUI.this,
530 "Cannot open telemetry file",
531 JOptionPane.ERROR_MESSAGE);
536 /* Connect to TeleMetrum, either directly or through
537 * a TeleDongle over the packet link
539 private void SaveFlightData() {
540 new AltosEepromDownload(AltosUI.this);
543 /* Create the AltosUI menus
545 private void createMenu() {
546 JMenuBar menubar = new JMenuBar();
549 JRadioButtonMenuItem radioitem;
553 menu = new JMenu("File");
554 menu.setMnemonic(KeyEvent.VK_F);
557 item = new JMenuItem("Quit",KeyEvent.VK_Q);
558 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
559 ActionEvent.CTRL_MASK));
560 item.addActionListener(new ActionListener() {
561 public void actionPerformed(ActionEvent e) {
570 menu = new JMenu("Device");
571 menu.setMnemonic(KeyEvent.VK_D);
574 item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
575 item.addActionListener(new ActionListener() {
576 public void actionPerformed(ActionEvent e) {
582 item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
583 item.addActionListener(new ActionListener() {
584 public void actionPerformed(ActionEvent e) {
585 DisconnectFromDevice();
592 item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
593 item.addActionListener(new ActionListener() {
594 public void actionPerformed(ActionEvent e) {
600 item = new JMenuItem("Replay",KeyEvent.VK_R);
601 item.addActionListener(new ActionListener() {
602 public void actionPerformed(ActionEvent e) {
610 menu = new JMenu("Log");
611 menu.setMnemonic(KeyEvent.VK_L);
614 item = new JMenuItem("New Log",KeyEvent.VK_N);
615 item.addActionListener(new ActionListener() {
616 public void actionPerformed(ActionEvent e) {
621 item = new JMenuItem("Configure Log",KeyEvent.VK_C);
622 item.addActionListener(new ActionListener() {
623 public void actionPerformed(ActionEvent e) {
624 AltosPreferences.ConfigureLog();
631 menu = new JMenu("Voice", true);
632 menu.setMnemonic(KeyEvent.VK_V);
635 radioitem = new JRadioButtonMenuItem("Enable Voice", AltosPreferences.voice());
636 radioitem.addActionListener(new ActionListener() {
637 public void actionPerformed(ActionEvent e) {
638 JRadioButtonMenuItem item = (JRadioButtonMenuItem) e.getSource();
639 boolean enabled = item.isSelected();
640 AltosPreferences.set_voice(enabled);
642 voice.speak_always("Enable voice.");
644 voice.speak_always("Disable voice.");
648 item = new JMenuItem("Test Voice",KeyEvent.VK_T);
649 item.addActionListener(new ActionListener() {
650 public void actionPerformed(ActionEvent e) {
651 voice.speak("That's one small step for man; one giant leap for mankind.");
659 menu = new JMenu("Channel", true);
660 menu.setMnemonic(KeyEvent.VK_C);
662 ButtonGroup group = new ButtonGroup();
664 for (int c = 0; c <= 9; c++) {
665 radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
667 c == AltosPreferences.channel());
668 radioitem.setActionCommand(String.format("%d", c));
669 radioitem.addActionListener(new ActionListener() {
670 public void actionPerformed(ActionEvent e) {
671 int new_channel = Integer.parseInt(e.getActionCommand());
672 AltosPreferences.set_channel(new_channel);
673 serial_line.set_channel(new_channel);
677 group.add(radioitem);
681 this.setJMenuBar(menubar);
684 public static void main(final String[] args) {
685 AltosUI altosui = new AltosUI();
686 altosui.setVisible(true);