Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce number of I2C reads on values already known #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions src/LSM9DS1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ int LSM9DS1Class::begin()
{
_wire->begin();

storedAccelFS = false;
storedGyroFS = false;
storedMagnetFS = false;

// reset
writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG8, 0x05);
writeRegister(LSM9DS1_ADDRESS_M, LSM9DS1_CTRL_REG2_M, 0x0c);
Expand Down Expand Up @@ -240,15 +244,20 @@ float LSM9DS1Class::getAccelBW() //Bandwidth setting 0,1,2,3 see documentation

int LSM9DS1Class::setAccelFS(uint8_t range) // 0: ±2g ; 1: ±16g ; 2: ±4g ; 3: ±8g
{ if (range >=4) return 0;
lastAccelFS = range;
storedAccelFS = true;
range = (range & 0b00000011) << 3;
uint8_t setting = ((readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL) & 0xE7) | range);
return writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL,setting) ;
return writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL,setting) ;
}

float LSM9DS1Class::getAccelFS() // Full scale dimensionless, but its value corresponds to g
{ float ranges[] ={2.0, 24.0, 4.0, 8.0}; //g
uint8_t setting = (readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL) & 0x18) >> 3;
return ranges[setting] ;
{ float ranges[] ={2.0, 24.0, 4.0, 8.0}; //g
if(!storedAccelFS) {
lastAccelFS = (readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL) & 0x18) >> 3;
storedAccelFS = true;
}
return ranges[lastAccelFS] ;
}

//************************************ Gyroscope *****************************************
Expand Down Expand Up @@ -367,15 +376,20 @@ float LSM9DS1Class::getGyroBW()

int LSM9DS1Class::setGyroFS(uint8_t range) // (0: 245 dps; 1: 500 dps; 2: 1000 dps; 3: 2000 dps)
{ if (range >=4) return 0;
lastGyroFS = range;
storedGyroFS = true;
range = (range & 0b00000011) << 3;
uint8_t setting = ((readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G) & 0xE7) | range );
uint8_t setting = ((readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G) & 0xE7) | range );
return writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G,setting) ;
}

float LSM9DS1Class::getGyroFS() // dimensionless, but its value defaults to deg/s
{ float Ranges[] ={245.0, 500.0, 1000.0, 2000.0}; //dps
uint8_t setting = (readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G) & 0x18) >> 3;
return Ranges[setting] ;
if(!storedGyroFS) {
lastGyroFS = (readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G) & 0x18) >> 3;
storedGyroFS = true;
}
return Ranges[lastGyroFS] ;
}

//************************************ Magnetic field *****************************************
Expand Down Expand Up @@ -430,14 +444,19 @@ void LSM9DS1Class::setMagnetSlope(float x, float y, float z)

int LSM9DS1Class::setMagnetFS(uint8_t range) // 0=400.0; 1=800.0; 2=1200.0 , 3=1600.0 (µT)
{ if (range >=4) return 0;
range = (range & 0b00000011) << 5;
lastMagnetFS = range;
storedMagnetFS = true;
range = (range & 0b00000011) << 5;
return writeRegister(LSM9DS1_ADDRESS_M, LSM9DS1_CTRL_REG2_M,range) ;
}

float LSM9DS1Class::getMagnetFS() // dimensionless, but its value defaults to µT
{ const float Ranges[] ={400.0, 800.0, 1200.0, 1600.0}; //
uint8_t setting = readRegister(LSM9DS1_ADDRESS_M, LSM9DS1_CTRL_REG2_M) >> 5;
return Ranges[setting] ;
if(!storedMagnetFS) {
lastMagnetFS = readRegister(LSM9DS1_ADDRESS_M, LSM9DS1_CTRL_REG2_M) >> 5;
storedMagnetFS = true;
}
return Ranges[lastMagnetFS] ;
}

int LSM9DS1Class::setMagnetODR(uint8_t range) // range (0..8) = {0.625,1.25,2.5,5,10,20,40,80,400}Hz
Expand All @@ -447,6 +466,7 @@ int LSM9DS1Class::setMagnetODR(uint8_t range) // range (0..8) = {0.625,1.25,2.5
writeRegister(LSM9DS1_ADDRESS_M, LSM9DS1_CTRL_REG1_M,setting) ;
uint16_t duration = 1750 / (range + 1); // 1750,875,666,500,400,333,285,250,222 calculate measuring time
magnetODR= measureMagnetODR(duration); //measure the actual ODR value
return 1;
}

float LSM9DS1Class::getMagnetODR() // Output {0.625, 1.25, 2.5, 5.0, 10.0, 20.0, 40.0 , 80.0}; //Hz
Expand Down
6 changes: 6 additions & 0 deletions src/LSM9DS1.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class LSM9DS1Class {
float accelOffset[3] = {0,0,0}; // zero point offset correction factor for calibration
float accelSlope[3] = {1,1,1}; // slope correction factor for calibration
float accelUnit = GRAVITY; // GRAVITY OR METERPERSECOND2
uint8_t lastAccelFS;
bool storedAccelFS;
virtual int readAccel(float& x, float& y, float& z); // Return calibrated data in unit of choise G or m/s2.
virtual int readRawAccel(float& x, float& y, float& z); // Return uncalibrated results
virtual int accelAvailable(); // Number of samples in the FIFO.
Expand All @@ -98,6 +100,8 @@ class LSM9DS1Class {
float gyroOffset[3] = {0,0,0}; // zero point offset correction factor for calibration
float gyroSlope[3] = {1,1,1}; // slope correction factor for calibration
float gyroUnit = DEGREEPERSECOND; // DEGREEPERSECOND RADIANSPERSECOND REVSPERMINUTE REVSPERSECOND
uint8_t lastGyroFS;
bool storedGyroFS;
virtual int readGyro(float& x, float& y, float& z); // Return calibrated data in in unit of choise °/s or rad/s.
virtual int readRawGyro(float& x, float& y, float& z); // Return uncalibrated results
virtual int gyroAvailable(); // Number of samples in the FIFO.
Expand All @@ -114,6 +118,8 @@ class LSM9DS1Class {
float magnetOffset[3] = {0,0,0}; // zero point offset correction factor for calibration
float magnetSlope[3] = {1,1,1}; // slope correction factor for calibration
float magnetUnit = MICROTESLA; // GAUSS, MICROTESLA NANOTESLA
uint8_t lastMagnetFS;
bool storedMagnetFS;
virtual int readMagnet(float& x, float& y, float& z); // Return calibrated data in unit of choise µT , nT or G
virtual int readRawMagnet(float& x, float& y, float& z); // Return uncalibrated results
virtual int magnetAvailable(); // Number of samples in the FIFO.
Expand Down