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