adam&chuang / LSM6DS3

Dependents:   IMU_Aq

Fork of LSM6DS3 by adam&chuang

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LSM6DS3.h Source File

LSM6DS3.h

00001 #ifndef __LSM6DS3IMU_H__
00002 #define __LSM6DS3IMU_H__
00003 
00004 
00005 #include "mbed.h"
00006 #include "stdint.h"
00007 #include "LSM6DS3_Types.h"
00008 #include "LSM6DS3_Registers.h"
00009 
00010 #include "math.h"
00011 
00012 #define I2C_MODE 0
00013 #define SPI_MODE 1
00014 
00015 // Return values 
00016 typedef enum
00017 {
00018     IMU_SUCCESS,
00019     IMU_HW_ERROR,
00020     IMU_NOT_SUPPORTED,
00021     IMU_GENERIC_ERROR,
00022     IMU_OUT_OF_BOUNDS,
00023     IMU_ALL_ONES_WARNING,
00024     //...
00025 } status_t;
00026 
00027 //This is the core operational class of the driver.
00028 //  LSM6DS3Core contains only read and write operations towards the IMU.
00029 //  To use the higher level functions, use the class LSM6DS3 which inherits
00030 //  this class.
00031 //========================self define=================
00032 
00033 //====================================================
00034 
00035 class LSM6DS3Core
00036 {
00037 public:
00038     LSM6DS3Core( uint8_t );
00039     LSM6DS3Core( uint8_t, uint8_t );
00040  //   ~LSM6DS3Core() = default;
00041     
00042     status_t beginCore( void );
00043     
00044     //The following utilities read and write to the IMU
00045 
00046     //ReadRegisterRegion takes a uint8 array address as input and reads
00047     //  a chunk of memory into that array.
00048     status_t readRegisterRegion(uint8_t*, uint8_t, uint8_t );
00049     
00050     //readRegister reads one 8-bit register
00051     status_t readRegister(uint8_t*, uint8_t);
00052     
00053     //Reads two 8-bit regs, LSByte then MSByte order, and concatenates them.
00054     //  Acts as a 16-bit read operation
00055     status_t readRegisterInt16(int16_t*, uint8_t offset );
00056     
00057     //Writes an 8-bit byte;
00058     status_t writeRegister(uint8_t, uint8_t);
00059     
00060     //Change to embedded page
00061     status_t embeddedPage( void );
00062     
00063     //Change to base page
00064     status_t basePage( void );
00065     SPI spi_;
00066     I2C i2c_;
00067     DigitalOut cs_;
00068 
00069     
00070 private:
00071     
00072     //Communication stuff
00073     uint8_t commInterface;
00074     uint8_t I2CAddress;
00075     uint8_t chipSelectPin;
00076 
00077 };
00078 
00079 //This struct holds the settings the driver uses to do calculations
00080 struct SensorSettings {
00081 public:
00082     //Gyro settings
00083     uint8_t gyroEnabled;
00084     uint16_t gyroRange;
00085     uint16_t gyroSampleRate;
00086     uint16_t gyroBandWidth;
00087 
00088     uint8_t gyroFifoEnabled;
00089     uint8_t gyroFifoDecimation;
00090 
00091     //Accelerometer settings
00092     uint8_t accelEnabled;
00093     uint8_t accelODROff;
00094     uint16_t accelRange;
00095     uint16_t accelSampleRate;
00096     uint16_t accelBandWidth;
00097     
00098     uint8_t accelFifoEnabled;
00099     uint8_t accelFifoDecimation;
00100     
00101     //Temperature settings
00102     uint8_t tempEnabled;
00103     
00104     //Non-basic mode settings
00105     uint8_t commMode;
00106     
00107     //FIFO control data
00108     uint16_t fifoThreshold;
00109     int16_t fifoSampleRate;
00110     uint8_t fifoModeWord;
00111     
00112 };
00113 
00114 
00115 //This is the highest level class of the driver.
00116 //
00117 //  class LSM6DS3 inherits the core and makes use of the beginCore()
00118 //method through it's own begin() method.  It also contains the
00119 //settings struct to hold user settings.
00120 
00121 class LSM6DS3 : public LSM6DS3Core
00122 {
00123 public:
00124     //IMU settings
00125     SensorSettings settings;
00126     
00127     //Error checking
00128     uint16_t allOnesCounter;
00129     uint16_t nonSuccessCounter;
00130 
00131     //Constructor generates default SensorSettings.
00132     //(over-ride after construction if desired)
00133     LSM6DS3( uint8_t busType = I2C_MODE, uint8_t inputArg = 0x6B );
00134 //    ~LSM6DS3() = default;
00135     
00136     //Call to apply SensorSettings
00137     status_t begin(void);
00138 
00139     //Returns the raw bits from the sensor cast as 16-bit signed integers
00140     int16_t readRawAccelX( void );
00141     int16_t readRawAccelY( void );
00142     int16_t readRawAccelZ( void );
00143     int16_t readRawGyroX( void );
00144     int16_t readRawGyroY( void );
00145     int16_t readRawGyroZ( void );
00146     
00147     int16_t accelOffset[3];
00148     int16_t gyroOffset[3];
00149     void setOffset(int16_t, int16_t, int16_t, int16_t, int16_t, int16_t);
00150     
00151 
00152 
00153     //Returns the values as floats.  Inside, this calls readRaw___();
00154     float readFloatAccelX( void );
00155     float readFloatAccelY( void );
00156     float readFloatAccelZ( void );
00157     float readFloatGyroX( void );
00158     float readFloatGyroY( void );
00159     float readFloatGyroZ( void );
00160 
00161     //Temperature related methods
00162     int16_t readRawTemp( void );
00163     float readTempC( void );
00164     float readTempF( void );
00165 
00166     //FIFO stuff
00167     void fifoBegin( void );
00168     void fifoClear( void );
00169     int16_t fifoRead( void );
00170     uint16_t fifoGetStatus( void );
00171     void fifoEnd( void );
00172     
00173     float calcGyro( int32_t );
00174     float calcAccel( int32_t );
00175     
00176 private:
00177 
00178 };
00179 
00180 #endif