altosui: Add ascent, descent and landed tabs
[fw/altos] / ao-tools / altosui / AltosDisplayThread.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 public class AltosDisplayThread extends Thread {
32
33         Frame                   parent;
34         IdleThread              idle_thread;
35         AltosVoice              voice;
36         String                  name;
37         AltosFlightReader       reader;
38         int                     crc_errors;
39         AltosFlightDisplay      display;
40
41         synchronized void show(AltosState state, int crc_errors) {
42                 if (state != null)
43                         display.show(state, crc_errors);
44         }
45
46         class IdleThread extends Thread {
47
48                 boolean started;
49                 private AltosState state;
50                 int     reported_landing;
51                 int     report_interval;
52                 long    report_time;
53
54                 public synchronized void report(boolean last) {
55                         if (state == null)
56                                 return;
57
58                         /* reset the landing count once we hear about a new flight */
59                         if (state.state < Altos.ao_flight_drogue)
60                                 reported_landing = 0;
61
62                         /* Shut up once the rocket is on the ground */
63                         if (reported_landing > 2) {
64                                 return;
65                         }
66
67                         /* If the rocket isn't on the pad, then report height */
68                         if (Altos.ao_flight_drogue <= state.state &&
69                             state.state < Altos.ao_flight_landed &&
70                             state.range >= 0)
71                         {
72                                 voice.speak("Height %d, bearing %d, elevation %d, range %d.\n",
73                                             (int) (state.height + 0.5),
74                                             (int) (state.from_pad.bearing + 0.5),
75                                             (int) (state.elevation + 0.5),
76                                             (int) (state.range + 0.5));
77                         } else if (state.state > Altos.ao_flight_pad) {
78                                 voice.speak("%d meters", (int) (state.height + 0.5));
79                         } else {
80                                 reported_landing = 0;
81                         }
82
83                         /* If the rocket is coming down, check to see if it has landed;
84                          * either we've got a landed report or we haven't heard from it in
85                          * a long time
86                          */
87                         if (state.state >= Altos.ao_flight_drogue &&
88                             (last ||
89                              System.currentTimeMillis() - state.report_time >= 15000 ||
90                              state.state == Altos.ao_flight_landed))
91                         {
92                                 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
93                                         voice.speak("rocket landed safely");
94                                 else
95                                         voice.speak("rocket may have crashed");
96                                 if (state.from_pad != null)
97                                         voice.speak("Bearing %d degrees, range %d meters.",
98                                                     (int) (state.from_pad.bearing + 0.5),
99                                                     (int) (state.from_pad.distance + 0.5));
100                                 ++reported_landing;
101                                 if (state.state != Altos.ao_flight_landed) {
102                                         state.state = Altos.ao_flight_landed;
103                                         show(state, 0);
104                                 }
105                         }
106                 }
107
108                 long now () {
109                         return System.currentTimeMillis();
110                 }
111
112                 void set_report_time() {
113                         report_time = now() + report_interval;
114                 }
115
116                 public void run () {
117
118                         reported_landing = 0;
119                         state = null;
120                         report_interval = 10000;
121                         try {
122                                 for (;;) {
123                                         set_report_time();
124                                         for (;;) {
125                                                 voice.drain();
126                                                 synchronized (this) {
127                                                         long    sleep_time = report_time - now();
128                                                         if (sleep_time <= 0)
129                                                                 break;
130                                                         wait(sleep_time);
131                                                 }
132                                         }
133                                         report(false);
134                                 }
135                         } catch (InterruptedException ie) {
136                                 try {
137                                         voice.drain();
138                                 } catch (InterruptedException iie) { }
139                         }
140                 }
141
142                 public synchronized void notice(AltosState new_state, boolean spoken) {
143                         AltosState old_state = state;
144                         state = new_state;
145                         if (!started && state.state > Altos.ao_flight_pad) {
146                                 started = true;
147                                 start();
148                         }
149
150                         if (state.state < Altos.ao_flight_drogue)
151                                 report_interval = 10000;
152                         else
153                                 report_interval = 20000;
154                         if (old_state != null && old_state.state != state.state) {
155                                 report_time = now();
156                                 this.notify();
157                         } else if (spoken)
158                                 set_report_time();
159                 }
160         }
161
162         boolean tell(AltosState state, AltosState old_state) {
163                 boolean ret = false;
164                 if (old_state == null || old_state.state != state.state) {
165                         voice.speak(state.data.state());
166                         if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
167                             state.state > Altos.ao_flight_boost) {
168                                 voice.speak("max speed: %d meters per second.",
169                                             (int) (state.max_speed + 0.5));
170                                 ret = true;
171                         } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
172                                    state.state >= Altos.ao_flight_drogue) {
173                                 voice.speak("max height: %d meters.",
174                                             (int) (state.max_height + 0.5));
175                                 ret = true;
176                         }
177                 }
178                 if (old_state == null || old_state.gps_ready != state.gps_ready) {
179                         if (state.gps_ready) {
180                                 voice.speak("GPS ready");
181                                 ret = true;
182                         }
183                         else if (old_state != null) {
184                                 voice.speak("GPS lost");
185                                 ret = true;
186                         }
187                 }
188                 old_state = state;
189                 return ret;
190         }
191
192         public void run() {
193                 boolean         interrupted = false;
194                 String          line;
195                 AltosState      state = null;
196                 AltosState      old_state = null;
197                 boolean         told;
198
199                 idle_thread = new IdleThread();
200
201                 display.reset();
202                 try {
203                         for (;;) {
204                                 try {
205                                         AltosRecord record = reader.read();
206                                         if (record == null)
207                                                 break;
208                                         old_state = state;
209                                         state = new AltosState(record, state);
210                                         reader.update(state);
211                                         show(state, crc_errors);
212                                         told = tell(state, old_state);
213                                         idle_thread.notice(state, told);
214                                 } catch (ParseException pp) {
215                                         System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
216                                 } catch (AltosCRCException ce) {
217                                         ++crc_errors;
218                                         show(state, crc_errors);
219                                 }
220                         }
221                 } catch (InterruptedException ee) {
222                         interrupted = true;
223                 } catch (IOException ie) {
224                         JOptionPane.showMessageDialog(parent,
225                                                       String.format("Error reading from \"%s\"", name),
226                                                       "Telemetry Read Error",
227                                                       JOptionPane.ERROR_MESSAGE);
228                 } finally {
229                         if (!interrupted)
230                                 idle_thread.report(true);
231                         reader.close(interrupted);
232                         idle_thread.interrupt();
233                         try {
234                                 idle_thread.join();
235                         } catch (InterruptedException ie) {}
236                 }
237         }
238
239         public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
240                 parent = in_parent;
241                 voice = in_voice;
242                 display = in_display;
243                 reader = in_reader;
244         }
245 }