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;
21 import java.util.concurrent.*;
23 class AltosSensorTM extends AltosRecordTM {
25 public AltosSensorTM(AltosLink link, AltosConfigData config_data) throws InterruptedException, TimeoutException {
29 String line = link.get_reply_no_dialog(5000);
31 throw new TimeoutException();
33 if (!line.startsWith("tick:"))
35 String[] items = line.split("\\s+");
36 for (int i = 0; i < items.length;) {
37 if (items[i].equals("tick:")) {
38 tick = Integer.parseInt(items[i+1]);
42 if (items[i].equals("accel:")) {
43 accel = Integer.parseInt(items[i+1]);
47 if (items[i].equals("pres:")) {
48 pres = Integer.parseInt(items[i+1]);
52 if (items[i].equals("temp:")) {
53 temp = Integer.parseInt(items[i+1]);
57 if (items[i].equals("batt:")) {
58 batt = Integer.parseInt(items[i+1]);
62 if (items[i].equals("drogue:")) {
63 drogue = Integer.parseInt(items[i+1]);
67 if (items[i].equals("main:")) {
68 main = Integer.parseInt(items[i+1]);
76 ground_accel = config_data.accel_cal_plus;
78 accel_plus_g = config_data.accel_cal_plus;
79 accel_minus_g = config_data.accel_cal_minus;
91 public AltosSensorMM(AltosLink link) throws InterruptedException, TimeoutException {
94 String line = link.get_reply_no_dialog(5000);
96 throw new TimeoutException();
98 if (!line.startsWith("tick:"))
100 String[] items = line.split("\\s+");
102 for (int i = 0; i < items.length;) {
103 if (items[i].equals("tick:")) {
104 tick = Integer.parseInt(items[i+1]);
108 if (items[i].equals("0:")) {
109 sense[0] = Integer.parseInt(items[i+1]);
113 if (items[i].equals("1:")) {
114 sense[1] = Integer.parseInt(items[i+1]);
118 if (items[i].equals("2:")) {
119 sense[2] = Integer.parseInt(items[i+1]);
123 if (items[i].equals("3:")) {
124 sense[3] = Integer.parseInt(items[i+1]);
128 if (items[i].equals("4:")) {
129 sense[4] = Integer.parseInt(items[i+1]);
133 if (items[i].equals("5:")) {
134 sense[5] = Integer.parseInt(items[i+1]);
138 if (items[i].equals("6:")) {
139 v_batt = Integer.parseInt(items[i+1]);
143 if (items[i].equals("7:")) {
144 v_pyro = Integer.parseInt(items[i+1]);
148 if (items[i].equals("8:")) {
149 accel = Integer.parseInt(items[i+1]);
153 if (items[i].equals("9:")) {
154 accel_ref = Integer.parseInt(items[i+1]);
165 class AltosIMUQuery extends AltosIMU {
167 public AltosIMUQuery (AltosLink link) throws InterruptedException, TimeoutException {
170 String line = link.get_reply_no_dialog(5000);
172 throw new TimeoutException();
174 if (!line.startsWith("Accel:"))
176 String[] items = line.split("\\s+");
177 if (items.length >= 8) {
178 accel_x = Integer.parseInt(items[1]);
179 accel_y = Integer.parseInt(items[2]);
180 accel_z = Integer.parseInt(items[3]);
181 gyro_x = Integer.parseInt(items[5]);
182 gyro_y = Integer.parseInt(items[6]);
183 gyro_z = Integer.parseInt(items[7]);
190 class AltosMs5607Query extends AltosMs5607 {
191 public AltosMs5607Query (AltosLink link) throws InterruptedException, TimeoutException {
192 link.printf("v\nB\n");
194 String line = link.get_reply_no_dialog(5000);
196 throw new TimeoutException();
198 String[] items = line.split("\\s+");
199 if (line.startsWith("Pressure:")) {
200 if (items.length >= 2)
201 raw_pres = Integer.parseInt(items[1]);
202 } else if (line.startsWith("Temperature:")) {
203 if (items.length >= 2)
204 raw_temp = Integer.parseInt(items[1]);
205 } else if (line.startsWith("ms5607 reserved:")) {
206 if (items.length >= 3)
207 reserved = Integer.parseInt(items[2]);
208 } else if (line.startsWith("ms5607 sens:")) {
209 if (items.length >= 3)
210 sens = Integer.parseInt(items[2]);
211 } else if (line.startsWith("ms5607 off:")) {
212 if (items.length >= 3)
213 off = Integer.parseInt(items[2]);
214 } else if (line.startsWith("ms5607 tcs:")) {
215 if (items.length >= 3)
216 tcs = Integer.parseInt(items[2]);
217 } else if (line.startsWith("ms5607 tco:")) {
218 if (items.length >= 3)
219 tco = Integer.parseInt(items[2]);
220 } else if (line.startsWith("ms5607 tref:")) {
221 if (items.length >= 3)
222 tref = Integer.parseInt(items[2]);
223 } else if (line.startsWith("ms5607 tempsens:")) {
224 if (items.length >= 3)
225 tempsens = Integer.parseInt(items[2]);
226 } else if (line.startsWith("ms5607 crc:")) {
227 if (items.length >= 3)
228 crc = Integer.parseInt(items[2]);
229 } else if (line.startsWith("Altitude"))
236 class AltosGPSQuery extends AltosGPS {
237 public AltosGPSQuery (AltosLink link, AltosConfigData config_data)
238 throws TimeoutException, InterruptedException {
239 boolean says_done = config_data.compare_version("1.0") >= 0;
242 String line = link.get_reply_no_dialog(5000);
244 throw new TimeoutException();
245 String[] bits = line.split("\\s+");
246 if (bits.length == 0)
248 if (line.startsWith("Date:")) {
251 String[] d = bits[1].split("/");
254 year = Integer.parseInt(d[0]) + 2000;
255 month = Integer.parseInt(d[1]);
256 day = Integer.parseInt(d[2]);
259 if (line.startsWith("Time:")) {
262 String[] d = bits[1].split(":");
265 hour = Integer.parseInt(d[0]);
266 minute = Integer.parseInt(d[1]);
267 second = Integer.parseInt(d[2]);
270 if (line.startsWith("Lat/Lon:")) {
273 lat = Integer.parseInt(bits[1]) * 1.0e-7;
274 lon = Integer.parseInt(bits[2]) * 1.0e-7;
277 if (line.startsWith("Alt:")) {
280 alt = Integer.parseInt(bits[1]);
283 if (line.startsWith("Flags:")) {
286 int status = Integer.decode(bits[1]);
287 connected = (status & AltosLib.AO_GPS_RUNNING) != 0;
288 locked = (status & AltosLib.AO_GPS_VALID) != 0;
293 if (line.startsWith("Sats:")) {
296 nsat = Integer.parseInt(bits[1]);
297 cc_gps_sat = new AltosGPSSat[nsat];
298 for (int i = 0; i < nsat; i++) {
299 int svid = Integer.parseInt(bits[2+i*2]);
300 int cc_n0 = Integer.parseInt(bits[3+i*2]);
301 cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
304 if (line.startsWith("done"))
306 if (line.startsWith("Syntax error"))
312 public class AltosIdleMonitor extends Thread {
314 AltosIdleMonitorListener listener;
318 AltosState previous_state;
319 AltosConfigData config_data;
322 int AltosRSSI() throws TimeoutException, InterruptedException {
324 String line = link.get_reply_no_dialog(5000);
326 throw new TimeoutException();
327 String[] items = line.split("\\s+");
328 if (items.length < 2)
330 if (!items[0].equals("RSSI:"))
332 int rssi = Integer.parseInt(items[1]);
336 void update_state() throws InterruptedException, TimeoutException {
337 AltosRecord record = null;
341 link.set_radio_frequency(frequency);
345 config_data = new AltosConfigData(link);
346 if (config_data.product.startsWith("TeleMetrum")) {
347 record = new AltosSensorTM(link, config_data);
348 } else if (config_data.product.startsWith("MegaMetrum")) {
349 AltosRecordMM record_mm = new AltosRecordMM();
350 AltosSensorMM sensor = new AltosSensorMM(link);
351 AltosMs5607 ms5607 = new AltosMs5607Query(link);
352 AltosIMU imu = new AltosIMUQuery(link);
354 record_mm.accel_plus_g = config_data.accel_cal_plus;
355 record_mm.accel_minus_g = config_data.accel_cal_minus;
357 record_mm.ground_accel = sensor.accel;
358 record_mm.accel = sensor.accel;
359 record_mm.ground_pres = ms5607.pa;
360 record_mm.pres = ms5607.pa;
361 record_mm.temp = ms5607.cc;
363 record_mm.v_batt = sensor.v_batt;
364 record_mm.v_pyro = sensor.v_pyro;
365 record_mm.sense = sensor.sense;
371 record = new AltosRecord();
373 gps = new AltosGPSQuery(link, config_data);
376 record.callsign = config_data.callsign;
377 record.serial = config_data.serial;
378 record.flight = config_data.log_available() > 0 ? 255 : 0;
380 record.state = AltosLib.ao_flight_idle;
382 record.new_gps = true;
383 state = new AltosState (record, state);
388 record.rssi = AltosRSSI();
397 public void set_frequency(double in_frequency) {
398 frequency = in_frequency;
401 public void post_state() {
402 listener.update(state);
411 } catch (TimeoutException te) {
415 } catch (InterruptedException ie) {
420 public AltosIdleMonitor(AltosIdleMonitorListener in_listener, AltosLink in_link, boolean in_remote)
421 throws FileNotFoundException, InterruptedException, TimeoutException {
422 listener = in_listener;