2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 package org.altusmetrum.altoslib_13;
21 import java.util.concurrent.*;
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;
29 public int gyro_x = AltosLib.MISSING;
30 public int gyro_y = AltosLib.MISSING;
31 public int gyro_z = AltosLib.MISSING;
33 public int mag_x = AltosLib.MISSING;
34 public int mag_y = AltosLib.MISSING;
35 public int mag_z = AltosLib.MISSING;
37 public static final double counts_per_g = 2048.0;
39 public static double convert_accel(double counts) {
40 return counts / counts_per_g * AltosConvert.gravity;
44 public static final double GYRO_FULLSCALE_DEGREES = 2000.0;
45 public static final double GYRO_COUNTS = 32767.0;
47 public static double gyro_degrees_per_second(double counts, double cal) {
48 return (counts - cal) * GYRO_FULLSCALE_DEGREES / GYRO_COUNTS;
51 public boolean parse_string(String line) {
52 if (!line.startsWith("Accel:"))
55 String[] items = line.split("\\s+");
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]);
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]);
73 public AltosIMU clone() {
74 AltosIMU n = new AltosIMU();
91 public static final int orient_telemega = 0;
92 public static final int orient_easymega_v2 = 1;
94 private int accel_across(int orient) {
98 case orient_easymega_v2:
101 return AltosLib.MISSING;
105 private int accel_along(int orient) {
107 case orient_telemega:
109 case orient_easymega_v2:
112 return AltosLib.MISSING;
116 private int accel_through(int orient) {
120 private int gyro_roll(int orient) {
122 case orient_telemega:
124 case orient_easymega_v2:
127 return AltosLib.MISSING;
131 private int gyro_pitch(int orient) {
133 case orient_telemega:
135 case orient_easymega_v2:
138 return AltosLib.MISSING;
142 private int gyro_yaw(int orient) {
146 private int mag_across(int orient) {
148 case orient_telemega:
150 case orient_easymega_v2:
153 return AltosLib.MISSING;
157 private int mag_along(int orient) {
159 case orient_telemega:
161 case orient_easymega_v2:
164 return AltosLib.MISSING;
168 private int mag_through(int orient) {
172 static public void provide_data(AltosDataListener listener, AltosLink link, int orient) throws InterruptedException {
174 AltosIMU imu = new AltosIMU(link);
175 AltosCalData cal_data = listener.cal_data();
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)));
190 } catch (TimeoutException te) {
195 accel_x = AltosLib.MISSING;
196 accel_y = AltosLib.MISSING;
197 accel_z = AltosLib.MISSING;
199 gyro_x = AltosLib.MISSING;
200 gyro_y = AltosLib.MISSING;
201 gyro_z = AltosLib.MISSING;
203 mag_x = AltosLib.MISSING;
204 mag_y = AltosLib.MISSING;
205 mag_z = AltosLib.MISSING;
208 public AltosIMU(AltosLink link) throws InterruptedException, TimeoutException {
212 String line = link.get_reply_no_dialog(5000);
214 throw new TimeoutException();
216 if (parse_string(line))