Skip to content

Instantly share code, notes, and snippets.

@rokroskar
Created February 28, 2024 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rokroskar/63a734125fff2592fe3a56195fb7cabe to your computer and use it in GitHub Desktop.
Save rokroskar/63a734125fff2592fe3a56195fb7cabe to your computer and use it in GitHub Desktop.
ToF 8x8 sensor data over USB
#
# This code goes on the Pico. See https://learn.pimoroni.com/article/micropython-and-vl53l5cx for
# information on how to get the VL53LCX sensor set up.
#
import micropython
import sys
import ustruct
import breakout_vl53l5cx
import pimoroni_i2c
from random import randint
import time
PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
i2c = pimoroni_i2c.PimoroniI2C(**PINS_BREAKOUT_GARDEN, baudrate=2_000_000)
sensor = breakout_vl53l5cx.VL53L5CX(i2c)
# Make sure to set resolution and other settings *before* you start ranging
sensor.set_resolution(breakout_vl53l5cx.RESOLUTION_8X8)
sensor.set_ranging_frequency_hz(10)
sensor.enable_motion_indicator(breakout_vl53l5cx.RESOLUTION_8X8)
sensor.set_motion_distance(400, 1400)
sensor.start_ranging()
fmt = 'i'*64
rlen = ustruct.calcsize(fmt)
buf = bytearray(rlen)
while True:
if sensor.data_ready():
data = sensor.get_data()
# ustruct.pack_into(fmt,buf, 0, *data.motion_indicator.motion)
# sys.stdout.buffer.write(buf)
break
time.sleep(.001)
#
# This runs on the host machine
#
import serial
import struct
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fmt = 'i'*64
rlen = struct.calcsize(fmt)
fig, ax = plt.subplots()
array_data = np.zeros((8, 8))
img = ax.imshow(array_data, cmap='viridis', vmin=0, vmax=4000)
s = serial.Serial(port="/dev/tty.usbmodem1101", baudrate=115200, parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE)
s.flush()
def stream_data():
while True:
mes = s.read(rlen)
res = struct.unpack_from(fmt,mes)
new_data = np.reshape(np.array(res), [8,8])[::-1]
yield new_data
# Function to update the plot with new array data
def update(new_data):
# Update the image data
img.set_array(new_data)
# Set up the animation
ani = FuncAnimation(fig, update, frames=stream_data, interval=60, blit=False)
# Show the plot
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment