Thermal camera (thermo AI device TiD) Python VL53L0X edition

The distance data of the Pololu Time-of-Flight distance sensor module VL53L0X is acquired by I2C communication.


  1. Introduction
  2. Sensor
  3. Sensor case
  4. Raspberry Pi
  5. Python 5.1 Form 5.2 OMRON Non-contact Temperature Sensor D6T-44L-06 Edition 5.3 Pololu ranging sensor VL53L0X edition 5.4 BOSCH Temperature / Humidity / Barometric Pressure Sensor BME280 5.5 Shutdown / Restart Switch 5.6 OpenCV 5.7 Speedup

vl53l0x.jpg Use the adafruit-circuitpython-vl53l0x module to get data from VL53L0X in Python on Raspberry Pi. If the adafruit-circuitpython-vl53l0x module is not installed, install it with the following command.

sudo pip3 install adafruit-circuitpython-vl53l0x

vl53l0x.py is imported and used as a package, but you can get the distance data by itself for testing. vl53l0x.py (compressed with ZIP)

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
MH VL53L0X script.
==================
"""
import threading
import time

i2c_enable = False
try:
    import adafruit_vl53l0x
    import board
    import busio
    i2c_enable = True
except:
    i2c_enable = False
import numpy as np
import random


class VL53L0X():
    def __init__(self, *args, **kwargs):
        """
        mesure length: 30 - 2000[mm]
        Optionally adjust the measurement timing budget to change speed and accuracy.
        See the example here for more details:
          https://github.com/pololu/vl53l0x-arduino/blob/master/examples/Single/Single.ino
        For example a higher speed but less accurate timing budget of 20ms:
        vl53.measurement_timing_budget = 20000
            Or a slower but more accurate timing budget of 200ms:
        vl53.measurement_timing_budget = 200000
        The default timing budget is 33ms, a good compromise of speed and accuracy.
        ***
        avg: Average counts.
        range: Default is (30, 2000).
        in_range: If distance is inth range then True.
        """
        self.i2c_enable = i2c_enable

        self._distance = 0
        self._history = []
        self._shutdowning = False
        self._in_range = False

        self._avg = 1
        self.avg = kwargs.get('avg', self._avg)

        self._range = (30, 2000)
        self.range = kwargs.get('range', self._range)

        self.vl53 = None
        if self.i2c_enable:
            i2c = busio.I2C(board.SCL, board.SDA)
            try:
                self.vl53 = adafruit_vl53l0x.VL53L0X(i2c)
            except:
                self.i2c_enable = False

        self._processing()

    def __call__(self, *args, **kwargs):
        return self.distance

    @property
    def avg(self):
        return self._avg
    @avg.setter
    def avg(self, value):
        self._avg = value
        self._history = [0]
        for _ in range(self.avg - 1):
            self._history.append(0)

    def __del__(self):
        self._shutdowning = True

    @property
    def distance(self):
        return self._distance

    @property
    def in_range(self):
        return self._in_range

    def _processing(self):
        if self._shutdowning:
            return

        tmp = 0
        if self.i2c_enable:
            # For I2C error at pushed power switch.
            try:
                tmp = self.vl53.range
            except:
                pass
        else:
            # This value is for D6T-44L-06.
            tmp = random.randint(500, 550)

        if tmp <= 2000:
            self._history[1: self.avg] = self._history[0: self.avg - 1]
            self._history[0] = tmp

        self._distance = np.average(self._history)

        if (self._distance >= self._range[0]) \
                & (self._distance <= self._range[1]):
            self._in_range = True
        else:
            self._in_range = False

        time.sleep(0.1)

        thread = threading.Thread(
            name='VL53L0X_Measuring',
            target=self._processing,
        )
        thread.daeom = True
        thread.start()

    @property
    def range(self):
        return self._range
    @range.setter
    def range(self, value):
        self._range = value

    def stop(self):
        self._shutdowning = True


if __name__ == '__main__':
    vl = VL53L0X(avg=10)
    while True:
        print(f'Range: {vl()}[mm] {vl.in_range}')
        time.sleep(0.1)
    pass

YouTube: Thermal Camera (Thermo AI Device TiD) Python Edition web: Thermo AI device TiD Python D6T (URL is subject to change.)

Recommended Posts

Thermal camera (thermo AI device TiD) Python VL53L0X edition
Thermal Camera (Thermo AI Device TiD) Python OpenCV Edition
Thermal Camera (Thermo AI Device TiD) Python D6T-44L-06 Edition
Thermal Camera (Thermo AI Device TiD) Python Form
Thermal Camera (Thermo AI Device TiD) Python Acceleration
Thermal camera (Thermo AI device TiD) Sensor edition
Thermal camera (Thermo AI device TiD) Python BME280
Thermal Camera (Thermo AI Device TiD) Raspberry Pi Edition
Thermal Camera (Thermo AI Device TiD) Python Form
Thermal Camera (Thermo AI Device TiD) Python Acceleration
Thermal camera (Thermo AI device TiD) Sensor edition
Thermal camera (Thermo AI device TiD) Python BME280
Thermal Camera (Thermo AI Device TiD) Raspberry Pi Edition
Thermal Camera (Thermo AI Device TiD) Python OpenCV Edition
Thermal camera (thermo AI device TiD) Python VL53L0X edition
Thermal Camera (Thermo AI Device TiD) Python D6T-44L-06 Edition
Raspberry Pi Security Infrared Camera (Python Edition)