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);
135 writeReg(LSM303D_ADDR, LSM303D_REG_CTRL2, 0x18);
136 if (lastError) {
return; }
142 writeReg(L3GD20H_ADDR, L3GD20H_REG_CTRL1, 0xFF);
143 if (lastError) {
return; }
147 writeReg(L3GD20H_ADDR, L3GD20H_REG_CTRL4, 0x20);
156 writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL1_XL, 0x3C);
157 if (lastError) {
return; }
163 writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL2_G, 0x7C);
178 writeReg(L3GD20H_ADDR, L3GD20H_REG_CTRL1, 0xFF);
179 if (lastError) {
return; }
183 writeReg(L3GD20H_ADDR, L3GD20H_REG_CTRL4, 0x20);
192 writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL2_G, 0x7C);
207 writeReg(LSM303D_ADDR, LSM303D_REG_CTRL1, 0x37);
216 writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL1_XL, 0x10);
223 Wire.beginTransmission(addr);
226 lastError = Wire.endTransmission();
231 Wire.beginTransmission(addr);
233 lastError = Wire.endTransmission();
234 if (lastError) {
return 0; }
236 uint8_t byteCount = Wire.requestFrom(addr, (uint8_t)1);
252 readAxes16Bit(LSM303D_ADDR, LSM303D_REG_OUT_X_L_A | (1 << 7),
a);
257 readAxes16Bit(LSM6DS33_ADDR, LSM6DS33_REG_OUTX_L_XL,
a);
269 readAxes16Bit(L3GD20H_ADDR, L3GD20H_REG_OUT_X_L | (1 << 7),
g);
274 readAxes16Bit(LSM6DS33_ADDR, LSM6DS33_REG_OUTX_L_G,
g);
286 readAxes16Bit(LSM303D_ADDR, LSM303D_REG_OUT_X_L_M | (1 << 7),
m);
291 readAxes16Bit(LIS3MDL_ADDR, LIS3MDL_REG_OUT_X_L | (1 << 7),
m);
301 if (lastError) {
return; }
303 if (lastError) {
return; }
312 return readReg(LSM303D_ADDR, LSM303D_REG_STATUS_A) & 0x08;
315 return readReg(LSM6DS33_ADDR, LSM6DS33_REG_STATUS_REG) & 0x01;
325 return readReg(L3GD20H_ADDR, L3GD20H_REG_STATUS) & 0x08;
328 return readReg(LSM6DS33_ADDR, LSM6DS33_REG_STATUS_REG) & 0x02;
338 return readReg(LSM303D_ADDR, LSM303D_REG_STATUS_M) & 0x08;
341 return readReg(LIS3MDL_ADDR, LIS3MDL_REG_STATUS_REG) & 0x08;
346int16_t Zumo32U4IMU::testReg(uint8_t addr, uint8_t reg)
348 Wire.beginTransmission(addr);
350 if (Wire.endTransmission() != 0)
352 return TEST_REG_ERROR;
355 uint8_t byteCount = Wire.requestFrom(addr, (uint8_t)1);
358 return TEST_REG_ERROR;
363void Zumo32U4IMU::readAxes16Bit(uint8_t addr, uint8_t firstReg, vector<int16_t> & v)
365 Wire.beginTransmission(addr);
366 Wire.write(firstReg);
367 lastError = Wire.endTransmission();
368 if (lastError) {
return; }
370 uint8_t byteCount = (Wire.requestFrom(addr, (uint8_t)6));
376 uint8_t xl = Wire.read();
377 uint8_t xh = Wire.read();
378 uint8_t yl = Wire.read();
379 uint8_t yh = Wire.read();
380 uint8_t zl = Wire.read();
381 uint8_t zh = Wire.read();
384 v.x = (int16_t)(xh << 8 | xl);
385 v.y = (int16_t)(yh << 8 | yl);
386 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.