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