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