Shadow Robot Distance Measurement with Bluetooth Code

Dependencies:   4DGL-uLCD-SE HALLFX_ENCODER Motor mbed-rtos mbed

Fork of rtos_mutex by mbed official

Committer:
vikram3
Date:
Tue Mar 14 14:51:26 2017 +0000
Revision:
9:8e1702463051
Parent:
8:0a2509a0b871
Shadow Robot Code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vikram3 8:0a2509a0b871 1 /******************************************************************************
vikram3 8:0a2509a0b871 2 SFE_LSM9DS1.h
vikram3 8:0a2509a0b871 3 SFE_LSM9DS1 Library Header File
vikram3 8:0a2509a0b871 4 Jim Lindblom @ SparkFun Electronics
vikram3 8:0a2509a0b871 5 Original Creation Date: February 27, 2015
vikram3 8:0a2509a0b871 6 https://github.com/sparkfun/LSM9DS1_Breakout
vikram3 8:0a2509a0b871 7
vikram3 8:0a2509a0b871 8 This file prototypes the LSM9DS1 class, implemented in SFE_LSM9DS1.cpp. In
vikram3 8:0a2509a0b871 9 addition, it defines every register in the LSM9DS1 (both the Gyro and Accel/
vikram3 8:0a2509a0b871 10 Magnetometer registers).
vikram3 8:0a2509a0b871 11
vikram3 8:0a2509a0b871 12 Development environment specifics:
vikram3 8:0a2509a0b871 13 IDE: Arduino 1.6.0
vikram3 8:0a2509a0b871 14 Hardware Platform: Arduino Uno
vikram3 8:0a2509a0b871 15 LSM9DS1 Breakout Version: 1.0
vikram3 8:0a2509a0b871 16
vikram3 8:0a2509a0b871 17 This code is beerware; if you see me (or any other SparkFun employee) at the
vikram3 8:0a2509a0b871 18 local, and you've found our code helpful, please buy us a round!
vikram3 8:0a2509a0b871 19
vikram3 8:0a2509a0b871 20 Distributed as-is; no warranty is given.
vikram3 8:0a2509a0b871 21 ******************************************************************************/
vikram3 8:0a2509a0b871 22 #ifndef __SparkFunLSM9DS1_H__
vikram3 8:0a2509a0b871 23 #define __SparkFunLSM9DS1_H__
vikram3 8:0a2509a0b871 24
vikram3 8:0a2509a0b871 25 //#if defined(ARDUINO) && ARDUINO >= 100
vikram3 8:0a2509a0b871 26 // #include "Arduino.h"
vikram3 8:0a2509a0b871 27 //#else
vikram3 8:0a2509a0b871 28 // #include "WProgram.h"
vikram3 8:0a2509a0b871 29 // #include "pins_arduino.h"
vikram3 8:0a2509a0b871 30 //#endif
vikram3 8:0a2509a0b871 31
vikram3 8:0a2509a0b871 32 #include "mbed.h"
vikram3 8:0a2509a0b871 33 #include <stdint.h>
vikram3 8:0a2509a0b871 34 #include "LSM9DS1_Registers.h"
vikram3 8:0a2509a0b871 35 #include "LSM9DS1_Types.h"
vikram3 8:0a2509a0b871 36
vikram3 8:0a2509a0b871 37 #define LSM9DS1_AG_ADDR(sa0) ((sa0) == 0 ? 0x6A : 0x6B)
vikram3 8:0a2509a0b871 38 #define LSM9DS1_M_ADDR(sa1) ((sa1) == 0 ? 0x1C : 0x1E)
vikram3 8:0a2509a0b871 39
vikram3 8:0a2509a0b871 40 enum lsm9ds1_axis {
vikram3 8:0a2509a0b871 41 X_AXIS,
vikram3 8:0a2509a0b871 42 Y_AXIS,
vikram3 8:0a2509a0b871 43 Z_AXIS,
vikram3 8:0a2509a0b871 44 ALL_AXIS
vikram3 8:0a2509a0b871 45 };
vikram3 8:0a2509a0b871 46
vikram3 8:0a2509a0b871 47 class LSM9DS1
vikram3 8:0a2509a0b871 48 {
vikram3 8:0a2509a0b871 49 public:
vikram3 8:0a2509a0b871 50 IMUSettings settings;
vikram3 8:0a2509a0b871 51
vikram3 8:0a2509a0b871 52 // We'll store the gyro, accel, and magnetometer readings in a series of
vikram3 8:0a2509a0b871 53 // public class variables. Each sensor gets three variables -- one for each
vikram3 8:0a2509a0b871 54 // axis. Call readGyro(), readAccel(), and readMag() first, before using
vikram3 8:0a2509a0b871 55 // these variables!
vikram3 8:0a2509a0b871 56 // These values are the RAW signed 16-bit readings from the sensors.
vikram3 8:0a2509a0b871 57 int16_t gx, gy, gz; // x, y, and z axis readings of the gyroscope
vikram3 8:0a2509a0b871 58 int16_t ax, ay, az; // x, y, and z axis readings of the accelerometer
vikram3 8:0a2509a0b871 59 int16_t mx, my, mz; // x, y, and z axis readings of the magnetometer
vikram3 8:0a2509a0b871 60 int16_t temperature; // Chip temperature
vikram3 8:0a2509a0b871 61 float gBias[3], aBias[3], mBias[3];
vikram3 8:0a2509a0b871 62 int16_t gBiasRaw[3], aBiasRaw[3], mBiasRaw[3];
vikram3 8:0a2509a0b871 63
vikram3 8:0a2509a0b871 64 // LSM9DS1 -- LSM9DS1 class constructor
vikram3 8:0a2509a0b871 65 // The constructor will set up a handful of private variables, and set the
vikram3 8:0a2509a0b871 66 // communication mode as well.
vikram3 8:0a2509a0b871 67 /**Input:
vikram3 8:0a2509a0b871 68 * - interface = Either IMU_MODE_SPI or IMU_MODE_I2C, whichever you're using
vikram3 8:0a2509a0b871 69 * to talk to the IC.
vikram3 8:0a2509a0b871 70 * - xgAddr = If IMU_MODE_I2C, this is the I2C address of the accel/gyroscope.
vikram3 8:0a2509a0b871 71 * If IMU_MODE_SPI, this is the chip select pin of the gyro (CS_AG)
vikram3 8:0a2509a0b871 72 * - mAddr = If IMU_MODE_I2C, this is the I2C address of the magnetometer.
vikram3 8:0a2509a0b871 73 * If IMU_MODE_SPI, this is the cs pin of the magnetometer (CS_M)
vikram3 8:0a2509a0b871 74
vikram3 8:0a2509a0b871 75 */
vikram3 8:0a2509a0b871 76 LSM9DS1(PinName sda, PinName scl, uint8_t xgAddr, uint8_t mAddr);
vikram3 8:0a2509a0b871 77 //LSM9DS1(interface_mode interface, uint8_t xgAddr, uint8_t mAddr);
vikram3 8:0a2509a0b871 78 //LSM9DS1();
vikram3 8:0a2509a0b871 79
vikram3 8:0a2509a0b871 80
vikram3 8:0a2509a0b871 81 /** begin() -- Initialize the gyro, accelerometer, and magnetometer.
vikram3 8:0a2509a0b871 82 *This will set up the scale and output rate of each sensor. The values set
vikram3 8:0a2509a0b871 83 * in the IMUSettings struct will take effect after calling this function.
vikram3 8:0a2509a0b871 84 */
vikram3 8:0a2509a0b871 85 uint16_t begin();
vikram3 8:0a2509a0b871 86
vikram3 8:0a2509a0b871 87 void calibrate(bool autoCalc = true);
vikram3 8:0a2509a0b871 88 void calibrateMag(bool loadIn = true);
vikram3 8:0a2509a0b871 89 void magOffset(uint8_t axis, int16_t offset);
vikram3 8:0a2509a0b871 90
vikram3 8:0a2509a0b871 91 /** accelAvailable() -- Polls the accelerometer status register to check
vikram3 8:0a2509a0b871 92 * if new data is available.
vikram3 8:0a2509a0b871 93 * Output: 1 - New data available
vikram3 8:0a2509a0b871 94 * 0 - No new data available
vikram3 8:0a2509a0b871 95 */
vikram3 8:0a2509a0b871 96 uint8_t accelAvailable();
vikram3 8:0a2509a0b871 97
vikram3 8:0a2509a0b871 98 /** gyroAvailable() -- Polls the gyroscope status register to check
vikram3 8:0a2509a0b871 99 * if new data is available.
vikram3 8:0a2509a0b871 100 * Output: 1 - New data available
vikram3 8:0a2509a0b871 101 * 0 - No new data available
vikram3 8:0a2509a0b871 102 */
vikram3 8:0a2509a0b871 103 uint8_t gyroAvailable();
vikram3 8:0a2509a0b871 104
vikram3 8:0a2509a0b871 105 /** gyroAvailable() -- Polls the temperature status register to check
vikram3 8:0a2509a0b871 106 * if new data is available.
vikram3 8:0a2509a0b871 107 * Output: 1 - New data available
vikram3 8:0a2509a0b871 108 * 0 - No new data available
vikram3 8:0a2509a0b871 109 */
vikram3 8:0a2509a0b871 110 uint8_t tempAvailable();
vikram3 8:0a2509a0b871 111
vikram3 8:0a2509a0b871 112 /** magAvailable() -- Polls the accelerometer status register to check
vikram3 8:0a2509a0b871 113 * if new data is available.
vikram3 8:0a2509a0b871 114 * Input:
vikram3 8:0a2509a0b871 115 * - axis can be either X_AXIS, Y_AXIS, Z_AXIS, to check for new data
vikram3 8:0a2509a0b871 116 * on one specific axis. Or ALL_AXIS (default) to check for new data
vikram3 8:0a2509a0b871 117 * on all axes.
vikram3 8:0a2509a0b871 118 * Output: 1 - New data available
vikram3 8:0a2509a0b871 119 * 0 - No new data available
vikram3 8:0a2509a0b871 120 */
vikram3 8:0a2509a0b871 121 uint8_t magAvailable(lsm9ds1_axis axis = ALL_AXIS);
vikram3 8:0a2509a0b871 122
vikram3 8:0a2509a0b871 123 /** readGyro() -- Read the gyroscope output registers.
vikram3 8:0a2509a0b871 124 * This function will read all six gyroscope output registers.
vikram3 8:0a2509a0b871 125 * The readings are stored in the class' gx, gy, and gz variables. Read
vikram3 8:0a2509a0b871 126 * those _after_ calling readGyro().
vikram3 8:0a2509a0b871 127 */
vikram3 8:0a2509a0b871 128 void readGyro();
vikram3 8:0a2509a0b871 129
vikram3 8:0a2509a0b871 130 /** int16_t readGyro(axis) -- Read a specific axis of the gyroscope.
vikram3 8:0a2509a0b871 131 * [axis] can be any of X_AXIS, Y_AXIS, or Z_AXIS.
vikram3 8:0a2509a0b871 132 * Input:
vikram3 8:0a2509a0b871 133 * - axis: can be either X_AXIS, Y_AXIS, or Z_AXIS.
vikram3 8:0a2509a0b871 134 * Output:
vikram3 8:0a2509a0b871 135 * A 16-bit signed integer with sensor data on requested axis.
vikram3 8:0a2509a0b871 136 */
vikram3 8:0a2509a0b871 137 int16_t readGyro(lsm9ds1_axis axis);
vikram3 8:0a2509a0b871 138
vikram3 8:0a2509a0b871 139 /** readAccel() -- Read the accelerometer output registers.
vikram3 8:0a2509a0b871 140 * This function will read all six accelerometer output registers.
vikram3 8:0a2509a0b871 141 * The readings are stored in the class' ax, ay, and az variables. Read
vikram3 8:0a2509a0b871 142 * those _after_ calling readAccel().
vikram3 8:0a2509a0b871 143 */
vikram3 8:0a2509a0b871 144 void readAccel();
vikram3 8:0a2509a0b871 145
vikram3 8:0a2509a0b871 146 /** int16_t readAccel(axis) -- Read a specific axis of the accelerometer.
vikram3 8:0a2509a0b871 147 * [axis] can be any of X_AXIS, Y_AXIS, or Z_AXIS.
vikram3 8:0a2509a0b871 148 * Input:
vikram3 8:0a2509a0b871 149 * - axis: can be either X_AXIS, Y_AXIS, or Z_AXIS.
vikram3 8:0a2509a0b871 150 * Output:
vikram3 8:0a2509a0b871 151 * A 16-bit signed integer with sensor data on requested axis.
vikram3 8:0a2509a0b871 152 */
vikram3 8:0a2509a0b871 153 int16_t readAccel(lsm9ds1_axis axis);
vikram3 8:0a2509a0b871 154
vikram3 8:0a2509a0b871 155 /** readMag() -- Read the magnetometer output registers.
vikram3 8:0a2509a0b871 156 * This function will read all six magnetometer output registers.
vikram3 8:0a2509a0b871 157 * The readings are stored in the class' mx, my, and mz variables. Read
vikram3 8:0a2509a0b871 158 * those _after_ calling readMag().
vikram3 8:0a2509a0b871 159 */
vikram3 8:0a2509a0b871 160 void readMag();
vikram3 8:0a2509a0b871 161
vikram3 8:0a2509a0b871 162 /** int16_t readMag(axis) -- Read a specific axis of the magnetometer.
vikram3 8:0a2509a0b871 163 * [axis] can be any of X_AXIS, Y_AXIS, or Z_AXIS.
vikram3 8:0a2509a0b871 164 * Input:
vikram3 8:0a2509a0b871 165 * - axis: can be either X_AXIS, Y_AXIS, or Z_AXIS.
vikram3 8:0a2509a0b871 166 * Output:
vikram3 8:0a2509a0b871 167 * A 16-bit signed integer with sensor data on requested axis.
vikram3 8:0a2509a0b871 168 */
vikram3 8:0a2509a0b871 169 int16_t readMag(lsm9ds1_axis axis);
vikram3 8:0a2509a0b871 170
vikram3 8:0a2509a0b871 171 /** readTemp() -- Read the temperature output register.
vikram3 8:0a2509a0b871 172 * This function will read two temperature output registers.
vikram3 8:0a2509a0b871 173 * The combined readings are stored in the class' temperature variables. Read
vikram3 8:0a2509a0b871 174 * those _after_ calling readTemp().
vikram3 8:0a2509a0b871 175 */
vikram3 8:0a2509a0b871 176 void readTemp();
vikram3 8:0a2509a0b871 177
vikram3 8:0a2509a0b871 178 /** calcGyro() -- Convert from RAW signed 16-bit value to degrees per second
vikram3 8:0a2509a0b871 179 * This function reads in a signed 16-bit value and returns the scaled
vikram3 8:0a2509a0b871 180 * DPS. This function relies on gScale and gRes being correct.
vikram3 8:0a2509a0b871 181 * Input:
vikram3 8:0a2509a0b871 182 * - gyro = A signed 16-bit raw reading from the gyroscope.
vikram3 8:0a2509a0b871 183 */
vikram3 8:0a2509a0b871 184 float calcGyro(int16_t gyro);
vikram3 8:0a2509a0b871 185
vikram3 8:0a2509a0b871 186 /** calcAccel() -- Convert from RAW signed 16-bit value to gravity (g's).
vikram3 8:0a2509a0b871 187 * This function reads in a signed 16-bit value and returns the scaled
vikram3 8:0a2509a0b871 188 * g's. This function relies on aScale and aRes being correct.
vikram3 8:0a2509a0b871 189 * Input:
vikram3 8:0a2509a0b871 190 * - accel = A signed 16-bit raw reading from the accelerometer.
vikram3 8:0a2509a0b871 191 */
vikram3 8:0a2509a0b871 192 float calcAccel(int16_t accel);
vikram3 8:0a2509a0b871 193
vikram3 8:0a2509a0b871 194 /** calcMag() -- Convert from RAW signed 16-bit value to Gauss (Gs)
vikram3 8:0a2509a0b871 195 * This function reads in a signed 16-bit value and returns the scaled
vikram3 8:0a2509a0b871 196 * Gs. This function relies on mScale and mRes being correct.
vikram3 8:0a2509a0b871 197 * Input:
vikram3 8:0a2509a0b871 198 * - mag = A signed 16-bit raw reading from the magnetometer.
vikram3 8:0a2509a0b871 199 */
vikram3 8:0a2509a0b871 200 float calcMag(int16_t mag);
vikram3 8:0a2509a0b871 201
vikram3 8:0a2509a0b871 202 /** setGyroScale() -- Set the full-scale range of the gyroscope.
vikram3 8:0a2509a0b871 203 * This function can be called to set the scale of the gyroscope to
vikram3 8:0a2509a0b871 204 * 245, 500, or 200 degrees per second.
vikram3 8:0a2509a0b871 205 * Input:
vikram3 8:0a2509a0b871 206 * - gScl = The desired gyroscope scale. Must be one of three possible
vikram3 8:0a2509a0b871 207 * values from the gyro_scale.
vikram3 8:0a2509a0b871 208 */
vikram3 8:0a2509a0b871 209 void setGyroScale(uint16_t gScl);
vikram3 8:0a2509a0b871 210
vikram3 8:0a2509a0b871 211 /** setAccelScale() -- Set the full-scale range of the accelerometer.
vikram3 8:0a2509a0b871 212 * This function can be called to set the scale of the accelerometer to
vikram3 8:0a2509a0b871 213 * 2, 4, 6, 8, or 16 g's.
vikram3 8:0a2509a0b871 214 * Input:
vikram3 8:0a2509a0b871 215 * - aScl = The desired accelerometer scale. Must be one of five possible
vikram3 8:0a2509a0b871 216 * values from the accel_scale.
vikram3 8:0a2509a0b871 217 */
vikram3 8:0a2509a0b871 218 void setAccelScale(uint8_t aScl);
vikram3 8:0a2509a0b871 219
vikram3 8:0a2509a0b871 220 /** setMagScale() -- Set the full-scale range of the magnetometer.
vikram3 8:0a2509a0b871 221 * This function can be called to set the scale of the magnetometer to
vikram3 8:0a2509a0b871 222 * 2, 4, 8, or 12 Gs.
vikram3 8:0a2509a0b871 223 * Input:
vikram3 8:0a2509a0b871 224 * - mScl = The desired magnetometer scale. Must be one of four possible
vikram3 8:0a2509a0b871 225 * values from the mag_scale.
vikram3 8:0a2509a0b871 226 */
vikram3 8:0a2509a0b871 227 void setMagScale(uint8_t mScl);
vikram3 8:0a2509a0b871 228
vikram3 8:0a2509a0b871 229 /** setGyroODR() -- Set the output data rate and bandwidth of the gyroscope
vikram3 8:0a2509a0b871 230 * Input:
vikram3 8:0a2509a0b871 231 * - gRate = The desired output rate and cutoff frequency of the gyro.
vikram3 8:0a2509a0b871 232 */
vikram3 8:0a2509a0b871 233 void setGyroODR(uint8_t gRate);
vikram3 8:0a2509a0b871 234
vikram3 8:0a2509a0b871 235 // setAccelODR() -- Set the output data rate of the accelerometer
vikram3 8:0a2509a0b871 236 // Input:
vikram3 8:0a2509a0b871 237 // - aRate = The desired output rate of the accel.
vikram3 8:0a2509a0b871 238 void setAccelODR(uint8_t aRate);
vikram3 8:0a2509a0b871 239
vikram3 8:0a2509a0b871 240 // setMagODR() -- Set the output data rate of the magnetometer
vikram3 8:0a2509a0b871 241 // Input:
vikram3 8:0a2509a0b871 242 // - mRate = The desired output rate of the mag.
vikram3 8:0a2509a0b871 243 void setMagODR(uint8_t mRate);
vikram3 8:0a2509a0b871 244
vikram3 8:0a2509a0b871 245 // configInactivity() -- Configure inactivity interrupt parameters
vikram3 8:0a2509a0b871 246 // Input:
vikram3 8:0a2509a0b871 247 // - duration = Inactivity duration - actual value depends on gyro ODR
vikram3 8:0a2509a0b871 248 // - threshold = Activity Threshold
vikram3 8:0a2509a0b871 249 // - sleepOn = Gyroscope operating mode during inactivity.
vikram3 8:0a2509a0b871 250 // true: gyroscope in sleep mode
vikram3 8:0a2509a0b871 251 // false: gyroscope in power-down
vikram3 8:0a2509a0b871 252 void configInactivity(uint8_t duration, uint8_t threshold, bool sleepOn);
vikram3 8:0a2509a0b871 253
vikram3 8:0a2509a0b871 254 // configAccelInt() -- Configure Accelerometer Interrupt Generator
vikram3 8:0a2509a0b871 255 // Input:
vikram3 8:0a2509a0b871 256 // - generator = Interrupt axis/high-low events
vikram3 8:0a2509a0b871 257 // Any OR'd combination of ZHIE_XL, ZLIE_XL, YHIE_XL, YLIE_XL, XHIE_XL, XLIE_XL
vikram3 8:0a2509a0b871 258 // - andInterrupts = AND/OR combination of interrupt events
vikram3 8:0a2509a0b871 259 // true: AND combination
vikram3 8:0a2509a0b871 260 // false: OR combination
vikram3 8:0a2509a0b871 261 void configAccelInt(uint8_t generator, bool andInterrupts = false);
vikram3 8:0a2509a0b871 262
vikram3 8:0a2509a0b871 263 // configAccelThs() -- Configure the threshold of an accelereomter axis
vikram3 8:0a2509a0b871 264 // Input:
vikram3 8:0a2509a0b871 265 // - threshold = Interrupt threshold. Possible values: 0-255.
vikram3 8:0a2509a0b871 266 // Multiply by 128 to get the actual raw accel value.
vikram3 8:0a2509a0b871 267 // - axis = Axis to be configured. Either X_AXIS, Y_AXIS, or Z_AXIS
vikram3 8:0a2509a0b871 268 // - duration = Duration value must be above or below threshold to trigger interrupt
vikram3 8:0a2509a0b871 269 // - wait = Wait function on duration counter
vikram3 8:0a2509a0b871 270 // true: Wait for duration samples before exiting interrupt
vikram3 8:0a2509a0b871 271 // false: Wait function off
vikram3 8:0a2509a0b871 272 void configAccelThs(uint8_t threshold, lsm9ds1_axis axis, uint8_t duration = 0, bool wait = 0);
vikram3 8:0a2509a0b871 273
vikram3 8:0a2509a0b871 274 // configGyroInt() -- Configure Gyroscope Interrupt Generator
vikram3 8:0a2509a0b871 275 // Input:
vikram3 8:0a2509a0b871 276 // - generator = Interrupt axis/high-low events
vikram3 8:0a2509a0b871 277 // Any OR'd combination of ZHIE_G, ZLIE_G, YHIE_G, YLIE_G, XHIE_G, XLIE_G
vikram3 8:0a2509a0b871 278 // - aoi = AND/OR combination of interrupt events
vikram3 8:0a2509a0b871 279 // true: AND combination
vikram3 8:0a2509a0b871 280 // false: OR combination
vikram3 8:0a2509a0b871 281 // - latch: latch gyroscope interrupt request.
vikram3 8:0a2509a0b871 282 void configGyroInt(uint8_t generator, bool aoi, bool latch);
vikram3 8:0a2509a0b871 283
vikram3 8:0a2509a0b871 284 // configGyroThs() -- Configure the threshold of a gyroscope axis
vikram3 8:0a2509a0b871 285 // Input:
vikram3 8:0a2509a0b871 286 // - threshold = Interrupt threshold. Possible values: 0-0x7FF.
vikram3 8:0a2509a0b871 287 // Value is equivalent to raw gyroscope value.
vikram3 8:0a2509a0b871 288 // - axis = Axis to be configured. Either X_AXIS, Y_AXIS, or Z_AXIS
vikram3 8:0a2509a0b871 289 // - duration = Duration value must be above or below threshold to trigger interrupt
vikram3 8:0a2509a0b871 290 // - wait = Wait function on duration counter
vikram3 8:0a2509a0b871 291 // true: Wait for duration samples before exiting interrupt
vikram3 8:0a2509a0b871 292 // false: Wait function off
vikram3 8:0a2509a0b871 293 void configGyroThs(int16_t threshold, lsm9ds1_axis axis, uint8_t duration, bool wait);
vikram3 8:0a2509a0b871 294
vikram3 8:0a2509a0b871 295 // configInt() -- Configure INT1 or INT2 (Gyro and Accel Interrupts only)
vikram3 8:0a2509a0b871 296 // Input:
vikram3 8:0a2509a0b871 297 // - interrupt = Select INT1 or INT2
vikram3 8:0a2509a0b871 298 // Possible values: XG_INT1 or XG_INT2
vikram3 8:0a2509a0b871 299 // - generator = Or'd combination of interrupt generators.
vikram3 8:0a2509a0b871 300 // Possible values: INT_DRDY_XL, INT_DRDY_G, INT1_BOOT (INT1 only), INT2_DRDY_TEMP (INT2 only)
vikram3 8:0a2509a0b871 301 // INT_FTH, INT_OVR, INT_FSS5, INT_IG_XL (INT1 only), INT1_IG_G (INT1 only), INT2_INACT (INT2 only)
vikram3 8:0a2509a0b871 302 // - activeLow = Interrupt active configuration
vikram3 8:0a2509a0b871 303 // Can be either INT_ACTIVE_HIGH or INT_ACTIVE_LOW
vikram3 8:0a2509a0b871 304 // - pushPull = Push-pull or open drain interrupt configuration
vikram3 8:0a2509a0b871 305 // Can be either INT_PUSH_PULL or INT_OPEN_DRAIN
vikram3 8:0a2509a0b871 306 void configInt(interrupt_select interupt, uint8_t generator,
vikram3 8:0a2509a0b871 307 h_lactive activeLow = INT_ACTIVE_LOW, pp_od pushPull = INT_PUSH_PULL);
vikram3 8:0a2509a0b871 308
vikram3 8:0a2509a0b871 309 /** configMagInt() -- Configure Magnetometer Interrupt Generator
vikram3 8:0a2509a0b871 310 * Input:
vikram3 8:0a2509a0b871 311 * - generator = Interrupt axis/high-low events
vikram3 8:0a2509a0b871 312 * Any OR'd combination of ZIEN, YIEN, XIEN
vikram3 8:0a2509a0b871 313 * - activeLow = Interrupt active configuration
vikram3 8:0a2509a0b871 314 * Can be either INT_ACTIVE_HIGH or INT_ACTIVE_LOW
vikram3 8:0a2509a0b871 315 * - latch: latch gyroscope interrupt request.
vikram3 8:0a2509a0b871 316 */
vikram3 8:0a2509a0b871 317 void configMagInt(uint8_t generator, h_lactive activeLow, bool latch = true);
vikram3 8:0a2509a0b871 318
vikram3 8:0a2509a0b871 319 /** configMagThs() -- Configure the threshold of a gyroscope axis
vikram3 8:0a2509a0b871 320 * Input:
vikram3 8:0a2509a0b871 321 * - threshold = Interrupt threshold. Possible values: 0-0x7FF.
vikram3 8:0a2509a0b871 322 * Value is equivalent to raw magnetometer value.
vikram3 8:0a2509a0b871 323 */
vikram3 8:0a2509a0b871 324 void configMagThs(uint16_t threshold);
vikram3 8:0a2509a0b871 325
vikram3 8:0a2509a0b871 326 //! getGyroIntSrc() -- Get contents of Gyroscope interrupt source register
vikram3 8:0a2509a0b871 327 uint8_t getGyroIntSrc();
vikram3 8:0a2509a0b871 328
vikram3 8:0a2509a0b871 329 //! getGyroIntSrc() -- Get contents of accelerometer interrupt source register
vikram3 8:0a2509a0b871 330 uint8_t getAccelIntSrc();
vikram3 8:0a2509a0b871 331
vikram3 8:0a2509a0b871 332 //! getGyroIntSrc() -- Get contents of magnetometer interrupt source register
vikram3 8:0a2509a0b871 333 uint8_t getMagIntSrc();
vikram3 8:0a2509a0b871 334
vikram3 8:0a2509a0b871 335 //! getGyroIntSrc() -- Get status of inactivity interrupt
vikram3 8:0a2509a0b871 336 uint8_t getInactivity();
vikram3 8:0a2509a0b871 337
vikram3 8:0a2509a0b871 338 /** sleepGyro() -- Sleep or wake the gyroscope
vikram3 8:0a2509a0b871 339 * Input:
vikram3 8:0a2509a0b871 340 * - enable: True = sleep gyro. False = wake gyro.
vikram3 8:0a2509a0b871 341 */
vikram3 8:0a2509a0b871 342 void sleepGyro(bool enable = true);
vikram3 8:0a2509a0b871 343
vikram3 8:0a2509a0b871 344 /** enableFIFO() - Enable or disable the FIFO
vikram3 8:0a2509a0b871 345 * Input:
vikram3 8:0a2509a0b871 346 * - enable: true = enable, false = disable.
vikram3 8:0a2509a0b871 347 */
vikram3 8:0a2509a0b871 348 void enableFIFO(bool enable = true);
vikram3 8:0a2509a0b871 349
vikram3 8:0a2509a0b871 350 /** setFIFO() - Configure FIFO mode and Threshold
vikram3 8:0a2509a0b871 351 * Input:
vikram3 8:0a2509a0b871 352 * - fifoMode: Set FIFO mode to off, FIFO (stop when full), continuous, bypass
vikram3 8:0a2509a0b871 353 * Possible inputs: FIFO_OFF, FIFO_THS, FIFO_CONT_TRIGGER, FIFO_OFF_TRIGGER, FIFO_CONT
vikram3 8:0a2509a0b871 354 * - fifoThs: FIFO threshold level setting
vikram3 8:0a2509a0b871 355 * Any value from 0-0x1F is acceptable.
vikram3 8:0a2509a0b871 356 */
vikram3 8:0a2509a0b871 357 void setFIFO(fifoMode_type fifoMode, uint8_t fifoThs);
vikram3 8:0a2509a0b871 358
vikram3 8:0a2509a0b871 359 //! getFIFOSamples() - Get number of FIFO samples
vikram3 8:0a2509a0b871 360 uint8_t getFIFOSamples();
vikram3 8:0a2509a0b871 361
vikram3 8:0a2509a0b871 362
vikram3 8:0a2509a0b871 363 protected:
vikram3 8:0a2509a0b871 364 // x_mAddress and gAddress store the I2C address or SPI chip select pin
vikram3 8:0a2509a0b871 365 // for each sensor.
vikram3 8:0a2509a0b871 366 uint8_t _mAddress, _xgAddress;
vikram3 8:0a2509a0b871 367
vikram3 8:0a2509a0b871 368 // gRes, aRes, and mRes store the current resolution for each sensor.
vikram3 8:0a2509a0b871 369 // Units of these values would be DPS (or g's or Gs's) per ADC tick.
vikram3 8:0a2509a0b871 370 // This value is calculated as (sensor scale) / (2^15).
vikram3 8:0a2509a0b871 371 float gRes, aRes, mRes;
vikram3 8:0a2509a0b871 372
vikram3 8:0a2509a0b871 373 // _autoCalc keeps track of whether we're automatically subtracting off
vikram3 8:0a2509a0b871 374 // accelerometer and gyroscope bias calculated in calibrate().
vikram3 8:0a2509a0b871 375 bool _autoCalc;
vikram3 8:0a2509a0b871 376
vikram3 8:0a2509a0b871 377 // init() -- Sets up gyro, accel, and mag settings to default.
vikram3 8:0a2509a0b871 378 // - interface - Sets the interface mode (IMU_MODE_I2C or IMU_MODE_SPI)
vikram3 8:0a2509a0b871 379 // - xgAddr - Sets either the I2C address of the accel/gyro or SPI chip
vikram3 8:0a2509a0b871 380 // select pin connected to the CS_XG pin.
vikram3 8:0a2509a0b871 381 // - mAddr - Sets either the I2C address of the magnetometer or SPI chip
vikram3 8:0a2509a0b871 382 // select pin connected to the CS_M pin.
vikram3 8:0a2509a0b871 383 void init(interface_mode interface, uint8_t xgAddr, uint8_t mAddr);
vikram3 8:0a2509a0b871 384
vikram3 8:0a2509a0b871 385 // initGyro() -- Sets up the gyroscope to begin reading.
vikram3 8:0a2509a0b871 386 // This function steps through all five gyroscope control registers.
vikram3 8:0a2509a0b871 387 // Upon exit, the following parameters will be set:
vikram3 8:0a2509a0b871 388 // - CTRL_REG1_G = 0x0F: Normal operation mode, all axes enabled.
vikram3 8:0a2509a0b871 389 // 95 Hz ODR, 12.5 Hz cutoff frequency.
vikram3 8:0a2509a0b871 390 // - CTRL_REG2_G = 0x00: HPF set to normal mode, cutoff frequency
vikram3 8:0a2509a0b871 391 // set to 7.2 Hz (depends on ODR).
vikram3 8:0a2509a0b871 392 // - CTRL_REG3_G = 0x88: Interrupt enabled on INT_G (set to push-pull and
vikram3 8:0a2509a0b871 393 // active high). Data-ready output enabled on DRDY_G.
vikram3 8:0a2509a0b871 394 // - CTRL_REG4_G = 0x00: Continuous update mode. Data LSB stored in lower
vikram3 8:0a2509a0b871 395 // address. Scale set to 245 DPS. SPI mode set to 4-wire.
vikram3 8:0a2509a0b871 396 // - CTRL_REG5_G = 0x00: FIFO disabled. HPF disabled.
vikram3 8:0a2509a0b871 397 void initGyro();
vikram3 8:0a2509a0b871 398
vikram3 8:0a2509a0b871 399 // initAccel() -- Sets up the accelerometer to begin reading.
vikram3 8:0a2509a0b871 400 // This function steps through all accelerometer related control registers.
vikram3 8:0a2509a0b871 401 // Upon exit these registers will be set as:
vikram3 8:0a2509a0b871 402 // - CTRL_REG0_XM = 0x00: FIFO disabled. HPF bypassed. Normal mode.
vikram3 8:0a2509a0b871 403 // - CTRL_REG1_XM = 0x57: 100 Hz data rate. Continuous update.
vikram3 8:0a2509a0b871 404 // all axes enabled.
vikram3 8:0a2509a0b871 405 // - CTRL_REG2_XM = 0x00: 2g scale. 773 Hz anti-alias filter BW.
vikram3 8:0a2509a0b871 406 // - CTRL_REG3_XM = 0x04: Accel data ready signal on INT1_XM pin.
vikram3 8:0a2509a0b871 407 void initAccel();
vikram3 8:0a2509a0b871 408
vikram3 8:0a2509a0b871 409 // initMag() -- Sets up the magnetometer to begin reading.
vikram3 8:0a2509a0b871 410 // This function steps through all magnetometer-related control registers.
vikram3 8:0a2509a0b871 411 // Upon exit these registers will be set as:
vikram3 8:0a2509a0b871 412 // - CTRL_REG4_XM = 0x04: Mag data ready signal on INT2_XM pin.
vikram3 8:0a2509a0b871 413 // - CTRL_REG5_XM = 0x14: 100 Hz update rate. Low resolution. Interrupt
vikram3 8:0a2509a0b871 414 // requests don't latch. Temperature sensor disabled.
vikram3 8:0a2509a0b871 415 // - CTRL_REG6_XM = 0x00: 2 Gs scale.
vikram3 8:0a2509a0b871 416 // - CTRL_REG7_XM = 0x00: Continuous conversion mode. Normal HPF mode.
vikram3 8:0a2509a0b871 417 // - INT_CTRL_REG_M = 0x09: Interrupt active-high. Enable interrupts.
vikram3 8:0a2509a0b871 418 void initMag();
vikram3 8:0a2509a0b871 419
vikram3 8:0a2509a0b871 420 // gReadByte() -- Reads a byte from a specified gyroscope register.
vikram3 8:0a2509a0b871 421 // Input:
vikram3 8:0a2509a0b871 422 // - subAddress = Register to be read from.
vikram3 8:0a2509a0b871 423 // Output:
vikram3 8:0a2509a0b871 424 // - An 8-bit value read from the requested address.
vikram3 8:0a2509a0b871 425 uint8_t mReadByte(uint8_t subAddress);
vikram3 8:0a2509a0b871 426
vikram3 8:0a2509a0b871 427 // gReadBytes() -- Reads a number of bytes -- beginning at an address
vikram3 8:0a2509a0b871 428 // and incrementing from there -- from the gyroscope.
vikram3 8:0a2509a0b871 429 // Input:
vikram3 8:0a2509a0b871 430 // - subAddress = Register to be read from.
vikram3 8:0a2509a0b871 431 // - * dest = A pointer to an array of uint8_t's. Values read will be
vikram3 8:0a2509a0b871 432 // stored in here on return.
vikram3 8:0a2509a0b871 433 // - count = The number of bytes to be read.
vikram3 8:0a2509a0b871 434 // Output: No value is returned, but the `dest` array will store
vikram3 8:0a2509a0b871 435 // the data read upon exit.
vikram3 8:0a2509a0b871 436 void mReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count);
vikram3 8:0a2509a0b871 437
vikram3 8:0a2509a0b871 438 // gWriteByte() -- Write a byte to a register in the gyroscope.
vikram3 8:0a2509a0b871 439 // Input:
vikram3 8:0a2509a0b871 440 // - subAddress = Register to be written to.
vikram3 8:0a2509a0b871 441 // - data = data to be written to the register.
vikram3 8:0a2509a0b871 442 void mWriteByte(uint8_t subAddress, uint8_t data);
vikram3 8:0a2509a0b871 443
vikram3 8:0a2509a0b871 444 // xmReadByte() -- Read a byte from a register in the accel/mag sensor
vikram3 8:0a2509a0b871 445 // Input:
vikram3 8:0a2509a0b871 446 // - subAddress = Register to be read from.
vikram3 8:0a2509a0b871 447 // Output:
vikram3 8:0a2509a0b871 448 // - An 8-bit value read from the requested register.
vikram3 8:0a2509a0b871 449 uint8_t xgReadByte(uint8_t subAddress);
vikram3 8:0a2509a0b871 450
vikram3 8:0a2509a0b871 451 // xmReadBytes() -- Reads a number of bytes -- beginning at an address
vikram3 8:0a2509a0b871 452 // and incrementing from there -- from the accelerometer/magnetometer.
vikram3 8:0a2509a0b871 453 // Input:
vikram3 8:0a2509a0b871 454 // - subAddress = Register to be read from.
vikram3 8:0a2509a0b871 455 // - * dest = A pointer to an array of uint8_t's. Values read will be
vikram3 8:0a2509a0b871 456 // stored in here on return.
vikram3 8:0a2509a0b871 457 // - count = The number of bytes to be read.
vikram3 8:0a2509a0b871 458 // Output: No value is returned, but the `dest` array will store
vikram3 8:0a2509a0b871 459 // the data read upon exit.
vikram3 8:0a2509a0b871 460 void xgReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count);
vikram3 8:0a2509a0b871 461
vikram3 8:0a2509a0b871 462 // xmWriteByte() -- Write a byte to a register in the accel/mag sensor.
vikram3 8:0a2509a0b871 463 // Input:
vikram3 8:0a2509a0b871 464 // - subAddress = Register to be written to.
vikram3 8:0a2509a0b871 465 // - data = data to be written to the register.
vikram3 8:0a2509a0b871 466 void xgWriteByte(uint8_t subAddress, uint8_t data);
vikram3 8:0a2509a0b871 467
vikram3 8:0a2509a0b871 468 // calcgRes() -- Calculate the resolution of the gyroscope.
vikram3 8:0a2509a0b871 469 // This function will set the value of the gRes variable. gScale must
vikram3 8:0a2509a0b871 470 // be set prior to calling this function.
vikram3 8:0a2509a0b871 471 void calcgRes();
vikram3 8:0a2509a0b871 472
vikram3 8:0a2509a0b871 473 // calcmRes() -- Calculate the resolution of the magnetometer.
vikram3 8:0a2509a0b871 474 // This function will set the value of the mRes variable. mScale must
vikram3 8:0a2509a0b871 475 // be set prior to calling this function.
vikram3 8:0a2509a0b871 476 void calcmRes();
vikram3 8:0a2509a0b871 477
vikram3 8:0a2509a0b871 478 // calcaRes() -- Calculate the resolution of the accelerometer.
vikram3 8:0a2509a0b871 479 // This function will set the value of the aRes variable. aScale must
vikram3 8:0a2509a0b871 480 // be set prior to calling this function.
vikram3 8:0a2509a0b871 481 void calcaRes();
vikram3 8:0a2509a0b871 482
vikram3 8:0a2509a0b871 483 //////////////////////
vikram3 8:0a2509a0b871 484 // Helper Functions //
vikram3 8:0a2509a0b871 485 //////////////////////
vikram3 8:0a2509a0b871 486 void constrainScales();
vikram3 8:0a2509a0b871 487
vikram3 8:0a2509a0b871 488 ///////////////////
vikram3 8:0a2509a0b871 489 // SPI Functions //
vikram3 8:0a2509a0b871 490 ///////////////////
vikram3 8:0a2509a0b871 491 // initSPI() -- Initialize the SPI hardware.
vikram3 8:0a2509a0b871 492 // This function will setup all SPI pins and related hardware.
vikram3 8:0a2509a0b871 493 void initSPI();
vikram3 8:0a2509a0b871 494
vikram3 8:0a2509a0b871 495 // SPIwriteByte() -- Write a byte out of SPI to a register in the device
vikram3 8:0a2509a0b871 496 // Input:
vikram3 8:0a2509a0b871 497 // - csPin = The chip select pin of the slave device.
vikram3 8:0a2509a0b871 498 // - subAddress = The register to be written to.
vikram3 8:0a2509a0b871 499 // - data = Byte to be written to the register.
vikram3 8:0a2509a0b871 500 void SPIwriteByte(uint8_t csPin, uint8_t subAddress, uint8_t data);
vikram3 8:0a2509a0b871 501
vikram3 8:0a2509a0b871 502 // SPIreadByte() -- Read a single byte from a register over SPI.
vikram3 8:0a2509a0b871 503 // Input:
vikram3 8:0a2509a0b871 504 // - csPin = The chip select pin of the slave device.
vikram3 8:0a2509a0b871 505 // - subAddress = The register to be read from.
vikram3 8:0a2509a0b871 506 // Output:
vikram3 8:0a2509a0b871 507 // - The byte read from the requested address.
vikram3 8:0a2509a0b871 508 uint8_t SPIreadByte(uint8_t csPin, uint8_t subAddress);
vikram3 8:0a2509a0b871 509
vikram3 8:0a2509a0b871 510 // SPIreadBytes() -- Read a series of bytes, starting at a register via SPI
vikram3 8:0a2509a0b871 511 // Input:
vikram3 8:0a2509a0b871 512 // - csPin = The chip select pin of a slave device.
vikram3 8:0a2509a0b871 513 // - subAddress = The register to begin reading.
vikram3 8:0a2509a0b871 514 // - * dest = Pointer to an array where we'll store the readings.
vikram3 8:0a2509a0b871 515 // - count = Number of registers to be read.
vikram3 8:0a2509a0b871 516 // Output: No value is returned by the function, but the registers read are
vikram3 8:0a2509a0b871 517 // all stored in the *dest array given.
vikram3 8:0a2509a0b871 518 void SPIreadBytes(uint8_t csPin, uint8_t subAddress,
vikram3 8:0a2509a0b871 519 uint8_t * dest, uint8_t count);
vikram3 8:0a2509a0b871 520
vikram3 8:0a2509a0b871 521 ///////////////////
vikram3 8:0a2509a0b871 522 // I2C Functions //
vikram3 8:0a2509a0b871 523 ///////////////////
vikram3 8:0a2509a0b871 524 // initI2C() -- Initialize the I2C hardware.
vikram3 8:0a2509a0b871 525 // This function will setup all I2C pins and related hardware.
vikram3 8:0a2509a0b871 526 void initI2C();
vikram3 8:0a2509a0b871 527
vikram3 8:0a2509a0b871 528 // I2CwriteByte() -- Write a byte out of I2C to a register in the device
vikram3 8:0a2509a0b871 529 // Input:
vikram3 8:0a2509a0b871 530 // - address = The 7-bit I2C address of the slave device.
vikram3 8:0a2509a0b871 531 // - subAddress = The register to be written to.
vikram3 8:0a2509a0b871 532 // - data = Byte to be written to the register.
vikram3 8:0a2509a0b871 533 void I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data);
vikram3 8:0a2509a0b871 534
vikram3 8:0a2509a0b871 535 // I2CreadByte() -- Read a single byte from a register over I2C.
vikram3 8:0a2509a0b871 536 // Input:
vikram3 8:0a2509a0b871 537 // - address = The 7-bit I2C address of the slave device.
vikram3 8:0a2509a0b871 538 // - subAddress = The register to be read from.
vikram3 8:0a2509a0b871 539 // Output:
vikram3 8:0a2509a0b871 540 // - The byte read from the requested address.
vikram3 8:0a2509a0b871 541 uint8_t I2CreadByte(uint8_t address, uint8_t subAddress);
vikram3 8:0a2509a0b871 542
vikram3 8:0a2509a0b871 543 // I2CreadBytes() -- Read a series of bytes, starting at a register via SPI
vikram3 8:0a2509a0b871 544 // Input:
vikram3 8:0a2509a0b871 545 // - address = The 7-bit I2C address of the slave device.
vikram3 8:0a2509a0b871 546 // - subAddress = The register to begin reading.
vikram3 8:0a2509a0b871 547 // - * dest = Pointer to an array where we'll store the readings.
vikram3 8:0a2509a0b871 548 // - count = Number of registers to be read.
vikram3 8:0a2509a0b871 549 // Output: No value is returned by the function, but the registers read are
vikram3 8:0a2509a0b871 550 // all stored in the *dest array given.
vikram3 8:0a2509a0b871 551 uint8_t I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count);
vikram3 8:0a2509a0b871 552
vikram3 8:0a2509a0b871 553 private:
vikram3 8:0a2509a0b871 554 I2C i2c;
vikram3 8:0a2509a0b871 555 };
vikram3 8:0a2509a0b871 556
vikram3 8:0a2509a0b871 557 #endif // SFE_LSM9DS1_H //