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.
18 package org.altusmetrum.AltosLib;
23 import java.util.prefs.*;
24 import java.util.concurrent.*;
35 public AltosSensorTM(AltosLink link) throws InterruptedException, TimeoutException {
38 String line = link.get_reply_no_dialog(5000);
40 throw new TimeoutException();
42 if (!line.startsWith("tick:"))
44 String[] items = line.split("\\s+");
45 for (int i = 0; i < items.length;) {
46 if (items[i].equals("tick:")) {
47 tick = Integer.parseInt(items[i+1]);
51 if (items[i].equals("accel:")) {
52 accel = Integer.parseInt(items[i+1]);
56 if (items[i].equals("pres:")) {
57 pres = Integer.parseInt(items[i+1]);
61 if (items[i].equals("temp:")) {
62 temp = Integer.parseInt(items[i+1]);
66 if (items[i].equals("batt:")) {
67 batt = Integer.parseInt(items[i+1]);
71 if (items[i].equals("drogue:")) {
72 drogue = Integer.parseInt(items[i+1]);
76 if (items[i].equals("main:")) {
77 main = Integer.parseInt(items[i+1]);
96 public AltosSensorMM(AltosLink link) throws InterruptedException, TimeoutException {
99 String line = link.get_reply_no_dialog(5000);
101 throw new TimeoutException();
103 if (!line.startsWith("tick:"))
105 String[] items = line.split("\\s+");
107 for (int i = 0; i < items.length;) {
108 if (items[i].equals("tick:")) {
109 tick = Integer.parseInt(items[i+1]);
113 if (items[i].equals("0:")) {
114 sense[0] = Integer.parseInt(items[i+1]);
118 if (items[i].equals("1:")) {
119 sense[1] = Integer.parseInt(items[i+1]);
123 if (items[i].equals("2:")) {
124 sense[2] = Integer.parseInt(items[i+1]);
128 if (items[i].equals("3:")) {
129 sense[3] = Integer.parseInt(items[i+1]);
133 if (items[i].equals("4:")) {
134 sense[4] = Integer.parseInt(items[i+1]);
138 if (items[i].equals("5:")) {
139 sense[5] = Integer.parseInt(items[i+1]);
143 if (items[i].equals("6:")) {
144 v_batt = Integer.parseInt(items[i+1]);
148 if (items[i].equals("7:")) {
149 v_pyro = Integer.parseInt(items[i+1]);
153 if (items[i].equals("8:")) {
154 accel = Integer.parseInt(items[i+1]);
158 if (items[i].equals("9:")) {
159 accel_ref = Integer.parseInt(items[i+1]);
170 class AltosIMUQuery extends AltosIMU {
172 public AltosIMUQuery (AltosLink link) throws InterruptedException, TimeoutException {
175 String line = link.get_reply_no_dialog(5000);
177 throw new TimeoutException();
179 if (!line.startsWith("Accel:"))
181 String[] items = line.split("\\s+");
182 if (items.length >= 8) {
183 accel_x = Integer.parseInt(items[1]);
184 accel_y = Integer.parseInt(items[2]);
185 accel_z = Integer.parseInt(items[3]);
186 gyro_x = Integer.parseInt(items[5]);
187 gyro_y = Integer.parseInt(items[6]);
188 gyro_z = Integer.parseInt(items[7]);
195 class AltosMs5607Query extends AltosMs5607 {
196 public AltosMs5607Query (AltosLink link) throws InterruptedException, TimeoutException {
197 link.printf("v\nB\n");
199 String line = link.get_reply_no_dialog(5000);
201 throw new TimeoutException();
203 String[] items = line.split("\\s+");
204 if (line.startsWith("Pressure:")) {
205 if (items.length >= 2)
206 raw_pres = Integer.parseInt(items[1]);
207 } else if (line.startsWith("Temperature:")) {
208 if (items.length >= 2)
209 raw_temp = Integer.parseInt(items[1]);
210 } else if (line.startsWith("ms5607 reserved:")) {
211 if (items.length >= 3)
212 reserved = Integer.parseInt(items[2]);
213 } else if (line.startsWith("ms5607 sens:")) {
214 if (items.length >= 3)
215 sens = Integer.parseInt(items[2]);
216 } else if (line.startsWith("ms5607 off:")) {
217 if (items.length >= 3)
218 off = Integer.parseInt(items[2]);
219 } else if (line.startsWith("ms5607 tcs:")) {
220 if (items.length >= 3)
221 tcs = Integer.parseInt(items[2]);
222 } else if (line.startsWith("ms5607 tco:")) {
223 if (items.length >= 3)
224 tco = Integer.parseInt(items[2]);
225 } else if (line.startsWith("ms5607 tref:")) {
226 if (items.length >= 3)
227 tref = Integer.parseInt(items[2]);
228 } else if (line.startsWith("ms5607 tempsens:")) {
229 if (items.length >= 3)
230 tempsens = Integer.parseInt(items[2]);
231 } else if (line.startsWith("ms5607 crc:")) {
232 if (items.length >= 3)
233 crc = Integer.parseInt(items[2]);
234 } else if (line.startsWith("Altitude"))
241 class AltosGPSQuery extends AltosGPS {
242 public AltosGPSQuery (AltosLink link, AltosConfigData config_data)
243 throws TimeoutException, InterruptedException {
244 boolean says_done = config_data.compare_version("1.0") >= 0;
247 String line = link.get_reply_no_dialog(5000);
249 throw new TimeoutException();
250 String[] bits = line.split("\\s+");
251 if (bits.length == 0)
253 if (line.startsWith("Date:")) {
256 String[] d = bits[1].split(":");
259 year = Integer.parseInt(d[0]) + 2000;
260 month = Integer.parseInt(d[1]);
261 day = Integer.parseInt(d[2]);
264 if (line.startsWith("Time:")) {
267 String[] d = bits[1].split("/");
270 hour = Integer.parseInt(d[0]);
271 minute = Integer.parseInt(d[1]);
272 second = Integer.parseInt(d[2]);
275 if (line.startsWith("Lat/Lon:")) {
278 lat = Integer.parseInt(bits[1]) * 1.0e-7;
279 lon = Integer.parseInt(bits[2]) * 1.0e-7;
282 if (line.startsWith("Alt:")) {
285 alt = Integer.parseInt(bits[1]);
288 if (line.startsWith("Flags:")) {
291 int status = Integer.decode(bits[1]);
292 connected = (status & AltosLib.AO_GPS_RUNNING) != 0;
293 locked = (status & AltosLib.AO_GPS_VALID) != 0;
298 if (line.startsWith("Sats:")) {
301 nsat = Integer.parseInt(bits[1]);
302 cc_gps_sat = new AltosGPSSat[nsat];
303 for (int i = 0; i < nsat; i++) {
304 int svid = Integer.parseInt(bits[2+i*2]);
305 int cc_n0 = Integer.parseInt(bits[3+i*2]);
306 cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
309 if (line.startsWith("done"))
311 if (line.startsWith("Syntax error"))
317 public class AltosIdleMonitor extends Thread {
319 AltosIdleMonitorListener listener;
323 AltosState previous_state;
324 AltosConfigData config_data;
327 int AltosRSSI() throws TimeoutException, InterruptedException {
329 String line = link.get_reply_no_dialog(5000);
331 throw new TimeoutException();
332 String[] items = line.split("\\s+");
333 if (items.length < 2)
335 if (!items[0].equals("RSSI:"))
337 int rssi = Integer.parseInt(items[1]);
341 void update_state() throws InterruptedException, TimeoutException {
347 link.set_radio_frequency(frequency);
351 config_data = new AltosConfigData(link);
352 if (config_data.product.startsWith("TeleMetrum")) {
353 AltosRecordTM record_tm = new AltosRecordTM();
354 AltosSensorTM sensor = new AltosSensorTM(link);
355 record_tm.accel = sensor.accel;
356 record_tm.pres = sensor.pres;
357 record_tm.batt = sensor.batt;
358 record_tm.temp = sensor.temp;
359 record_tm.drogue = sensor.drogue;
360 record_tm.main = sensor.main;
361 record_tm.ground_accel = record_tm.accel;
362 record_tm.ground_pres = record_tm.pres;
363 record_tm.accel_plus_g = config_data.accel_cal_plus;
364 record_tm.accel_minus_g = config_data.accel_cal_minus;
365 record_tm.tick = sensor.tick;
367 } else if (config_data.product.startsWith("MegaMetrum")) {
368 AltosRecordMM record_mm = new AltosRecordMM();
369 AltosSensorMM sensor = new AltosSensorMM(link);
370 AltosMs5607 ms5607 = new AltosMs5607Query(link);
371 AltosIMU imu = new AltosIMUQuery(link);
373 record_mm.accel_plus_g = config_data.accel_cal_plus;
374 record_mm.accel_minus_g = config_data.accel_cal_minus;
376 record_mm.ground_accel = sensor.accel;
377 record_mm.accel = sensor.accel;
378 record_mm.ground_pres = ms5607.pa;
379 record_mm.pres = ms5607.pa;
380 record_mm.temp = ms5607.cc;
382 record_mm.v_batt = sensor.v_batt;
383 record_mm.v_pyro = sensor.v_pyro;
384 record_mm.sense = sensor.sense;
390 record = new AltosRecord();
392 gps = new AltosGPSQuery(link, config_data);
402 record.callsign = config_data.callsign;
403 record.serial = config_data.serial;
404 record.flight = config_data.log_available() > 0 ? 255 : 0;
407 record.state = AltosLib.ao_flight_idle;
410 state = new AltosState (record, state);
413 public void set_frequency(double in_frequency) {
414 frequency = in_frequency;
417 public void post_state() {
418 listener.update(state);
427 } catch (TimeoutException te) {
431 } catch (InterruptedException ie) {
436 public AltosIdleMonitor(AltosIdleMonitorListener in_listener, AltosLink in_link, boolean in_remote)
437 throws FileNotFoundException, InterruptedException, TimeoutException {
438 listener = in_listener;