Radar-like distance scanner on ESP32-S3/CircuitPython 9, using SG90 servo motor, VL53L0X distance sensor and SSD1306 OLED.
With previous exercises of
SSD1306 I2C OLED, VL53L0X distance sensor
and
SG90 servo motor
run on
ESP32-S3-Zero
with
CircuitPython 9.0.0-beta.0 installed. The following exercise implement a Radar-like distance scanner.
cpy_C3-Zero_ssd1306_fb_servo_scanner.py
"""
CircuitPython 9.0.0-beta.0 exercise run on Waveshare ESP32-S3-Zero:
- drive servo moto SG90 using adafruit_motor
- detect distance using using adafruit_vl53l0x
- display on SSD1306 I2C OLED using adafruit_ssd1306
lib needed:
- adafruit_framebuf.mpy
- adafruit_simplemath.mpy
- adafruit_ssd1306.mpy
- adafruit_vl53l0x.mpy
- adafruit_motor folder
"""
import os
import sys
import board
import busio
import time
import math
import adafruit_ssd1306
import pwmio
from adafruit_motor import servo
import adafruit_vl53l0x
from adafruit_simplemath import map_range
# create a PWMOut object on Pin A2.
pwm = pwmio.PWMOut(board.IO7, duty_cycle=2 ** 15, frequency=50)
# Create a servo object, my_servo.
#my_servo = servo.Servo(pwm, min_pulse=500, max_pulse=2500)
my_servo = servo.Servo(pwm, min_pulse=400, max_pulse=2400)
# Create the I2C interface and display object of SSD1306_I2C.
ssd1306_i2c_addr = 0x3C
DISP_WIDTH = 128
DISP_HEIGHT = 64
SCL = board.IO1
SDA = board.IO2
i2c = busio.I2C(SCL, SDA)
VL53L0X_i2c_addr = 0x29
vl53l0x_sensor = adafruit_vl53l0x.VL53L0X(i2c)
print(adafruit_vl53l0x)
# display = adafruit_ssd1306.SSD1306_I2C(DISP_WIDTH, DISP_HEIGHT, i2c)
# Or pass with an addr parameter:
display = adafruit_ssd1306.SSD1306_I2C(DISP_WIDTH, DISP_HEIGHT, i2c, addr=ssd1306_i2c_addr)
display.rotation = 0
print("=================================================")
info = sys.implementation[0] + ' ' + os.uname()[3] + '\n' + \
'run on ' + os.uname()[4]
print(info)
print("=================================================")
print(servo.__name__, servo.__version__)
print(adafruit_vl53l0x.__name__, adafruit_vl53l0x.__version__)
print(adafruit_ssd1306.__name__, adafruit_ssd1306.__version__)
print("SCL: ", SCL)
print("SDA: ", SDA)
print("display.width x height: ",
display.width, " x ", display.height)
display.fill(0)
display.show()
time.sleep(1)
display.fill(1)
display.show()
time.sleep(1)
display.fill(0)
#draw rectangle
display.fill(0)
display.show()
CX = int(display.width/2)-1
CY = 0
R = 60
def project(r, degree):
rev_degree = 180 - degree
project_x = int(r * math.cos(math.radians(rev_degree))) + CX
project_y = int(r * math.sin(math.radians(rev_degree)))
return project_x, project_y
def convAngleToBar(ang):
bar = ""
for a in range(int(ang/5)):
bar = bar + "="
for a in range(int((180-ang)/5)):
bar = bar + " "
return bar
def scan_and_show(ang):
my_servo.angle = ang
print("{:03}".format(ang), convAngleToBar(ang), end="\r")
time.sleep(interval)
distance = vl53l0x_sensor.range
dist_length = int(map_range(distance, 0, 500, 0, R))
x, y = project(dist_length, ang)
display.line(CX, CY, x, y, 1)
display.show()
interval = 0.1
while True:
for angle in range(0, 180, 5): # 0 - 180 degrees, 5 degrees at a time.
scan_and_show(angle)
time.sleep(interval)
display.fill(0)
display.show()
for angle in range(180, 0, -5): # 180 - 0 degrees, 5 degrees at a time.
scan_and_show(angle)
time.sleep(interval)
display.fill(0)
display.show()
Comments
Post a Comment