altoslib: Correct ADXL counts_per_g
[fw/altos] / altoslib / AltosIMU.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 AltosIMU implements Cloneable {
25         public int              accel_x = AltosLib.MISSING;
26         public int              accel_y = AltosLib.MISSING;
27         public int              accel_z = AltosLib.MISSING;
28
29         public int              gyro_x = AltosLib.MISSING;
30         public int              gyro_y = AltosLib.MISSING;
31         public int              gyro_z = AltosLib.MISSING;
32
33         public int              mag_x = AltosLib.MISSING;
34         public int              mag_y = AltosLib.MISSING;
35         public int              mag_z = AltosLib.MISSING;
36
37         public static final double      counts_per_g_mpu = 2048.0;
38         public static final double      counts_per_g_bmx = 2048.0;
39         public static final double      counts_per_g_adxl = 20.5;
40
41         private static double counts_per_g(int imu_type) {
42                 switch (imu_type) {
43                 case imu_type_telemega_v1_v2:
44                 case imu_type_telemega_v3:
45                 case imu_type_easymega_v1:
46                 case imu_type_easymega_v2:
47                         return counts_per_g_mpu;
48                 case  imu_type_telemega_v4:
49                 case imu_type_easytimer_v1:
50                         return counts_per_g_bmx;
51                 case imu_type_easymotor_v2:
52                         return counts_per_g_adxl;
53                 default:
54                         return AltosLib.MISSING;
55                 }
56         }
57
58         public static double convert_accel(double counts, int imu_type) {
59                 return counts / counts_per_g(imu_type) * AltosConvert.gravity;
60         }
61
62         public static final double      GYRO_FULLSCALE_DEGREES_MPU = 2000.0;
63         public static final double      GYRO_COUNTS_MPU = 32767.0;
64         public static final double      counts_per_degree_mpu = GYRO_COUNTS_MPU / GYRO_FULLSCALE_DEGREES_MPU;
65         public static final double      GYRO_FULLSCALE_DEGREES_BMX = 2000.0;
66         public static final double      GYRO_COUNTS_BMX = 32767.0;
67         public static final double      counts_per_degree_bmx = GYRO_COUNTS_BMX / GYRO_FULLSCALE_DEGREES_BMX;
68
69         private static double counts_per_degree(int imu_type) {
70                 switch (imu_type) {
71                 case imu_type_telemega_v1_v2:
72                 case imu_type_telemega_v3:
73                 case imu_type_easymega_v1:
74                 case imu_type_easymega_v2:
75                         return counts_per_degree_mpu;
76                 case imu_type_telemega_v4:
77                 case imu_type_easytimer_v1:
78                         return counts_per_degree_bmx;
79                 default:
80                         return AltosLib.MISSING;
81                 }
82         }
83
84         public static double gyro_degrees_per_second(double counts, int imu_type) {
85                 return counts / counts_per_degree(imu_type);
86         }
87
88         public static final int imu_axis_x = 0;
89         public static final int imu_axis_y = 1;
90         public static final int imu_axis_z = 2;
91
92         public static final double MAG_FULLSCALE_GAUSS_MPU = 48.00;     /* 4800µT */
93         public static final double MAG_COUNTS_MPU = 32767.0;
94         public static final double counts_per_gauss_mpu = MAG_COUNTS_MPU / MAG_FULLSCALE_GAUSS_MPU;
95
96         public static final double counts_per_gauss_bmx = 100.0;        /* BMX driver converts to µT */
97
98         public static double counts_per_gauss(int imu_type, int axis) {
99                 switch(imu_type) {
100                 case imu_type_telemega_v1_v2:
101                 case imu_type_easymega_v1:
102                         return AltosMag.counts_per_gauss;
103                 case imu_type_telemega_v3:
104                 case imu_type_easymega_v2:
105                         return counts_per_gauss_mpu;
106                 case imu_type_telemega_v4:
107                 case imu_type_easytimer_v1:
108                         return 100.0;
109                 default:
110                         return AltosLib.MISSING;
111                 }
112         }
113
114         public static double convert_gauss(double counts, int imu_type, int imu_axis) {
115                 return counts / counts_per_gauss(imu_type, imu_axis);
116         }
117
118         public boolean parse_string(String line) {
119                 if (!line.startsWith("Accel:"))
120                         return false;
121
122                 String[] items = line.split("\\s+");
123
124                 if (items.length >= 8) {
125                         accel_x = Integer.parseInt(items[1]);
126                         accel_y = Integer.parseInt(items[2]);
127                         accel_z = Integer.parseInt(items[3]);
128                         gyro_x = Integer.parseInt(items[5]);
129                         gyro_y = Integer.parseInt(items[6]);
130                         gyro_z = Integer.parseInt(items[7]);
131                 }
132                 if (items.length >= 12) {
133                         mag_x = Integer.parseInt(items[9]);
134                         mag_y = Integer.parseInt(items[10]);
135                         mag_z = Integer.parseInt(items[11]);
136                 }
137                 return true;
138         }
139
140         public AltosIMU clone() {
141                 AltosIMU        n = new AltosIMU();
142
143                 n.accel_x = accel_x;
144                 n.accel_y = accel_y;
145                 n.accel_z = accel_z;
146
147                 n.gyro_x = gyro_x;
148                 n.gyro_y = gyro_y;
149                 n.gyro_z = gyro_z;
150
151                 n.mag_x = mag_x;
152                 n.mag_y = mag_y;
153                 n.mag_z = mag_z;
154
155                 return n;
156         }
157
158         public static final int imu_type_telemega_v1_v2 = 0;    /* MPU6000 */
159         public static final int imu_type_telemega_v3 = 1;       /* MPU9250 */
160         public static final int imu_type_telemega_v4 = 2;       /* BMX160 */
161
162         public static final int imu_type_easymega_v1 = 3;       /* MPU6000 */
163         public static final int imu_type_easymega_v2 = 4;       /* MPU9250 */
164
165         public static final int imu_type_easytimer_v1 = 5;      /* BMX160 */
166
167         public static final int imu_type_easymotor_v2 = 6;      /* ADXL375 (accel only) */
168
169         private int accel_across(int imu_type) {
170                 switch (imu_type) {
171                 case imu_type_telemega_v1_v2:
172                 case imu_type_telemega_v3:
173                 case imu_type_easymega_v1:
174                         return accel_x;
175                 case imu_type_easymega_v2:
176                         return -accel_y;
177                 case imu_type_telemega_v4:
178                 case imu_type_easytimer_v1:
179                         return -accel_y;
180                 case imu_type_easymotor_v2:
181                         return accel_y;
182                 default:
183                         return AltosLib.MISSING;
184                 }
185         }
186
187         private int accel_along(int imu_type) {
188                 switch (imu_type) {
189                 case imu_type_telemega_v1_v2:
190                 case imu_type_telemega_v3:
191                 case imu_type_easymega_v1:
192                         return accel_y;
193                 case imu_type_easymega_v2:
194                 case imu_type_telemega_v4:
195                 case imu_type_easytimer_v1:
196                         return accel_x;
197                 case imu_type_easymotor_v2:
198                         return -accel_x;
199                 default:
200                         return AltosLib.MISSING;
201                 }
202         }
203
204         private int accel_through(int imu_type) {
205                 return accel_z;
206         }
207
208         private int gyro_roll(int imu_type) {
209                 switch (imu_type) {
210                 case imu_type_telemega_v1_v2:
211                 case imu_type_telemega_v3:
212                 case imu_type_easymega_v1:
213                         return gyro_y;
214                 case imu_type_easymega_v2:
215                 case imu_type_telemega_v4:
216                 case imu_type_easytimer_v1:
217                         return gyro_x;
218                 default:
219                         return AltosLib.MISSING;
220                 }
221         }
222
223         private int gyro_pitch(int imu_type) {
224                 switch (imu_type) {
225                 case imu_type_telemega_v1_v2:
226                 case imu_type_telemega_v3:
227                 case imu_type_easymega_v1:
228                         return gyro_x;
229                 case imu_type_easymega_v2:
230                         return -gyro_y;
231                 case imu_type_telemega_v4:
232                 case imu_type_easytimer_v1:
233                         return -gyro_y;
234                 default:
235                         return AltosLib.MISSING;
236                 }
237         }
238
239         private int gyro_yaw(int imu_type) {
240                 return gyro_z;
241         }
242
243         public static int mag_across_axis(int imu_type) {
244                 switch (imu_type) {
245                 case imu_type_telemega_v1_v2:
246                 case imu_type_telemega_v3:
247                 case imu_type_easymega_v1:
248                         return imu_axis_x;
249                 case imu_type_easymega_v2:
250                 case imu_type_telemega_v4:
251                 case imu_type_easytimer_v1:
252                         return imu_axis_y;
253                 default:
254                         return AltosLib.MISSING;
255                 }
256         }
257
258         private int mag_across(int imu_type) {
259                 switch (imu_type) {
260                 case imu_type_telemega_v1_v2:
261                 case imu_type_telemega_v3:
262                 case imu_type_easymega_v1:
263                         return mag_x;
264                 case imu_type_easymega_v2:
265                         return -mag_y;
266                 case imu_type_telemega_v4:
267                 case imu_type_easytimer_v1:
268                         return mag_y;
269                 default:
270                         return AltosLib.MISSING;
271                 }
272         }
273
274         public static int mag_along_axis(int imu_type) {
275                 switch (imu_type) {
276                 case imu_type_telemega_v1_v2:
277                 case imu_type_telemega_v3:
278                 case imu_type_easymega_v1:
279                         return imu_axis_y;
280                 case imu_type_easymega_v2:
281                 case imu_type_telemega_v4:
282                 case imu_type_easytimer_v1:
283                         return imu_axis_x;
284                 default:
285                         return AltosLib.MISSING;
286                 }
287         }
288
289         private int mag_along(int imu_type) {
290                 switch (imu_type) {
291                 case imu_type_telemega_v1_v2:
292                 case imu_type_telemega_v3:
293                 case imu_type_easymega_v1:
294                         return mag_y;
295                 case imu_type_easymega_v2:
296                 case imu_type_telemega_v4:
297                 case imu_type_easytimer_v1:
298                         return mag_x;
299                 default:
300                         return AltosLib.MISSING;
301                 }
302         }
303
304         public static int mag_through_axis(int imu_type) {
305                 return imu_axis_z;
306         }
307
308         private int mag_through(int imu_type) {
309                 return mag_z;
310         }
311
312         static public void provide_data(AltosDataListener listener, AltosLink link, int imu_type) throws InterruptedException {
313                 try {
314                         AltosIMU        imu = new AltosIMU(link);
315                         AltosCalData    cal_data = listener.cal_data();
316
317                         cal_data.set_imu_type(imu_type);
318
319                         if (imu != null) {
320                                 if (imu.gyro_x != AltosLib.MISSING) {
321                                         listener.set_gyro(cal_data.gyro_roll(imu.gyro_roll(imu_type)),
322                                                           cal_data.gyro_pitch(imu.gyro_pitch(imu_type)),
323                                                           cal_data.gyro_yaw(imu.gyro_yaw(imu_type)));
324                                 }
325                                 listener.set_accel_ground(cal_data.accel_along(imu.accel_along(imu_type)),
326                                                           cal_data.accel_across(imu.accel_across(imu_type)),
327                                                           cal_data.accel_through(imu.accel_through(imu_type)));
328                                 listener.set_accel(cal_data.accel_along(imu.accel_along(imu_type)),
329                                                    cal_data.accel_across(imu.accel_across(imu_type)),
330                                                    cal_data.accel_through(imu.accel_through(imu_type)));
331                                 if (imu.mag_x != AltosLib.MISSING) {
332                                         listener.set_mag(cal_data.mag_along(imu.mag_along(imu_type)),
333                                                          cal_data.mag_across(imu.mag_across(imu_type)),
334                                                          cal_data.mag_through(imu.mag_through(imu_type)));
335                                 }
336                         }
337                 } catch (TimeoutException te) {
338                 }
339         }
340
341         public AltosIMU() {
342                 accel_x = AltosLib.MISSING;
343                 accel_y = AltosLib.MISSING;
344                 accel_z = AltosLib.MISSING;
345
346                 gyro_x = AltosLib.MISSING;
347                 gyro_y = AltosLib.MISSING;
348                 gyro_z = AltosLib.MISSING;
349
350                 mag_x = AltosLib.MISSING;
351                 mag_y = AltosLib.MISSING;
352                 mag_z = AltosLib.MISSING;
353         }
354
355         public AltosIMU(AltosLink link) throws InterruptedException, TimeoutException {
356                 this();
357                 link.printf("I\n");
358                 for (;;) {
359                         String line = link.get_reply_no_dialog(5000);
360                         if (line == null) {
361                                 throw new TimeoutException();
362                         }
363                         if (parse_string(line))
364                                 break;
365                 }
366         }
367 }