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_12;
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 static public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException {
93 AltosIMU imu = new AltosIMU(link);
94 AltosCalData cal_data = listener.cal_data();
97 listener.set_gyro(cal_data.gyro_roll(imu.gyro_y),
98 cal_data.gyro_pitch(imu.gyro_x),
99 cal_data.gyro_yaw(imu.gyro_z));
100 listener.set_accel_ground(imu.accel_y,
103 if (imu.mag_x != AltosLib.MISSING) {
104 listener.set_mag(cal_data.mag_along(imu.mag_y),
105 cal_data.mag_across(imu.mag_x),
106 cal_data.mag_through(imu.mag_z));
109 } catch (TimeoutException te) {
114 accel_x = AltosLib.MISSING;
115 accel_y = AltosLib.MISSING;
116 accel_z = AltosLib.MISSING;
118 gyro_x = AltosLib.MISSING;
119 gyro_y = AltosLib.MISSING;
120 gyro_z = AltosLib.MISSING;
122 mag_x = AltosLib.MISSING;
123 mag_y = AltosLib.MISSING;
124 mag_z = AltosLib.MISSING;
127 public AltosIMU(AltosLink link) throws InterruptedException, TimeoutException {
131 String line = link.get_reply_no_dialog(5000);
133 throw new TimeoutException();
135 if (parse_string(line))