AHRS Library

Committer:
altb
Date:
Tue Dec 04 15:49:48 2018 +0000
Revision:
3:6811c0ce95f6
Parent:
0:6661e1395e30
AHRS Klasse mit Mahony filter etc

Who changed what in which revision?

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