Skip to content
Closed
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
2 changes: 1 addition & 1 deletion examples/SimpleTemperature/SimpleTemperature.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void loop()
{
if (IMU.temperatureAvailable())
{
int temperature_deg = 0;
float temperature_deg = 0.0;
IMU.readTemperature(temperature_deg);

Serial.print("LSM6DSOX Temperature = ");
Expand Down
43 changes: 35 additions & 8 deletions src/LSM6DSOX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,48 @@ int LSM6DSOXClass::gyroscopeAvailable()
return 0;
}

int LSM6DSOXClass::readTemperature(int & temperature_deg)
/*
* Datasheet:https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwjc9uzl47X5AhWvkokEHXYPCFAQFnoECBEQAQ&url=https%3A%2F%2Fwww.st.com%2Fresource%2Fen%2Fdatasheet%2Flsm6dsox.pdf&usg=AOvVaw3yto92UevKnnlUg845zeDh
* Page 70
*
* It says temperature is stored in 16 bit space with 2's complement. In oher words, the temperature is 15 bit... which covers
* awfully large number. Not -40 degree to 85 degree.
*
* Considering how ADCs usually work, this big 16 bit number seems to be a quantization for
* the sensor's full range: -40 to 85, 125 degrees.
*
* Also, it says (at Table 4) the value is zero when the temperature is around 25°C.
*
* So I've devised this code... but it seems temperature read itself is a bit higher than
* real temperature.
*
*/
int LSM6DSOXClass::readTemperature(float & temperature_deg)
{
/* Read the raw temperature from the sensor. */
int16_t temperature_raw = 0;
uint8_t temp_L = 0;
uint8_t temp_H = 0;
uint16_t temp_raw = 0;
int16_t temp_conv = 0;

if (readRegisters(LSM6DSOX_OUT_TEMP_L, reinterpret_cast<uint8_t*>(&temperature_raw), sizeof(temperature_raw)) != 1) {
if (readRegisters(LSM6DSOX_OUT_TEMP_L, reinterpret_cast<uint8_t*>(&temp_L), sizeof(temp_L)) != 1 ) {
return 0;
}
if (readRegisters(LSM6DSOX_OUT_TEMP_H, reinterpret_cast<uint8_t*>(&temp_H), sizeof(temp_H)) != 1 ) {
return 0;
}

temp_raw += ((int16_t)temp_H & 0b0000000011111111);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0x00FF is incredibly easier to read than 0b0000000011111111, please fix.

temp_raw = ((temp_raw << 8) & 0b1111111100000000);
temp_raw += ((int16_t)temp_L & 0b0000000011111111);
temp_conv = (int16_t)temp_raw;

/* Convert to °C. */
static int const TEMPERATURE_LSB_per_DEG = 256;
static int const TEMPERATURE_OFFSET_DEG = 25;

temperature_deg = (static_cast<int>(temperature_raw) / TEMPERATURE_LSB_per_DEG) + TEMPERATURE_OFFSET_DEG;
float fs = 125.0; /* -40 to 85 */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name fs does not feel like an improvement over TEMPERATURE_LSB_per_DEG. P

static float const TEMPERATURE_OFFSET_DEG = 25.0;

temperature_deg = (static_cast<float>(temp_conv)/static_cast<float>(1<<16))*fs + TEMPERATURE_OFFSET_DEG;

return 1;
}

Expand Down Expand Up @@ -269,4 +296,4 @@ int LSM6DSOXClass::writeRegister(uint8_t address, uint8_t value)
LSM6DSOXClass IMU(LSM6DS_DEFAULT_SPI, PIN_SPI_SS1, LSM6DS_INT);
#else
LSM6DSOXClass IMU(Wire, LSM6DSOX_ADDRESS);
#endif
#endif
2 changes: 1 addition & 1 deletion src/LSM6DSOX.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class LSM6DSOXClass {
int gyroscopeAvailable(); // Check for available data from gyroscope

// Temperature
int readTemperature(int & temperature_deg);
int readTemperature(float & temperature_deg);
int temperatureAvailable();

private:
Expand Down