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