Pololu Zumo Shield Arduino Library
ZumoIMU.h
Go to the documentation of this file.
1 // Copyright Pololu Corporation. For more information, see http://www.pololu.com/
2 
5 #pragma once
6 
12 #define LSM303DLHC_ACC_ADDR 0b0011001
13 #define LSM303DLHC_MAG_ADDR 0b0011110
14 #define LSM303D_ADDR 0b0011101
15 #define L3GD20H_ADDR 0b1101011
16 #define LSM6DS33_ADDR 0b1101011
17 #define LIS3MDL_ADDR 0b0011110
18 
25 #define LSM303DLHC_REG_CTRL_REG1_A 0x20
26 #define LSM303DLHC_REG_CTRL_REG4_A 0x23
27 #define LSM303DLHC_REG_STATUS_REG_A 0x27
28 #define LSM303DLHC_REG_OUT_X_L_A 0x28
29 
30 #define LSM303DLHC_REG_CRA_REG_M 0x00
31 #define LSM303DLHC_REG_CRB_REG_M 0x01
32 #define LSM303DLHC_REG_MR_REG_M 0x02
33 #define LSM303DLHC_REG_OUT_X_H_M 0x03
34 #define LSM303DLHC_REG_SR_REG_M 0x09
35 
36 #define LSM303D_REG_STATUS_M 0x07
37 #define LSM303D_REG_OUT_X_L_M 0x08
38 #define LSM303D_REG_WHO_AM_I 0x0F
39 #define LSM303D_REG_CTRL1 0x20
40 #define LSM303D_REG_CTRL2 0x21
41 #define LSM303D_REG_CTRL5 0x24
42 #define LSM303D_REG_CTRL6 0x25
43 #define LSM303D_REG_CTRL7 0x26
44 #define LSM303D_REG_STATUS_A 0x27
45 #define LSM303D_REG_OUT_X_L_A 0x28
46 
47 #define L3GD20H_REG_WHO_AM_I 0x0F
48 #define L3GD20H_REG_CTRL1 0x20
49 #define L3GD20H_REG_CTRL4 0x23
50 #define L3GD20H_REG_STATUS 0x27
51 #define L3GD20H_REG_OUT_X_L 0x28
52 
53 #define LSM6DS33_REG_WHO_AM_I 0x0F
54 #define LSM6DS33_REG_CTRL1_XL 0x10
55 #define LSM6DS33_REG_CTRL2_G 0x11
56 #define LSM6DS33_REG_CTRL3_C 0x12
57 #define LSM6DS33_REG_STATUS_REG 0x1E
58 #define LSM6DS33_REG_OUTX_L_G 0x22
59 #define LSM6DS33_REG_OUTX_L_XL 0x28
60 
61 #define LIS3MDL_REG_WHO_AM_I 0x0F
62 #define LIS3MDL_REG_CTRL_REG1 0x20
63 #define LIS3MDL_REG_CTRL_REG2 0x21
64 #define LIS3MDL_REG_CTRL_REG3 0x22
65 #define LIS3MDL_REG_CTRL_REG4 0x23
66 #define LIS3MDL_REG_STATUS_REG 0x27
67 #define LIS3MDL_REG_OUT_X_L 0x28
68 
71 enum class ZumoIMUType : uint8_t {
73  Unknown,
75  LSM303DLHC,
77  LSM303D_L3GD20H,
80 };
81 
90 class ZumoIMU
91 {
92 public:
93 
96  template <typename T> struct vector
97  {
98  T x, y, z;
99  };
100 
102  vector<int16_t> a = {0, 0, 0};
103 
105  vector<int16_t> g = {0, 0, 0};
106 
108  vector<int16_t> m = {0, 0, 0};
109 
112  uint8_t getLastError() { return lastError; }
113 
118  bool init();
119 
125  ZumoIMUType getType() { return type; }
126 
129  void enableDefault();
130 
134 
140  void writeReg(uint8_t addr, uint8_t reg, uint8_t value);
141 
148  uint8_t readReg(uint8_t addr, uint8_t reg);
149 
152  void readAcc();
153 
156  void readGyro();
157 
160  void readMag();
161 
165  void read();
166 
171  bool accDataReady();
172 
177  bool gyroDataReady();
178 
183  bool magDataReady();
184 
185 private:
186 
187  uint8_t lastError = 0;
188  ZumoIMUType type = ZumoIMUType::Unknown;
189 
190  int16_t testReg(uint8_t addr, uint8_t reg);
191  void readAxes16Bit(uint8_t addr, uint8_t firstReg, vector<int16_t> & v);
192  uint16_t swapBytes(uint16_t value);
193 };
ZumoIMU::accDataReady
bool accDataReady()
Indicates whether the accelerometer has new measurement data ready.
Definition: ZumoIMU.cpp:298
ZumoIMU::magDataReady
bool magDataReady()
Indicates whether the magnetometer has new measurement data ready.
Definition: ZumoIMU.cpp:327
ZumoIMU::readMag
void readMag()
Takes a reading from the magnetometer and makes the measurements available in m.
Definition: ZumoIMU.cpp:261
ZumoIMU::m
vector< int16_t > m
Raw magnetometer readings.
Definition: ZumoIMU.h:108
ZumoIMU::getType
ZumoIMUType getType()
Returns the type of the inertial sensors on the Zumo Shield.
Definition: ZumoIMU.h:125
ZumoIMU::g
vector< int16_t > g
Raw gyro readings.
Definition: ZumoIMU.h:105
ZumoIMU::gyroDataReady
bool gyroDataReady()
Indicates whether the gyro has new measurement data ready.
Definition: ZumoIMU.cpp:314
ZumoIMU::readGyro
void readGyro()
Takes a reading from the gyro and makes the measurements available in g.
Definition: ZumoIMU.cpp:244
ZumoIMU::readAcc
void readAcc()
Takes a reading from the accelerometer and makes the measurements available in a.
Definition: ZumoIMU.cpp:222
ZumoIMU
Interfaces with the inertial sensors on the Zumo Shield.
Definition: ZumoIMU.h:91
ZumoIMUType::Unknown
@ Unknown
ZumoIMU::readReg
uint8_t readReg(uint8_t addr, uint8_t reg)
Reads an 8-bit sensor register.
Definition: ZumoIMU.cpp:205
ZumoIMU::vector
Represents a 3-dimensional vector with x, y, and z components.
Definition: ZumoIMU.h:97
ZumoIMU::enableDefault
void enableDefault()
Enables all of the inertial sensors with a default configuration.
Definition: ZumoIMU.cpp:39
ZumoIMU::getLastError
uint8_t getLastError()
Returns 0 if the last I2C communication with the IMU was successful, or a non-zero status code if the...
Definition: ZumoIMU.h:112
ZumoIMU::a
vector< int16_t > a
Raw accelerometer readings.
Definition: ZumoIMU.h:102
ZumoIMU::read
void read()
Takes a reading from all three sensors (accelerometer, gyro, and magnetometer) and makes their measur...
Definition: ZumoIMU.cpp:289
ZumoIMU::writeReg
void writeReg(uint8_t addr, uint8_t reg, uint8_t value)
Writes an 8-bit sensor register.
Definition: ZumoIMU.cpp:197
ZumoIMU::configureForCompassHeading
void configureForCompassHeading()
Configures the sensors with settings optimized for determining a compass heading with the magnetomete...
Definition: ZumoIMU.cpp:164
ZumoIMU::init
bool init()
Initializes the inertial sensors and detects their type.
Definition: ZumoIMU.cpp:11
ZumoIMUType
ZumoIMUType
The type of the inertial sensors.
Definition: ZumoIMU.h:71