4 #define TEST_REG_ERROR -1
6 #define LSM303D_WHO_ID 0x49
7 #define L3GD20H_WHO_ID 0xD7
8 #define LSM6DS33_WHO_ID 0x69
9 #define LIS3MDL_WHO_ID 0x3D
13 if (testReg(LSM303D_ADDR, LSM303D_REG_WHO_AM_I) == LSM303D_WHO_ID &&
14 testReg(L3GD20H_ADDR, L3GD20H_REG_WHO_AM_I) == L3GD20H_WHO_ID)
19 else if (testReg(LSM6DS33_ADDR, LSM6DS33_REG_WHO_AM_I) == LSM6DS33_WHO_ID &&
20 testReg( LIS3MDL_ADDR, LIS3MDL_REG_WHO_AM_I) == LIS3MDL_WHO_ID)
41 writeReg(LSM303D_ADDR, LSM303D_REG_CTRL1, 0x57);
42 if (lastError) {
return; }
46 writeReg(LSM303D_ADDR, LSM303D_REG_CTRL2, 0x00);
47 if (lastError) {
return; }
53 writeReg(LSM303D_ADDR, LSM303D_REG_CTRL5, 0x64);
54 if (lastError) {
return; }
58 writeReg(LSM303D_ADDR, LSM303D_REG_CTRL6, 0x20);
59 if (lastError) {
return; }
63 writeReg(LSM303D_ADDR, LSM303D_REG_CTRL7, 0x00);
64 if (lastError) {
return; }
70 writeReg(L3GD20H_ADDR, L3GD20H_REG_CTRL1, 0x7F);
71 if (lastError) {
return; }
75 writeReg(L3GD20H_ADDR, L3GD20H_REG_CTRL4, 0x00);
84 writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL1_XL, 0x30);
85 if (lastError) {
return; }
91 writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL2_G, 0x50);
92 if (lastError) {
return; }
98 writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL3_C, 0x04);
99 if (lastError) {
return; }
105 writeReg(LIS3MDL_ADDR, LIS3MDL_REG_CTRL_REG1, 0x70);
106 if (lastError) {
return; }
110 writeReg(LIS3MDL_ADDR, LIS3MDL_REG_CTRL_REG2, 0x00);
111 if (lastError) {
return; }
115 writeReg(LIS3MDL_ADDR, LIS3MDL_REG_CTRL_REG3, 0x00);
116 if (lastError) {
return; }
120 writeReg(LIS3MDL_ADDR, LIS3MDL_REG_CTRL_REG4, 0x0C);
138 writeReg(LSM303D_ADDR, LSM303D_REG_CTRL2, 0x18);
139 if (lastError) {
return; }
145 writeReg(L3GD20H_ADDR, L3GD20H_REG_CTRL1, 0xFF);
146 if (lastError) {
return; }
150 writeReg(L3GD20H_ADDR, L3GD20H_REG_CTRL4, 0x20);
159 writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL1_XL, 0x3C);
160 if (lastError) {
return; }
166 writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL2_G, 0x7C);
184 writeReg(L3GD20H_ADDR, L3GD20H_REG_CTRL1, 0xFF);
185 if (lastError) {
return; }
189 writeReg(L3GD20H_ADDR, L3GD20H_REG_CTRL4, 0x20);
198 writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL2_G, 0x7C);
216 writeReg(LSM303D_ADDR, LSM303D_REG_CTRL1, 0x37);
225 writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL1_XL, 0x10);
235 Wire.beginTransmission(addr);
238 lastError = Wire.endTransmission();
243 Wire.beginTransmission(addr);
245 lastError = Wire.endTransmission();
246 if (lastError) {
return 0; }
248 uint8_t byteCount = Wire.requestFrom(addr, (uint8_t)1);
264 readAxes16Bit(LSM303D_ADDR, LSM303D_REG_OUT_X_L_A | (1 << 7),
a);
269 readAxes16Bit(LSM6DS33_ADDR, LSM6DS33_REG_OUTX_L_XL,
a);
284 readAxes16Bit(L3GD20H_ADDR, L3GD20H_REG_OUT_X_L | (1 << 7),
g);
289 readAxes16Bit(LSM6DS33_ADDR, LSM6DS33_REG_OUTX_L_G,
g);
304 readAxes16Bit(LSM303D_ADDR, LSM303D_REG_OUT_X_L_M | (1 << 7),
m);
309 readAxes16Bit(LIS3MDL_ADDR, LIS3MDL_REG_OUT_X_L | (1 << 7),
m);
322 if (lastError) {
return; }
324 if (lastError) {
return; }
333 return readReg(LSM303D_ADDR, LSM303D_REG_STATUS_A) & 0x08;
336 return readReg(LSM6DS33_ADDR, LSM6DS33_REG_STATUS_REG) & 0x01;
348 return readReg(L3GD20H_ADDR, L3GD20H_REG_STATUS) & 0x08;
351 return readReg(LSM6DS33_ADDR, LSM6DS33_REG_STATUS_REG) & 0x02;
363 return readReg(LSM303D_ADDR, LSM303D_REG_STATUS_M) & 0x08;
366 return readReg(LIS3MDL_ADDR, LIS3MDL_REG_STATUS_REG) & 0x08;
373 int16_t Zumo32U4IMU::testReg(uint8_t addr, uint8_t reg)
375 Wire.beginTransmission(addr);
377 if (Wire.endTransmission() != 0)
379 return TEST_REG_ERROR;
382 uint8_t byteCount = Wire.requestFrom(addr, (uint8_t)1);
385 return TEST_REG_ERROR;
390 void Zumo32U4IMU::readAxes16Bit(uint8_t addr, uint8_t firstReg, vector<int16_t> & v)
392 Wire.beginTransmission(addr);
393 Wire.write(firstReg);
394 lastError = Wire.endTransmission();
395 if (lastError) {
return; }
397 uint8_t byteCount = (Wire.requestFrom(addr, (uint8_t)6));
403 uint8_t xl = Wire.read();
404 uint8_t xh = Wire.read();
405 uint8_t yl = Wire.read();
406 uint8_t yh = Wire.read();
407 uint8_t zl = Wire.read();
408 uint8_t zh = Wire.read();
411 v.x = (int16_t)(xh << 8 | xl);
412 v.y = (int16_t)(yh << 8 | yl);
413 v.z = (int16_t)(zh << 8 | zl);
void readAcc()
Takes a reading from the accelerometer and makes the measurements available in a.
void readGyro()
Takes a reading from the gyro and makes the measurements available in g.
vector< int16_t > a
Raw accelerometer readings.
vector< int16_t > m
Raw magnetometer readings.
vector< int16_t > g
Raw gyro readings.
bool accDataReady()
Indicates whether the accelerometer has new measurement data ready.
void configureForBalancing()
Configures the sensors with settings optimized for balancing.
bool gyroDataReady()
Indicates whether the gyro has new measurement data ready.
void readMag()
Takes a reading from the magnetometer and makes the measurements available in m.
uint8_t readReg(uint8_t addr, uint8_t reg)
Reads an 8-bit sensor register.
void read()
Takes a reading from all three sensors (accelerometer, gyro, and magnetometer) and makes their measur...
void writeReg(uint8_t addr, uint8_t reg, uint8_t value)
Writes an 8-bit sensor register.
bool init()
Initializes the inertial sensors and detects their type.
void enableDefault()
Enables all of the inertial sensors with a default configuration.
void configureForTurnSensing()
Configures the sensors with settings optimized for turn sensing.
bool magDataReady()
Indicates whether the magnetometer has new measurement data ready.
void configureForFaceUphill()
Configures the sensors with settings optimized for the FaceUphill example program.