altoslib: Support TeleMega v5.0
[fw/altos] / altoslib / AltosMag.java
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.altoslib_14;
20
21 import java.util.concurrent.*;
22 import java.io.*;
23
24 public class AltosMag implements Cloneable {
25         public int              along = AltosLib.MISSING;
26         public int              across = AltosLib.MISSING;
27         public int              through = AltosLib.MISSING;
28
29         public static final int model_hmc5883 = 0;
30         public static final int model_mmc5983 = 1;
31
32         public int mag_model = AltosLib.MISSING;
33
34         public static final double counts_per_gauss_hmc5883 = 1090;
35         public static final double counts_per_gauss_mmc5983 = 4096;
36
37         public static double counts_per_gauss(int imu_type, int mag_model) {
38                 switch(mag_model) {
39                 case AltosLib.model_hmc5883:
40                         return counts_per_gauss_hmc5883;
41                 case AltosLib.model_mmc5983:
42                         return counts_per_gauss_mmc5983;
43                 }
44                 switch (imu_type) {
45                 case AltosIMU.imu_type_telemega_v1_v2:
46                 case AltosIMU.imu_type_easymega_v1:
47                         return counts_per_gauss_hmc5883;
48                 }
49
50                 return AltosIMU.counts_per_gauss(imu_type, mag_model);
51         }
52
53         public static double convert_gauss(double counts, int imu_type, int mag_model) {
54                 double cpg = counts_per_gauss(imu_type, mag_model);
55
56                 if (cpg == AltosLib.MISSING)
57                         return AltosLib.MISSING;
58
59                 return counts / cpg;
60         }
61
62         public boolean parse_string(String line) {
63
64                 if (line.startsWith("X:")) {
65
66                         String[] items = line.split("\\s+");
67
68                         mag_model = model_hmc5883;
69
70                         if (items.length >= 6) {
71                                 across = Integer.parseInt(items[1]);
72                                 through = Integer.parseInt(items[3]);
73                                 along = Integer.parseInt(items[5]);
74                         }
75                         return true;
76                 }
77                 if (line.startsWith("MMC5983:")) {
78                         String[] items = line.split("\\s+");
79
80                         mag_model = model_mmc5983;
81
82                         if (items.length >= 4) {
83                                 along = Integer.parseInt(items[1]);
84                                 across = Integer.parseInt(items[2]);
85                                 through = Integer.parseInt(items[3]);
86                         }
87                         return true;
88                 }
89
90                 return false;
91         }
92
93         public AltosMag clone() {
94                 AltosMag n = new AltosMag();
95
96                 n.along = along;
97                 n.across = across;
98                 n.through = through;
99                 return n;
100         }
101
102         public AltosMag() {
103         }
104
105         static public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException {
106                 try {
107                         AltosMag        mag = new AltosMag(link);
108                         AltosCalData    cal_data = listener.cal_data();
109
110                         if (mag != null) {
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));
116                         }
117                 } catch (TimeoutException te) {
118                 }
119         }
120
121         public AltosMag(AltosLink link) throws InterruptedException, TimeoutException {
122                 this();
123                 link.printf("M\n");
124                 for (;;) {
125                         String line = link.get_reply_no_dialog(5000);
126                         if (line == null) {
127                                 throw new TimeoutException();
128                         }
129                         if (parse_string(line))
130                                 break;
131                 }
132         }
133 }