I2C Library for the LSM9DS0 IMU

Dependents:   4180_LSM9DS0_lab HW2_P2 HW2_P3 HW2_P4 ... more

Committer:
aswild
Date:
Mon Jan 26 06:38:38 2015 +0000
Revision:
1:7c1e26d377ed
Parent:
0:3a1dce39106c
finally fixed documentation (I hope)

Who changed what in which revision?

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