|
@@ -33,7 +33,7 @@
|
33
|
33
|
|
34
|
34
|
using namespace upm;
|
35
|
35
|
|
36
|
|
-LSM303::LSM303(int bus, int addrMag, int addrAcc)
|
|
36
|
+LSM303::LSM303(int bus, int addrMag, int addrAcc, int accScale)
|
37
|
37
|
{
|
38
|
38
|
mraa_result_t ret = MRAA_SUCCESS;
|
39
|
39
|
|
|
@@ -45,8 +45,14 @@ LSM303::LSM303(int bus, int addrMag, int addrAcc)
|
45
|
45
|
// 0x27 is the 'normal' mode with X/Y/Z enable
|
46
|
46
|
setRegisterSafe(m_addrAcc, CTRL_REG1_A, 0x27);
|
47
|
47
|
|
48
|
|
- // scale == 2, can be 4 or 8
|
49
|
|
- setRegisterSafe(m_addrAcc, CTRL_REG4_A, 0x00);
|
|
48
|
+ // scale can be 2, 4 or 8
|
|
49
|
+ if (2 == accScale) {
|
|
50
|
+ setRegisterSafe(m_addrAcc, CTRL_REG4_A, 0x00);
|
|
51
|
+ } else if (4 == accScale) {
|
|
52
|
+ setRegisterSafe(m_addrAcc, CTRL_REG4_A, 0x10);
|
|
53
|
+ } else { // default; equivalent to 8g
|
|
54
|
+ setRegisterSafe(m_addrAcc, CTRL_REG4_A, 0x30);
|
|
55
|
+ }
|
50
|
56
|
|
51
|
57
|
// 0x10 = minimum datarate ~15Hz output rate
|
52
|
58
|
setRegisterSafe(m_addrMag, CRA_REG_M, 0x10);
|
|
@@ -71,42 +77,42 @@ LSM303::getHeading()
|
71
|
77
|
return -1;
|
72
|
78
|
}
|
73
|
79
|
|
74
|
|
- float heading = 180 * atan2(coor[Y], coor[X])/M_PI;
|
|
80
|
+ float heading = 180.0 * atan2(double(coor[Y]), double(coor[X]))/M_PI;
|
75
|
81
|
|
76
|
|
- if (heading < 0)
|
77
|
|
- heading += 360;
|
|
82
|
+ if (heading < 0.0)
|
|
83
|
+ heading += 360.0;
|
78
|
84
|
|
79
|
85
|
return heading;
|
80
|
86
|
}
|
81
|
87
|
|
82
|
|
-uint8_t*
|
|
88
|
+int16_t*
|
83
|
89
|
LSM303::getRawAccelData()
|
84
|
90
|
{
|
85
|
91
|
return &accel[0];
|
86
|
92
|
}
|
87
|
93
|
|
88
|
|
-uint8_t*
|
|
94
|
+int16_t*
|
89
|
95
|
LSM303::getRawCoorData()
|
90
|
96
|
{
|
91
|
97
|
return &coor[0];
|
92
|
98
|
}
|
93
|
99
|
|
94
|
|
-uint8_t
|
95
|
|
-LSM303::getAccelY()
|
|
100
|
+int16_t
|
|
101
|
+LSM303::getAccelX()
|
96
|
102
|
{
|
97
|
|
- return accel[2];
|
|
103
|
+ return accel[X];
|
98
|
104
|
}
|
99
|
105
|
|
100
|
|
-uint8_t
|
101
|
|
-LSM303::getAccelZ()
|
|
106
|
+int16_t
|
|
107
|
+LSM303::getAccelY()
|
102
|
108
|
{
|
103
|
|
- return accel[0];
|
|
109
|
+ return accel[Y];
|
104
|
110
|
}
|
105
|
111
|
|
106
|
|
-uint8_t
|
107
|
|
-LSM303::getAccelX()
|
|
112
|
+int16_t
|
|
113
|
+LSM303::getAccelZ()
|
108
|
114
|
{
|
109
|
|
- return accel[1];
|
|
115
|
+ return accel[Z];
|
110
|
116
|
}
|
111
|
117
|
|
112
|
118
|
mraa_result_t
|
|
@@ -124,15 +130,35 @@ LSM303::getCoordinates()
|
124
|
130
|
}
|
125
|
131
|
// convert to coordinates
|
126
|
132
|
for (int i=0; i<3; i++) {
|
127
|
|
- coor[i] = (buf[2*i] << 8) | buf[(2*i)+1];
|
|
133
|
+ coor[i] = (int16_t(buf[2*i] << 8))
|
|
134
|
+ | int16_t(buf[(2*i)+1]);
|
128
|
135
|
}
|
129
|
|
- // note that coor array is in XZY order
|
|
136
|
+ // swap elements 1 and 2 to get things in natural XYZ order
|
|
137
|
+ int16_t t = coor[2];
|
|
138
|
+ coor[2] = coor[1];
|
|
139
|
+ coor[1] = t;
|
130
|
140
|
//printf("X=%x, Y=%x, Z=%x\n", coor[X], coor[Y], coor[Z]);
|
131
|
141
|
|
132
|
142
|
return ret;
|
133
|
143
|
}
|
134
|
144
|
|
|
145
|
+int16_t
|
|
146
|
+LSM303::getCoorX() {
|
|
147
|
+ return coor[X];
|
|
148
|
+}
|
|
149
|
+
|
|
150
|
+int16_t
|
|
151
|
+LSM303::getCoorY() {
|
|
152
|
+ return coor[Y];
|
|
153
|
+}
|
|
154
|
+
|
|
155
|
+int16_t
|
|
156
|
+LSM303::getCoorZ() {
|
|
157
|
+ return coor[Z];
|
|
158
|
+}
|
|
159
|
+
|
135
|
160
|
// helper function that writes a value to the acc and then reads
|
|
161
|
+// FIX: shouldn't this be write-then-read?
|
136
|
162
|
int
|
137
|
163
|
LSM303::readThenWrite(uint8_t reg)
|
138
|
164
|
{
|
|
@@ -147,9 +173,12 @@ LSM303::getAcceleration()
|
147
|
173
|
{
|
148
|
174
|
mraa_result_t ret = MRAA_SUCCESS;
|
149
|
175
|
|
150
|
|
- accel[2] = (readThenWrite(OUT_X_L_A) << 8) | (readThenWrite(OUT_X_H_A));
|
151
|
|
- accel[0] = (readThenWrite(OUT_Y_L_A) << 8) | (readThenWrite(OUT_Y_H_A));
|
152
|
|
- accel[1] = (readThenWrite(OUT_Z_L_A) << 8) | (readThenWrite(OUT_Z_H_A));
|
|
176
|
+ accel[X] = (int16_t(readThenWrite(OUT_X_H_A)) << 8)
|
|
177
|
+ | int16_t(readThenWrite(OUT_X_L_A));
|
|
178
|
+ accel[Y] = (int16_t(readThenWrite(OUT_Y_H_A)) << 8)
|
|
179
|
+ | int16_t(readThenWrite(OUT_Y_L_A));
|
|
180
|
+ accel[Z] = (int16_t(readThenWrite(OUT_Z_H_A)) << 8)
|
|
181
|
+ | int16_t(readThenWrite(OUT_Z_L_A));
|
153
|
182
|
//printf("X=%x, Y=%x, Z=%x\n", accel[X], accel[Y], accel[Z]);
|
154
|
183
|
|
155
|
184
|
return ret;
|