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