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 public class AltosDisplayThread extends Thread {
34 IdleThread idle_thread;
36 AltosFlightReader reader;
38 AltosFlightDisplay display;
40 void show_internal(AltosState state, int crc_errors) {
42 display.show(state, crc_errors);
45 void show_safely(AltosState in_state, int in_crc_errors) {
46 final AltosState state = in_state;
47 final int crc_errors = in_crc_errors;
48 Runnable r = new Runnable() {
51 show_internal(state, crc_errors);
52 } catch (Exception ex) {
56 SwingUtilities.invokeLater(r);
59 void reading_error_internal() {
60 JOptionPane.showMessageDialog(parent,
61 String.format("Error reading from \"%s\"", reader.name),
62 "Telemetry Read Error",
63 JOptionPane.ERROR_MESSAGE);
66 void reading_error_safely() {
67 Runnable r = new Runnable() {
70 reading_error_internal();
71 } catch (Exception ex) {
75 SwingUtilities.invokeLater(r);
78 class IdleThread extends Thread {
81 private AltosState state;
86 public synchronized void report(boolean last) {
90 /* reset the landing count once we hear about a new flight */
91 if (state.state < Altos.ao_flight_drogue)
94 /* Shut up once the rocket is on the ground */
95 if (reported_landing > 2) {
99 /* If the rocket isn't on the pad, then report height */
100 if (Altos.ao_flight_drogue <= state.state &&
101 state.state < Altos.ao_flight_landed &&
104 voice.speak("Height %d, bearing %s %d, elevation %d, range %d.\n",
105 (int) (state.height + 0.5),
106 state.from_pad.bearing_words(
107 AltosGreatCircle.BEARING_VOICE),
108 (int) (state.from_pad.bearing + 0.5),
109 (int) (state.elevation + 0.5),
110 (int) (state.range + 0.5));
111 } else if (state.state > Altos.ao_flight_pad) {
112 voice.speak("%d meters", (int) (state.height + 0.5));
114 reported_landing = 0;
117 /* If the rocket is coming down, check to see if it has landed;
118 * either we've got a landed report or we haven't heard from it in
121 if (state.state >= Altos.ao_flight_drogue &&
123 System.currentTimeMillis() - state.report_time >= 15000 ||
124 state.state == Altos.ao_flight_landed))
126 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
127 voice.speak("rocket landed safely");
129 voice.speak("rocket may have crashed");
130 if (state.from_pad != null)
131 voice.speak("Bearing %d degrees, range %d meters.",
132 (int) (state.from_pad.bearing + 0.5),
133 (int) (state.from_pad.distance + 0.5));
135 if (state.state != Altos.ao_flight_landed) {
136 state.state = Altos.ao_flight_landed;
137 show_safely(state, 0);
143 return System.currentTimeMillis();
146 void set_report_time() {
147 report_time = now() + report_interval;
156 synchronized (this) {
157 long sleep_time = report_time - now();
165 } catch (InterruptedException ie) {
168 } catch (InterruptedException iie) { }
172 public synchronized void notice(AltosState new_state, boolean spoken) {
173 AltosState old_state = state;
175 if (!started && state.state > Altos.ao_flight_pad) {
180 if (state.state < Altos.ao_flight_drogue)
181 report_interval = 10000;
183 report_interval = 20000;
184 if (old_state != null && old_state.state != state.state) {
191 public IdleThread() {
193 reported_landing = 0;
194 report_interval = 10000;
198 boolean tell(AltosState state, AltosState old_state) {
200 if (old_state == null || old_state.state != state.state) {
201 voice.speak(state.data.state());
202 if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
203 state.state > Altos.ao_flight_boost) {
204 voice.speak("max speed: %d meters per second.",
205 (int) (state.max_speed + 0.5));
207 } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
208 state.state >= Altos.ao_flight_drogue) {
209 voice.speak("max height: %d meters.",
210 (int) (state.max_height + 0.5));
214 if (old_state == null || old_state.gps_ready != state.gps_ready) {
215 if (state.gps_ready) {
216 voice.speak("GPS ready");
219 else if (old_state != null) {
220 voice.speak("GPS lost");
229 boolean interrupted = false;
231 AltosState state = null;
232 AltosState old_state = null;
235 idle_thread = new IdleThread();
240 AltosRecord record = reader.read();
244 state = new AltosState(record, state);
245 reader.update(state);
246 show_safely(state, crc_errors);
247 told = tell(state, old_state);
248 idle_thread.notice(state, told);
249 } catch (ParseException pp) {
250 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
251 } catch (AltosCRCException ce) {
253 show_safely(state, crc_errors);
256 } catch (InterruptedException ee) {
258 } catch (IOException ie) {
259 reading_error_safely();
262 idle_thread.report(true);
263 reader.close(interrupted);
264 idle_thread.interrupt();
267 } catch (InterruptedException ie) {}
271 public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
274 display = in_display;