altos/lisp: Fix uninitialized values in ao_lisp_make_const
[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_11;
20
21 import java.util.concurrent.*;
22 import java.io.*;
23
24 public class AltosIMU implements Cloneable {
25         public int              accel_along;
26         public int              accel_across;
27         public int              accel_through;
28
29         public int              gyro_roll;
30         public int              gyro_pitch;
31         public int              gyro_yaw;
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.GRAVITATIONAL_ACCELERATION);
37         }
38
39         public static final double      counts_per_degsec = 16.4;
40
41         public static double convert_gyro(double counts) {
42                 return counts / counts_per_degsec;
43         }
44
45         public boolean parse_string(String line) {
46                 if (!line.startsWith("Accel:"))
47                         return false;
48
49                 String[] items = line.split("\\s+");
50
51                 if (items.length >= 8) {
52                         accel_along = Integer.parseInt(items[1]);
53                         accel_across = Integer.parseInt(items[2]);
54                         accel_through = Integer.parseInt(items[3]);
55                         gyro_roll = Integer.parseInt(items[5]);
56                         gyro_pitch = Integer.parseInt(items[6]);
57                         gyro_yaw = Integer.parseInt(items[7]);
58                 }
59                 return true;
60         }
61
62         public AltosIMU clone() {
63                 AltosIMU        n = new AltosIMU();
64
65                 n.accel_along = accel_along;
66                 n.accel_across = accel_across;
67                 n.accel_through = accel_through;
68
69                 n.gyro_roll = gyro_roll;
70                 n.gyro_pitch = gyro_pitch;
71                 n.gyro_yaw = gyro_yaw;
72                 return n;
73         }
74
75         static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException {
76                 try {
77                         AltosIMU        imu = new AltosIMU(link);
78
79                         if (imu != null)
80                                 state.set_imu(imu);
81                 } catch (TimeoutException te) {
82                 }
83         }
84
85         public AltosIMU() {
86                 accel_along = AltosLib.MISSING;
87                 accel_across = AltosLib.MISSING;
88                 accel_through = AltosLib.MISSING;
89
90                 gyro_roll = AltosLib.MISSING;
91                 gyro_pitch = AltosLib.MISSING;
92                 gyro_yaw = AltosLib.MISSING;
93         }
94
95         public AltosIMU(int accel_along, int accel_across, int accel_through,
96                         int gyro_roll, int gyro_pitch, int gyro_yaw) {
97
98                 this.accel_along = accel_along;
99                 this.accel_across = accel_across;
100                 this.accel_through = accel_through;
101
102                 this.gyro_roll = gyro_roll;
103                 this.gyro_pitch = gyro_pitch;
104                 this.gyro_yaw = gyro_yaw;
105         }
106
107         public AltosIMU(AltosLink link) throws InterruptedException, TimeoutException {
108                 this();
109                 link.printf("I\n");
110                 for (;;) {
111                         String line = link.get_reply_no_dialog(5000);
112                         if (line == null) {
113                                 throw new TimeoutException();
114                         }
115                         if (parse_string(line))
116                                 break;
117                 }
118         }
119 }