-
Notifications
You must be signed in to change notification settings - Fork 18
readTemperature now returns float. #20
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| 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 */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable name |
||
| 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; | ||
| } | ||
|
|
||
|
|
@@ -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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0x00FFis incredibly easier to read than0b0000000011111111, please fix.