forked from ivyknob/bno055_stm32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbno055.c
238 lines (201 loc) · 6.82 KB
/
bno055.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "bno055.h"
#include <string.h>
uint16_t accelScale = 100;
uint16_t tempScale = 1;
uint16_t angularRateScale = 16;
uint16_t eulerScale = 16;
uint16_t magScale = 16;
uint16_t quaScale = (1<<14); // 2^14
void bno055_setPage(uint8_t page) { bno055_writeData(BNO055_PAGE_ID, page); }
bno055_opmode_t bno055_getOperationMode() {
bno055_opmode_t mode;
bno055_readData(BNO055_OPR_MODE, &mode, 1);
return mode;
}
void bno055_setOperationMode(bno055_opmode_t mode) {
bno055_writeData(BNO055_OPR_MODE, mode);
if (mode == BNO055_OPERATION_MODE_CONFIG) {
bno055_delay(19);
} else {
bno055_delay(7);
}
}
void bno055_setOperationModeConfig() {
bno055_setOperationMode(BNO055_OPERATION_MODE_CONFIG);
}
void bno055_setOperationModeNDOF() {
bno055_setOperationMode(BNO055_OPERATION_MODE_NDOF);
}
void bno055_setExternalCrystalUse(bool state) {
bno055_setPage(0);
uint8_t tmp = 0;
bno055_readData(BNO055_SYS_TRIGGER, &tmp, 1);
tmp |= (state == true) ? 0x80 : 0x0;
bno055_writeData(BNO055_SYS_TRIGGER, tmp);
bno055_delay(700);
}
void bno055_enableExternalCrystal() { bno055_setExternalCrystalUse(true); }
void bno055_disableExternalCrystal() { bno055_setExternalCrystalUse(false); }
void bno055_reset() {
bno055_writeData(BNO055_SYS_TRIGGER, 0x20);
bno055_delay(700);
}
int8_t bno055_getTemp() {
bno055_setPage(0);
uint8_t t;
bno055_readData(BNO055_TEMP, &t, 1);
return t;
}
void bno055_setup() {
bno055_reset();
uint8_t id = 0;
bno055_readData(BNO055_CHIP_ID, &id, 1);
if (id != BNO055_ID) {
printf("Can't find BNO055, id: 0x%02x. Please check your wiring.\r\n", id);
}
bno055_setPage(0);
bno055_writeData(BNO055_SYS_TRIGGER, 0x0);
// Select BNO055 config mode
bno055_setOperationModeConfig();
bno055_delay(10);
}
int16_t bno055_getSWRevision() {
bno055_setPage(0);
uint8_t buffer[2];
bno055_readData(BNO055_SW_REV_ID_LSB, buffer, 2);
return (int16_t)((buffer[1] << 8) | buffer[0]);
}
uint8_t bno055_getBootloaderRevision() {
bno055_setPage(0);
uint8_t tmp;
bno055_readData(BNO055_BL_REV_ID, &tmp, 1);
return tmp;
}
uint8_t bno055_getSystemStatus() {
bno055_setPage(0);
uint8_t tmp;
bno055_readData(BNO055_SYS_STATUS, &tmp, 1);
return tmp;
}
bno055_self_test_result_t bno055_getSelfTestResult() {
bno055_setPage(0);
uint8_t tmp;
bno055_self_test_result_t res = {
.mcuState = 0, .gyrState = 0, .magState = 0, .accState = 0};
bno055_readData(BNO055_ST_RESULT, &tmp, 1);
res.mcuState = (tmp >> 3) & 0x01;
res.gyrState = (tmp >> 2) & 0x01;
res.magState = (tmp >> 1) & 0x01;
res.accState = (tmp >> 0) & 0x01;
return res;
}
uint8_t bno055_getSystemError() {
bno055_setPage(0);
uint8_t tmp;
bno055_readData(BNO055_SYS_ERR, &tmp, 1);
return tmp;
}
bno055_calibration_state_t bno055_getCalibrationState() {
bno055_setPage(0);
bno055_calibration_state_t cal = {.sys = 0, .gyro = 0, .mag = 0, .accel = 0};
uint8_t calState = 0;
bno055_readData(BNO055_CALIB_STAT, &calState, 1);
cal.sys = (calState >> 6) & 0x03;
cal.gyro = (calState >> 4) & 0x03;
cal.accel = (calState >> 2) & 0x03;
cal.mag = calState & 0x03;
return cal;
}
bno055_calibration_data_t bno055_getCalibrationData() {
bno055_calibration_data_t calData;
uint8_t buffer[22];
bno055_opmode_t operationMode = bno055_getOperationMode();
bno055_setOperationModeConfig();
bno055_setPage(0);
bno055_readData(BNO055_ACC_OFFSET_X_LSB, buffer, 22);
// Assumes little endian processor
memcpy(&calData.offset.accel, buffer, 6);
memcpy(&calData.offset.mag, buffer + 6, 6);
memcpy(&calData.offset.gyro, buffer + 12, 6);
memcpy(&calData.radius.accel, buffer + 18, 2);
memcpy(&calData.radius.mag, buffer + 20, 2);
bno055_setOperationMode(operationMode);
return calData;
}
void bno055_setCalibrationData(bno055_calibration_data_t calData) {
uint8_t buffer[22];
bno055_opmode_t operationMode = bno055_getOperationMode();
bno055_setOperationModeConfig();
bno055_setPage(0);
// Assumes litle endian processor
memcpy(buffer, &calData.offset.accel, 6);
memcpy(buffer + 6, &calData.offset.mag, 6);
memcpy(buffer + 12, &calData.offset.gyro, 6);
memcpy(buffer + 18, &calData.radius.accel, 2);
memcpy(buffer + 20, &calData.radius.mag, 2);
for (uint8_t i=0; i < 22; i++) {
// TODO(oliv4945): create multibytes write
bno055_writeData(BNO055_ACC_OFFSET_X_LSB+i, buffer[i]);
}
bno055_setOperationMode(operationMode);
}
bno055_vector_t bno055_getVector(uint8_t vec) {
bno055_setPage(0);
uint8_t buffer[8]; // Quaternion need 8 bytes
if (vec == BNO055_VECTOR_QUATERNION)
bno055_readData(vec, buffer, 8);
else
bno055_readData(vec, buffer, 6);
double scale = 1;
if (vec == BNO055_VECTOR_MAGNETOMETER) {
scale = magScale;
} else if (vec == BNO055_VECTOR_ACCELEROMETER ||
vec == BNO055_VECTOR_LINEARACCEL || vec == BNO055_VECTOR_GRAVITY) {
scale = accelScale;
} else if (vec == BNO055_VECTOR_GYROSCOPE) {
scale = angularRateScale;
} else if (vec == BNO055_VECTOR_EULER) {
scale = eulerScale;
} else if (vec == BNO055_VECTOR_QUATERNION) {
scale = quaScale;
}
bno055_vector_t xyz = {.w = 0, .x = 0, .y = 0, .z = 0};
if (vec == BNO055_VECTOR_QUATERNION) {
xyz.w = (int16_t)((buffer[1] << 8) | buffer[0]) / scale;
xyz.x = (int16_t)((buffer[3] << 8) | buffer[2]) / scale;
xyz.y = (int16_t)((buffer[5] << 8) | buffer[4]) / scale;
xyz.z = (int16_t)((buffer[7] << 8) | buffer[6]) / scale;
} else {
xyz.x = (int16_t)((buffer[1] << 8) | buffer[0]) / scale;
xyz.y = (int16_t)((buffer[3] << 8) | buffer[2]) / scale;
xyz.z = (int16_t)((buffer[5] << 8) | buffer[4]) / scale;
}
return xyz;
}
bno055_vector_t bno055_getVectorAccelerometer() {
return bno055_getVector(BNO055_VECTOR_ACCELEROMETER);
}
bno055_vector_t bno055_getVectorMagnetometer() {
return bno055_getVector(BNO055_VECTOR_MAGNETOMETER);
}
bno055_vector_t bno055_getVectorGyroscope() {
return bno055_getVector(BNO055_VECTOR_GYROSCOPE);
}
bno055_vector_t bno055_getVectorEuler() {
return bno055_getVector(BNO055_VECTOR_EULER);
}
bno055_vector_t bno055_getVectorLinearAccel() {
return bno055_getVector(BNO055_VECTOR_LINEARACCEL);
}
bno055_vector_t bno055_getVectorGravity() {
return bno055_getVector(BNO055_VECTOR_GRAVITY);
}
bno055_vector_t bno055_getVectorQuaternion() {
return bno055_getVector(BNO055_VECTOR_QUATERNION);
}
void bno055_setAxisMap(bno055_axis_map_t axis) {
uint8_t axisRemap = (axis.z << 4) | (axis.y << 2) | (axis.x);
uint8_t axisMapSign = (axis.x_sign << 2) | (axis.y_sign << 1) | (axis.z_sign);
bno055_writeData(BNO055_AXIS_MAP_CONFIG, axisRemap);
bno055_writeData(BNO055_AXIS_MAP_SIGN, axisMapSign);
}