Ratchapong T / Mbed 2 deprecated MBED3

Dependencies:   LSM9DS0 mbed

Fork of 4180_LSM9DS0_lab by Allen Wild

Committer:
aswild
Date:
Mon Jan 26 01:43:00 2015 +0000
Revision:
0:29ab304ca8ce
Child:
2:4d1fd40fbf43
Initial Publish

Who changed what in which revision?

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