Receiver code for SLVM

Dependencies:   mbed-rtos mbed

Committer:
jnagendran3
Date:
Tue Dec 09 01:15:37 2014 +0000
Revision:
0:fd289b2e6b74
first

Who changed what in which revision?

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