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

VL53L1X GetOffset Method Returns Incorrect Value for Negative Offset #45

Open
dhrynyk opened this issue Apr 11, 2021 · 0 comments
Open

Comments

@dhrynyk
Copy link

dhrynyk commented Apr 11, 2021

Issue:
For a VL53L1X_SetOffset method call with a value of -1, VL53L1X_GetOffset will return a value of 2047.

Diagnosis:
The right shift operation (Temp = Temp >> 5;) in VL53L1X_GetOffset does not correctly address a negative offset value. The shift right is intended to restore the offset to the original value after correctly setting the sign with the shift left operation. However, the shift right for a negative arithmetic operand in C++ is implementation dependent. For the RPI 4B, zeros are placed in the leading bit(s) and an incorrect positive offset are returned. Using the division by 2**5 will ensure the correct positive or negative value is restored.

Recommendation:

Replace the lines (vl53l1x_class.cpp 639-640):
Temp = Temp >> 5;
*offset = (int16_t)(Temp);
with
*offset = (static_cast<int16_t>(Temp))/32;

I have successfully tested this change on a RPI 4B using g++ version 10.2.0 and the C++17 standard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant