오픈 뮤직비디오 기반 승용차

3720 단어 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))

좋은 웹페이지 즐겨찾기