想实现如下功能:上电时等待检测信号(亮蓝灯),信号触发中断之后开始检测线段(此时亮绿灯),检测到之后红灯闪一下,又开始等待下一次的检测信号。代码如下:脱机运行正常,但是接IDE调试时外部中断触发两三次就会报错:Uncaught exception in ExtInt interrupt handler line 15 RuntimeError:
import sensor, image, time,pyb,micropython,utime
from pyb import Pin, ExtInt
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # grayscale is faster
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
blue = pyb.LED(3)
green = pyb.LED(2)
red = pyb.LED(1)
global start_flag
start_flag =0 #上电时等待开始检测的信号
def start(line):
global start_flag
start_flag = 1
#外部中断P0口 上升沿触发 回调函数为start
extint = pyb.ExtInt('P0', pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_NONE, start)
ok = Pin('P1', Pin.OUT_PP)
while(True):
clock.tick()
img = sensor.snapshot()
if enable_lens_corr:
img.lens_corr(7.8) # 防畸变
if start_flag == 1:
blue.off()
green.on() #开始检测指示灯
for l in img.find_line_segments((0,45,160,30),merge_distance = 10, max_theta_diff = 180):
if l.length()>30:
img.draw_line(l.line(), color = (255, 255, 255))
start_flag = 0 #找到理想的线之后置零,不在检测
ok.high()
red.on()
utime.sleep_ms(100)
ok.low()
red.off()
elif start_flag == 0:#等待信号时为蓝灯 工作时为绿灯 找到焊缝时开始等待
blue.on()
green.off()
# print('FPS %f' % clock.fps())