def init_dist_sensor(): global threshold try: # init distance sensor i2c = busio.I2C(board.SCL, board.SDA) dist_sensor = adafruit_vl53l0x.VL53L0X(i2c) dist_sensor.measurement_timing_budget = 200000 # define the average distance from i mesures thrs = [] i = 5 while i > 0: # blink led time.sleep(0.5) GPIO.output(LED, GPIO.HIGH) time.sleep(0.5) GPIO.output(LED, GPIO.LOW) # mesure and store distance distance = dist_sensor.range // 10 if DEBUG: logger.debug("distance = %d" % distance) if 0 < distance < 50: thrs.append(distance) i -= 1 # compute average distance if len(thrs): new_thr = sum(thrs) // len(thrs) if 0 < new_thr <= 50: threshold = new_thr if DEBUG: logger.debug("threshold = %d" % threshold) except Exception as e: if DEBUG: logger.info("Error setting dist sensor %s" % str(e)) return dist_sensor
def __init__(self): self.i2c = busio.I2C(board.SCL, board.SDA) self.sensor = adafruit_vl53l0x.VL53L0X(self.i2c) # High accuracy measurement: 200 ms timing budget self.sensor.measurement_timing_budget = 200000
def __init__(self): try: self.i2c = busio.I2C(board.SCL, board.SDA) self.vl53 = adafruit_vl53l0x.VL53L0X(self.i2c) self.alive = True except: self.alive = False print("No se puede Inicializar VL5310X")
def __init__(self, channel, tca=None, precision=3): if tca: channel = tca[channel] device = adafruit_vl53l0x.VL53L0X(channel) self.device = device self.precision = precision self.accepted_units = ["mm", "in"] self.MM_TO_INCH = 0.0393701
def main(): # Startup # VL53L0x i2c = busio.I2C(board.SCL, board.SDA) vl53 = adafruit_vl53l0x.VL53L0X(i2c) vl53.measurement_timing_budget = TIMING_BUDGET # Steppers stp_base = STP.A0591(A0591_PINS, True) stp_sensor = STP.A4988(A4988_PINS, 16) # Limit switch GPIO.setup(SWITCH_PIN, GPIO.IN) print("Moving towards limit switch") # Position distance sensor while GPIO.input(SWITCH_PIN) == 0: stp_sensor.step(-1) while GPIO.input(SWITCH_PIN) == 1: stp_sensor.step(1) # Find object height for height in range(0, 2401, SENSOR_STEPS): stp_sensor.step(SENSOR_STEPS) rng = vl53.range print("Looking for end of object... Height = {}, Range = {}".format( height, rng)) if rng > 1000: break # Start measurements (top-down) angle = 0 direction = 1 measurements = [] while height > 0 and GPIO.input(SWITCH_PIN) == 0: stp_sensor.step(-SENSOR_STEPS) height -= SENSOR_STEPS angle = 0 if direction > 0 else 360 while angle <= 360 and angle >= 0: dist = vl53.range print("Height: {}\t Angle: {}\t Dist: {}".format( height, angle, dist)) measurements.append({ 'Height': height, 'Angle': angle, 'Range': dist }) angle += BASE_ANGLE * direction stp_base.step(BASE_STEPS * direction) direction *= -1 print(measurements) save(measurements)
def get_reading(): i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_vl53l0x.VL53L0X(i2c) range_readings_arr = [] for num in range(3): range_readings_arr.append(sensor.range) time.sleep(1) return range_readings_arr
def __init__(self): print("Initializing VL53L0X sensor") ## Initialize I2C bus and sensor. i2c = busio.I2C( board.SCL, board.SDA ) #i2c: busio.I2C object at 0x7668fb90 --- class busio.I2C self.vl53 = adafruit_vl53l0x.VL53L0X( i2c ) #vl53: adafruit_vl53l0x.VL53L0X object at 0x76546f90 --- class adafruit_vl53l0x.VL53L0X self.vl53.measurement_timing_budget = 200000 print("VL53L0X sensor initializaton successfull")
def __init__(self): super(TimeOfFlight, self).__init__() # Initialize I2C bus and sensor. i2c = busio.I2C(board.SCL, board.SDA) self.vl53 = adafruit_vl53l0x.VL53L0X(i2c) self.vl53.measurement_timing_budget = 200000 self.past_measures = [] self.avg_measurement = 0
def antigo(self): self.i2c = busio.I2C(board.SCL, board.SDA) xshut = [ DigitalInOut(board.D4), DigitalInOut(board.D17),DigitalInOut(board.D27)] for power_pin in xshut: power_pin.switch_to_output(value=False) vl53 = [] for i, power_pin in enumerate(xshut): power_pin.value = True vl53.insert(i, adafruit_vl53l0x.VL53L0X(self.i2c))
def initVL53(): global vl53l0x import board import busio import adafruit_vl53l0x try: i2c = busio.I2C(board.SCL, board.SDA) vl53l0x = adafruit_vl53l0x.VL53L0X(i2c) except: vl53l0x = None
def __init__(self, sensor): # Initialize I2C bus and sensor. self.i2c = busio.I2C(board.SCL, board.SDA) if sensor == 'vl53': self.sensor = adafruit_vl53l0x.VL53L0X(self.i2c) elif sensor == 'vl61': self.sensor = adafruit_vl6180x.VL6180X(self.i2c) if self.sensor: # set timing budget in milliseconds self.sensor.measurement_timing_budget = 10000
class Sensor(I2cDevice): servo_pos = 90 servo_delta = 5 i2c = busio.I2C(board.SCL, board.SDA) vl53 = adafruit_vl53l0x.VL53L0X(i2c) def __init__(self, address): self.address = address def set_delta(self, delta): self.servo_delta = delta self.write(0) self.write(delta) def range(self): return self.servo_pos, self.vl53.range def turn_right(self): if self.servo_pos + self.servo_delta <= 180: self.servo_pos += self.servo_delta self.write(5) def turn_left(self): if self.servo_pos - self.servo_delta >= 0: self.servo_pos -= self.servo_delta self.write(6) def scan(self, delta=2): if 180 % delta != 0: delta = 2 distances = [] self.set_delta(90) self.turn_left() self.set_delta(delta) for i in range(int(180 / delta)): distances.append(self.range()) self.turn_right() self.set_delta(90) self.turn_left() self.set_delta(self.servo_delta) return distances
def __init__(self): # self.camera = PiCamera() # self.camera.rotation = 180 # i2c = busio.I2C(board.SCL, board.SDA) try: self.vl53l0x = adafruit_vl53l0x.VL53L0X(i2c, address=28) except: input('Unplug VL6180 (top Time of Flight sensor)') # self.change_addr(i2c) self.vl53l0x = self.change_addr(i2c) input('Plug it back in and press enter to continue. ') try: self.vl6180X = adafruit_vl6180x.VL6180X(i2c) except: input('Plug in the VL6180x Sensor. Press enter to continue.') self.vl6180X = adafruit_vl6180x.VL6180X(i2c) time.sleep(0.25)
class robot: i2c = busio.I2C(board.SCL, board.SDA) vl53 = adafruit_vl53l0x.VL53L0X(i2c) ser = serial.Serial('/dev/ttyS0', 38400, timeout=1) tof = 0 mL = [0, 0] #direction, speed mR = [0, 0] #direction, speed ir = [0, 0, 0, 0] #top left, top right, bottom left, bottom right bttn = [0, 0, 0, 0] #front , right, left, back imu = 0 #don't know how this works yet sensorData = 0 #keeps data that we can parse through def tof(self): self.tof = vl53.range return self.tof
import board import busio import adafruit_vl53l0x import time i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_vl53l0x.VL53L0X(i2c) sensor.measurement_timing_budget = 200000 while True: measurement = sensor.range # mm to cm measurement = measurement / 10 level = 13.3 - measurement level = level + 4.85 print("Distance from bottom: {:.1f}cm".format(level)) volume = 7 * 12.5 * (level) weights = [0, 263, 539, 846, 1010] volumes = [0, 320, 740, 1070, 1281] print("Volume: {:.1f}ml".format(volume)) for i in range(3):
import adafruit_lis3dh import adafruit_thermistor #initialize the thermometer thermometer = adafruit_thermistor.Thermistor( board.TEMPERATURE, 10000, 10000, 25, 3950 ) #initialize the light sensor light_sensor = analogio.AnalogIn(board.LIGHT) #initialize the angle sensor angle_sensor = analogio.AnalogIn(board.A1) #initialize the rangefinder i2c_bus_offboard = busio.I2C( board.SCL, board.SDA ) rangefinder = adafruit_vl53l0x.VL53L0X( i2c_bus_offboard ) #initialize the accelerometer i2c_bus_accel = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA) interrupt_1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT) accelerometer = adafruit_lis3dh.LIS3DH_I2C(i2c_bus_accel, address=0x19, int1=interrupt_1) # Set range of accelerometer (can be RANGE_2_G, RANGE_4_G, RANGE_8_G or RANGE_16_G). accelerometer.range = adafruit_lis3dh.RANGE_2_G while True: if True: angle_normalized = round( angle_sensor.value/ 65536, 3) print( "Angle = {}".format( angle_normalized )) if True: temp_c = thermometer.temperature print( "Temperature = {} C".format( temp_c ))
def sensors(): # Temperature Sensor 1 # VCC - RED - 3.5v # GND - BLK - Ground # SDA - YLW - SDA # SCL - BRN - SCL sensor1 = TMP006.TMP006() sensor1 = TMP006.TMP006(address=0x40, busnum=1) # Default i2C address is 0x40 and bus is 1. sensor1.begin() # Accelerometer Sensor # 3v3 - RED - 3.5v # GND - BLK - Ground # SDA - YLW - SDA # SCL - BRN - SCL accelerometer = adafruit_adxl34x.ADXL345(i2c) # Distance Sensor # VIN - RED - 3.5v # GND - BLK - Ground # SDA - YLW - SDA # SCL - BRN - SCL vl53 = adafruit_vl53l0x.VL53L0X(i2c) while True: # Distance Sensor global distance distance = vl53.range print ("Range: {0}mm".format(distance)) #ser.write (b'Range: %d '%(distance)+b' mm \n') time.sleep(.1) # Temperature Sensor 1 global obj1 global die1 obj1 = sensor1.readObjTempC() die1 = sensor1.readDieTempC() # Temperature Sensor 1 Serial out #ser.write (b'Sensor 1 object Temperature: %d \n'%(obj1)) #ser.write (b'Sensor 1 die Temperature: %d \n'%(die1)) print ('Object temperature: {0:0.3F}*C / {1:0.3F}*F'.format(obj1, TempConversion(obj1))) print ('Die temperature: {0:0.3F}*C / {1:0.3F}*F'.format(die1, TempConversion(die1))) time.sleep(.1) # Accelerometer tupple parse global xAxis global yAxis global zAxis xAxis = (round(accelerometer.acceleration[0],1)) yAxis = (round(accelerometer.acceleration[1],1)) zAxis = (round(accelerometer.acceleration[2],1)) ''' # Accelerometer Serial out ser.write (b'X Axis: %d \n'%(xAxis)) ser.write (b'Y Axis: %d \n'%(yAxis)) ser.write (b'Z Axis: %d \n'%(zAxis)) ''' print ('X Axis: %d \n'%(xAxis)) print ('Y Axis: %d \n'%(yAxis)) print ('Z Axis: %d \n'%(zAxis)) #Write all data write_sensors() time.sleep(1)
def dist_create_input(): i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_vl53l0x.VL53L0X(i2c) return sensor
xshut_right = DigitalInOut(board.D3) xshut_right.direction = Direction.OUTPUT pixels = DigitalInOut(board.D4) pixels.direction = Direction.OUTPUT # Reset all the sensors xshut_left.value = False xshut_right.value = False time.sleep(.01) xshut_left.value = True xshut_right.value = True time.sleep(.01) # Initialize the sensors one at a time so we can assign them different addresses xshut_right.value = False sensor_left = adafruit_vl53l0x.VL53L0X(i2c, I2C_ADDRESS_LEFT, TIMEOUT) xshut_right.value = True sensor_right = adafruit_vl53l0x.VL53L0X(i2c, I2C_ADDRESS_RIGHT, TIMEOUT) # change read time sensor_left.measurement_timing_budget = READ_TIME sensor_right.measurement_timing_budget = READ_TIME Curtains = LED_curtains(neopixels_pin=pixels, led_number=LED_NUMBER, curtain_start=CURTAIN_START, curtain_reverse=CURTAIN_REVERSE, curtain_sensors=[sensor_right, sensor_left], curtain_offsets=CURTAIN_OFFSETS, mirror_relative_width=MIRROR_RELATIVE_WIDTH, mirror_offset=MIRROR_OFFSET,
# Simple demo of the VL53L0X distance sensor. # Will print the sensed range/distance every second. import time import board import busio import adafruit_vl53l0x # Initialize I2C bus and sensor. i2c = busio.I2C(board.SCL, board.SDA) vl53 = adafruit_vl53l0x.VL53L0X(i2c, 40) # 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. # Main loop will read the range and print it every second. while True: print('Range: {0}mm'.format(vl53.range)) time.sleep(1.0)
def __init__(self, name: str = "time of flight VL53L0X adafruit"): super().__init__(name) i2c = busio.I2C(board.SCL, board.SDA) self.vl53 = adafruit_vl53l0x.VL53L0X(i2c) self.vl53.measurement_timing_budget = 40000
import microcontroller # for checking CPU temperature import gc # for checking memory capacity from simpleio import map_range trellis = adafruit_trellism4.TrellisM4Express() # Initialize external and internal I2C bus, sensor, and display acc_i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA) # for accelerometer ext_i2c = busio.I2C(board.SCL, board.SDA) # for DACs # Initialize accelerometer and DACs accelerometer = adafruit_adxl34x.ADXL345(acc_i2c) pitch_dac = adafruit_mcp4725.MCP4725(ext_i2c, address=0x62) gate_dac = adafruit_mcp4725.MCP4725(ext_i2c, address=0x63) vl53 = adafruit_vl53l0x.VL53L0X(ext_i2c) display = segments.Seg14x4(ext_i2c) gate_dac.value = 0 pitch_dac.value = 0 #vl53.measurement_timing_budget = 33000 # default: compromise #vl53.measurement_timing_budget = 20000 # fast and inaccurate #vl53.measurement_timing_budget = 200000 # slow and accurate # ### Setup ### # ### Dictionaries and Lists ### # ### Helpers ###
# # ST VL53L0X Time-of-Flight [ToF] ranging sensor # import board import digitalio import busio import time import adafruit_vl53l0x # Ensure automatic deinit with digitalio.DigitalInOut(board.SENSOR_POWER_ENABLE) as en: en.direction = digitalio.Direction.OUTPUT en.value = True time.sleep(0.1) # Important: sleep to let power stabilize with busio.I2C(board.SCL, board.SDA) as i2c: vl53 = adafruit_vl53l0x.VL53L0X(i2c, address=0x29) while True: print("Range: {0}mm".format(vl53.range)) time.sleep(2)
import cv2 import numpy from enum import Enum from operator import itemgetter import board import busio import adafruit_lsm303 import adafruit_l3gd20 from kkos.afmotorshield import * import math import adafruit_vl53l0x from threading import Thread TOF_SENSOR_ADDRRESSE = 0x29 i2c_bus = busio.I2C(board.SCL, board.SDA) tof_sensor = adafruit_vl53l0x.VL53L0X(i2c_bus, address=TOF_SENSOR_ADDRRESSE) tof_sensor.measurement_timing_budget = 20000 PIXEL_BREITE = 320 PIXEL_HOEHE = 240 GRUEN = (0, 255, 0) GELB = (0, 255, 255) ROT = (0, 0, 255) BLAU = (255, 0, 0) helligkeits_schwelle = 50 gruen_untere_schwelle = numpy.uint8([5, 80, 20]) gruen_obere_schwelle = numpy.uint8([89, 255, 200]) SLICE_HOEHE = 30
short_wait = 0.10 medium_wait = 0.20 #Both range sensors default to same i2c address #Set all shutdown pins low to turn off each Sensor #After sensors are OFF, we can power one on and change address GPIO.output(sensor1_shutdown, GPIO.LOW) GPIO.output(sensor2_shutdown, GPIO.LOW) time.sleep(short_wait) #Turn range sensor 1 on by setting shutdown pin to HIGH GPIO.output(sensor1_shutdown, GPIO.HIGH) time.sleep(short_wait) #Initialize Sensor 1 and change address vl53 = adafruit_vl53l0x.VL53L0X(i2c) vl53.set_address(0x30) #Turn range sensor 2 on, initialize, and change address GPIO.output(sensor2_shutdown, GPIO.HIGH) time.sleep(short_wait) #vl6180 = adafruit_vl6180x.VL6180X(i2c,address = 0x29) vl6180 = adafruit_vl53l0x.VL53L0X(i2c) #Initialize accelerometers #accel_1 address is changed from default via a 3.3v line to the sensor accel_1 = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19) accel_2 = adafruit_lis3dh.LIS3DH_I2C(i2c) #Set accelerometer scale accel_1_range = adafruit_lis3dh.RANGE_8_G
f.write(lcd.convert(16, 0).get_buffer()) # We can then close our access to the framebuffer f.close() #time.sleep(0.1) # Now we've got a function that can get the bytes from a pygame surface to the TFT framebuffer, # we can use the usual pygame primitives to draw on our surface before calling the refresh function. # Here we just blink the screen background in a few colors with the "Hello World!" text pygame.font.init() defaultFont = pygame.font.SysFont(None, 30) #initialize the sensor sensor = adafruit_amg88xx.AMG88XX(i2c_bus) laserRange = adafruit_vl53l0x.VL53L0X(i2c_bus) # pylint: disable=invalid-slice-index points = [(math.floor(ix / 8), (ix % 8)) for ix in range(0, 64)] grid_x, grid_y = np.mgrid[0:7:32j, 0:7:32j] # pylint: enable=invalid-slice-index #sensor is an 8x8 grid so lets do a square height = 240 width = 240 #the list of colors we can choose from blue = Color("indigo") colors = list(blue.range_to(Color("red"), COLORDEPTH)) #create the array of colors
def __init__(self): print("Initializing VL53L0X sensor") i2c = busio.I2C(board.SCL, board.SDA) self.vl53 = adafruit_vl53l0x.VL53L0X(i2c) print("VL53L0X sensor initialization successfull")
def __init__(self): #self.address= address # Initialize I2C bus and sensor. i2c = busio.I2C(board.SCL, board.SDA) self.vl53 = adafruit_vl53l0x.VL53L0X(i2c)
import adafruit_vl53l0x from time import sleep import board import busio i2c = busio.I2C(board.SCL, board.SDA) sleep(3) vl53l0x = adafruit_vl53l0x.VL53L0X(i2c) sleep(2) vl53l0x.set_address(28) print('I think everything worked')
def getrange(): i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_vl53l0x.VL53L0X(i2c) return sensor.range