2 * Copyright © 2011 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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_12;
23 import java.util.concurrent.*;
25 public class AltosConfigData {
27 /* Version information */
28 public String manufacturer;
29 public String product;
32 public int log_format;
34 public String version;
35 public int altitude_32;
36 public int config_major, config_minor;
38 /* Config information */
40 public int main_deploy;
41 public int apogee_delay;
42 public int apogee_lockout;
45 public int radio_frequency;
46 public String callsign;
47 public int radio_enable;
48 public int radio_calibration;
49 public int telemetry_rate;
50 /* Old HAS_RADIO values */
51 public int radio_channel;
52 public int radio_setting;
55 private int accel_cal_plus_raw, accel_cal_minus_raw;
56 private int accel_cal_plus_cooked, accel_cal_minus_cooked;
57 private boolean accel_cal_adjusted;
58 public int pad_orientation;
61 public int flight_log_max;
65 public int ignite_mode;
68 public String aes_key;
71 public AltosPyro[] pyros;
74 public double pyro_firing_time;
77 public int aprs_interval;
79 public int aprs_format;
84 /* Storage info replies */
85 public int storage_size;
86 public int storage_erase_unit;
88 /* Log listing replies */
89 public int stored_flight;
92 public int tracker_motion;
93 public int tracker_interval;
96 public int accel_zero_along, accel_zero_across, accel_zero_through;
101 public AltosMs5607 ms5607() {
103 ms5607 = new AltosMs5607();
107 public static String get_string(String line, String label) throws ParseException {
108 if (line.startsWith(label)) {
109 String quoted = line.substring(label.length()).trim();
111 if (quoted.startsWith("\""))
112 quoted = quoted.substring(1);
113 if (quoted.endsWith("\""))
114 quoted = quoted.substring(0,quoted.length()-1);
117 throw new ParseException("mismatch", 0);
120 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
121 if (line.startsWith(label)) {
122 String tail = line.substring(label.length()).trim();
123 String[] tokens = tail.split("\\s+");
124 if (tokens.length > 0)
125 return Integer.parseInt(tokens[0]);
127 throw new ParseException("mismatch", 0);
130 public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
131 if (line.startsWith(label)) {
132 String tail = line.substring(label.length()).trim();
133 String[] tokens = tail.split("\\s+");
134 if (tokens.length > 1) {
135 int[] values = new int[2];
136 values[0] = Integer.parseInt(tokens[0]);
137 values[1] = Integer.parseInt(tokens[1]);
141 throw new ParseException("mismatch", 0);
144 public int log_space() {
145 if (log_space != AltosLib.MISSING)
148 if (storage_size != AltosLib.MISSING) {
149 int space = storage_size;
151 if (storage_erase_unit != AltosLib.MISSING && use_flash_for_config())
152 space -= storage_erase_unit;
154 if (space != AltosLib.MISSING)
160 public int log_available() {
161 switch (log_format) {
162 case AltosLib.AO_LOG_FORMAT_TINY:
163 if (stored_flight == 0)
166 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
167 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
170 if (flight_log_max <= 0)
172 int log_max = flight_log_max * 1024;
173 int log_space = log_space();
176 if (stored_flight <= 0)
179 log_used = stored_flight * log_max;
182 if (log_used >= log_space)
185 log_avail = (log_space - log_used) / log_max;
191 public int invert_accel_value(int value) {
192 if (value == AltosLib.MISSING)
193 return AltosLib.MISSING;
195 switch (log_format) {
196 case AltosLib.AO_LOG_FORMAT_FULL:
197 return 0x7fff - value;
198 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
199 case AltosLib.AO_LOG_FORMAT_TELEMETRUM:
200 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
201 case AltosLib.AO_LOG_FORMAT_TELEMEGA_3:
204 return AltosLib.MISSING;
208 public boolean has_monitor_battery() {
209 if (product.startsWith("TeleBT"))
214 int[] parse_version(String v) {
215 String[] parts = v.split("\\.");
216 int r[] = new int[parts.length];
218 for (int i = 0; i < parts.length; i++) {
220 r[i] = (int) AltosLib.fromdec(parts[i]);
221 } catch (NumberFormatException n) {
229 public boolean altitude_32() {
230 return altitude_32 == 1;
233 public int compare_version(String other) {
234 int[] me = parse_version(version);
235 int[] them = parse_version(other);
237 int l = Math.min(me.length, them.length);
239 for (int i = 0; i < l; i++) {
240 int d = me[i] - them[i];
251 public void reset() {
254 serial = AltosLib.MISSING;
255 flight = AltosLib.MISSING;
256 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
257 log_space = AltosLib.MISSING;
259 config_major = AltosLib.MISSING;
260 config_minor = AltosLib.MISSING;
262 main_deploy = AltosLib.MISSING;
263 apogee_delay = AltosLib.MISSING;
264 apogee_lockout = AltosLib.MISSING;
266 radio_frequency = AltosLib.MISSING;
268 radio_enable = AltosLib.MISSING;
269 radio_calibration = AltosLib.MISSING;
270 radio_channel = AltosLib.MISSING;
271 radio_setting = AltosLib.MISSING;
272 telemetry_rate = AltosLib.MISSING;
274 accel_cal_plus_cooked = AltosLib.MISSING;
275 accel_cal_minus_cooked = AltosLib.MISSING;
276 accel_cal_plus_raw = AltosLib.MISSING;
277 accel_cal_minus_raw = AltosLib.MISSING;
278 pad_orientation = AltosLib.MISSING;
279 accel_cal_adjusted = false;
281 flight_log_max = AltosLib.MISSING;
282 log_fixed = AltosLib.MISSING;
283 ignite_mode = AltosLib.MISSING;
287 pyro = AltosLib.MISSING;
288 npyro = AltosLib.MISSING;
290 pyro_firing_time = AltosLib.MISSING;
292 aprs_interval = AltosLib.MISSING;
293 aprs_ssid = AltosLib.MISSING;
294 aprs_format = AltosLib.MISSING;
296 beep = AltosLib.MISSING;
298 tracker_motion = AltosLib.MISSING;
299 tracker_interval = AltosLib.MISSING;
301 storage_size = AltosLib.MISSING;
302 storage_erase_unit = AltosLib.MISSING;
303 stored_flight = AltosLib.MISSING;
305 accel_zero_along = AltosLib.MISSING;
306 accel_zero_across = AltosLib.MISSING;
307 accel_zero_through = AltosLib.MISSING;
310 /* Return + accel calibration relative to a specific pad orientation */
311 public int accel_cal_plus(int pad_orientation) {
312 switch (pad_orientation) {
313 case AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP:
314 return accel_cal_plus_cooked;
315 case AltosLib.AO_PAD_ORIENTATION_ANTENNA_DOWN:
316 return invert_accel_value(accel_cal_minus_cooked);
318 return AltosLib.MISSING;
322 /* Return - accel calibration relative to a specific pad orientation */
323 public int accel_cal_minus(int pad_orientation) {
324 switch (pad_orientation) {
325 case AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP:
326 return accel_cal_minus_cooked;
327 case AltosLib.AO_PAD_ORIENTATION_ANTENNA_DOWN:
328 return invert_accel_value(accel_cal_plus_cooked);
330 return AltosLib.MISSING;
334 /* Once we have all of the values from the config data, compute the
335 * accel cal values relative to Antenna Up orientation.
337 private void adjust_accel_cal() {
338 if (!accel_cal_adjusted &&
339 pad_orientation != AltosLib.MISSING &&
340 accel_cal_plus_raw != AltosLib.MISSING &&
341 accel_cal_minus_raw != AltosLib.MISSING &&
342 log_format != AltosLib.AO_LOG_FORMAT_UNKNOWN)
344 switch (pad_orientation) {
345 case AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP:
346 accel_cal_plus_cooked = accel_cal_plus_raw;
347 accel_cal_minus_cooked = accel_cal_minus_raw;
348 accel_cal_adjusted = true;
350 case AltosLib.AO_PAD_ORIENTATION_ANTENNA_DOWN:
351 accel_cal_plus_cooked = invert_accel_value(accel_cal_minus_raw);
352 accel_cal_minus_cooked = invert_accel_value(accel_cal_plus_raw);
353 accel_cal_adjusted = true;
361 public void parse_line(String line) {
363 /* Version replies */
364 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
365 try { product = get_string(line, "product"); } catch (Exception e) {}
366 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
367 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
368 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
369 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
370 try { altitude_32 = get_int(line, "altitude-32"); } catch (Exception e) {}
371 try { version = get_string(line, "software-version"); } catch (Exception e) {}
373 /* Version also contains MS5607 info, which we ignore here */
375 try { ms5607().reserved = get_int(line, "ms5607 reserved:"); } catch (Exception e) {}
376 try { ms5607().sens = get_int(line, "ms5607 sens:"); } catch (Exception e) {}
377 try { ms5607().off = get_int(line, "ms5607 off:"); } catch (Exception e) {}
378 try { ms5607().tcs = get_int(line, "ms5607 tcs:"); } catch (Exception e) {}
379 try { ms5607().tco = get_int(line, "ms5607 tco:"); } catch (Exception e) {}
380 try { ms5607().tref = get_int(line, "ms5607 tref:"); } catch (Exception e) {}
381 try { ms5607().tempsens = get_int(line, "ms5607 tempsens:"); } catch (Exception e) {}
382 try { ms5607().crc = get_int(line, "ms5607 crc:"); } catch (Exception e) {}
384 /* Config show replies */
387 if (line.startsWith("Config version")) {
388 String[] bits = line.split("\\s+");
389 if (bits.length >= 3) {
390 String[] cfg = bits[2].split("\\.");
392 if (cfg.length >= 2) {
393 config_major = Integer.parseInt(cfg[0]);
394 config_minor = Integer.parseInt(cfg[1]);
398 } catch (Exception e) {}
401 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
402 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
403 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
407 radio_frequency = get_int(line, "Frequency:");
408 if (radio_frequency < 0)
409 radio_frequency = 434550;
410 } catch (Exception e) {}
411 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
412 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
413 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
414 try { telemetry_rate = get_int(line, "Telemetry rate:"); } catch (Exception e) {}
416 /* Old HAS_RADIO values */
417 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
418 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
422 if (line.startsWith("Accel cal")) {
423 String[] bits = line.split("\\s+");
424 if (bits.length >= 6) {
425 accel_cal_plus_raw = Integer.parseInt(bits[3]);
426 accel_cal_minus_raw = Integer.parseInt(bits[5]);
427 accel_cal_adjusted = false;
430 } catch (Exception e) {}
431 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
434 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
435 try { log_fixed = get_int(line, "Log fixed:"); } catch (Exception e) {}
438 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
441 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
445 npyro = get_int(line, "Pyro-count:");
446 pyros = new AltosPyro[npyro];
448 } catch (Exception e) {}
449 if (npyro != AltosLib.MISSING) {
451 AltosPyro p = new AltosPyro(pyro, line);
454 } catch (Exception e) {}
456 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
459 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
460 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
461 try { aprs_format = get_int(line, "APRS format:"); } catch (Exception e) {}
464 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
468 int[] values = get_values(line, "Tracker setting:");
469 tracker_motion = values[0];
470 tracker_interval = values[1];
471 } catch (Exception e) {}
473 /* Storage info replies */
474 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
475 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
477 /* Log listing replies */
478 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
482 if (line.startsWith("IMU cal along")) {
483 String[] bits = line.split("\\s+");
484 if (bits.length >= 8) {
485 accel_zero_along = Integer.parseInt(bits[3]);
486 accel_zero_across = Integer.parseInt(bits[5]);
487 accel_zero_through = Integer.parseInt(bits[7]);
490 } catch (Exception e) {}
492 /* Fix accel cal as soon as all of the necessary values appear */
496 public AltosConfigData() {
500 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
502 String line = link.get_reply();
504 throw new TimeoutException();
505 if (line.contains("Syntax error"))
507 this.parse_line(line);
509 /* signals the end of the version info */
510 if (line.startsWith(finished))
515 public boolean has_frequency() {
516 return radio_frequency != AltosLib.MISSING || radio_setting != AltosLib.MISSING || radio_channel != AltosLib.MISSING;
519 public boolean has_telemetry_rate() {
520 return telemetry_rate != AltosLib.MISSING;
523 public void set_frequency(double freq) {
524 int frequency = radio_frequency;
525 int setting = radio_setting;
527 if (frequency != AltosLib.MISSING) {
528 radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
529 radio_channel = AltosLib.MISSING;
530 } else if (setting != AltosLib.MISSING) {
531 radio_setting =AltosConvert.radio_frequency_to_setting(freq, radio_calibration);
532 radio_channel = AltosLib.MISSING;
534 radio_channel = AltosConvert.radio_frequency_to_channel(freq);
538 public double frequency() {
539 int channel = radio_channel;
540 int setting = radio_setting;
542 if (radio_frequency == AltosLib.MISSING && channel == AltosLib.MISSING && setting == AltosLib.MISSING)
543 return AltosLib.MISSING;
545 if (channel == AltosLib.MISSING)
547 if (setting == AltosLib.MISSING)
550 return AltosConvert.radio_to_frequency(radio_frequency,
556 boolean use_flash_for_config() {
557 if (product.startsWith("TeleMega"))
559 if (product.startsWith("TeleMetrum-v2"))
561 if (product.startsWith("EasyMega"))
567 public boolean mma655x_inverted() throws AltosUnknownProduct {
568 if (product != null) {
569 if (product.startsWith("EasyMega-v1"))
571 if (product.startsWith("TeleMetrum-v2"))
573 if (product.startsWith("TeleMega-v2"))
575 if (product.startsWith("TeleMega-v1"))
578 throw new AltosUnknownProduct(product);
581 public void get_values(AltosConfigValues source) throws AltosConfigDataException {
584 if (main_deploy != AltosLib.MISSING)
585 main_deploy = source.main_deploy();
586 if (apogee_delay != AltosLib.MISSING)
587 apogee_delay = source.apogee_delay();
588 if (apogee_lockout != AltosLib.MISSING)
589 apogee_lockout = source.apogee_lockout();
593 set_frequency(source.radio_frequency());
594 if (radio_enable != AltosLib.MISSING)
595 radio_enable = source.radio_enable();
596 if (callsign != null)
597 callsign = source.callsign();
598 if (telemetry_rate != AltosLib.MISSING)
599 telemetry_rate = source.telemetry_rate();
602 if (pad_orientation != AltosLib.MISSING)
603 pad_orientation = source.pad_orientation();
605 if (accel_cal_plus_cooked != AltosLib.MISSING)
606 accel_cal_plus_cooked = source.accel_cal_plus();
608 if (accel_cal_minus_cooked != AltosLib.MISSING)
609 accel_cal_minus_cooked = source.accel_cal_minus();
612 if (flight_log_max != AltosLib.MISSING)
613 flight_log_max = source.flight_log_max();
616 if (ignite_mode != AltosLib.MISSING)
617 ignite_mode = source.ignite_mode();
620 if (npyro != AltosLib.MISSING)
621 pyros = source.pyros();
622 if (pyro_firing_time != AltosLib.MISSING)
623 pyro_firing_time = source.pyro_firing_time();
626 if (aprs_interval != AltosLib.MISSING)
627 aprs_interval = source.aprs_interval();
628 if (aprs_ssid != AltosLib.MISSING)
629 aprs_ssid = source.aprs_ssid();
630 if (aprs_format != AltosLib.MISSING)
631 aprs_format = source.aprs_format();
634 if (beep != AltosLib.MISSING)
635 beep = source.beep();
637 if (tracker_motion != AltosLib.MISSING)
638 tracker_motion = source.tracker_motion();
639 if (tracker_interval != AltosLib.MISSING)
640 tracker_interval = source.tracker_interval();
643 public void set_values(AltosConfigValues dest) {
644 dest.set_serial(serial);
645 dest.set_product(product);
646 dest.set_version(version);
647 dest.set_altitude_32(altitude_32);
648 dest.set_main_deploy(main_deploy);
649 dest.set_apogee_delay(apogee_delay);
650 dest.set_apogee_lockout(apogee_lockout);
651 dest.set_radio_calibration(radio_calibration);
652 dest.set_radio_frequency(frequency());
653 dest.set_telemetry_rate(telemetry_rate);
654 boolean max_enabled = true;
656 if (log_space() == 0)
659 if (log_fixed != AltosLib.MISSING)
662 switch (log_format) {
663 case AltosLib.AO_LOG_FORMAT_TINY:
667 if (stored_flight != AltosLib.MISSING)
672 dest.set_flight_log_max_enabled(max_enabled);
673 dest.set_radio_enable(radio_enable);
674 dest.set_flight_log_max_limit(log_space() / 1024);
675 dest.set_flight_log_max(flight_log_max);
676 dest.set_ignite_mode(ignite_mode);
677 dest.set_pad_orientation(pad_orientation);
678 dest.set_accel_cal(accel_cal_plus(AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP),
679 accel_cal_minus(AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP));
680 dest.set_callsign(callsign);
681 if (npyro != AltosLib.MISSING)
682 dest.set_pyros(pyros);
684 dest.set_pyros(null);
685 dest.set_pyro_firing_time(pyro_firing_time);
686 dest.set_aprs_interval(aprs_interval);
687 dest.set_aprs_ssid(aprs_ssid);
688 dest.set_aprs_format(aprs_format);
690 dest.set_tracker_motion(tracker_motion);
691 dest.set_tracker_interval(tracker_interval);
694 public boolean log_has_state() {
695 switch (log_format) {
696 case AltosLib.AO_LOG_FORMAT_TELEGPS:
702 public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
705 if (main_deploy != AltosLib.MISSING)
706 link.printf("c m %d\n", main_deploy);
707 if (apogee_delay != AltosLib.MISSING)
708 link.printf("c d %d\n", apogee_delay);
709 if (apogee_lockout != AltosLib.MISSING)
710 link.printf("c L %d\n", apogee_lockout);
713 if (has_frequency()) {
714 boolean has_frequency = radio_frequency != AltosLib.MISSING;
715 boolean has_setting = radio_setting != AltosLib.MISSING;
716 double frequency = frequency();
717 link.set_radio_frequency(frequency,
721 /* When remote, reset the dongle frequency at the same time */
725 link.set_radio_frequency(frequency);
731 if (telemetry_rate != AltosLib.MISSING) {
732 link.printf("c T %d\n", telemetry_rate);
736 link.set_telemetry_rate(telemetry_rate);
742 if (callsign != null) {
743 link.printf("c c %s\n", callsign);
747 link.set_callsign(callsign);
753 if (radio_enable != AltosLib.MISSING)
754 link.printf("c e %d\n", radio_enable);
757 /* set orientation first so that we know how to set the accel cal */
758 if (pad_orientation != AltosLib.MISSING)
759 link.printf("c o %d\n", pad_orientation);
760 int plus = accel_cal_plus(pad_orientation);
761 int minus = accel_cal_minus(pad_orientation);
762 if (plus != AltosLib.MISSING && minus != AltosLib.MISSING)
763 link.printf("c a %d %d\n", plus, minus);
766 if (flight_log_max != 0)
767 link.printf("c l %d\n", flight_log_max);
770 if (ignite_mode != AltosLib.MISSING)
771 link.printf("c i %d\n", ignite_mode);
774 /* UI doesn't support AES key config */
777 if (npyro != AltosLib.MISSING) {
778 for (int p = 0; p < pyros.length; p++) {
779 link.printf("c P %s\n",
780 pyros[p].toString());
783 if (pyro_firing_time != AltosLib.MISSING)
784 link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
787 if (aprs_interval != AltosLib.MISSING)
788 link.printf("c A %d\n", aprs_interval);
789 if (aprs_ssid != AltosLib.MISSING)
790 link.printf("c S %d\n", aprs_ssid);
791 if (aprs_format != AltosLib.MISSING)
792 link.printf("c C %d\n", aprs_format);
795 if (beep != AltosLib.MISSING)
796 link.printf("c b %d\n", beep);
799 if (tracker_motion != AltosLib.MISSING && tracker_interval != AltosLib.MISSING)
800 link.printf("c t %d %d\n", tracker_motion, tracker_interval);
803 /* UI doesn't support accel cal */
805 link.printf("c w\n");
809 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
811 link.printf("c s\nf\nv\n");
812 read_link(link, "software-version");
813 switch (log_format) {
814 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
815 case AltosLib.AO_LOG_FORMAT_NONE:
819 read_link(link, "done");