logo

OpenMV4 H7 Plus 硬件和代码保持原样,无法正常运行

2025-05-01 13:46
M
ek4n

问题描述:OpenMV4 H7 Plus 在2024年1月的代码和硬件,功能实时运行正常。硬件和代码保持原样,无法正常运行,图像能识别,舵机没有反应。

代码如下:

import sensor, image, time

from pid import PID
from pyb import Servo
from pyb import Pin
s1=Servo(1)
s2=Servo(2)

#pan_servo.calibration(500,2500,500)
#tilt_servo.calibration(500,2500,500)
pin_0 = Pin('P0', Pin.IN, Pin.PULL_UP)
pin_1 = Pin('P1', Pin.IN, Pin.PULL_UP)

pin_2 = Pin('P2', Pin.IN, Pin.PULL_UP)
#pin_2 = Pin('P2', Pin.OUT_PP, Pin.PULL_NONE)
pin_3 = Pin('P3', Pin.OUT_PP, Pin.PULL_NONE)
#red_threshold  = (30, 100, 15, 127, -40, 127)(70, 0, 49, 127, -128, 127)
red_threshold =(75, 0, 127, 25, -128, 127) #红色光点阈值(50, 6, 74, 15, 67, -5)(100, 30, 42, 10, 20, -2)(15, 75, 18, 90, 0, 60)
green_threshold = (46, 92, -35, 87, -12, 11)#[(25, 100, -65, -15, -15, 40)](54, 41, -128, -11, -128, 127)
#pan_pid = PID(p=0.06, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
#tilt_pid = PID(p=0.05, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
#pan_pid = PID(p=0.08, i=0, imax=90)#在线调试使用这个PID
#tilt_pid = PID(p=0.09, i=0, imax=90)#在线调试使用这个PID

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.VGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
#sensor.set_auto_whitebal(False) # turn this off.
sensor.set_auto_exposure (False, exposure_us=54000)
sensor.set_windowing((440, 440))       # Set 240x240 window.
#sensor.set_brightness(-2)   #设置亮度
#sensor.set_contrast(3) #对比度
#sensor.set_vflip(True)
clock = time.clock() # Tracks FPS.

def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob[2]*blob[3] > max_size:
            max_blob=blob
            max_size = blob[2]*blob[3]
    return max_blob


s1.angle(0)#-14----12   原点2,4
s2.angle(0)#270°舵机要乘以0.66666  +13-----  -13
pin_2.value(0)
time.sleep_ms(1000)

def find_red():
    x_error=0
    y_error=0

    x_red=0
    y_red=0
    Total_y=0
    Total_x=0
    x_red_last=0
    y_red_last=0

    x_green_last=0
    y_green_last=0

    x_green=0
    y_green=0
    Px=0.006
    Py=0.006

    Ix=0
    Iy=0

    Dx=0       #Dx=0.00007#
    Dy=0

    Total_x=0
    Total_y=0

    Px_num=0
    Ix_num=0
    Dx_num=0

    Py_num=0
    Iy_num=0
    Dy_num=0

    last1_error_x=0
    last1_error_y=0

    last2_error_x=0
    last2_error_y=0

    dt=1/13
    while(True):
        clock.tick() # Track elapsed milliseconds between snapshots().
        img = sensor.snapshot() # Take a picture and return the image.
        value0 = pin_0.value() # get value, 0 or 1#读入p_in引脚的值
        #if value0==0 :
            #time.sleep_ms(1000)
            #while(True) :
                #s1.angle(Total_y)
                #s2.angle(Total_x)
                #value0 = pin_0.value() # get value, 0 or 1#读入p_in引脚的值
                #value1 = pin_1.value() # get value, 0 or 1#读入p_in引脚的值
                #pin_2.value(0)
                #pin_3.value(0)
                #if value0==0 or value1==0 :
                    #time.sleep_ms(200)
                    #break
        blobs = img.find_blobs([red_threshold])  #红色!!!!!!!!!!!!
        if blobs:
            max_blob = find_max(blobs)
            x_red = max_blob.cx()
            y_red = max_blob.cy()

            x_red=x_red*0.6+x_red_last*0.4
            x_red_last=x_red

            y_red=y_red*0.6+y_red_last*0.4
            y_red_last=y_red
            print("RED")

    #        print(pan_error,tilt_error)

            img.draw_rectangle(max_blob.rect()) # rect
            img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy

    #        pan_output=pan_pid.get_pid(pan_error_,1)/2
    #        tilt_output=tilt_pid.get_pid(tilt_error,1)
    #        print("pan_output",pan_output)
    #        pan_servo.angle(pan_servo.angle()+pan_output)
    #        tilt_servo.angle(tilt_servo.angle()-tilt_output)


        blobs = img.find_blobs([green_threshold])#飞机
        if blobs:
            max_blob = find_max(blobs)
            x_green = max_blob.cx()
            y_green = max_blob.cy()

            x_green= x_green*0.6+x_green_last*0.4
            x_green_last=x_green

            y_green= y_green*0.6+y_green_last*0.4
            y_green_last=y_green
            print("fjfjfjfjfj")
    #        print("GREEN",x_green,y_green)
            img.draw_rectangle(max_blob.rect()) # rect
            img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
        x_error=x_green-x_red
        y_error=y_green-y_red#计算误差

        Px_num=x_error-last1_error_x
        Ix_num=x_error*dt
        if Ix_num >20:
            Ix_num=20
        if Ix_num <-20:
            Ix_num=-20
        Dx_num=x_error-last1_error_x*last1_error_x+last2_error_x


        Total_x=x_error*Px+Ix_num*Ix+Dx_num*Dx+Total_x

        if Total_x>20 :
            Total_x=20
        if Total_x<-20 :
            Total_x=-20



        Py_num=Py*y_error
        Iy_num=y_error*dt
        if Iy_num >5:
            Iy_num=5
        if Iy_num <-5:
            Iy_num=-5
        Dy_num=y_error-last1_error_y*last1_error_y+last2_error_y
        Total_y=Py*y_error+Iy_num*Iy+Dy_num*Dy+Total_y
        print(Total_x,Total_y)
        if Total_y>20 :
            Total_y=20
        if Total_y<-20 :
            Total_y=-20
        value1 = pin_1.value() # get value, 0 or 1#读入p_in引脚的值
        if value1==0 :
           break
        value2 = pin_2.value() # get value, 0 or 1#读入p_in引脚的值
        if value2==0 :
            time.sleep_ms(1000)
            while(True):
                    value2 = pin_2.value() # get value, 0 or 1#读入p_in引脚的值
                    if value2==0 :
                        break
        #s1.angle(Total_y)
        s2.angle(Total_x)


#        print(Total_x,x_error)
        last2_error_x=last1_error_x
        last2_error_y=last1_error_y
        last1_error_x=x_error
        last1_error_y=y_error
        dt=1/clock.fps()
    #    print(dt)
    #    print(clock.fps())
    #        print("pan_output",pan_output)


    #        pan_output=pan_pid.get_pid(pan_error,1)/2
    #        tilt_output=tilt_pid.get_pid(tilt_error,1)
    #        print("pan_output",pan_output)
    #        pan_servo.angle(pan_servo.angle()+pan_output)
    #        tilt_servo.angle(tilt_servo.angle()-tilt_output)

while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # Take a picture and return the image.
    value0 = pin_0.value() # get value, 0 or 1#读入p_in引脚的值
    value1 = pin_1.value() # get value, 0 or 1#读入p_in引脚的值
    value2 = pin_2.value() # get value, 0 or 1#读入p_in引脚的值
    #pin_2.value(0)
    #pin_3.value(0)
    print(value0,value1,value2)
    s1.angle(0)#-14----12   原点2,4
    s2.angle(0)#270°舵机要乘以0.66666  +13-----  -13
    if value0==0 :
           find_red()
2025-05-07 19:15
I
网红小智智 星瞳科技

那你要用示波器看看引脚输出的波形,然后检查舵机的电源