altoslib: Correct monitor idle IMU data for EasyMega v2
[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 = 2048.0;
38
39         public static double convert_accel(double counts) {
40                 return counts / counts_per_g * AltosConvert.gravity;
41         }
42
43         /* In radians */
44         public static final double      GYRO_FULLSCALE_DEGREES = 2000.0;
45         public static final double      GYRO_COUNTS = 32767.0;
46
47         public static double gyro_degrees_per_second(double counts, double cal) {
48                 return (counts - cal) * GYRO_FULLSCALE_DEGREES / GYRO_COUNTS;
49         }
50
51         public boolean parse_string(String line) {
52                 if (!line.startsWith("Accel:"))
53                         return false;
54
55                 String[] items = line.split("\\s+");
56
57                 if (items.length >= 8) {
58                         accel_x = Integer.parseInt(items[1]);
59                         accel_y = Integer.parseInt(items[2]);
60                         accel_z = Integer.parseInt(items[3]);
61                         gyro_x = Integer.parseInt(items[5]);
62                         gyro_y = Integer.parseInt(items[6]);
63                         gyro_z = Integer.parseInt(items[7]);
64                 }
65                 if (items.length >= 12) {
66                         mag_x = Integer.parseInt(items[9]);
67                         mag_y = Integer.parseInt(items[10]);
68                         mag_z = Integer.parseInt(items[11]);
69                 }
70                 return true;
71         }
72
73         public AltosIMU clone() {
74                 AltosIMU        n = new AltosIMU();
75
76                 n.accel_x = accel_x;
77                 n.accel_y = accel_y;
78                 n.accel_z = accel_z;
79
80                 n.gyro_x = gyro_x;
81                 n.gyro_y = gyro_y;
82                 n.gyro_z = gyro_z;
83
84                 n.mag_x = mag_x;
85                 n.mag_y = mag_y;
86                 n.mag_z = mag_z;
87
88                 return n;
89         }
90
91         public static final int orient_telemega = 0;
92         public static final int orient_easymega_v2 = 1;
93
94         private int accel_across(int orient) {
95                 switch (orient) {
96                 case orient_telemega:
97                         return accel_x;
98                 case orient_easymega_v2:
99                         return -accel_y;
100                 default:
101                         return AltosLib.MISSING;
102                 }
103         }
104
105         private int accel_along(int orient) {
106                 switch (orient) {
107                 case orient_telemega:
108                         return accel_y;
109                 case orient_easymega_v2:
110                         return accel_x;
111                 default:
112                         return AltosLib.MISSING;
113                 }
114         }
115
116         private int accel_through(int orient) {
117                 return accel_z;
118         }
119
120         private int gyro_roll(int orient) {
121                 switch (orient) {
122                 case orient_telemega:
123                         return gyro_y;
124                 case orient_easymega_v2:
125                         return gyro_x;
126                 default:
127                         return AltosLib.MISSING;
128                 }
129         }
130
131         private int gyro_pitch(int orient) {
132                 switch (orient) {
133                 case orient_telemega:
134                         return gyro_x;
135                 case orient_easymega_v2:
136                         return -gyro_y;
137                 default:
138                         return AltosLib.MISSING;
139                 }
140         }
141
142         private int gyro_yaw(int orient) {
143                 return gyro_z;
144         }
145
146         private int mag_across(int orient) {
147                 switch (orient) {
148                 case orient_telemega:
149                         return mag_x;
150                 case orient_easymega_v2:
151                         return -mag_y;
152                 default:
153                         return AltosLib.MISSING;
154                 }
155         }
156
157         private int mag_along(int orient) {
158                 switch (orient) {
159                 case orient_telemega:
160                         return mag_y;
161                 case orient_easymega_v2:
162                         return mag_x;
163                 default:
164                         return AltosLib.MISSING;
165                 }
166         }
167
168         private int mag_through(int orient) {
169                 return mag_z;
170         }
171
172         static public void provide_data(AltosDataListener listener, AltosLink link, int orient) throws InterruptedException {
173                 try {
174                         AltosIMU        imu = new AltosIMU(link);
175                         AltosCalData    cal_data = listener.cal_data();
176
177                         if (imu != null) {
178                                 listener.set_gyro(cal_data.gyro_roll(imu.gyro_roll(orient)),
179                                                   cal_data.gyro_pitch(imu.gyro_pitch(orient)),
180                                                   cal_data.gyro_yaw(imu.gyro_yaw(orient)));
181                                 listener.set_accel_ground(imu.accel_along(orient),
182                                                           imu.accel_across(orient),
183                                                           imu.accel_through(orient));
184                                 if (imu.mag_x != AltosLib.MISSING) {
185                                         listener.set_mag(cal_data.mag_along(imu.mag_along(orient)),
186                                                          cal_data.mag_across(imu.mag_across(orient)),
187                                                          cal_data.mag_through(imu.mag_through(orient)));
188                                 }
189                         }
190                 } catch (TimeoutException te) {
191                 }
192         }
193
194         public AltosIMU() {
195                 accel_x = AltosLib.MISSING;
196                 accel_y = AltosLib.MISSING;
197                 accel_z = AltosLib.MISSING;
198
199                 gyro_x = AltosLib.MISSING;
200                 gyro_y = AltosLib.MISSING;
201                 gyro_z = AltosLib.MISSING;
202
203                 mag_x = AltosLib.MISSING;
204                 mag_y = AltosLib.MISSING;
205                 mag_z = AltosLib.MISSING;
206         }
207
208         public AltosIMU(AltosLink link) throws InterruptedException, TimeoutException {
209                 this();
210                 link.printf("I\n");
211                 for (;;) {
212                         String line = link.get_reply_no_dialog(5000);
213                         if (line == null) {
214                                 throw new TimeoutException();
215                         }
216                         if (parse_string(line))
217                                 break;
218                 }
219         }
220 }