欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Openmv小结(电设、智能车)

程序员文章站 2024-01-22 23:00:58
...

距离上次做这些嵌入式的竞赛已经过去一年了,这里简单总结一下去年用的比较多的一个器件–openmv吧
以恩智浦版openmv为例(以后貌似都没有恩智浦赞助了)
Openmv小结(电设、智能车)

电赛板球系统

'''
Design by ***
device:IMXRT1062_openmv
@brief:17年国赛板球系统代码
'''
import sensor, image, time
from pyb import UART
from struct import pack
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...

uart = UART(4, 115200)
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # use 灰度
sensor.set_framesize(sensor.QQVGA) # sensor.QVGA: 320x240 QQVGA: 160x120
sensor.skip_frames(10) # Let new settings take affect.
#灰度图不需要关闭白平衡
clock = time.clock() # Tracks FPS.
red_threshold_01 = (105,122)
while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    #img = sensor.snapshot()
    img = sensor.snapshot().lens_corr(strength = 1.8, zoom = 1.0) # Take a picture and return the image.
   #  pixels_threshold=100, area_threshold=100
    area=(8,1,142,119)
    #blobs = img.find_blobs([red_threshold_01])
    blobs = img.find_blobs([red_threshold_01],roi=area,area_threshold=8)
    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
            ##在目标颜色区域的中心画十字形标记
            #data=bytearray([0xfe,0xfa,b[5],b[6],0xff])
            data=pack("<bbHHb",0xfe,0xfa,b[5],b[6],0xff)
            uart.write(data)

    print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
    # connected to your computer. The FPS should increase once disconnected.

识别AprilTag

'''
Design by CWJ
device:IMXRT1062
找最近的AprilTag
'''
import sensor, image, time, math
import pyb
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
sensor.set_auto_exposure(False, exposure_us=3000) # make smaller to go faster
clock = time.clock()
uart = pyb.UART(4, 115200)
uart.init(115200, bits=8, parity=None, stop=1)
while(True):
    img = sensor.snapshot()
    tag=img.find_apriltags(families=image.TAG25H9)
    if len(tag)>0:
        key=0
        max_x=0
        max_y=0
        for i in range(len(tag)):
            if tag[i].cy() > max_y:
                max_x=tag[i].cx()
                max_y=tag[i].cy()
                key=i
                if tag[i].cx() > max_x :
                    max_x=tag[i].cx()
                    max_y=tag[i].cy()
                    key=tag[i].id()
        tag=tag[key]
        img.draw_rectangle(tag.rect(), color = (255, 0, 0))
        img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))
        x=int(128+tag.x_translation()*10)
        y=int(128+tag.y_translation()*10)
        uart.writechar(0xfe)
        uart.writechar(0xfa)
        uart.writechar(tag.id())
        uart.writechar(x)
        uart.writechar(y)
        uart.writechar(int(180*tag.rotation()/math.pi*100)&0xff)
        uart.writechar((int(180*tag.rotation()/math.pi*100)>>8)&0xff)
        uart.writechar(0xff)
        print(180*tag.rotation()/math.pi)
        print(tag)

串口模块

'''
Design by ***
IMXRT1062_uart_module
'''
# UART Control 串口通信

import time
from pyb import UART

uart = UART(4, 115200)                         # 使用给定波特率初始化
uart.init(115200, bits=8, parity=None, stop=1) # 使用给定参数初始化

'''
发送数据:
一帧数据的每一个Byte必须要以字节的显示发送(data = bytearray([x,y])),
而不能是用16进制发送(uart.write("%x %x \r"%(x,y))),
他们两个函数在串口助手里面看到的内容是一样的(大小写的区别),
但是后者是无法让单片机接收到的。
如果采用后者发送方式,就会出现openmv和单片机分别于PC通讯没问题,但是二者之间却无法通讯的问题
https://blog.csdn.net/LJH_1999/article/details/88782943
'''
def sending_data():
    global uart
    '''
    data=bytearray([0xFF,0xFA,X_black_Sign,Y_black_Sign,0xFD,0XF6])
    0xFF, 0xFA...0xFD, 0xF6
    data=struct.pack("<bbHHb",0xfe,0xfa,b[5],b[6],0xff)
    '''
    uart.write("haha\n")
'''
接收数据:
openmv收数据时前加b'后加'
例:电脑发444r\n
收到b'444r\n'
'''
def recive_data():
    global uart

    if uart.any():
        tmp_data = uart.readline()
        uart.write("RECIVED : %s\n"%tmp_data)
        print(tmp_data)

while(True):
    sending_data()
    recive_data()
    time.sleep(1000)

相关标签: Electronics