基于openmv的小车

原来采用RGB565识别红色再转换为灰度识别继续进行下面的循迹代码,
但是无法实现多次识别,只能在摄像头第一次初始化后可识别红色,
但转化为灰色后,进入了死循环,程序转换为在灰度识别下对黑线的识别,
无法运行下面的循迹代码。根据这一特点,我将初始化摄像头加入while循环,发现多次初始化摄像头图像会不断闪烁。

最终,重新编写代码,关于识别色块的程序,一但识别的红色则车速为0,一但识别到黑色向左加速,一但没有识别到黑色向右加速,这样就能实现在跑道内左转。欢迎大家评论指出不足之处

main.py

import sensor, image, time
import car
 
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
red_threshold_01 = (61, 48, -128, 127, -128, 127)
black_threshold_02=(0, 22, -22, 127, -33, 18)
#设置红色的阈值,括号里面的数值分别是L A B 的最大值和最小值(minL, maxL, minA,
# maxA, minB, maxB),LAB的值在图像左侧三个坐标图中选取。如果是灰度图,则只需
#设置(min, max)两个数字即可。
 
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.
 
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False)
#关闭白平衡。白平衡是默认开启的,在颜色识别中,需要关闭白平衡。
clock = time.clock() # Tracks FPS.
 
while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # Take a picture and return the image.
    #  pixels_threshold=100, area_threshold=100
    blobs = img.find_blobs([red_threshold_01], area_threshold=150)
 
    if blobs:
    #如果找到了目标颜色
        print(blobs)
        for b in blobs:
        #迭代找到的目标颜色区域
            # Draw a rect around the blob.
            img.draw_rectangle(b[0:4]) # rect
            #用矩形标记出目标颜色区域
            img.draw_cross(b[5], b[6]) # cx, cy
            #在目标颜色区域的中心画十字形标记
            car.run(0,0)
    else:
        clock.tick() # Track elapsed milliseconds between snapshots().
        img = sensor.snapshot() # Take a picture and return the image.
    #  pixels_threshold=100, area_threshold=100
        blobs = img.find_blobs([black_threshold_02], area_threshold=150)
 
        if blobs:
    #如果找到了目标颜色
            print(blobs)
            for b in blobs:
        #迭代找到的目标颜色区域
            # Draw a rect around the blob.
                img.draw_rectangle(b[0:4]) # rect
            #用矩形标记出目标颜色区域
                img.draw_cross(b[5], b[6]) # cx, cy
            #在目标颜色区域的中心画十字形标记
                car.run(30,60)
        else:
                car.run(60,30)
          #car.run(60,60)
 
 
    print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
    # connected to your computer. The FPS should increase once disconnected.

Car.py

from pyb import Pin, Timer
inverse_left=False  #change it to True to inverse left wheel
inverse_right=False #change it to True to inverse right wheel
 
ain1 =  Pin('P0', Pin.OUT_PP)
ain2 =  Pin('P1', Pin.OUT_PP)
bin1 =  Pin('P2', Pin.OUT_PP)
bin2 =  Pin('P3', Pin.OUT_PP)
ain1.low()
ain2.low()
bin1.low()
bin2.low()
 
pwma = Pin('P7')
pwmb = Pin('P8')
tim = Timer(4, freq=1000)
ch1 = tim.channel(1, Timer.PWM, pin=pwma)
ch2 = tim.channel(2, Timer.PWM, pin=pwmb)
ch1.pulse_width_percent(0)
ch2.pulse_width_percent(0)
 
def run(left_speed, right_speed):
    if inverse_left==True:
        left_speed=(-left_speed)
    if inverse_right==True:
        right_speed=(-right_speed)
 
    if left_speed < 0:
        ain1.low()
        ain2.high()
    else:
        ain1.high()
        ain2.low()
    ch1.pulse_width_percent(abs(left_speed))
 
    if right_speed < 0:
        bin1.low()
        bin2.high()
    else:
        bin1.high()
        bin2.low()
    ch2.pulse_width_percent(abs(right_speed))
发布了31 篇原创文章 · 获赞 28 · 访问量 9513

猜你喜欢

转载自blog.csdn.net/visual_eagle/article/details/102803724
今日推荐