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