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