Skip to content

Commit

Permalink
Merge pull request #4 from haplm/FemmeVerbeek-Version-2.0
Browse files Browse the repository at this point in the history
Small changes in code style
  • Loading branch information
FemmeVerbeek authored May 25, 2020
2 parents 273ed57 + 2c33798 commit 1f5a19d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 51 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ Arduino_LSM9DS1 1.0.0 - 2019.07.31
Arduino_LSM9DS1 1.1.0 - 2020.02.11

* Added support for FIFO continuous reading of values

Arduino_LSM9DS1 2.0.0 - 2020.05.15

* Offset, Full scale, sample rate on all DOF.
* Calibration parameters integrated
14 changes: 0 additions & 14 deletions CHANGELOG.txt

This file was deleted.

46 changes: 23 additions & 23 deletions src/LSM9DS1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ int LSM9DS1Class::accelAvailable()
return 0;
}

void LSM9DS1Class::setAccelOffset(float x, float y, float z) //Look out, from measurements only
{ accelOffset[0] = x /(accelUnit * accelSlope[0]);
accelOffset[1] = y /(accelUnit * accelSlope[1]);
accelOffset[2] = z /(accelUnit * accelSlope[2]);
void LSM9DS1Class::setAccelOffset(float x, float y, float z)
{ accelOffset[0] = x /(accelUnit * gyroSlope[0]);
accelOffset[1] = y /(accelUnit * gyroSlope[1]);
accelOffset[2] = z /(accelUnit * gyroSlope[2]);
}

void LSM9DS1Class::setAccelSlope(float x, float y, float z)
Expand All @@ -166,8 +166,8 @@ void LSM9DS1Class::setAccelSlope(float x, float y, float z)

int LSM9DS1Class::setAccelODR(int8_t range) //Sample Rate 0:off, 1:10Hz, 2:50Hz, 3:119Hz, 4:238Hz, 5:476Hz, 6:952Hz, 7:NA
{ if (range==7) range =0;
range = (range & B00000111) << 5;
uint8_t setting = ((readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL) & B00011111) | range);
range = (range & 0b00000111) << 5;
uint8_t setting = ((readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL) & 0b00011111) | range);
return writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL,setting) ;
}
float LSM9DS1Class::getAccelODR()
Expand All @@ -177,22 +177,22 @@ float LSM9DS1Class::getAccelODR()
}

float LSM9DS1Class::setAccelBW(int8_t range) //0,1,2,3 Override autoBandwidth setting see doc.table 67
{ range = range & B00000011;
uint8_t RegIs = readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL) & B11111000;
RegIs = RegIs | B00000100 | range ;
{ range = range & 0b00000011;
uint8_t RegIs = readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL) & 0b11111000;
RegIs = RegIs | 0b00000100 | range ;
return writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL,RegIs) ;
}

float LSM9DS1Class::getAccelBW() //Bandwidth setting 0,1,2,3 see documentation table 67
{ float autoRange[] ={0.0, 408.0, 408.0, 50.0, 105.0, 211.0, 408.0, 0.0 };
float BWXLRange[] ={ 408.0, 211.0, 105.0, 50.0 };
uint8_t RegIs = readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL);
if (bitRead(2,RegIs)) return BWXLRange [RegIs & B00000011];
if (bitRead(2,RegIs)) return BWXLRange [RegIs & 0b00000011];
else return autoRange [ RegIs >> 5 ];
}

int LSM9DS1Class::setAccelFS(int8_t range) // 0: ±2g ; 1: ±16g ; 2: ±4g ; 3: ±8g
{ range = (range & B00000011) << 3;
{ 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) ;
}
Expand Down Expand Up @@ -228,7 +228,7 @@ int LSM9DS1Class::gyroAvailable()
return 0;
}

void LSM9DS1Class::setGyroOffset(float x, float y, float z) //Look out, from measurements only
void LSM9DS1Class::setGyroOffset(float x, float y, float z)
{ gyroOffset[0] = x /(gyroUnit * gyroSlope[0]);
gyroOffset[1] = y /(gyroUnit * gyroSlope[1]);
gyroOffset[2] = z /(gyroUnit * gyroSlope[2]);
Expand All @@ -242,8 +242,8 @@ void LSM9DS1Class::setGyroSlope(float x, float y, float z)

int LSM9DS1Class::setGyroODR(int8_t range) // 0:off, 1:10Hz, 2:50Hz, 3:119Hz, 4:238Hz, 5:476Hz, 6:952Hz, 7:NA
{ if (range==7) range =0;
range = (range & B00000111) << 5;
uint8_t setting = ((readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G) & B00011111) | range);
range = (range & 0b00000111) << 5;
uint8_t setting = ((readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G) & 0b00011111) | range);
return writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G,setting) ;
}

Expand All @@ -254,8 +254,8 @@ float LSM9DS1Class::getGyroODR()
}

int LSM9DS1Class::setGyroBW(int8_t range)
{ range = range & B00000011;
uint8_t setting = readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G) & B11111100;
{ range = range & 0b00000011;
uint8_t setting = readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G) & 0b11111100;
return writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G,setting | range) ;
}

Expand All @@ -274,12 +274,12 @@ float BWtable[ ODRrows ][ BWcols ] = // acc to
float LSM9DS1Class::getGyroBW()
{ uint8_t setting = readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G) ;
uint8_t ODR = setting >> 5;
uint8_t BW = setting & B00000011;
uint8_t BW = setting & 0b00000011;
return BWtable[ODR][BW];
}

int LSM9DS1Class::setGyroFS(int8_t range) // (0: 245 dps; 1: 500 dps; 2: 1000 dps; 3: 2000 dps)
{ range = (range & B00000011) << 3;
{ range = (range & 0b00000011) << 3;
uint8_t setting = ((readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G) & 0xE7) | range );
return writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G,setting) ;
}
Expand Down Expand Up @@ -316,7 +316,7 @@ int LSM9DS1Class::magneticFieldAvailable()
return 0;
}

void LSM9DS1Class::setMagnetOffset(float x, float y, float z) //Look out, from measurements only
void LSM9DS1Class::setMagnetOffset(float x, float y, float z)
{ magnetOffset[0] = x /(magnetUnit * magnetSlope[0]);
magnetOffset[1] = y /(magnetUnit * magnetSlope[1]);
magnetOffset[2] = z /(magnetUnit * magnetSlope[2]);
Expand All @@ -329,7 +329,7 @@ void LSM9DS1Class::setMagnetSlope(float x, float y, float z)
}

int LSM9DS1Class::setMagnetFS(int8_t range) // 0=400.0; 1=800.0; 2=1200.0 , 3=1600.0 (µT)
{ range = (range & B00000011) << 5;
{ range = (range & 0b00000011) << 5;
return writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG2_M,range) ;
}

Expand All @@ -340,14 +340,14 @@ float LSM9DS1Class::getMagnetFS() //
}

int LSM9DS1Class::setMagnetODR(int8_t range) // range (0..7) corresponds to {0.625,1.25,2.5,5.0,10.0,20.0,40.0,80.0}Hz
{ range = (range & B00000111) << 2;
uint8_t setting = ((readRegister(LSM9DS1_ADDRESS_M, LSM9DS1_CTRL_REG1_M) & B11100011) | range);
{ range = (range & 0b00000111) << 2;
uint8_t setting = ((readRegister(LSM9DS1_ADDRESS_M, LSM9DS1_CTRL_REG1_M) & 0b11100011) | range);
return writeRegister(LSM9DS1_ADDRESS_M, LSM9DS1_CTRL_REG1_M,setting) ;
}

float LSM9DS1Class::getMagnetODR() // Output {0.625, 1.25, 2.5, 5.0, 10.0, 20.0, 40.0 , 80.0}; //Hz
{ const float ranges[] ={0.625, 1.25,2.5, 5.0, 10.0, 20.0, 40.0 , 80.0}; //Hz
uint8_t setting = (readRegister(LSM9DS1_ADDRESS_M, LSM9DS1_CTRL_REG1_M) & B00011100) >> 2;
uint8_t setting = (readRegister(LSM9DS1_ADDRESS_M, LSM9DS1_CTRL_REG1_M) & 0b00011100) >> 2;
return ranges[setting];
}

Expand Down
28 changes: 14 additions & 14 deletions src/LSM9DS1.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
version 2.0.0
*/

#ifndef LSM9DS1_V2.0
#define LSM9DS1_V2.0
#ifndef LSM9DS1_V2
#define LSM9DS1_V2
#endif

#define accelerationSampleRate getAccelODR
Expand Down Expand Up @@ -74,12 +74,12 @@ class LSM9DS1Class {

// Accelerometer
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
float accelSlope[3] = {1,1,1}; // slope correction factor for calibration
float accelUnit = GRAVITY; // GRAVITY OR METERPERSECOND2
virtual int readAccel(float& x, float& y, float& z); // Results are in G (earth gravity) or m/s2.
virtual int accelAvailable(); // Number of samples in the FIFO.
virtual void setAccelOffset(float x, float y, float z); //Look out, from measurements only
virtual void setAccelSlope(float x, float y, float z);
virtual void setAccelOffset(float x, float y, float z); //Look out, from measurements only
virtual void setAccelSlope(float x, float y, float z);
virtual int setAccelODR(int8_t range); // Sample Rate 0:off, 1:10Hz, 2:50Hz, 3:119Hz, 4:238Hz, 5:476Hz, 6:952Hz, 7:NA Automatic setting of BW
virtual float getAccelODR(); // Sample Rate of the sensor.
virtual float setAccelBW(int8_t range); //0,1,2,3 Override autoBandwidth setting see doc.table 67
Expand All @@ -88,13 +88,13 @@ class LSM9DS1Class {
virtual float getAccelFS(); // Full Scale setting (output = 2.0, 16.0 , 4.0 , 8.0)

// Gyroscope
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 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 or RADIANSPERSECOND
virtual int readGyro(float& x, float& y, float& z); // Results are in degrees/second or rad/s.
virtual int gyroAvailable(); // Number of samples in the FIFO.
virtual void setGyroOffset(float x, float y, float z); //Look out, from measurements only
virtual void setGyroSlope(float x, float y, float z);
virtual void setGyroOffset(float x, float y, float z); //Look out, from measurements only
virtual void setGyroSlope(float x, float y, float z);
virtual int setGyroODR(int8_t range); //Sampling Rate 0:off, 1:10Hz, 2:50Hz, 3:119Hz, 4:238Hz, 5:476Hz, 6:952Hz, 7:NA
virtual float getGyroODR(); // Sampling rate of the sensor.
virtual int setGyroBW(int8_t range); //Bandwidth setting 0,1,2,3 see documentation table 46 and 47
Expand All @@ -103,13 +103,13 @@ class LSM9DS1Class {
virtual float getGyroFS(); // (output = 245.0, 500.0 , 1000.0, 2000.0)

// Magnetometer
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 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 or MICROTESLA
virtual int readMagnet(float& x, float& y, float& z); // Default results are in uT (micro Tesla)
virtual int magnetAvailable(); // Number of samples in the FIFO.
virtual void setMagnetOffset(float x, float y, float z); //Look out, from measurements only
virtual void setMagnetSlope(float x, float y, float z);
virtual void setMagnetOffset(float x, float y, float z); //Look out, from measurements only
virtual void setMagnetSlope(float x, float y, float z);
virtual int setMagnetODR(int8_t range); // Sampling rate (0..7) corresponds to {0.625,1.25,2.5,5.0,10.0,20.0,40.0,80.0}Hz
virtual float getMagnetODR(); // Sampling rate of the sensor in Hz.
virtual int setMagnetFS(int8_t range); // 0=400.0; 1=800.0; 2=1200.0 , 3=1600.0 (µT)
Expand Down

0 comments on commit 1f5a19d

Please sign in to comment.