OPENMV与主控进行通信

本文介绍如何使用OpenMV摄像头进行绿色物体追踪,并通过UART实现实时位置传输到单片机。通过Blob Detection技术,调整相机参数以适应追踪环境,同时展示了代码片段及串口通信的处理过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

openmv代码:

# Blob Detection and uart transport
import sensor, image, time
from pyb import UART
import json
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
green_threshold   = ((66, 93, -76, 5, 5, 30))
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.

EXPOSURE_TIME_SCALE = 0.5

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.图像大小160*120,中心点坐标8060
sensor.set_auto_exposure(False) # 设置自动曝光
sensor.skip_frames(time = 200)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.

sensor.set_auto_whitebal(False) # turn this off.
sensor.set_auto_gain(False)
sensor.skip_frames(time=500) # Let new settings take affect.
current_exposure_time_in_microseconds = sensor.get_exposure_us()
sensor.set_auto_exposure(False, \
    exposure_us = int(current_exposure_time_in_microseconds * EXPOSURE_TIME_SCALE))



uart = UART(1, 115200)
def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob.pixels() > max_size:
            max_blob=blob
            max_size = blob.pixels()
    return max_blob

while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot() # Take a picture and return the image.

    blobs = img.find_blobs([green_threshold])
    if blobs:

        FH = bytearray([0xb3,0xb3])
        uart.write(FH)
        max_blob=find_max(blobs)
       # print('sum :', len(blobs))
        img.draw_rectangle(max_blob.rect())
        img.draw_cross(max_blob.cx(), max_blob.cy())
        print(max_blob.cx(), max_blob.cy(), end = ',')
        print('\r\n')
        #output_str="%3d %3d" % (max_blob.cx(),max_blob.cy()) #方式1
        #output_str=json.dumps([max_blob.cx(),max_blob.cy()]) #方式2
        #print('a'+output_str)
        data = bytearray([max_blob.cx(),max_blob.cy()])
        uart.write(data)
        FOVER=bytearray([0x0d,0x0a])
        uart.write(FOVER)



单片机部分代码段:

if(USART_RX_STA&0x8000)
		{
				if(USART_RX_BUF[0]==0xb3&&USART_RX_BUF[1]==0xb3) //判断数据头
				{
					x=USART_RX_BUF[2];
					y=USART_RX_BUF[3];
					USART_RX_STA=0;
				}
			
				else
				{
				  USART_RX_STA=0;
				}
		}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值