This is a port from the library for Arduino provided by Sparkfun with their breakout board of the LSM9DS0. The original library can be found here: https://github.com/sparkfun/SparkFun_LSM9DS0_Arduino_Library/tree/V_1.0.1 It is also provided an AHRS example based on Madgwick, also a port from an Arduino example. All of this was tested on a Nucleo F411RE and a Sparkfun breakout board.

Dependencies:   mbed

Committer:
olimexsmart
Date:
Sat Dec 05 16:23:36 2015 +0000
Revision:
0:32b177f0030e
What in this world I am supposed to type here?

Who changed what in which revision?

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