Pololu3piPlus32U4 library
Pololu3piPlus32U4IMU.cpp
1 // Copyright (C) Pololu Corporation. See www.pololu.com for details.
2 
4 
5 #define LSM6DS33_WHO_ID 0x69
6 #define LIS3MDL_WHO_ID 0x3D
7 
8 namespace Pololu3piPlus32U4
9 {
10 
11 bool IMU::init()
12 {
13  if (testReg(LSM6DS33_ADDR, LSM6DS33_REG_WHO_AM_I) == LSM6DS33_WHO_ID &&
14  testReg( LIS3MDL_ADDR, LIS3MDL_REG_WHO_AM_I) == LIS3MDL_WHO_ID)
15  {
17  return true;
18  }
19  else
20  {
21  return false;
22  }
23 }
24 
26 {
27  switch (type)
28  {
30 
31  // Accelerometer
32 
33  // 0x30 = 0b00110000
34  // ODR = 0011 (52 Hz (high performance)); FS_XL = 00 (+/- 2 g full scale)
35  writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL1_XL, 0x30);
36  if (lastError) { return; }
37 
38  // Gyro
39 
40  // 0x50 = 0b01010000
41  // ODR = 0101 (208 Hz (high performance)); FS_G = 00 (+/- 245 dps full scale)
42  writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL2_G, 0x50);
43  if (lastError) { return; }
44 
45  // Accelerometer + Gyro
46 
47  // 0x04 = 0b00000100
48  // IF_INC = 1 (automatically increment register address)
49  writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL3_C, 0x04);
50  if (lastError) { return; }
51 
52  // Magnetometer
53 
54  // 0x70 = 0b01110000
55  // OM = 11 (ultra-high-performance mode for X and Y); DO = 100 (10 Hz ODR)
56  writeReg(LIS3MDL_ADDR, LIS3MDL_REG_CTRL_REG1, 0x70);
57  if (lastError) { return; }
58 
59  // 0x00 = 0b00000000
60  // FS = 00 (+/- 4 gauss full scale)
61  writeReg(LIS3MDL_ADDR, LIS3MDL_REG_CTRL_REG2, 0x00);
62  if (lastError) { return; }
63 
64  // 0x00 = 0b00000000
65  // MD = 00 (continuous-conversion mode)
66  writeReg(LIS3MDL_ADDR, LIS3MDL_REG_CTRL_REG3, 0x00);
67  if (lastError) { return; }
68 
69  // 0x0C = 0b00001100
70  // OMZ = 11 (ultra-high-performance mode for Z)
71  writeReg(LIS3MDL_ADDR, LIS3MDL_REG_CTRL_REG4, 0x0C);
72  return;
73  default:
74  return;
75  }
76 }
77 
79 {
80  switch (type)
81  {
83 
84  // Gyro
85 
86  // 0x7C = 0b01111100
87  // ODR = 0111 (833 Hz (high performance)); FS_G = 11 (+/- 2000 dps full scale)
88  writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL2_G, 0x7C);
89  return;
90  default:
91  return;
92  }
93 }
94 
96 {
97  switch (type)
98  {
100 
101  // Accelerometer
102 
103  // 0x10 = 0b00010000
104  // ODR = 0001 (13 Hz (high performance)); FS_XL = 00 (+/- 2 g full scale)
105  writeReg(LSM6DS33_ADDR, LSM6DS33_REG_CTRL1_XL, 0x10);
106  return;
107  default:
108  return;
109  }
110 }
111 
113 {
114  switch (type)
115  {
117 
118  // Magnetometer
119 
120  // 0x7C = 0b01111100
121  // OM = 11 (ultra-high-performance mode for X and Y); DO = 111 (80 Hz ODR)
122  writeReg(LIS3MDL_ADDR, LIS3MDL_REG_CTRL_REG1, 0x7C);
123  return;
124  default:
125  return;
126  }
127 }
128 
129 // Reads the 3 accelerometer channels and stores them in vector a
130 void IMU::readAcc(void)
131 {
132  switch (type)
133  {
135  // assumes register address auto-increment is enabled (IF_INC in CTRL3_C)
136  readAxes16Bit(LSM6DS33_ADDR, LSM6DS33_REG_OUTX_L_XL, a);
137  return;
138  default:
139  return;
140  }
141 }
142 
143 // Reads the 3 gyro channels and stores them in vector g
145 {
146  switch (type)
147  {
149  // assumes register address auto-increment is enabled (IF_INC in CTRL3_C)
150  readAxes16Bit(LSM6DS33_ADDR, LSM6DS33_REG_OUTX_L_G, g);
151  return;
152  default:
153  return;
154  }
155 }
156 
157 // Reads the 3 magnetometer channels and stores them in vector m
159 {
160  switch (type)
161  {
163  // set MSB of register address for auto-increment
164  readAxes16Bit(LIS3MDL_ADDR, LIS3MDL_REG_OUT_X_L | (1 << 7), m);
165  return;
166  default:
167  return;
168  }
169 }
170 
171 // Reads all 9 accelerometer, gyro, and magnetometer channels and stores them
172 // in the respective vectors
173 void IMU::read()
174 {
175  readAcc();
176  if (lastError) { return; }
177  readGyro();
178  if (lastError) { return; }
179  readMag();
180 }
181 
183 {
184  switch (type)
185  {
187  return readReg(LSM6DS33_ADDR, LSM6DS33_REG_STATUS_REG) & 0x01;
188  default:
189  return false;
190  }
191 }
192 
194 {
195  switch (type)
196  {
198  return readReg(LSM6DS33_ADDR, LSM6DS33_REG_STATUS_REG) & 0x02;
199  default:
200  return false;
201  }
202 }
203 
205 {
206  switch (type)
207  {
209  return readReg(LIS3MDL_ADDR, LIS3MDL_REG_STATUS_REG) & 0x08;
210  default:
211  return false;
212  }
213 }
214 
215 }
void configureForFaceUphill()
Configures the sensors with settings optimized for the FaceUphill example program.
void readGyro()
Takes a reading from the gyro and makes the measurements available in g.
bool gyroDataReady()
Indicates whether the gyro has new measurement data ready.
bool accDataReady()
Indicates whether the accelerometer has new measurement data ready.
bool init()
Initializes the inertial sensors and detects their type.
void writeReg(uint8_t addr, uint8_t reg, uint8_t value)
Writes an 8-bit sensor register.
vector< int16_t > m
Raw magnetometer readings.
void configureForCompassHeading()
Configures the sensors with settings optimized for determining a compass heading with the magnetomete...
void readAcc()
Takes a reading from the accelerometer and makes the measurements available in a.
void readMag()
Takes a reading from the magnetometer and makes the measurements available in m.
void enableDefault()
Enables all of the inertial sensors with a default configuration.
uint8_t readReg(uint8_t addr, uint8_t reg)
Reads an 8-bit sensor register.
vector< int16_t > a
Raw accelerometer readings.
vector< int16_t > g
Raw gyro readings.
void read()
Takes a reading from all three sensors (accelerometer, gyro, and magnetometer) and makes their measur...
void configureForTurnSensing()
Configures the sensors with settings optimized for turn sensing.
bool magDataReady()
Indicates whether the magnetometer has new measurement data ready.
Top-level namespace for the Pololu3piPlus32U4 library.
@ LSM6DS33_LIS3MDL
LSM6DS33 gyro + accelerometer, LIS3MDL magnetometer.