Pololu3piPlus32U4 library
Pololu3piPlus32U4IMU_declaration.h
Go to the documentation of this file.
1 // Copyright (C) Pololu Corporation. See www.pololu.com for details.
2 
4 
5 #pragma once
6 
7 #include <Arduino.h>
8 
9 namespace Pololu3piPlus32U4
10 {
11 
12 
17 #define LSM6DS33_ADDR 0b1101011
18 #define LIS3MDL_ADDR 0b0011110
20 
25 #define LSM6DS33_REG_WHO_AM_I 0x0F
26 #define LSM6DS33_REG_CTRL1_XL 0x10
27 #define LSM6DS33_REG_CTRL2_G 0x11
28 #define LSM6DS33_REG_CTRL3_C 0x12
29 #define LSM6DS33_REG_STATUS_REG 0x1E
30 #define LSM6DS33_REG_OUTX_L_G 0x22
31 #define LSM6DS33_REG_OUTX_L_XL 0x28
32 
33 #define LIS3MDL_REG_WHO_AM_I 0x0F
34 #define LIS3MDL_REG_CTRL_REG1 0x20
35 #define LIS3MDL_REG_CTRL_REG2 0x21
36 #define LIS3MDL_REG_CTRL_REG3 0x22
37 #define LIS3MDL_REG_CTRL_REG4 0x23
38 #define LIS3MDL_REG_STATUS_REG 0x27
39 #define LIS3MDL_REG_OUT_X_L 0x28
41 
43 enum class IMUType : uint8_t {
45  Unknown,
48 };
49 
66 class IMU
67 {
68 public:
69 
71  template <typename T> struct vector
72  {
73  T x, y, z;
74  };
75 
77  vector<int16_t> a = {0, 0, 0};
78 
80  vector<int16_t> g = {0, 0, 0};
81 
83  vector<int16_t> m = {0, 0, 0};
84 
87  uint8_t getLastError() { return lastError; }
88 
92  bool init();
93 
99  IMUType getType() { return type; }
100 
102  void enableDefault();
103 
106 
109  void configureForFaceUphill();
110 
114 
120  void writeReg(uint8_t addr, uint8_t reg, uint8_t value);
121 
128  uint8_t readReg(uint8_t addr, uint8_t reg);
129 
132  void readAcc();
133 
136  void readGyro();
137 
140  void readMag();
141 
145  void read();
146 
151  bool accDataReady();
152 
156  bool gyroDataReady();
157 
161  bool magDataReady();
162 
163 private:
164 
165  uint8_t lastError = 0;
166  IMUType type = IMUType::Unknown;
167 
168  int16_t testReg(uint8_t addr, uint8_t reg);
169  void readAxes16Bit(uint8_t addr, uint8_t firstReg, vector<int16_t> & v);
170 };
171 
172 }
Interfaces with the inertial sensors on the 3pi+ 32U4.
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.
IMUType getType()
Returns the type of the inertial sensors on the 3pi+ 32U4.
void writeReg(uint8_t addr, uint8_t reg, uint8_t value)
Writes an 8-bit sensor register.
vector< int16_t > m
Raw magnetometer readings.
uint8_t getLastError()
Returns 0 if the last I2C communication with the IMU was successful, or a non-zero status code if the...
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.
IMUType
The type of the inertial sensors.
@ Unknown
Unknown or unrecognized.
@ LSM6DS33_LIS3MDL
LSM6DS33 gyro + accelerometer, LIS3MDL magnetometer.
Represents a 3-dimensional vector with x, y, and z components.