altosui: Fetch RF calibration value for TBT v4.0 units from web
[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         static public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException {
92                 try {
93                         AltosIMU        imu = new AltosIMU(link);
94                         AltosCalData    cal_data = listener.cal_data();
95
96                         if (imu != null) {
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,
101                                                           imu.accel_x,
102                                                           imu.accel_z);
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));
107                                 }
108                         }
109                 } catch (TimeoutException te) {
110                 }
111         }
112
113         public AltosIMU() {
114                 accel_x = AltosLib.MISSING;
115                 accel_y = AltosLib.MISSING;
116                 accel_z = AltosLib.MISSING;
117
118                 gyro_x = AltosLib.MISSING;
119                 gyro_y = AltosLib.MISSING;
120                 gyro_z = AltosLib.MISSING;
121
122                 mag_x = AltosLib.MISSING;
123                 mag_y = AltosLib.MISSING;
124                 mag_z = AltosLib.MISSING;
125         }
126
127         public AltosIMU(AltosLink link) throws InterruptedException, TimeoutException {
128                 this();
129                 link.printf("I\n");
130                 for (;;) {
131                         String line = link.get_reply_no_dialog(5000);
132                         if (line == null) {
133                                 throw new TimeoutException();
134                         }
135                         if (parse_string(line))
136                                 break;
137                 }
138         }
139 }