sparkfun / SparkFun_VL53L1X_Arduino_Library

A library for the laser based VL53L1X Time Of Flight distance sensor capable of detecting a target 4m away!
MIT License
88 stars 50 forks source link

Suggestion: Add getGetRangeStatus() to example to properly handle empty rooms and out of range. #54

Open jhaand opened 2 years ago

jhaand commented 2 years ago

Expected: Doing a getDistance() with the VL53L1x sensor towards an empty room results in a large distance reading. Observed: Sometimes the getDistance() towards an empty room results in values close to the sensor (500 mm or less.) work-around: Check the quality of the measured value by doing a getRangeStatus() over the observed reading. If it's not 0 or 1, then discard the de read distance.

Reference: St forum discussion here. https://community.st.com/s/question/0D50X00009sUiJUSA0/out-of-range-readings-of-vl53l1x

note: This has taken me more than a day to figure out. It would help if the default example would also show the getRangeStatus() of the sensor to help people get their application going. See the example below.

        distanceSensor.startRanging(); // Write configuration bytes to initiate measurement
        while (!distanceSensor.checkForDataReady()) {
            delay(1);
        } 
        distance = distanceSensor.getDistance(); // Get the result of the measurement from the sensor
        sensor_state = distanceSensor.getRangeStatus(); // Check if the sensor actually sees an object.
        distanceSensor.clearInterrupt();
        distanceSensor.stopRanging();
    }
    if (sensor_state > 0) {   // If no object is seen then return an status value. 
        distance = -1;
    }
    return distance;