self-balancing-robot

Dependencies:   mbed mbed-rtos Motor

Committer:
pandirimukund
Date:
Mon Apr 27 16:57:40 2020 +0000
Revision:
41:b9c8d527dd2b
Parent:
13:8d8ac3189984
added comments and cleaned up code

Who changed what in which revision?

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