logo

使用测距模块频繁在运行中出现错误,整体代码没有错误能够运行,只在运行过程中报错,引脚接触良好,供电足够,报错信息如下

2023-03-18 16:05
P
dnl1
Traceback (most recent call last):
  File "<stdin>", line 42, in test1
  File "vl53l1x.py", line 128, in read
OSError: [Errno 19] ENODEV

初始化代码如下:

THRESHOLD = (19, 56, 19, 127, 4, 127)
import sensor, image, time,pyb
import IR
from machine import I2C
from vl53l1x import VL53L1X
from pyb import UART
from pyb import LED
from pid import PID
from pyb import Pin
import car
import _thread

rho_pid = PID(p=1.1, i=0) #偏移距离PID
theta_pid = PID(p=0.8, i=0)#偏移角度PID

i2c = I2C(2)
distance = VL53L1X(i2c)

uart = UART(1,115200)
uart.init(115200, bits=8, parity=None, stop=1)

L =0
Information = [00,00,00,00,00] #[模式,速度,红外运动类型,遇障停止信号]
tem =0

#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.QQVGA) # 80x60 (4,800 pixels) - O(N^2) max = 2,3040,000.
#sensor.set_windowing([0,20,80,40])
sensor.skip_frames(time = 2000)     #等待2000ms让摄像头启动
clock = time.clock()                #创建一个时钟来追踪FPS(每秒拍摄帧数)

#线程1
def test1(*args, **kwargs):
    while True: 
        pyb.delay(70) # 延时 50 毫秒
        L = distance.read() #测距传感器
        if(L<150):
            Information[4]=0
            print("mm:",L)
        else:
            Information[4]=1


thread_1 = _thread.start_new_thread(test1, (1,)) #创建线程
2023-03-22 18:05
I
网红小智智 星瞳科技

不要用线程_thread。这个库并不是线程安全,没加锁。而且用thread没啥意义。