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