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,中心点坐标80,60
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;
}
}