Update failed read check. #8
Merged
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.
The way I initially created this check was not sufficient. The current way it works, it fails with "Sensor not found" if you hold an object too close to the sensor. It turns out that UART will return
None
under multiple situations: no sensor found, wiring incorrect, object too close to sensor. I worked with @dhalbert to debug the issue and determined that simply checking forif not data
was not robust enough. With Dan's help, we created a much more robust check that allows for a single failed read, includes a delay to handle the fact that the sensor can't handle quick successive reads, and instead of throwing an error, returnsNone
. I also updated thelen(data) != 2
check to return None. This means in the event of that failure, the user would not need to include try/except code to have the sensor code continue running.I was able to get the sensor to return
None
for_uart.read
under all three listed situations: incorrect wiring, sensor not found and an object too close to the sensor. This is what triggered me requesting assistance to update the check because placing my hand over the sensor was triggering the currentRuntimeError: Sensor not found. Check wiring!
.I was not however able to get the sensor to fail the
len(data) != 2
check by either moving it around very quickly, or pointing it at the sky with no object in front of it. Moving it around quickly returns various distances. Pointing it at the sky returned 1122.9 for distance, and did not fail. I can not verify that the explanation in the docstring for this check is accurate, but I did not remove it as it was included in the original. I simply updated it to explain that it returnsNone
.Fixed what I believe to be typo in the docstring - I believe it was meant to say the sensor can not detect objects over 460 cm away (the "datasheet" states max 450, so this approximately matches what I assume the docstring was meant to say).