altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / altoslib / AltosAdxl375.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_14;
20
21 import java.util.concurrent.*;
22
23 public class AltosAdxl375 implements Cloneable {
24
25         private int[]   accels = new int[3];
26         private int     axis;
27
28         public static final int X_AXIS = 0;
29         public static final int Y_AXIS = 1;
30         public static final int Z_AXIS = 2;
31
32         public boolean parse_line(String line) throws NumberFormatException {
33                 if (line.startsWith("ADXL375 value")) {
34                         String[] items = line.split("\\s+");
35                         if (axis == AltosLib.MISSING)
36                                 throw new NumberFormatException("No ADXL375 axis specified");
37                         if (items.length >= 3) {
38                                 for (int i = 0; i < 3; i++)
39                                         accels[i] = Integer.parseInt(items[2+i]);
40                                 return true;
41                         }
42                 }
43                 return false;
44         }
45
46         public AltosAdxl375 clone() {
47                 AltosAdxl375    n = new AltosAdxl375(axis);
48
49                 for (int i = 0; i < 3; i++)
50                         n.accels[i] = accels[i];
51                 return n;
52         }
53
54         private int accel_along() {
55                 return accels[axis];
56         }
57
58         private int accel_across() {
59                 if (axis == X_AXIS)
60                         return accels[Y_AXIS];
61                 else
62                         return accels[X_AXIS];
63         }
64
65         private int accel_through() {
66                 return accels[Z_AXIS];
67         }
68
69         static public void provide_data(AltosDataListener listener, AltosLink link, boolean three_axis, int imu_type) throws InterruptedException, AltosUnknownProduct {
70                 try {
71                         AltosCalData    cal_data = listener.cal_data();
72                         AltosAdxl375    adxl375 = new AltosAdxl375(link, cal_data.adxl375_axis);
73
74                         if (adxl375 != null) {
75                                 int accel = adxl375.accel_along();
76                                 if (!cal_data.adxl375_inverted)
77                                         accel = -accel;
78                                 if (cal_data.pad_orientation == 1)
79                                         accel = -accel;
80                                 listener.set_acceleration(cal_data.acceleration(accel));
81                                 if (three_axis) {
82                                         cal_data.set_imu_type(imu_type);
83                                         double accel_along = cal_data.accel_along(-accel);
84                                         double accel_across = cal_data.accel_across(adxl375.accel_across());
85                                         double accel_through = cal_data.accel_through(adxl375.accel_through());
86                                         listener.set_accel_ground(accel_along,
87                                                                   accel_across,
88                                                                   accel_through);
89                                         listener.set_accel(accel_along,
90                                                            accel_across,
91                                                            accel_through);
92                                 }
93                         }
94                 } catch (TimeoutException te) {
95                 } catch (NumberFormatException ne) {
96                 }
97         }
98
99         public AltosAdxl375() {
100                 for (int i = 0; i < 3; i++)
101                         accels[i] = AltosLib.MISSING;
102                 axis = AltosLib.MISSING;
103         }
104
105         public AltosAdxl375(int axis) {
106                 this();
107                 this.axis = axis;
108         }
109
110         public AltosAdxl375(AltosLink link, int axis) throws InterruptedException, TimeoutException, NumberFormatException {
111                 this(axis);
112                 link.printf("A\n");
113                 for (;;) {
114                         String line = link.get_reply_no_dialog(5000);
115                         if (line == null)
116                                 throw new TimeoutException();
117                         if (parse_line(line))
118                                 break;
119                 }
120         }
121 }