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 public class AltosIdleMonitor extends Thread {
238 AltosIdleMonitorListener listener;
242 AltosState previous_state;
243 AltosConfigData config_data;
246 int AltosRSSI() throws TimeoutException, InterruptedException {
248 String line = link.get_reply_no_dialog(5000);
250 throw new TimeoutException();
251 String[] items = line.split("\\s+");
252 if (items.length < 2)
254 if (!items[0].equals("RSSI:"))
256 int rssi = Integer.parseInt(items[1]);
260 void update_state() throws InterruptedException, TimeoutException {
261 AltosRecord record = null;
265 link.set_radio_frequency(frequency);
269 config_data = new AltosConfigData(link);
270 if (config_data.product.startsWith("TeleMetrum")) {
271 record = new AltosSensorTM(link, config_data);
272 } else if (config_data.product.startsWith("MegaMetrum")) {
273 AltosRecordMM record_mm = new AltosRecordMM();
274 AltosSensorMM sensor = new AltosSensorMM(link);
275 AltosMs5607 ms5607 = new AltosMs5607Query(link);
276 AltosIMU imu = new AltosIMUQuery(link);
278 record_mm.accel_plus_g = config_data.accel_cal_plus;
279 record_mm.accel_minus_g = config_data.accel_cal_minus;
281 record_mm.ground_accel = sensor.accel;
282 record_mm.accel = sensor.accel;
283 record_mm.ground_pres = ms5607.pa;
284 record_mm.pres = ms5607.pa;
285 record_mm.temp = ms5607.cc;
287 record_mm.v_batt = sensor.v_batt;
288 record_mm.v_pyro = sensor.v_pyro;
289 record_mm.sense = sensor.sense;
295 record = new AltosRecord();
297 gps = new AltosGPSQuery(link, config_data);
300 record.callsign = config_data.callsign;
301 record.serial = config_data.serial;
302 record.flight = config_data.log_available() > 0 ? 255 : 0;
304 record.state = AltosLib.ao_flight_idle;
306 record.new_gps = true;
307 state = new AltosState (record, state);
312 record.rssi = AltosRSSI();
321 public void set_frequency(double in_frequency) {
322 frequency = in_frequency;
325 public void post_state() {
326 listener.update(state);
335 } catch (TimeoutException te) {
339 } catch (InterruptedException ie) {
344 public AltosIdleMonitor(AltosIdleMonitorListener in_listener, AltosLink in_link, boolean in_remote)
345 throws FileNotFoundException, InterruptedException, TimeoutException {
346 listener = in_listener;