From 7a82af0ce31fb7c5ff416df8cb0a04feb7693082 Mon Sep 17 00:00:00 2001 From: Mark Kamp Date: Wed, 19 Apr 2023 14:54:03 +0200 Subject: [PATCH 1/5] Update adafruit_vl53l4cd.py Added range_status() function to get the measurement validity of the current measurement. --- adafruit_vl53l4cd.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/adafruit_vl53l4cd.py b/adafruit_vl53l4cd.py index 653ab08..0b3f719 100644 --- a/adafruit_vl53l4cd.py +++ b/adafruit_vl53l4cd.py @@ -199,6 +199,32 @@ def distance(self): dist = struct.unpack(">H", dist)[0] return dist / 10 + @property + def range_status(self): + """Measurement validity. Returns a number between 0 to 255 where: + 0 - None - Returned distance is valid + 1 - Warning - Sigma is above the defined threshold + 2 - Warning - Signal is below the defined threshold + 3 - Error - Measured distance is below detection threshold + 4 - Error - Phase out of valid limit + 5 - Error - Hardware fail + 6 - Warning - Phase valid but no wrap around check performed + 7 - Error - Wrapped target, phase does not match + 8 - Error - Processing fail + 9 - Error - Crosstalk signal fail + 10 - Error - Interrupt error + 11 - Error - Merged target + 12 - Error - Signal is too low + 255 - Error - Other error (for example, boot error) + """ + status_rtn = [255, 255, 255, 5, 2, 4, 1, 7, 3, 0, 255, 255, 9, 13, 255, 255, 255, 255, 10, 6, 255, 255, 11, 12] + status = self._read_register(_VL53L4CD_RESULT_RANGE_STATUS, 1) + status = struct.unpack(">B", status)[0] + status = status & 0x1F + if status < 24: + status = status_rtn[status] + return status + @property def timing_budget(self): """Ranging duration in milliseconds. Valid range is 10ms to 200ms.""" From 41ef3c87705f540adfdfbbda239db5866f01c2dc Mon Sep 17 00:00:00 2001 From: Mark Kamp Date: Wed, 19 Apr 2023 14:56:23 +0200 Subject: [PATCH 2/5] Update adafruit_vl53l4cd.py Added sigma() function to get the estimated noise for the latest reported distance. --- adafruit_vl53l4cd.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/adafruit_vl53l4cd.py b/adafruit_vl53l4cd.py index 0b3f719..119716a 100644 --- a/adafruit_vl53l4cd.py +++ b/adafruit_vl53l4cd.py @@ -225,6 +225,13 @@ def range_status(self): status = status_rtn[status] return status + @property + def sigma(self): + """Sigma estimator for the noise in the reported target distance in units of centimeters.""" + sigma = self._read_register(_VL53L4CD_RESULT_SIGMA, 2) + sigma = struct.unpack(">H", sigma)[0] + return sigma / 40 + @property def timing_budget(self): """Ranging duration in milliseconds. Valid range is 10ms to 200ms.""" From 3394c12a7c9c5c8ce9c89abc70151676c738b7ee Mon Sep 17 00:00:00 2001 From: Mark Kamp Date: Wed, 19 Apr 2023 16:10:29 +0200 Subject: [PATCH 3/5] Code check with pre-commit --- adafruit_vl53l4cd.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/adafruit_vl53l4cd.py b/adafruit_vl53l4cd.py index 119716a..4e7f509 100644 --- a/adafruit_vl53l4cd.py +++ b/adafruit_vl53l4cd.py @@ -217,7 +217,32 @@ def range_status(self): 12 - Error - Signal is too low 255 - Error - Other error (for example, boot error) """ - status_rtn = [255, 255, 255, 5, 2, 4, 1, 7, 3, 0, 255, 255, 9, 13, 255, 255, 255, 255, 10, 6, 255, 255, 11, 12] + status_rtn = [ + 255, + 255, + 255, + 5, + 2, + 4, + 1, + 7, + 3, + 0, + 255, + 255, + 9, + 13, + 255, + 255, + 255, + 255, + 10, + 6, + 255, + 255, + 11, + 12, + ] status = self._read_register(_VL53L4CD_RESULT_RANGE_STATUS, 1) status = struct.unpack(">B", status)[0] status = status & 0x1F From a116de982281c97d4219fe146ddfa241bb36410c Mon Sep 17 00:00:00 2001 From: Mark Kamp Date: Wed, 17 May 2023 14:49:00 +0200 Subject: [PATCH 4/5] Increase readability, range status as constants --- adafruit_vl53l4cd.py | 82 +++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/adafruit_vl53l4cd.py b/adafruit_vl53l4cd.py index 4e7f509..784a41c 100644 --- a/adafruit_vl53l4cd.py +++ b/adafruit_vl53l4cd.py @@ -66,6 +66,21 @@ _VL53L4CD_FIRMWARE_SYSTEM_STATUS = const(0x00E5) _VL53L4CD_IDENTIFICATION_MODEL_ID = const(0x010F) +_VL53L4CD_RANGE_VALID = const(0x00) +_VL53L4CD_RANGE_WARN_SIGMA_ABOVE_THRESHOLD = const(0x01) +_VL53L4CD_RANGE_WARN_SIGMA_BELOW_THRESHOLD = const(0x02) +_VL53L4CD_RANGE_ERROR_DISTANCE_BELOW_DETECTION_THRESHOLD = const(0x03) +_VL53L4CD_RANGE_ERROR_INVALID_PHASE = const(0x04) +_VL53L4CD_RANGE_ERROR_HW_FAIL = const(0x05) +_VL53L4CD_RANGE_WARN_NO_WRAP_AROUND_CHECK = const(0x06) +_VL53L4CD_RANGE_ERROR_WRAPPED_TARGET_PHASE_MISMATCH = const(0x07) +_VL53L4CD_RANGE_ERROR_PROCESSING_FAIL = const(0x08) +_VL53L4CD_RANGE_ERROR_CROSSTALK_FAIL = const(0x09) +_VL53L4CD_RANGE_ERROR_INTERRUPT = const(0x0A) +_VL53L4CD_RANGE_ERROR_MERGED_TARGET = const(0x0B) +_VL53L4CD_RANGE_ERROR_SIGNAL_TOO_WEAK = const(0x0C) +_VL53L4CD_RANGE_ERROR_OTHER = const(0xFF) + class VL53L4CD: """Driver for the VL53L4CD distance sensor.""" @@ -201,53 +216,40 @@ def distance(self): @property def range_status(self): - """Measurement validity. Returns a number between 0 to 255 where: - 0 - None - Returned distance is valid - 1 - Warning - Sigma is above the defined threshold - 2 - Warning - Signal is below the defined threshold - 3 - Error - Measured distance is below detection threshold - 4 - Error - Phase out of valid limit - 5 - Error - Hardware fail - 6 - Warning - Phase valid but no wrap around check performed - 7 - Error - Wrapped target, phase does not match - 8 - Error - Processing fail - 9 - Error - Crosstalk signal fail - 10 - Error - Interrupt error - 11 - Error - Merged target - 12 - Error - Signal is too low - 255 - Error - Other error (for example, boot error) - """ + """Measurement validity. If the range status is equal to 0, the distance is valid.""" status_rtn = [ - 255, - 255, - 255, - 5, - 2, - 4, - 1, - 7, - 3, - 0, - 255, - 255, - 9, - 13, - 255, - 255, - 255, - 255, - 10, - 6, - 255, - 255, - 11, - 12, + _VL53L4CD_RANGE_ERROR_OTHER, + _VL53L4CD_RANGE_ERROR_OTHER, + _VL53L4CD_RANGE_ERROR_OTHER, + _VL53L4CD_RANGE_ERROR_HW_FAIL, + _VL53L4CD_RANGE_WARN_SIGMA_BELOW_THRESHOLD, + _VL53L4CD_RANGE_ERROR_INVALID_PHASE, + _VL53L4CD_RANGE_WARN_SIGMA_ABOVE_THRESHOLD, + _VL53L4CD_RANGE_ERROR_WRAPPED_TARGET_PHASE_MISMATCH, + _VL53L4CD_RANGE_ERROR_DISTANCE_BELOW_DETECTION_THRESHOLD, + _VL53L4CD_RANGE_VALID, + _VL53L4CD_RANGE_ERROR_OTHER, + _VL53L4CD_RANGE_ERROR_OTHER, + _VL53L4CD_RANGE_ERROR_CROSSTALK_FAIL, + _VL53L4CD_RANGE_ERROR_OTHER, + _VL53L4CD_RANGE_ERROR_OTHER, + _VL53L4CD_RANGE_ERROR_OTHER, + _VL53L4CD_RANGE_ERROR_OTHER, + _VL53L4CD_RANGE_ERROR_OTHER, + _VL53L4CD_RANGE_ERROR_INTERRUPT, + _VL53L4CD_RANGE_WARN_NO_WRAP_AROUND_CHECK, + _VL53L4CD_RANGE_ERROR_OTHER, + _VL53L4CD_RANGE_ERROR_OTHER, + _VL53L4CD_RANGE_ERROR_MERGED_TARGET, + _VL53L4CD_RANGE_ERROR_SIGNAL_TOO_WEAK, ] status = self._read_register(_VL53L4CD_RESULT_RANGE_STATUS, 1) status = struct.unpack(">B", status)[0] status = status & 0x1F if status < 24: status = status_rtn[status] + else: + status = _VL53L4CD_RANGE_ERROR_OTHER return status @property From 346f7face85923f0e3041738354713ccd76ff73c Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 26 Feb 2024 10:27:00 -0600 Subject: [PATCH 5/5] constant names --- adafruit_vl53l4cd.py | 78 ++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/adafruit_vl53l4cd.py b/adafruit_vl53l4cd.py index 784a41c..c5dc24a 100644 --- a/adafruit_vl53l4cd.py +++ b/adafruit_vl53l4cd.py @@ -66,20 +66,20 @@ _VL53L4CD_FIRMWARE_SYSTEM_STATUS = const(0x00E5) _VL53L4CD_IDENTIFICATION_MODEL_ID = const(0x010F) -_VL53L4CD_RANGE_VALID = const(0x00) -_VL53L4CD_RANGE_WARN_SIGMA_ABOVE_THRESHOLD = const(0x01) -_VL53L4CD_RANGE_WARN_SIGMA_BELOW_THRESHOLD = const(0x02) -_VL53L4CD_RANGE_ERROR_DISTANCE_BELOW_DETECTION_THRESHOLD = const(0x03) -_VL53L4CD_RANGE_ERROR_INVALID_PHASE = const(0x04) -_VL53L4CD_RANGE_ERROR_HW_FAIL = const(0x05) -_VL53L4CD_RANGE_WARN_NO_WRAP_AROUND_CHECK = const(0x06) -_VL53L4CD_RANGE_ERROR_WRAPPED_TARGET_PHASE_MISMATCH = const(0x07) -_VL53L4CD_RANGE_ERROR_PROCESSING_FAIL = const(0x08) -_VL53L4CD_RANGE_ERROR_CROSSTALK_FAIL = const(0x09) -_VL53L4CD_RANGE_ERROR_INTERRUPT = const(0x0A) -_VL53L4CD_RANGE_ERROR_MERGED_TARGET = const(0x0B) -_VL53L4CD_RANGE_ERROR_SIGNAL_TOO_WEAK = const(0x0C) -_VL53L4CD_RANGE_ERROR_OTHER = const(0xFF) +RANGE_VALID = const(0x00) +RANGE_WARN_SIGMA_ABOVE = const(0x01) +RANGE_WARN_SIGMA_BELOW = const(0x02) +RANGE_ERROR_DISTANCE_BELOW_DETECTION_THRESHOLD = const(0x03) +RANGE_ERROR_INVALID_PHASE = const(0x04) +RANGE_ERROR_HW_FAIL = const(0x05) +RANGE_WARN_NO_WRAP_AROUND_CHECK = const(0x06) +RANGE_ERROR_WRAPPED_TARGET_PHASE_MISMATCH = const(0x07) +RANGE_ERROR_PROCESSING_FAIL = const(0x08) +RANGE_ERROR_CROSSTALK_FAIL = const(0x09) +RANGE_ERROR_INTERRUPT = const(0x0A) +RANGE_ERROR_MERGED_TARGET = const(0x0B) +RANGE_ERROR_SIGNAL_TOO_WEAK = const(0x0C) +RANGE_ERROR_OTHER = const(0xFF) class VL53L4CD: @@ -218,30 +218,30 @@ def distance(self): def range_status(self): """Measurement validity. If the range status is equal to 0, the distance is valid.""" status_rtn = [ - _VL53L4CD_RANGE_ERROR_OTHER, - _VL53L4CD_RANGE_ERROR_OTHER, - _VL53L4CD_RANGE_ERROR_OTHER, - _VL53L4CD_RANGE_ERROR_HW_FAIL, - _VL53L4CD_RANGE_WARN_SIGMA_BELOW_THRESHOLD, - _VL53L4CD_RANGE_ERROR_INVALID_PHASE, - _VL53L4CD_RANGE_WARN_SIGMA_ABOVE_THRESHOLD, - _VL53L4CD_RANGE_ERROR_WRAPPED_TARGET_PHASE_MISMATCH, - _VL53L4CD_RANGE_ERROR_DISTANCE_BELOW_DETECTION_THRESHOLD, - _VL53L4CD_RANGE_VALID, - _VL53L4CD_RANGE_ERROR_OTHER, - _VL53L4CD_RANGE_ERROR_OTHER, - _VL53L4CD_RANGE_ERROR_CROSSTALK_FAIL, - _VL53L4CD_RANGE_ERROR_OTHER, - _VL53L4CD_RANGE_ERROR_OTHER, - _VL53L4CD_RANGE_ERROR_OTHER, - _VL53L4CD_RANGE_ERROR_OTHER, - _VL53L4CD_RANGE_ERROR_OTHER, - _VL53L4CD_RANGE_ERROR_INTERRUPT, - _VL53L4CD_RANGE_WARN_NO_WRAP_AROUND_CHECK, - _VL53L4CD_RANGE_ERROR_OTHER, - _VL53L4CD_RANGE_ERROR_OTHER, - _VL53L4CD_RANGE_ERROR_MERGED_TARGET, - _VL53L4CD_RANGE_ERROR_SIGNAL_TOO_WEAK, + RANGE_ERROR_OTHER, + RANGE_ERROR_OTHER, + RANGE_ERROR_OTHER, + RANGE_ERROR_HW_FAIL, + RANGE_WARN_SIGMA_BELOW, + RANGE_ERROR_INVALID_PHASE, + RANGE_WARN_SIGMA_ABOVE, + RANGE_ERROR_WRAPPED_TARGET_PHASE_MISMATCH, + RANGE_ERROR_DISTANCE_BELOW_DETECTION_THRESHOLD, + RANGE_VALID, + RANGE_ERROR_OTHER, + RANGE_ERROR_OTHER, + RANGE_ERROR_CROSSTALK_FAIL, + RANGE_ERROR_OTHER, + RANGE_ERROR_OTHER, + RANGE_ERROR_OTHER, + RANGE_ERROR_OTHER, + RANGE_ERROR_OTHER, + RANGE_ERROR_INTERRUPT, + RANGE_WARN_NO_WRAP_AROUND_CHECK, + RANGE_ERROR_OTHER, + RANGE_ERROR_OTHER, + RANGE_ERROR_MERGED_TARGET, + RANGE_ERROR_SIGNAL_TOO_WEAK, ] status = self._read_register(_VL53L4CD_RESULT_RANGE_STATUS, 1) status = struct.unpack(">B", status)[0] @@ -249,7 +249,7 @@ def range_status(self): if status < 24: status = status_rtn[status] else: - status = _VL53L4CD_RANGE_ERROR_OTHER + status = RANGE_ERROR_OTHER return status @property