Allows for reading accelerometer, gyroscope, and magnetometer data from an LSM9DS0 IMU device

Dependencies:   mbed

Dependents:   uVGA_4180 uLCD_4180_mini ECE4781_Project

Committer:
randrews33
Date:
Tue Oct 21 18:11:45 2014 +0000
Revision:
0:1b975a6ae539
Child:
4:bf8f4e7c9905
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
randrews33 0:1b975a6ae539 1 #ifndef _LSM9DS0_H__
randrews33 0:1b975a6ae539 2 #define _LSM9DS0_H__
randrews33 0:1b975a6ae539 3
randrews33 0:1b975a6ae539 4 #include "mbed.h"
randrews33 0:1b975a6ae539 5 #include "I2Cdev.h"
randrews33 0:1b975a6ae539 6
randrews33 0:1b975a6ae539 7
randrews33 0:1b975a6ae539 8 ////////////////////////////
randrews33 0:1b975a6ae539 9 // LSM9DS0 Gyro Registers //
randrews33 0:1b975a6ae539 10 ////////////////////////////
randrews33 0:1b975a6ae539 11 #define WHO_AM_I_G 0x0F
randrews33 0:1b975a6ae539 12 #define CTRL_REG1_G 0x20
randrews33 0:1b975a6ae539 13 #define CTRL_REG2_G 0x21
randrews33 0:1b975a6ae539 14 #define CTRL_REG3_G 0x22
randrews33 0:1b975a6ae539 15 #define CTRL_REG4_G 0x23
randrews33 0:1b975a6ae539 16 #define CTRL_REG5_G 0x24
randrews33 0:1b975a6ae539 17 #define REFERENCE_G 0x25
randrews33 0:1b975a6ae539 18 #define STATUS_REG_G 0x27
randrews33 0:1b975a6ae539 19 #define OUT_X_L_G 0x28
randrews33 0:1b975a6ae539 20 #define OUT_X_H_G 0x29
randrews33 0:1b975a6ae539 21 #define OUT_Y_L_G 0x2A
randrews33 0:1b975a6ae539 22 #define OUT_Y_H_G 0x2B
randrews33 0:1b975a6ae539 23 #define OUT_Z_L_G 0x2C
randrews33 0:1b975a6ae539 24 #define OUT_Z_H_G 0x2D
randrews33 0:1b975a6ae539 25 #define FIFO_CTRL_REG_G 0x2E
randrews33 0:1b975a6ae539 26 #define FIFO_SRC_REG_G 0x2F
randrews33 0:1b975a6ae539 27 #define INT1_CFG_G 0x30
randrews33 0:1b975a6ae539 28 #define INT1_SRC_G 0x31
randrews33 0:1b975a6ae539 29 #define INT1_THS_XH_G 0x32
randrews33 0:1b975a6ae539 30 #define INT1_THS_XL_G 0x33
randrews33 0:1b975a6ae539 31 #define INT1_THS_YH_G 0x34
randrews33 0:1b975a6ae539 32 #define INT1_THS_YL_G 0x35
randrews33 0:1b975a6ae539 33 #define INT1_THS_ZH_G 0x36
randrews33 0:1b975a6ae539 34 #define INT1_THS_ZL_G 0x37
randrews33 0:1b975a6ae539 35 #define INT1_DURATION_G 0x38
randrews33 0:1b975a6ae539 36
randrews33 0:1b975a6ae539 37 //////////////////////////////////////////
randrews33 0:1b975a6ae539 38 // LSM9DS0 Accel/Magneto (XM) Registers //
randrews33 0:1b975a6ae539 39 //////////////////////////////////////////
randrews33 0:1b975a6ae539 40 #define OUT_TEMP_L_XM 0x05
randrews33 0:1b975a6ae539 41 #define OUT_TEMP_H_XM 0x06
randrews33 0:1b975a6ae539 42 #define STATUS_REG_M 0x07
randrews33 0:1b975a6ae539 43 #define OUT_X_L_M 0x08
randrews33 0:1b975a6ae539 44 #define OUT_X_H_M 0x09
randrews33 0:1b975a6ae539 45 #define OUT_Y_L_M 0x0A
randrews33 0:1b975a6ae539 46 #define OUT_Y_H_M 0x0B
randrews33 0:1b975a6ae539 47 #define OUT_Z_L_M 0x0C
randrews33 0:1b975a6ae539 48 #define OUT_Z_H_M 0x0D
randrews33 0:1b975a6ae539 49 #define WHO_AM_I_XM 0x0F
randrews33 0:1b975a6ae539 50 #define INT_CTRL_REG_M 0x12
randrews33 0:1b975a6ae539 51 #define INT_SRC_REG_M 0x13
randrews33 0:1b975a6ae539 52 #define INT_THS_L_M 0x14
randrews33 0:1b975a6ae539 53 #define INT_THS_H_M 0x15
randrews33 0:1b975a6ae539 54 #define OFFSET_X_L_M 0x16
randrews33 0:1b975a6ae539 55 #define OFFSET_X_H_M 0x17
randrews33 0:1b975a6ae539 56 #define OFFSET_Y_L_M 0x18
randrews33 0:1b975a6ae539 57 #define OFFSET_Y_H_M 0x19
randrews33 0:1b975a6ae539 58 #define OFFSET_Z_L_M 0x1A
randrews33 0:1b975a6ae539 59 #define OFFSET_Z_H_M 0x1B
randrews33 0:1b975a6ae539 60 #define REFERENCE_X 0x1C
randrews33 0:1b975a6ae539 61 #define REFERENCE_Y 0x1D
randrews33 0:1b975a6ae539 62 #define REFERENCE_Z 0x1E
randrews33 0:1b975a6ae539 63 #define CTRL_REG0_XM 0x1F
randrews33 0:1b975a6ae539 64 #define CTRL_REG1_XM 0x20
randrews33 0:1b975a6ae539 65 #define CTRL_REG2_XM 0x21
randrews33 0:1b975a6ae539 66 #define CTRL_REG3_XM 0x22
randrews33 0:1b975a6ae539 67 #define CTRL_REG4_XM 0x23
randrews33 0:1b975a6ae539 68 #define CTRL_REG5_XM 0x24
randrews33 0:1b975a6ae539 69 #define CTRL_REG6_XM 0x25
randrews33 0:1b975a6ae539 70 #define CTRL_REG7_XM 0x26
randrews33 0:1b975a6ae539 71 #define STATUS_REG_A 0x27
randrews33 0:1b975a6ae539 72 #define OUT_X_L_A 0x28
randrews33 0:1b975a6ae539 73 #define OUT_X_H_A 0x29
randrews33 0:1b975a6ae539 74 #define OUT_Y_L_A 0x2A
randrews33 0:1b975a6ae539 75 #define OUT_Y_H_A 0x2B
randrews33 0:1b975a6ae539 76 #define OUT_Z_L_A 0x2C
randrews33 0:1b975a6ae539 77 #define OUT_Z_H_A 0x2D
randrews33 0:1b975a6ae539 78 #define FIFO_CTRL_REG 0x2E
randrews33 0:1b975a6ae539 79 #define FIFO_SRC_REG 0x2F
randrews33 0:1b975a6ae539 80 #define INT_GEN_1_REG 0x30
randrews33 0:1b975a6ae539 81 #define INT_GEN_1_SRC 0x31
randrews33 0:1b975a6ae539 82 #define INT_GEN_1_THS 0x32
randrews33 0:1b975a6ae539 83 #define INT_GEN_1_DURATION 0x33
randrews33 0:1b975a6ae539 84 #define INT_GEN_2_REG 0x34
randrews33 0:1b975a6ae539 85 #define INT_GEN_2_SRC 0x35
randrews33 0:1b975a6ae539 86 #define INT_GEN_2_THS 0x36
randrews33 0:1b975a6ae539 87 #define INT_GEN_2_DURATION 0x37
randrews33 0:1b975a6ae539 88 #define CLICK_CFG 0x38
randrews33 0:1b975a6ae539 89 #define CLICK_SRC 0x39
randrews33 0:1b975a6ae539 90 #define CLICK_THS 0x3A
randrews33 0:1b975a6ae539 91 #define TIME_LIMIT 0x3B
randrews33 0:1b975a6ae539 92 #define TIME_LATENCY 0x3C
randrews33 0:1b975a6ae539 93 #define TIME_WINDOW 0x3D
randrews33 0:1b975a6ae539 94 #define ACT_THS 0x3E
randrews33 0:1b975a6ae539 95 #define ACT_DUR 0x3F
randrews33 0:1b975a6ae539 96
randrews33 0:1b975a6ae539 97
randrews33 0:1b975a6ae539 98 class LSM9DS0
randrews33 0:1b975a6ae539 99 {
randrews33 0:1b975a6ae539 100 public:
randrews33 0:1b975a6ae539 101 // gyro_scale defines the possible full-scale ranges of the gyroscope:
randrews33 0:1b975a6ae539 102 enum gyro_scale
randrews33 0:1b975a6ae539 103 {
randrews33 0:1b975a6ae539 104 G_SCALE_245DPS, // 00: +/- 245 degrees per second
randrews33 0:1b975a6ae539 105 G_SCALE_500DPS, // 01: +/- 500 dps
randrews33 0:1b975a6ae539 106 G_SCALE_2000DPS, // 10: +/- 2000 dps
randrews33 0:1b975a6ae539 107 };
randrews33 0:1b975a6ae539 108 // accel_scale defines all possible FSR's of the accelerometer:
randrews33 0:1b975a6ae539 109 enum accel_scale
randrews33 0:1b975a6ae539 110 {
randrews33 0:1b975a6ae539 111 A_SCALE_2G, // 000: +/- 2g
randrews33 0:1b975a6ae539 112 A_SCALE_4G, // 001: +/- 4g
randrews33 0:1b975a6ae539 113 A_SCALE_6G, // 010: +/- 6g
randrews33 0:1b975a6ae539 114 A_SCALE_8G, // 011: +/- 8g
randrews33 0:1b975a6ae539 115 A_SCALE_16G // 100: +/- 16g
randrews33 0:1b975a6ae539 116 };
randrews33 0:1b975a6ae539 117 // mag_scale defines all possible FSR's of the magnetometer:
randrews33 0:1b975a6ae539 118 enum mag_scale
randrews33 0:1b975a6ae539 119 {
randrews33 0:1b975a6ae539 120 M_SCALE_2GS, // 00: +/- 2Gs
randrews33 0:1b975a6ae539 121 M_SCALE_4GS, // 01: +/- 4Gs
randrews33 0:1b975a6ae539 122 M_SCALE_8GS, // 10: +/- 8Gs
randrews33 0:1b975a6ae539 123 M_SCALE_12GS, // 11: +/- 12Gs
randrews33 0:1b975a6ae539 124 };
randrews33 0:1b975a6ae539 125 // gyro_odr defines all possible data rate/bandwidth combos of the gyro:
randrews33 0:1b975a6ae539 126 enum gyro_odr
randrews33 0:1b975a6ae539 127 { // ODR (Hz) --- Cutoff
randrews33 0:1b975a6ae539 128 G_ODR_95_BW_125 = 0x0, // 95 12.5
randrews33 0:1b975a6ae539 129 G_ODR_95_BW_25 = 0x1, // 95 25
randrews33 0:1b975a6ae539 130 // 0x2 and 0x3 define the same data rate and bandwidth
randrews33 0:1b975a6ae539 131 G_ODR_190_BW_125 = 0x4, // 190 12.5
randrews33 0:1b975a6ae539 132 G_ODR_190_BW_25 = 0x5, // 190 25
randrews33 0:1b975a6ae539 133 G_ODR_190_BW_50 = 0x6, // 190 50
randrews33 0:1b975a6ae539 134 G_ODR_190_BW_70 = 0x7, // 190 70
randrews33 0:1b975a6ae539 135 G_ODR_380_BW_20 = 0x8, // 380 20
randrews33 0:1b975a6ae539 136 G_ODR_380_BW_25 = 0x9, // 380 25
randrews33 0:1b975a6ae539 137 G_ODR_380_BW_50 = 0xA, // 380 50
randrews33 0:1b975a6ae539 138 G_ODR_380_BW_100 = 0xB, // 380 100
randrews33 0:1b975a6ae539 139 G_ODR_760_BW_30 = 0xC, // 760 30
randrews33 0:1b975a6ae539 140 G_ODR_760_BW_35 = 0xD, // 760 35
randrews33 0:1b975a6ae539 141 G_ODR_760_BW_50 = 0xE, // 760 50
randrews33 0:1b975a6ae539 142 G_ODR_760_BW_100 = 0xF, // 760 100
randrews33 0:1b975a6ae539 143 };
randrews33 0:1b975a6ae539 144 // accel_oder defines all possible output data rates of the accelerometer:
randrews33 0:1b975a6ae539 145 enum accel_odr
randrews33 0:1b975a6ae539 146 {
randrews33 0:1b975a6ae539 147 A_POWER_DOWN, // Power-down mode (0x0)
randrews33 0:1b975a6ae539 148 A_ODR_3125, // 3.125 Hz (0x1)
randrews33 0:1b975a6ae539 149 A_ODR_625, // 6.25 Hz (0x2)
randrews33 0:1b975a6ae539 150 A_ODR_125, // 12.5 Hz (0x3)
randrews33 0:1b975a6ae539 151 A_ODR_25, // 25 Hz (0x4)
randrews33 0:1b975a6ae539 152 A_ODR_50, // 50 Hz (0x5)
randrews33 0:1b975a6ae539 153 A_ODR_100, // 100 Hz (0x6)
randrews33 0:1b975a6ae539 154 A_ODR_200, // 200 Hz (0x7)
randrews33 0:1b975a6ae539 155 A_ODR_400, // 400 Hz (0x8)
randrews33 0:1b975a6ae539 156 A_ODR_800, // 800 Hz (9)
randrews33 0:1b975a6ae539 157 A_ODR_1600 // 1600 Hz (0xA)
randrews33 0:1b975a6ae539 158 };
randrews33 0:1b975a6ae539 159 // accel_oder defines all possible output data rates of the magnetometer:
randrews33 0:1b975a6ae539 160 enum mag_odr
randrews33 0:1b975a6ae539 161 {
randrews33 0:1b975a6ae539 162 M_ODR_3125, // 3.125 Hz (0x00)
randrews33 0:1b975a6ae539 163 M_ODR_625, // 6.25 Hz (0x01)
randrews33 0:1b975a6ae539 164 M_ODR_125, // 12.5 Hz (0x02)
randrews33 0:1b975a6ae539 165 M_ODR_25, // 25 Hz (0x03)
randrews33 0:1b975a6ae539 166 M_ODR_50, // 50 (0x04)
randrews33 0:1b975a6ae539 167 M_ODR_100, // 100 Hz (0x05)
randrews33 0:1b975a6ae539 168 };
randrews33 0:1b975a6ae539 169
randrews33 0:1b975a6ae539 170 // We'll store the gyro, accel, and magnetometer readings in a series of
randrews33 0:1b975a6ae539 171 // public class variables. Each sensor gets three variables -- one for each
randrews33 0:1b975a6ae539 172 // axis. Call readGyro(), readAccel(), and readMag() first, before using
randrews33 0:1b975a6ae539 173 // these variables!
randrews33 0:1b975a6ae539 174 // These values are the RAW signed 16-bit readings from the sensors.
randrews33 0:1b975a6ae539 175 int16_t gx, gy, gz; // x, y, and z axis readings of the gyroscope
randrews33 0:1b975a6ae539 176 int16_t ax, ay, az; // x, y, and z axis readings of the accelerometer
randrews33 0:1b975a6ae539 177 int16_t mx, my, mz; // x, y, and z axis readings of the magnetometer
randrews33 0:1b975a6ae539 178
randrews33 0:1b975a6ae539 179 // LSM9DS0 -- LSM9DS0 class constructor
randrews33 0:1b975a6ae539 180 // The constructor will set up a handful of private variables, and set the
randrews33 0:1b975a6ae539 181 // communication mode as well.
randrews33 0:1b975a6ae539 182 // Input:
randrews33 0:1b975a6ae539 183 // - interface = Either MODE_SPI or MODE_I2C, whichever you're using
randrews33 0:1b975a6ae539 184 // to talk to the IC.
randrews33 0:1b975a6ae539 185 // - gAddr = If MODE_I2C, this is the I2C address of the gyroscope.
randrews33 0:1b975a6ae539 186 // If MODE_SPI, this is the chip select pin of the gyro (CSG)
randrews33 0:1b975a6ae539 187 // - xmAddr = If MODE_I2C, this is the I2C address of the accel/mag.
randrews33 0:1b975a6ae539 188 // If MODE_SPI, this is the cs pin of the accel/mag (CSXM)
randrews33 0:1b975a6ae539 189 LSM9DS0(PinName sda, PinName scl, uint8_t gAddr, uint8_t xmAddr);
randrews33 0:1b975a6ae539 190
randrews33 0:1b975a6ae539 191 // begin() -- Initialize the gyro, accelerometer, and magnetometer.
randrews33 0:1b975a6ae539 192 // This will set up the scale and output rate of each sensor. It'll also
randrews33 0:1b975a6ae539 193 // "turn on" every sensor and every axis of every sensor.
randrews33 0:1b975a6ae539 194 // Input:
randrews33 0:1b975a6ae539 195 // - gScl = The scale of the gyroscope. This should be a gyro_scale value.
randrews33 0:1b975a6ae539 196 // - aScl = The scale of the accelerometer. Should be a accel_scale value.
randrews33 0:1b975a6ae539 197 // - mScl = The scale of the magnetometer. Should be a mag_scale value.
randrews33 0:1b975a6ae539 198 // - gODR = Output data rate of the gyroscope. gyro_odr value.
randrews33 0:1b975a6ae539 199 // - aODR = Output data rate of the accelerometer. accel_odr value.
randrews33 0:1b975a6ae539 200 // - mODR = Output data rate of the magnetometer. mag_odr value.
randrews33 0:1b975a6ae539 201 // Output: The function will return an unsigned 16-bit value. The most-sig
randrews33 0:1b975a6ae539 202 // bytes of the output are the WHO_AM_I reading of the accel. The
randrews33 0:1b975a6ae539 203 // least significant two bytes are the WHO_AM_I reading of the gyro.
randrews33 0:1b975a6ae539 204 // All parameters have a defaulted value, so you can call just "begin()".
randrews33 0:1b975a6ae539 205 // Default values are FSR's of: +/- 245DPS, 2g, 2Gs; ODRs of 95 Hz for
randrews33 0:1b975a6ae539 206 // gyro, 100 Hz for accelerometer, 100 Hz for magnetometer.
randrews33 0:1b975a6ae539 207 // Use the return value of this function to verify communication.
randrews33 0:1b975a6ae539 208 uint16_t begin(gyro_scale gScl = G_SCALE_245DPS,
randrews33 0:1b975a6ae539 209 accel_scale aScl = A_SCALE_2G, mag_scale mScl = M_SCALE_2GS,
randrews33 0:1b975a6ae539 210 gyro_odr gODR = G_ODR_95_BW_125, accel_odr aODR = A_ODR_50,
randrews33 0:1b975a6ae539 211 mag_odr mODR = M_ODR_50);
randrews33 0:1b975a6ae539 212
randrews33 0:1b975a6ae539 213 // readGyro() -- Read the gyroscope output registers.
randrews33 0:1b975a6ae539 214 // This function will read all six gyroscope output registers.
randrews33 0:1b975a6ae539 215 // The readings are stored in the class' gx, gy, and gz variables. Read
randrews33 0:1b975a6ae539 216 // those _after_ calling readGyro().
randrews33 0:1b975a6ae539 217 void readGyro();
randrews33 0:1b975a6ae539 218
randrews33 0:1b975a6ae539 219 // readAccel() -- Read the accelerometer output registers.
randrews33 0:1b975a6ae539 220 // This function will read all six accelerometer output registers.
randrews33 0:1b975a6ae539 221 // The readings are stored in the class' ax, ay, and az variables. Read
randrews33 0:1b975a6ae539 222 // those _after_ calling readAccel().
randrews33 0:1b975a6ae539 223 void readAccel();
randrews33 0:1b975a6ae539 224
randrews33 0:1b975a6ae539 225 // readMag() -- Read the magnetometer output registers.
randrews33 0:1b975a6ae539 226 // This function will read all six magnetometer output registers.
randrews33 0:1b975a6ae539 227 // The readings are stored in the class' mx, my, and mz variables. Read
randrews33 0:1b975a6ae539 228 // those _after_ calling readMag().
randrews33 0:1b975a6ae539 229 void readMag();
randrews33 0:1b975a6ae539 230
randrews33 0:1b975a6ae539 231 // calcGyro() -- Convert from RAW signed 16-bit value to degrees per second
randrews33 0:1b975a6ae539 232 // This function reads in a signed 16-bit value and returns the scaled
randrews33 0:1b975a6ae539 233 // DPS. This function relies on gScale and gRes being correct.
randrews33 0:1b975a6ae539 234 // Input:
randrews33 0:1b975a6ae539 235 // - gyro = A signed 16-bit raw reading from the gyroscope.
randrews33 0:1b975a6ae539 236 float calcGyro(int16_t gyro);
randrews33 0:1b975a6ae539 237
randrews33 0:1b975a6ae539 238 // calcAccel() -- Convert from RAW signed 16-bit value to gravity (g's).
randrews33 0:1b975a6ae539 239 // This function reads in a signed 16-bit value and returns the scaled
randrews33 0:1b975a6ae539 240 // g's. This function relies on aScale and aRes being correct.
randrews33 0:1b975a6ae539 241 // Input:
randrews33 0:1b975a6ae539 242 // - accel = A signed 16-bit raw reading from the accelerometer.
randrews33 0:1b975a6ae539 243 float calcAccel(int16_t accel);
randrews33 0:1b975a6ae539 244
randrews33 0:1b975a6ae539 245 // calcMag() -- Convert from RAW signed 16-bit value to Gauss (Gs)
randrews33 0:1b975a6ae539 246 // This function reads in a signed 16-bit value and returns the scaled
randrews33 0:1b975a6ae539 247 // Gs. This function relies on mScale and mRes being correct.
randrews33 0:1b975a6ae539 248 // Input:
randrews33 0:1b975a6ae539 249 // - mag = A signed 16-bit raw reading from the magnetometer.
randrews33 0:1b975a6ae539 250 float calcMag(int16_t mag);
randrews33 0:1b975a6ae539 251
randrews33 0:1b975a6ae539 252 // setGyroScale() -- Set the full-scale range of the gyroscope.
randrews33 0:1b975a6ae539 253 // This function can be called to set the scale of the gyroscope to
randrews33 0:1b975a6ae539 254 // 245, 500, or 200 degrees per second.
randrews33 0:1b975a6ae539 255 // Input:
randrews33 0:1b975a6ae539 256 // - gScl = The desired gyroscope scale. Must be one of three possible
randrews33 0:1b975a6ae539 257 // values from the gyro_scale enum.
randrews33 0:1b975a6ae539 258 void setGyroScale(gyro_scale gScl);
randrews33 0:1b975a6ae539 259
randrews33 0:1b975a6ae539 260 // setAccelScale() -- Set the full-scale range of the accelerometer.
randrews33 0:1b975a6ae539 261 // This function can be called to set the scale of the accelerometer to
randrews33 0:1b975a6ae539 262 // 2, 4, 6, 8, or 16 g's.
randrews33 0:1b975a6ae539 263 // Input:
randrews33 0:1b975a6ae539 264 // - aScl = The desired accelerometer scale. Must be one of five possible
randrews33 0:1b975a6ae539 265 // values from the accel_scale enum.
randrews33 0:1b975a6ae539 266 void setAccelScale(accel_scale aScl);
randrews33 0:1b975a6ae539 267
randrews33 0:1b975a6ae539 268 // setMagScale() -- Set the full-scale range of the magnetometer.
randrews33 0:1b975a6ae539 269 // This function can be called to set the scale of the magnetometer to
randrews33 0:1b975a6ae539 270 // 2, 4, 8, or 12 Gs.
randrews33 0:1b975a6ae539 271 // Input:
randrews33 0:1b975a6ae539 272 // - mScl = The desired magnetometer scale. Must be one of four possible
randrews33 0:1b975a6ae539 273 // values from the mag_scale enum.
randrews33 0:1b975a6ae539 274 void setMagScale(mag_scale mScl);
randrews33 0:1b975a6ae539 275
randrews33 0:1b975a6ae539 276 // setGyroODR() -- Set the output data rate and bandwidth of the gyroscope
randrews33 0:1b975a6ae539 277 // Input:
randrews33 0:1b975a6ae539 278 // - gRate = The desired output rate and cutoff frequency of the gyro.
randrews33 0:1b975a6ae539 279 // Must be a value from the gyro_odr enum (check above, there're 14).
randrews33 0:1b975a6ae539 280 void setGyroODR(gyro_odr gRate);
randrews33 0:1b975a6ae539 281
randrews33 0:1b975a6ae539 282 // setAccelODR() -- Set the output data rate of the accelerometer
randrews33 0:1b975a6ae539 283 // Input:
randrews33 0:1b975a6ae539 284 // - aRate = The desired output rate of the accel.
randrews33 0:1b975a6ae539 285 // Must be a value from the accel_odr enum (check above, there're 11).
randrews33 0:1b975a6ae539 286 void setAccelODR(accel_odr aRate);
randrews33 0:1b975a6ae539 287
randrews33 0:1b975a6ae539 288 // setMagODR() -- Set the output data rate of the magnetometer
randrews33 0:1b975a6ae539 289 // Input:
randrews33 0:1b975a6ae539 290 // - mRate = The desired output rate of the mag.
randrews33 0:1b975a6ae539 291 // Must be a value from the mag_odr enum (check above, there're 6).
randrews33 0:1b975a6ae539 292 void setMagODR(mag_odr mRate);
randrews33 0:1b975a6ae539 293
randrews33 0:1b975a6ae539 294 // configGyroInt() -- Configure the gyro interrupt output.
randrews33 0:1b975a6ae539 295 // Triggers can be set to either rising above or falling below a specified
randrews33 0:1b975a6ae539 296 // threshold. This function helps setup the interrupt configuration and
randrews33 0:1b975a6ae539 297 // threshold values for all axes.
randrews33 0:1b975a6ae539 298 // Input:
randrews33 0:1b975a6ae539 299 // - int1Cfg = A 8-bit value that is sent directly to the INT1_CFG_G
randrews33 0:1b975a6ae539 300 // register. This sets AND/OR and high/low interrupt gen for each axis
randrews33 0:1b975a6ae539 301 // - int1ThsX = 16-bit interrupt threshold value for x-axis
randrews33 0:1b975a6ae539 302 // - int1ThsY = 16-bit interrupt threshold value for y-axis
randrews33 0:1b975a6ae539 303 // - int1ThsZ = 16-bit interrupt threshold value for z-axis
randrews33 0:1b975a6ae539 304 // - duration = Duration an interrupt holds after triggered. This value
randrews33 0:1b975a6ae539 305 // is copied directly into the INT1_DURATION_G register.
randrews33 0:1b975a6ae539 306 // Before using this function, read about the INT1_CFG_G register and
randrews33 0:1b975a6ae539 307 // the related INT1* registers in the LMS9DS0 datasheet.
randrews33 0:1b975a6ae539 308 void configGyroInt(uint8_t int1Cfg, uint16_t int1ThsX = 0,
randrews33 0:1b975a6ae539 309 uint16_t int1ThsY = 0, uint16_t int1ThsZ = 0,
randrews33 0:1b975a6ae539 310 uint8_t duration = 0);
randrews33 0:1b975a6ae539 311
randrews33 0:1b975a6ae539 312 private:
randrews33 0:1b975a6ae539 313 // xmAddress and gAddress store the I2C address or SPI chip select pin
randrews33 0:1b975a6ae539 314 // for each sensor.
randrews33 0:1b975a6ae539 315 uint8_t xmAddress, gAddress;
randrews33 0:1b975a6ae539 316
randrews33 0:1b975a6ae539 317 // gScale, aScale, and mScale store the current scale range for each
randrews33 0:1b975a6ae539 318 // sensor. Should be updated whenever that value changes.
randrews33 0:1b975a6ae539 319 gyro_scale gScale;
randrews33 0:1b975a6ae539 320 accel_scale aScale;
randrews33 0:1b975a6ae539 321 mag_scale mScale;
randrews33 0:1b975a6ae539 322
randrews33 0:1b975a6ae539 323 // gRes, aRes, and mRes store the current resolution for each sensor.
randrews33 0:1b975a6ae539 324 // Units of these values would be DPS (or g's or Gs's) per ADC tick.
randrews33 0:1b975a6ae539 325 // This value is calculated as (sensor scale) / (2^15).
randrews33 0:1b975a6ae539 326 float gRes, aRes, mRes;
randrews33 0:1b975a6ae539 327
randrews33 0:1b975a6ae539 328 // initGyro() -- Sets up the gyroscope to begin reading.
randrews33 0:1b975a6ae539 329 // This function steps through all five gyroscope control registers.
randrews33 0:1b975a6ae539 330 // Upon exit, the following parameters will be set:
randrews33 0:1b975a6ae539 331 // - CTRL_REG1_G = 0x0F: Normal operation mode, all axes enabled.
randrews33 0:1b975a6ae539 332 // 95 Hz ODR, 12.5 Hz cutoff frequency.
randrews33 0:1b975a6ae539 333 // - CTRL_REG2_G = 0x00: HPF set to normal mode, cutoff frequency
randrews33 0:1b975a6ae539 334 // set to 7.2 Hz (depends on ODR).
randrews33 0:1b975a6ae539 335 // - CTRL_REG3_G = 0x88: Interrupt enabled on INT_G (set to push-pull and
randrews33 0:1b975a6ae539 336 // active high). Data-ready output enabled on DRDY_G.
randrews33 0:1b975a6ae539 337 // - CTRL_REG4_G = 0x00: Continuous update mode. Data LSB stored in lower
randrews33 0:1b975a6ae539 338 // address. Scale set to 245 DPS. SPI mode set to 4-wire.
randrews33 0:1b975a6ae539 339 // - CTRL_REG5_G = 0x00: FIFO disabled. HPF disabled.
randrews33 0:1b975a6ae539 340 void initGyro();
randrews33 0:1b975a6ae539 341
randrews33 0:1b975a6ae539 342 // initAccel() -- Sets up the accelerometer to begin reading.
randrews33 0:1b975a6ae539 343 // This function steps through all accelerometer related control registers.
randrews33 0:1b975a6ae539 344 // Upon exit these registers will be set as:
randrews33 0:1b975a6ae539 345 // - CTRL_REG0_XM = 0x00: FIFO disabled. HPF bypassed. Normal mode.
randrews33 0:1b975a6ae539 346 // - CTRL_REG1_XM = 0x57: 100 Hz data rate. Continuous update.
randrews33 0:1b975a6ae539 347 // all axes enabled.
randrews33 0:1b975a6ae539 348 // - CTRL_REG2_XM = 0x00: +/- 2g scale. 773 Hz anti-alias filter BW.
randrews33 0:1b975a6ae539 349 // - CTRL_REG3_XM = 0x04: Accel data ready signal on INT1_XM pin.
randrews33 0:1b975a6ae539 350 void initAccel();
randrews33 0:1b975a6ae539 351
randrews33 0:1b975a6ae539 352 // initMag() -- Sets up the magnetometer to begin reading.
randrews33 0:1b975a6ae539 353 // This function steps through all magnetometer-related control registers.
randrews33 0:1b975a6ae539 354 // Upon exit these registers will be set as:
randrews33 0:1b975a6ae539 355 // - CTRL_REG4_XM = 0x04: Mag data ready signal on INT2_XM pin.
randrews33 0:1b975a6ae539 356 // - CTRL_REG5_XM = 0x14: 100 Hz update rate. Low resolution. Interrupt
randrews33 0:1b975a6ae539 357 // requests don't latch. Temperature sensor disabled.
randrews33 0:1b975a6ae539 358 // - CTRL_REG6_XM = 0x00: +/- 2 Gs scale.
randrews33 0:1b975a6ae539 359 // - CTRL_REG7_XM = 0x00: Continuous conversion mode. Normal HPF mode.
randrews33 0:1b975a6ae539 360 // - INT_CTRL_REG_M = 0x09: Interrupt active-high. Enable interrupts.
randrews33 0:1b975a6ae539 361 void initMag();
randrews33 0:1b975a6ae539 362
randrews33 0:1b975a6ae539 363 // gReadByte() -- Reads a byte from a specified gyroscope register.
randrews33 0:1b975a6ae539 364 // Input:
randrews33 0:1b975a6ae539 365 // - subAddress = Register to be read from.
randrews33 0:1b975a6ae539 366 // Output:
randrews33 0:1b975a6ae539 367 // - An 8-bit value read from the requested address.
randrews33 0:1b975a6ae539 368 uint8_t gReadByte(uint8_t subAddress);
randrews33 0:1b975a6ae539 369
randrews33 0:1b975a6ae539 370 // gReadBytes() -- Reads a number of bytes -- beginning at an address
randrews33 0:1b975a6ae539 371 // and incrementing from there -- from the gyroscope.
randrews33 0:1b975a6ae539 372 // Input:
randrews33 0:1b975a6ae539 373 // - subAddress = Register to be read from.
randrews33 0:1b975a6ae539 374 // - * dest = A pointer to an array of uint8_t's. Values read will be
randrews33 0:1b975a6ae539 375 // stored in here on return.
randrews33 0:1b975a6ae539 376 // - count = The number of bytes to be read.
randrews33 0:1b975a6ae539 377 // Output: No value is returned, but the `dest` array will store
randrews33 0:1b975a6ae539 378 // the data read upon exit.
randrews33 0:1b975a6ae539 379 void gReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count);
randrews33 0:1b975a6ae539 380
randrews33 0:1b975a6ae539 381 // gWriteByte() -- Write a byte to a register in the gyroscope.
randrews33 0:1b975a6ae539 382 // Input:
randrews33 0:1b975a6ae539 383 // - subAddress = Register to be written to.
randrews33 0:1b975a6ae539 384 // - data = data to be written to the register.
randrews33 0:1b975a6ae539 385 void gWriteByte(uint8_t subAddress, uint8_t data);
randrews33 0:1b975a6ae539 386
randrews33 0:1b975a6ae539 387 // xmReadByte() -- Read a byte from a register in the accel/mag sensor
randrews33 0:1b975a6ae539 388 // Input:
randrews33 0:1b975a6ae539 389 // - subAddress = Register to be read from.
randrews33 0:1b975a6ae539 390 // Output:
randrews33 0:1b975a6ae539 391 // - An 8-bit value read from the requested register.
randrews33 0:1b975a6ae539 392 uint8_t xmReadByte(uint8_t subAddress);
randrews33 0:1b975a6ae539 393
randrews33 0:1b975a6ae539 394 // xmReadBytes() -- Reads a number of bytes -- beginning at an address
randrews33 0:1b975a6ae539 395 // and incrementing from there -- from the accelerometer/magnetometer.
randrews33 0:1b975a6ae539 396 // Input:
randrews33 0:1b975a6ae539 397 // - subAddress = Register to be read from.
randrews33 0:1b975a6ae539 398 // - * dest = A pointer to an array of uint8_t's. Values read will be
randrews33 0:1b975a6ae539 399 // stored in here on return.
randrews33 0:1b975a6ae539 400 // - count = The number of bytes to be read.
randrews33 0:1b975a6ae539 401 // Output: No value is returned, but the `dest` array will store
randrews33 0:1b975a6ae539 402 // the data read upon exit.
randrews33 0:1b975a6ae539 403 void xmReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count);
randrews33 0:1b975a6ae539 404
randrews33 0:1b975a6ae539 405 // xmWriteByte() -- Write a byte to a register in the accel/mag sensor.
randrews33 0:1b975a6ae539 406 // Input:
randrews33 0:1b975a6ae539 407 // - subAddress = Register to be written to.
randrews33 0:1b975a6ae539 408 // - data = data to be written to the register.
randrews33 0:1b975a6ae539 409 void xmWriteByte(uint8_t subAddress, uint8_t data);
randrews33 0:1b975a6ae539 410
randrews33 0:1b975a6ae539 411 // calcgRes() -- Calculate the resolution of the gyroscope.
randrews33 0:1b975a6ae539 412 // This function will set the value of the gRes variable. gScale must
randrews33 0:1b975a6ae539 413 // be set prior to calling this function.
randrews33 0:1b975a6ae539 414 void calcgRes();
randrews33 0:1b975a6ae539 415
randrews33 0:1b975a6ae539 416 // calcmRes() -- Calculate the resolution of the magnetometer.
randrews33 0:1b975a6ae539 417 // This function will set the value of the mRes variable. mScale must
randrews33 0:1b975a6ae539 418 // be set prior to calling this function.
randrews33 0:1b975a6ae539 419 void calcmRes();
randrews33 0:1b975a6ae539 420
randrews33 0:1b975a6ae539 421 // calcaRes() -- Calculate the resolution of the accelerometer.
randrews33 0:1b975a6ae539 422 // This function will set the value of the aRes variable. aScale must
randrews33 0:1b975a6ae539 423 // be set prior to calling this function.
randrews33 0:1b975a6ae539 424 void calcaRes();
randrews33 0:1b975a6ae539 425
randrews33 0:1b975a6ae539 426
randrews33 0:1b975a6ae539 427 ///////////////////
randrews33 0:1b975a6ae539 428 // I2C Functions //
randrews33 0:1b975a6ae539 429 ///////////////////
randrews33 0:1b975a6ae539 430 I2Cdev* i2c_;
randrews33 0:1b975a6ae539 431
randrews33 0:1b975a6ae539 432
randrews33 0:1b975a6ae539 433 // I2CwriteByte() -- Write a byte out of I2C to a register in the device
randrews33 0:1b975a6ae539 434 // Input:
randrews33 0:1b975a6ae539 435 // - address = The 7-bit I2C address of the slave device.
randrews33 0:1b975a6ae539 436 // - subAddress = The register to be written to.
randrews33 0:1b975a6ae539 437 // - data = Byte to be written to the register.
randrews33 0:1b975a6ae539 438 void I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data);
randrews33 0:1b975a6ae539 439
randrews33 0:1b975a6ae539 440 // I2CreadByte() -- Read a single byte from a register over I2C.
randrews33 0:1b975a6ae539 441 // Input:
randrews33 0:1b975a6ae539 442 // - address = The 7-bit I2C address of the slave device.
randrews33 0:1b975a6ae539 443 // - subAddress = The register to be read from.
randrews33 0:1b975a6ae539 444 // Output:
randrews33 0:1b975a6ae539 445 // - The byte read from the requested address.
randrews33 0:1b975a6ae539 446 uint8_t I2CreadByte(uint8_t address, uint8_t subAddress);
randrews33 0:1b975a6ae539 447
randrews33 0:1b975a6ae539 448 // I2CreadBytes() -- Read a series of bytes, starting at a register via SPI
randrews33 0:1b975a6ae539 449 // Input:
randrews33 0:1b975a6ae539 450 // - address = The 7-bit I2C address of the slave device.
randrews33 0:1b975a6ae539 451 // - subAddress = The register to begin reading.
randrews33 0:1b975a6ae539 452 // - * dest = Pointer to an array where we'll store the readings.
randrews33 0:1b975a6ae539 453 // - count = Number of registers to be read.
randrews33 0:1b975a6ae539 454 // Output: No value is returned by the function, but the registers read are
randrews33 0:1b975a6ae539 455 // all stored in the *dest array given.
randrews33 0:1b975a6ae539 456 void I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count);
randrews33 0:1b975a6ae539 457 };
randrews33 0:1b975a6ae539 458
randrews33 0:1b975a6ae539 459 #endif // _LSM9DS0_H //