Ratchapong T / Mbed 2 deprecated MBED3

Dependencies:   LSM9DS0 mbed

Fork of 4180_LSM9DS0_lab by Allen Wild

Committer:
aswild
Date:
Mon Jan 26 06:32:58 2015 +0000
Revision:
2:4d1fd40fbf43
Parent:
0:29ab304ca8ce
Doxygen for LSM9DS0

Who changed what in which revision?

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