logo

openmv脱机运行控制舵机有时会重启?

2022-09-25 13:56
1
tvfa
import sensor, image, time,lcd,math,pyb
import json
from pyb import UART
from pyb import Servo
from pyb import Pin
from pyb import LED
led = LED(4)
a=0
q=0
c=0
H=0
cir_r=0
rec_w=0
rec_h=0
led.toggle()
blue_threshold   = (18, 38, 9, 58, -90, -33)
red_threshold   =  (29, 53, 34, 74, 15, 55)#(13, 38, 30, 58, 16, 45)
yellow_threshold   = [(54, 67, -13, 4, 49, 71),(72, 95, -17, 9, 54, 82)]
fangkuai_thresholds =[(13, 38, 75, 61, 17, 52),(19, 57, 19, 87, 16, 58)]
thresholds = [(13, 38, 6, 61, 17, 52)]
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(20)
sensor.set_auto_whitebal(False)
s2=Servo(2)
s1=Servo(1)
uart = UART(3, 115200)
clock = time.clock()
#lcd.init()
s1.angle(-55)
while(True):
    clock.tick()
    a=uart.readchar()
    led = LED(1)
    led = LED(4)
    led.off()
    
    if a==49 and c!=2:
        led = LED(2)
        led.off()
        s1=Servo(2)
        s2=Servo(1)
        s2.angle(-85)
        s1.angle(30)
        pyb.delay(200)
    if a==52 and c!=2:
        s2=Servo(2)
        s1=Servo(1)
        img = sensor.snapshot()
        sensor.set_framesize(sensor.QVGA)
        led = LED(1)
        led.off()
        sensor.set_windowing((156,1,117,240))
        led = LED(4)
        led.off()
        blobs0 = img.find_blobs([blue_threshold],pixels_threshold=100)
        blobs1 = img.find_blobs([red_threshold],pixels_threshold=40)
        if blobs1 :
            led.off()
            s1.angle(-15)
            s2.angle(60)
            pyb.delay(200)
            s1.angle(70)
            pyb.delay(200)
            s1.angle(-15)
            pyb.delay(200)
            c=c+1
    if a==50 and c!=2:
        sensor.set_framesize(sensor.QVGA)
        sensor.set_windowing((165,20,148,100))
        s2=Servo(2)
        s1=Servo(1)
        led = LED(4)
        led.on()
        img = sensor.snapshot()
        blobs0 = img.find_blobs([blue_threshold],pixels_threshold=100)
        blobs1 = img.find_blobs([red_threshold],pixels_threshold=40)
        blobs2 = img.find_blobs(yellow_threshold,pixels_threshold=40)
        if blobs0 :
            s1.angle(-15)
        elif blobs2 :
            s1.angle(-15)
            s2.angle(-5)
            pyb.delay(200)
            s1.angle(45)
            pyb.delay(200)
            s1.angle(-15)
            pyb.delay(200)
        elif blobs1:
            s1.angle(-15)
            s2.angle(60)
            pyb.delay(200)
            s1.angle(45)
            pyb.delay(200)
            s1.angle(-15)
            pyb.delay(200)
    if  c==2:
        s1.angle(-75)
        led = LED(2)
        led.on()
        sensor.set_framesize(sensor.QVGA)
        img = sensor.snapshot()
        sensor.set_windowing((0,0,307,233))
        #img.lens_corr(1.8)
        for code in img.find_qrcodes():
            q=code.payload()
        led = LED(4)
        led.off()
        blobs3 = img.find_blobs(fangkuai_thresholds,pixels_threshold=20)
        if q=='R':
            uart.write('1')
            s2.angle(30)
            print(1)
        if blobs3:
            for blob in img.find_blobs(fangkuai_thresholds,pixels_threshold=50):
                if blob.density()>0.68:
                uart.write('1')
                s2.angle(30)
                led = LED(4)
                led.on()
                print(1)
        if   0.61>blob.density()>0.20:
            img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
            img.draw_circle((blob.cx(), blob.cy(),int((blob.w()+blob.h())/4)))
            uart.write('2')
            s2.angle(30)
            led = LED(4)
            led.on()
            print(2)
2022-09-26 20:13
I
网红小智智 星瞳科技

首先代码118行缩进有错误。

先找到复现问题的情况,你的代码逻辑太复杂。比如每个状态单独测试。