THRESHOLD = (3, 34, -69, 115, -48, 127)
import sensor, image, time, pyb
from pyb import LED
from pid import PID
from pyb import UART
import math
from machine import I2C
from vl53l1x import VL53L1X
rho_pid = PID(p=0.4, i=0)
theta_pid = PID(p=0.001, i=0)
uart = UART(3, 115200)
i2c = I2C(2)
distance_sensor = VL53L1X(i2c)
LED(1).on()
LED(2).on()
LED(3).on()
sensor.reset()
sensor.set_vflip(True)
sensor.set_hmirror(True)
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQQVGA)
sensor.skip_frames(time = 2000)
data1 = 0x2c
data2 = 0x2b
clock = time.clock()
roi1 = [(0, 0, 25, 20),
(0, 45, 32, 50),
(50, 45, 30, 50)
]
while True:
clock.tick()
img = sensor.snapshot().binary([THRESHOLD])
ROI_L = (22, 31, 4, 5)
ROI_R = (43, 31, 6, 5)
ROI1 = (34, 12, 12, 31)
ROI2 = (27, 45, 15, 15)
ROI3 = (32, 30, 6, 12)
blobs1 = img.find_blobs([(100, 100)], roi=ROI_L, area_threshold=15, merge=True)
blobs2 = img.find_blobs([(100, 100)], roi=ROI_R, area_threshold=15, merge=True)
blobs3 = img.find_blobs([(100, 100)], roi=ROI1, area_threshold=15, merge=True)
blobs4 = img.find_blobs([(100, 100)], roi=ROI2, area_threshold=15, merge=True)
blobs5 = img.find_blobs([(100, 100)], roi=ROI3, area_threshold=15, merge=True)
img = sensor.snapshot().binary([THRESHOLD])
line = img.get_regression([(100, 100)], robust=True)
img.draw_line((25, 0, 25, 60), color=(0, 255, 0))
img.draw_line((47, 0, 47, 60), color=(0, 255, 0))
img.draw_line((0, 45, 25, 45), color=(0, 255, 0))
img.draw_line((47, 45, 80, 45), color=(0, 255, 0))
if blobs1 and blobs2:
#print(666)
output1 = int(50) + 50
output_str1 = bytearray([output1])
uart.write('@')
uart.write(bytearray(b'd'))
uart.write('\r\n')
if line:
rho_err = abs(line.rho()) - img.width() / 2
if line.theta() > 90:
theta_err = line.theta() - 180
else:
theta_err = line.theta()
img.draw_line(line.line(), color=127)
if line.magnitude() > 8:
rho_output = rho_pid.get_pid(rho_err, 1)
theta_output = theta_pid.get_pid(theta_err, 1)
output = rho_output + theta_output
output = int(output) + 50
OUTPUT = str(round(output))
outut_str = bytearray([output])
uart.write(bytes([0x2C]))
uart.write(outut_str )
uart.write(bytes([0x5B])) # 将16进制转换为字
print(outut_str )
# 读取测距模块数据
distance = distance_sensor.read()
# 对测距数据进行处理
distance = distance / 10 # 假设测距模块返回的是以毫米为单位的距离数据
distance = int(distance)
print("Distance:", distance, "mm")
outut_str2 = bytearray([distance])
uart.write(bytes([0x2C]))
uart.write(outut_str2 )
uart.write(bytes([0x5B]))
time.sleep(0.1)原因:串口3和i2c引脚冲突。
解决办法:把串口3改为串口1,使用P0P1引脚发送串口数据。