用户在更新OpenMV固件库后,原本可正常运行的代码出现故障,无法执行。以下是详细信息汇总:
import sensor, image,time,lcd,math,re
from pyb import UART,Timer,LED,Pin
thresholds = [(52, 93, 12, 70, -5, 36),
(83, 100, -30, 11, 13, 99),
(60, 92, -66, -17, -2, 51)]
blue_threshold =[(12, 49, -17, 9, -42, -13)]
yellow_threshold =[(62, 100, -31, -12, 28, 81)]
red_threshold =[(11, 38, 8, 55, -7, 51)]
sbtx = []
def servo_control_2B():
pin = Pin('P9')
timer = Timer(4, freq=50)
pwm = timer.channel(3, Timer.PWM, pin=pin, pulse_width_percent=10)
time.sleep(1)
pwm.pulse_width_percent(10)
def servo_control_2E():
pin = Pin('P9')
timer = Timer(4, freq=50)
pwm = timer.channel(3, Timer.PWM, pin=pin, pulse_width_percent=10.5)
time.sleep(1)
pwm.pulse_width_percent(10.5)
def servo_control_2D():
pin = Pin('P9')
timer = Timer(4, freq=50)
pwm = timer.channel(3, Timer.PWM, pin=pin, pulse_width_percent=11)
time.sleep(1)
pwm.pulse_width_percent(11)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames()
sensor.set_auto_whitebal(False)
sensor.set_auto_gain(False)
clock = time.clock()
lcd.init()
uart = UART(3,115200,8,None,1)
data = []
LED_Red = LED(1)
LED_Green = LED(2)
LED_Blue = LED(3)
ROI = (6,6,290,210)
tim = Timer(4,freq=1)
tim.deinit()
FlagOK = 0
show_numTab = ["0","1","2","3","4","5","6","7","8","9"]
num = 0
returnData = [0x55,0x02,0x92,0x02,0x02,0x00,0x00,0xBB]
runData = [0x55,0x02,0x92,0x03,0x02,0x00,0x00,0xBB]
def tick(timer):
global FlagOK,num,returnData
num = num-1
if(num == 0):
num = 9
FlagOK = 2
tim.deinit()
def USART_Send(src,length):
for i in range(length):
uart.writechar(src[i])
def Color_Check(srcbuf):
global FlagOK,num,c
if(FlagOK == 1):
for code in img.find_qrcodes():
FlagOK = 0
tim.deinit()
print(code)
qr_Tab = code.payload()
uart.writechar(0x55)
uart.writechar(0x02)
uart.writechar(0x92)
uart.writechar(0x01)
qrdata = []
match = re.search(r'\*\*(.*?)\#\#', qr_Tab)
if match:
content = match.group(1)
for c in content:
if ('A' <= c <= 'Z') or ('a' <= c <= 'z') or ('0' <= c <= '9'):
qrdata.append(c)
print(qrdata)
uart.writechar(len(qrdata)) # 数据长度
for b in qrdata:
uart.writechar(ord(b)) # 发送字符的 ASCII 码值
print(type(b))
uart.writechar(0xBB)
break # 跳出二维码识别循环
# for c in qr_Tab:
# if c >= 'A' and c <= 'Z' or c >= 'a' and c <= 'z' or c >= '0' and c <= '9':
# qrdata.append(c) #append()是在列表末尾添加新的对象,且将添加的对象最为一个整体
# print(qrdata)
# uart.writechar(len(qrdata)) #数据长度
# for b in qrdata:
# uart.writechar(ord(b)) #ord()是填入字符参数返回相对应的十六进制数
# print(type(b))
# uart.writechar(0xBB)
# break
if(FlagOK == 2): #发送识别失败指令
for rdata in returnData: #[0x55,0x02,0x92,0x02,0x02,0x00,0x00,0xBB]
uart.writechar(rdata) #串口输出识别失败
FlagOK = 0
while(True):
c = []
img = sensor.snapshot()
if(uart.any()):
data = uart.read(8)
if( len(data) >= 8):
if((data[0] == 0x55)&(data[1] == 0x02)&(data[7] == 0xBB)):
if(data[2] == 0x92):
if(data[3] !=0x05):
print("计算次幂二维码内容")
for jshs in img.find_qrcodes():
print(jshs)
hs = jshs.payload()
uart.writechar(0x55)
uart.writechar(0x02)
uart.writechar(0x92)
uart.writechar(0x01)
n = str(data[3])
y = str(data[4])
r = str(data[5])
n=chr(int(n))
y=chr(int(y))
r=chr(int(r))
hc = hs.replace('n',n)
ha = hc.replace('y',y)
hx = ha.replace('r',r)
he = hx.replace('^','**')
he=eval(he)
he = int(he)
he=str(he)
print(he)
uart.writechar(0x01)
for s in he:
uart.writechar(ord(s))
print(he)
uart.writechar(0xBB)
break
if(data[2] == 0x91):
if(data[3] == 0x03):
if(data[4] == 0x2B):
print("舵机上升")
servo_control_2B()
if(data[4] == 0x2E):
print("舵机中间")
if(data[4] == 0x2D):
print("舵机下降")
servo_control_2D()
if(data[4] == 0x2C):
print("舵机复位")
if(data[2] == 0x91):
if(data[4] == 0x91):
print("识别交通灯")
for blob in img.find_blobs(thresholds,x_stride=10,y_stride=10, pixels_threshold=100, area_threshold=100):
ys = blob.code()
uart.writechar(0x55)
uart.writechar(0x02)
uart.writechar(0x99)
uart.writechar(0x98)
uart.writechar(ys)
uart.writechar(0xBB)
img.draw_rectangle(blob.rect())
img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
print(ys)
if (data[4] == 0x92):
print("识别图像")
for i in range(30):
img = sensor.snapshot().lens_corr(1.8)
if img.find_blobs(blue_threshold ,x_stride=5,y_stride=5, pixels_threshold=10, area_threshold=10):
c = img.find_circles(threshold = 4000,x_margin = 30, y_margin = 30, r_margin = 30,r_min = 1, r_max = 100, r_step = 1)
sbtx.append(len(c))
print(sbtx)
uart.writechar(0x55)
uart.writechar(0x02)
uart.writechar(0x92)
uart.writechar(0x01)
uart.writechar(0x01)
if sbtx:
print(max(set(sbtx),key=sbtx.count))
uart.writechar(max(set(sbtx),key=sbtx.count))
else:
uart.writechar(0x01)
uart.writechar(0xBB)
if (data[4] == 0x93):
print("计算二维码车库目标层数")
for jshs in img.find_qrcodes():
print(jshs)
hs=jshs.payload()
uart.writechar(0x55)
uart.writechar(0x02)
uart.writechar(0x92)
uart.writechar(0x01)
n = str(data[3])
y = str(data[4])
r = str(data[5])
n=chr(int(n))
y=chr(int(y))
r=chr(int(r))
hc = hs.replace('n',n)
ha = hc.replace('y',y)
hx = ha.replace('r',r)
hx=eval(hx)
hx=int(hx)
hx=str(hx)
uart.writechar(0x01)
for s in hx:
uart.writechar(ord(s))
print(hx)
uart.writechar(0xBB)
if(data[2] == 0x92):
if(data[3] == 0x05):
print("识别二维码")
if(FlagOK == 0):
FlagOK = 1
num = 9
print("开始识别")
tim.callback(tick)
else:
print("正在识别")
Color_Check(data)
lcd.display(img)


同样的问题
# This work is licensed under the MIT license.
# Copyright (c) 2013-2025 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# LCD Shield Example
#
# Note: To run this example you will need a LCD Shield for your OpenMV Cam.
#
# The LCD shield allows you to view your OpenMV Cam's frame buffer on the go.
import sensor
import time
import display
import image
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.LCD)
# Initialize the lcd screen.
# Note: A DAC or a PWM backlight controller can be used to control the
# backlight intensity if supported:
# lcd = display.SPIDisplay(backlight=display.DACBacklight(channel=2))
# lcd.backlight(25) # 25% intensity
# Otherwise the default GPIO (on/off) controller is used.
# OpenMV Cam M4/M7/H7/H7 Plus -> DAC and GPIO Support
# OpenMV Cam RT1062 -> GPIO Support
# OpenMV Cam N6 -> PWM and GPIO Support
lcd = display.SPIDisplay(vflip=True, hmirror=True)
clock = time.clock()
while True:
clock.tick()
lcd.write(sensor.snapshot(), hint=image.CENTER | image.SCALE_ASPECT_KEEP)
print(clock.fps())
OpenMV中并没有LCD这个模块,而是使用display