adafruit / Adafruit_CircuitPython_VL53L1X

CircuitPython module for interacting with the VL53L1X distance sensor.
MIT License
8 stars 10 forks source link

ValueError from vl53.distance #9

Closed Idirianuk closed 2 years ago

Idirianuk commented 2 years ago
Adafruit CircuitPython 7.3.0 on 2022-05-23; FeatherS3 with ESP32S3
Board ID:unexpectedmaker_feathers3

I can't ascertain the previous version (at previous code update, 2022-05-30, so before then, whichever bundle that was, worked perfectly, bundle dated 2022-06-11 is not working). I've currently reverted to the older version.

Code Snippet (generating a string because I want to send it to an oled):

if vl53.data_ready:
        text = "Distance: {:.1f}cm".format(vl53.distance)
        print(text)
        oled.fill(0)
        oled.text(text, 0, 0, 1)

In this context (in my main code loop) it quits to the REPL on the second line above. Error Generated:

Traceback (most recent call last):
  File "code.py", line 129, in <module>
ValueError: unknown format code 'f' for object of type 'NoneType'

The error is not always generated, but after a few iterations, it does fail, giving a distance reading of "None"

This is the code and sample output from the example code given on learn.adafruit.com


vl53.start_ranging()

while True:
    if vl53.data_ready:
        print("Distance: {} cm".format(vl53.distance))
        vl53.clear_interrupt()
        time.sleep(1.0)

VL53L1X Simple Test.
--------------------
Model ID: 0xEA
Module Type: 0xCC
Mask Revision: 0x10
Distance Mode: LONG
Timing Budget: 50
--------------------
Distance: 195.7 cm
Distance: 188.7 cm
Distance: 191.4 cm
Distance: 188.0 cm
Distance: None cm
Distance: None cm
Distance: 193.9 cm
Distance: 186.8 cm
Distance: None cm
Distance: 188.2 cm
Idirianuk commented 2 years ago

Having looked more closely, it seems that some defaults have changed.

It's now working if I specify a distance_mode (it seems to have been defaulting to 1, SHORT, I needed LONG) and check the validity of the returned distance.

vl53.distance_mode = 2
vl53.timing_budget = 100

I've also added a check for the returned distance being out of range:

if vl53.data_ready:
    distance = vl53.distance
    if distance:    # Check for valid range
        text = "Distance: {:.1f}cm".format(distance)
        print(text)
        oled.fill(0)
        oled.text(text, 0, 0, 1)

    vl53.clear_interrupt()
caternuson commented 2 years ago

This is expected and what you've done is the correct general fix - i.e, you need to check the validity of distance in your code since it may now return None if the ranging was invalid for some reason. For example, if there is no object in front of the sensor.

The basic example is tolerant of this since it does not do any special formatting in the print:

        print("Distance: {} cm".format(vl53.distance))

and will simply print Distance: None cm.

Here is the PR that added this feature: https://github.com/adafruit/Adafruit_CircuitPython_VL53L1X/pull/8 and the associated issue: https://github.com/adafruit/Adafruit_CircuitPython_VL53L1X/issues/7

Idirianuk commented 2 years ago

Many thanks. It was the apparent change in behaviour which threw me.

Closing. 😀