altoslib: Make cal_data private in AltosDataListener
[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_12;
20
21 import java.util.concurrent.*;
22 import java.io.*;
23
24 public class AltosIMU implements Cloneable {
25         public int              accel_x;
26         public int              accel_y;
27         public int              accel_z;
28
29         public int              gyro_x;
30         public int              gyro_y;
31         public int              gyro_z;
32
33         public static final double      counts_per_g = 2048.0;
34
35         public static double convert_accel(double counts) {
36                 return counts / counts_per_g * AltosConvert.gravity;
37         }
38
39         /* In radians */
40         public static final double      GYRO_FULLSCALE_DEGREES = 2000.0;
41         public static final double      GYRO_COUNTS = 32767.0;
42
43         public static double gyro_degrees_per_second(double counts, double cal) {
44                 return (counts - cal) * GYRO_FULLSCALE_DEGREES / GYRO_COUNTS;
45         }
46
47         public boolean parse_string(String line) {
48                 if (!line.startsWith("Accel:"))
49                         return false;
50
51                 String[] items = line.split("\\s+");
52
53                 if (items.length >= 8) {
54                         accel_x = Integer.parseInt(items[1]);
55                         accel_y = Integer.parseInt(items[2]);
56                         accel_z = Integer.parseInt(items[3]);
57                         gyro_x = Integer.parseInt(items[5]);
58                         gyro_y = Integer.parseInt(items[6]);
59                         gyro_z = Integer.parseInt(items[7]);
60                 }
61                 return true;
62         }
63
64         public AltosIMU clone() {
65                 AltosIMU        n = new AltosIMU();
66
67                 n.accel_x = accel_x;
68                 n.accel_y = accel_y;
69                 n.accel_z = accel_z;
70
71                 n.gyro_x = gyro_x;
72                 n.gyro_y = gyro_y;
73                 n.gyro_z = gyro_z;
74                 return n;
75         }
76
77         static public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException {
78                 try {
79                         AltosIMU        imu = new AltosIMU(link);
80                         AltosCalData    cal_data = listener.cal_data();
81
82                         if (imu != null) {
83                                 listener.set_gyro(cal_data.gyro_roll(imu.gyro_y),
84                                                   cal_data.gyro_pitch(imu.gyro_x),
85                                                   cal_data.gyro_yaw(imu.gyro_z));
86                                 listener.set_accel_ground(cal_data.accel_along(imu.accel_y),
87                                                           cal_data.accel_across(imu.accel_x),
88                                                           cal_data.accel_through(imu.accel_z));
89                         }
90                 } catch (TimeoutException te) {
91                 }
92         }
93
94         public AltosIMU() {
95                 accel_x = AltosLib.MISSING;
96                 accel_y = AltosLib.MISSING;
97                 accel_z = AltosLib.MISSING;
98
99                 gyro_x = AltosLib.MISSING;
100                 gyro_y = AltosLib.MISSING;
101                 gyro_z = AltosLib.MISSING;
102         }
103
104         public AltosIMU(AltosLink link) throws InterruptedException, TimeoutException {
105                 this();
106                 link.printf("I\n");
107                 for (;;) {
108                         String line = link.get_reply_no_dialog(5000);
109                         if (line == null) {
110                                 throw new TimeoutException();
111                         }
112                         if (parse_string(line))
113                                 break;
114                 }
115         }
116 }