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