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;
48 case AltosIMU.imu_type_easytimer_v2:
49 return counts_per_gauss_mmc5983;
52 return AltosIMU.counts_per_gauss(imu_type, mag_model);
55 public static double convert_gauss(double counts, int imu_type, int mag_model) {
56 double cpg = counts_per_gauss(imu_type, mag_model);
58 if (cpg == AltosLib.MISSING)
59 return AltosLib.MISSING;
64 public boolean parse_string(String line) {
66 if (line.startsWith("X:")) {
68 String[] items = line.split("\\s+");
70 mag_model = model_hmc5883;
72 if (items.length >= 6) {
73 across = Integer.parseInt(items[1]);
74 through = Integer.parseInt(items[3]);
75 along = Integer.parseInt(items[5]);
79 if (line.startsWith("MMC5983:")) {
80 String[] items = line.split("\\s+");
82 mag_model = model_mmc5983;
84 if (items.length >= 4) {
85 along = Integer.parseInt(items[1]);
86 across = Integer.parseInt(items[2]);
87 through = Integer.parseInt(items[3]);
95 public AltosMag clone() {
96 AltosMag n = new AltosMag();
107 static public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException {
109 AltosMag mag = new AltosMag(link);
110 AltosCalData cal_data = listener.cal_data();
113 if (mag.mag_model != AltosLib.MISSING)
114 cal_data.set_mag_model(mag.mag_model);
115 listener.set_mag(cal_data.mag_along(mag.along),
116 cal_data.mag_across(mag.across),
117 cal_data.mag_through(mag.through));
119 } catch (TimeoutException te) {
123 public AltosMag(AltosLink link) throws InterruptedException, TimeoutException {
127 String line = link.get_reply_no_dialog(5000);
129 throw new TimeoutException();
131 if (parse_string(line))