主要是说缓冲区太小的问题,目前找不到解决办法,使用的是例程视频里面的循迹小车部分,就是主函数里面多加了一个串口发送的,发送出去输出的output的有符号的值,每当一遇到跳变很大的或者如遇到图片情况就会报错:
Traceback (most recent call last):
File "<stdin>", line 39, in <module>:第39行是sending_data(int(output))
File "<stdin>", line 9, in sending_data:第9行是byte_value = data1.to_bytes(1, 'big', True)
OverflowError: buffer too small
OpenMV v4.6.20; MicroPython v1.24.62; OPENMV4P with STM32H743
Type "help()" for more information.
main.py
THRESHOLD = (1, 28, -21, 5, -20, 35)
import sensor,image,time,gc
from pyb import LED
from pid import PID
from pyb import UART
uart = UART(3,115200,bits=8, parity=None, stop=1, timeout_char=2000, read_buf_len=1024)
def sending_data(data1):
global uart
byte_value = data1.to_bytes(1, 'big', True)
data = bytearray([0x23, byte_value[0], 0x40])
uart.write(data);
rho_pid = PID(p=0.6, i=0, d=0.15)
theta_pid = PID(p=0.003, i=0, d=0.002)
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)
clock = time.clock()
while(True):
clock.tick()
img = sensor.snapshot().binary([THRESHOLD])
line = img.get_regression([(100,100)], robust = True)
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
sending_data(int(output))
print(int(output))
pass
gc.collect()pid.py
THRESHOLD = (1, 28, -21, 5, -20, 35)
import sensor,image,time,gc
from pyb import LED
from pid import PID
from pyb import UART
uart = UART(3,115200,bits=8, parity=None, stop=1, timeout_char=2000, read_buf_len=1024)
def sending_data(data1):
global uart
data = bytearray([0x23, data1, 0x40])
uart.write(data);
rho_pid = PID(p=0.6, i=0, d=0.15)
theta_pid = PID(p=0.003, i=0, d=0.002)
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)
clock = time.clock()
while(True):
clock.tick()
img = sensor.snapshot().binary([THRESHOLD])
line = img.get_regression([(100,100)], robust = True)
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
sending_data(int(output))
print(int(output))
pass
gc.collect()