2 * Copyright © 2012 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_14;
21 import java.util.concurrent.*;
24 public class AltosMag implements Cloneable {
25 public int along = AltosLib.MISSING;
26 public int across = AltosLib.MISSING;
27 public int through = AltosLib.MISSING;
29 public static final int model_hmc5883 = 0;
30 public static final int model_mmc5983 = 1;
32 public int mag_model = AltosLib.MISSING;
34 public static final double counts_per_gauss_hmc5883 = 1090;
35 public static final double counts_per_gauss_mmc5983 = 4096;
37 public static double counts_per_gauss(int imu_type, int mag_model) {
39 case AltosLib.model_hmc5883:
40 return counts_per_gauss_hmc5883;
41 case AltosLib.model_mmc5983:
42 return counts_per_gauss_mmc5983;
45 case AltosIMU.imu_type_telemega_v1_v2:
46 case AltosIMU.imu_type_easymega_v1:
47 return counts_per_gauss_hmc5883;
50 return AltosIMU.counts_per_gauss(imu_type, mag_model);
53 public static double convert_gauss(double counts, int imu_type, int mag_model) {
54 double cpg = counts_per_gauss(imu_type, mag_model);
56 if (cpg == AltosLib.MISSING)
57 return AltosLib.MISSING;
62 public boolean parse_string(String line) {
64 if (line.startsWith("X:")) {
66 String[] items = line.split("\\s+");
68 mag_model = model_hmc5883;
70 if (items.length >= 6) {
71 across = Integer.parseInt(items[1]);
72 through = Integer.parseInt(items[3]);
73 along = Integer.parseInt(items[5]);
77 if (line.startsWith("MMC5983:")) {
78 String[] items = line.split("\\s+");
80 mag_model = model_mmc5983;
82 if (items.length >= 4) {
83 along = Integer.parseInt(items[1]);
84 across = Integer.parseInt(items[2]);
85 through = Integer.parseInt(items[3]);
93 public AltosMag clone() {
94 AltosMag n = new AltosMag();
105 static public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException {
107 AltosMag mag = new AltosMag(link);
108 AltosCalData cal_data = listener.cal_data();
111 if (mag.mag_model != AltosLib.MISSING)
112 cal_data.set_mag_model(mag.mag_model);
113 listener.set_mag(cal_data.mag_along(mag.along),
114 cal_data.mag_across(mag.across),
115 cal_data.mag_through(mag.through));
117 } catch (TimeoutException te) {
121 public AltosMag(AltosLink link) throws InterruptedException, TimeoutException {
125 String line = link.get_reply_no_dialog(5000);
127 throw new TimeoutException();
129 if (parse_string(line))