-
Notifications
You must be signed in to change notification settings - Fork 13
/
DFRobot_BMX160.cpp
267 lines (247 loc) · 7.5 KB
/
DFRobot_BMX160.cpp
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*!
* @file DFRobot_BMX160.cpp
* @brief define DFRobot_BMX160 class infrastructure, the implementation of basic methods
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [luoyufeng] ([email protected])
* @maintainer [Fary]([email protected])
* @version V1.0
* @date 2021-10-20
* @url https://github.com/DFRobot/DFRobot_BMX160
*/
#include "DFRobot_BMX160.h"
DFRobot_BMX160::DFRobot_BMX160(TwoWire *pWire)
{
_pWire = pWire;
Obmx160 = (sBmx160Dev_t *)malloc(sizeof(sBmx160Dev_t));
Oaccel = ( sBmx160SensorData_t*)malloc(sizeof( sBmx160SensorData_t));
Ogyro = ( sBmx160SensorData_t*)malloc(sizeof( sBmx160SensorData_t));
Omagn = ( sBmx160SensorData_t*)malloc(sizeof( sBmx160SensorData_t));
}
const uint8_t int_mask_lookup_table[13] = {
BMX160_INT1_SLOPE_MASK,
BMX160_INT1_SLOPE_MASK,
BMX160_INT2_LOW_STEP_DETECT_MASK,
BMX160_INT1_DOUBLE_TAP_MASK,
BMX160_INT1_SINGLE_TAP_MASK,
BMX160_INT1_ORIENT_MASK,
BMX160_INT1_FLAT_MASK,
BMX160_INT1_HIGH_G_MASK,
BMX160_INT1_LOW_G_MASK,
BMX160_INT1_NO_MOTION_MASK,
BMX160_INT2_DATA_READY_MASK,
BMX160_INT2_FIFO_FULL_MASK,
BMX160_INT2_FIFO_WM_MASK
};
bool DFRobot_BMX160::begin()
{
_pWire->begin();
if (scan() == true){
softReset();
writeBmxReg(BMX160_COMMAND_REG_ADDR, 0x11);
delay(50);
/* Set gyro to normal mode */
writeBmxReg(BMX160_COMMAND_REG_ADDR, 0x15);
delay(100);
/* Set mag to normal mode */
writeBmxReg(BMX160_COMMAND_REG_ADDR, 0x19);
delay(10);
setMagnConf();
return true;
}
else
return false;
}
void DFRobot_BMX160::setLowPower(){
softReset();
delay(100);
setMagnConf();
delay(100);
writeBmxReg(BMX160_COMMAND_REG_ADDR, 0x12);
delay(100);
/* Set gyro to normal mode */
writeBmxReg(BMX160_COMMAND_REG_ADDR, 0x17);
delay(100);
/* Set mag to normal mode */
writeBmxReg(BMX160_COMMAND_REG_ADDR, 0x1B);
delay(100);
}
void DFRobot_BMX160::wakeUp(){
softReset();
delay(100);
setMagnConf();
delay(100);
writeBmxReg(BMX160_COMMAND_REG_ADDR, 0x11);
delay(100);
/* Set gyro to normal mode */
writeBmxReg(BMX160_COMMAND_REG_ADDR, 0x15);
delay(100);
/* Set mag to normal mode */
writeBmxReg(BMX160_COMMAND_REG_ADDR, 0x19);
delay(100);
}
bool DFRobot_BMX160::softReset()
{
int8_t rslt=BMX160_OK;
if (Obmx160 == NULL){
rslt = BMX160_E_NULL_PTR;
}
rslt = _softReset(Obmx160);
if (rslt == 0)
return true;
else
return false;
}
int8_t DFRobot_BMX160:: _softReset(sBmx160Dev_t *dev)
{
int8_t rslt=BMX160_OK;
uint8_t data = BMX160_SOFT_RESET_CMD;
if (dev==NULL){
rslt = BMX160_E_NULL_PTR;
}
writeBmxReg(BMX160_COMMAND_REG_ADDR, data);
delay(BMX160_SOFT_RESET_DELAY_MS);
if (rslt == BMX160_OK){
DFRobot_BMX160::defaultParamSettg(dev);
}
return rslt;
}
void DFRobot_BMX160::defaultParamSettg(sBmx160Dev_t *dev)
{
// Initializing accel and gyro params with
dev->gyroCfg.bw = BMX160_GYRO_BW_NORMAL_MODE;
dev->gyroCfg.odr = BMX160_GYRO_ODR_100HZ;
dev->gyroCfg.power = BMX160_GYRO_SUSPEND_MODE;
dev->gyroCfg.range = BMX160_GYRO_RANGE_2000_DPS;
dev->accelCfg.bw = BMX160_ACCEL_BW_NORMAL_AVG4;
dev->accelCfg.odr = BMX160_ACCEL_ODR_100HZ;
dev->accelCfg.power = BMX160_ACCEL_SUSPEND_MODE;
dev->accelCfg.range = BMX160_ACCEL_RANGE_2G;
dev->prevMagnCfg = dev->magnCfg;
dev->prevGyroCfg = dev->gyroCfg;
dev->prevAccelCfg = dev->accelCfg;
}
void DFRobot_BMX160::setMagnConf()
{
writeBmxReg(BMX160_MAGN_IF_0_ADDR, 0x80);
delay(50);
// Sleep mode
writeBmxReg(BMX160_MAGN_IF_3_ADDR, 0x01);
writeBmxReg(BMX160_MAGN_IF_2_ADDR, 0x4B);
// REPXY regular preset
writeBmxReg(BMX160_MAGN_IF_3_ADDR, 0x04);
writeBmxReg(BMX160_MAGN_IF_2_ADDR, 0x51);
// REPZ regular preset
writeBmxReg(BMX160_MAGN_IF_3_ADDR, 0x0E);
writeBmxReg(BMX160_MAGN_IF_2_ADDR, 0x52);
writeBmxReg(BMX160_MAGN_IF_3_ADDR, 0x02);
writeBmxReg(BMX160_MAGN_IF_2_ADDR, 0x4C);
writeBmxReg(BMX160_MAGN_IF_1_ADDR, 0x42);
writeBmxReg(BMX160_MAGN_CONFIG_ADDR, 0x08);
writeBmxReg(BMX160_MAGN_IF_0_ADDR, 0x03);
delay(50);
}
void DFRobot_BMX160::setGyroRange(eGyroRange_t bits){
switch (bits){
case eGyroRange_125DPS:
gyroRange = BMX160_GYRO_SENSITIVITY_125DPS;
break;
case eGyroRange_250DPS:
gyroRange = BMX160_GYRO_SENSITIVITY_250DPS;
break;
case eGyroRange_500DPS:
gyroRange = BMX160_GYRO_SENSITIVITY_500DPS;
break;
case eGyroRange_1000DPS:
gyroRange = BMX160_GYRO_SENSITIVITY_1000DPS;
break;
case eGyroRange_2000DPS:
gyroRange = BMX160_GYRO_SENSITIVITY_2000DPS;
break;
default:
gyroRange = BMX160_GYRO_SENSITIVITY_250DPS;
break;
}
}
void DFRobot_BMX160::setAccelRange(eAccelRange_t bits){
switch (bits){
case eAccelRange_2G:
accelRange = BMX160_ACCEL_MG_LSB_2G * 10;
break;
case eAccelRange_4G:
accelRange = BMX160_ACCEL_MG_LSB_4G * 10;
break;
case eAccelRange_8G:
accelRange = BMX160_ACCEL_MG_LSB_8G * 10;
break;
case eAccelRange_16G:
accelRange = BMX160_ACCEL_MG_LSB_16G * 10;
break;
default:
accelRange = BMX160_ACCEL_MG_LSB_2G * 10;
break;
}
}
void DFRobot_BMX160::getAllData(sBmx160SensorData_t *magn, sBmx160SensorData_t *gyro, sBmx160SensorData_t *accel){
uint8_t data[23] = {0};
int16_t x=0,y=0,z=0;
// put your main code here, to run repeatedly:
readReg(BMX160_MAG_DATA_ADDR, data, 23);
if(magn){
x = (int16_t) (((uint16_t)data[1] << 8) | data[0]);
y = (int16_t) (((uint16_t)data[3] << 8) | data[2]);
z = (int16_t) (((uint16_t)data[5] << 8) | data[4]);
magn->x = x * BMX160_MAGN_UT_LSB;
magn->y = y * BMX160_MAGN_UT_LSB;
magn->z = z * BMX160_MAGN_UT_LSB;
}
if(gyro){
x = (int16_t) (((uint16_t)data[9] << 8) | data[8]);
y = (int16_t) (((uint16_t)data[11] << 8) | data[10]);
z = (int16_t) (((uint16_t)data[13] << 8) | data[12]);
gyro->x = x * gyroRange;
gyro->y = y * gyroRange;
gyro->z = z * gyroRange;
}
if(accel){
x = (int16_t) (((uint16_t)data[15] << 8) | data[14]);
y = (int16_t) (((uint16_t)data[17] << 8) | data[16]);
z = (int16_t) (((uint16_t)data[19] << 8) | data[18]);
accel->x = x * accelRange;
accel->y = y * accelRange;
accel->z = z * accelRange;
}
}
void DFRobot_BMX160::writeBmxReg(uint8_t reg, uint8_t value)
{
uint8_t buffer[1] = {value};
writeReg(reg, buffer, 1);
}
void DFRobot_BMX160::writeReg(uint8_t reg, uint8_t *pBuf, uint16_t len)
{
_pWire->beginTransmission(_addr);
_pWire->write(reg);
for(uint16_t i = 0; i < len; i ++)
_pWire->write(pBuf[i]);
_pWire->endTransmission();
}
void DFRobot_BMX160::readReg(uint8_t reg, uint8_t *pBuf, uint16_t len)
{
_pWire->beginTransmission(_addr);
_pWire->write(reg);
if(_pWire->endTransmission() != 0)
return;
_pWire->requestFrom(_addr, (uint8_t) len);
for(uint16_t i = 0; i < len; i ++) {
pBuf[i] =_pWire->read();
}
_pWire->endTransmission();
}
bool DFRobot_BMX160::scan()
{
_pWire->beginTransmission(_addr);
if (_pWire->endTransmission() == 0){
return true;
}
return false;
}