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_11;
21 import java.util.concurrent.*;
24 public class AltosIMU implements Cloneable {
25 public int accel_along;
26 public int accel_across;
27 public int accel_through;
30 public int gyro_pitch;
33 public static final double counts_per_g = 2048.0;
35 public static double convert_accel(double counts) {
36 return counts / counts_per_g * AltosConvert.gravity;
40 public static final double GYRO_FULLSCALE_DEGREES = 2000.0;
41 public static final double GYRO_COUNTS = 32767.0;
43 public static double gyro_degrees_per_second(double counts, double cal) {
44 return (counts - cal) * GYRO_FULLSCALE_DEGREES / GYRO_COUNTS;
47 public boolean parse_string(String line) {
48 if (!line.startsWith("Accel:"))
51 String[] items = line.split("\\s+");
53 if (items.length >= 8) {
54 accel_along = Integer.parseInt(items[1]);
55 accel_across = Integer.parseInt(items[2]);
56 accel_through = Integer.parseInt(items[3]);
57 gyro_roll = Integer.parseInt(items[5]);
58 gyro_pitch = Integer.parseInt(items[6]);
59 gyro_yaw = Integer.parseInt(items[7]);
64 public AltosIMU clone() {
65 AltosIMU n = new AltosIMU();
67 n.accel_along = accel_along;
68 n.accel_across = accel_across;
69 n.accel_through = accel_through;
71 n.gyro_roll = gyro_roll;
72 n.gyro_pitch = gyro_pitch;
73 n.gyro_yaw = gyro_yaw;
77 static public void provide_data(AltosDataListener listener, AltosLink link, AltosCalData cal_data) throws InterruptedException {
79 AltosIMU imu = new AltosIMU(link);
82 listener.set_accel(cal_data.accel_along(imu.accel_along),
83 cal_data.accel_across(imu.accel_across),
84 cal_data.accel_through(imu.accel_through));
85 listener.set_gyro(cal_data.gyro_roll(imu.gyro_roll),
86 cal_data.gyro_pitch(imu.gyro_pitch),
87 cal_data.gyro_yaw(imu.gyro_yaw));
89 } catch (TimeoutException te) {
94 accel_along = AltosLib.MISSING;
95 accel_across = AltosLib.MISSING;
96 accel_through = AltosLib.MISSING;
98 gyro_roll = AltosLib.MISSING;
99 gyro_pitch = AltosLib.MISSING;
100 gyro_yaw = AltosLib.MISSING;
103 public AltosIMU(int accel_along, int accel_across, int accel_through,
104 int gyro_roll, int gyro_pitch, int gyro_yaw) {
106 this.accel_along = accel_along;
107 this.accel_across = accel_across;
108 this.accel_through = accel_through;
110 this.gyro_roll = gyro_roll;
111 this.gyro_pitch = gyro_pitch;
112 this.gyro_yaw = gyro_yaw;
115 public AltosIMU(AltosLink link) throws InterruptedException, TimeoutException {
119 String line = link.get_reply_no_dialog(5000);
121 throw new TimeoutException();
123 if (parse_string(line))