@kidswong999
import sensor, image, time
from pyb import UART
Always pass UART 3 for the UART number for your OpenMV Cam.
The second argument is the UART baud rate. For a more advanced UART control
example see the BLE-Shield driver.
threshold_index = 0 # 0 for red, 1 for green, 2 for blue
thresholds = [(6,22,-12,16,-7, 20), # generic_red_thresholds
(30, 100, -64, -8, -32, 32), # generic_green_thresholds
(0, 30, 0, 64, -128, 0)] # generic_blue_thresholds
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
uart = UART(3, 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()
img = sensor.snapshot()
blobs = img.find_blobs([thresholds[threshold_index]])
if blobs:
max_blob=find_max(blobs)
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy())
a=int(max_blob.cx()/4)
b=int(max_blob.cy()/4)
dotx_high=(a>>8)&0XFF
dotx_low=a&0XFF
doty_high=(b>>8)&0XFF
doty_low=b&0XFF
print(a,b)
#uart.write(hex(170)+hex(175)+hex(242)+hex(7)+hex(0)+hex(175)+a1+hex(0)+b1+hex(12)+hex(242)+hex(1)+hex(0))
c=max_blob.pixels()
pix_high=(c>>8)&0XFF
pix_low=c&0XFF
xy=[0xAA, 0xAF,0XF2,0x07,dotx_high,dotx_low,doty_high,doty_low,pix_high,pix_low,0x01,0x00]
uart.write(bytes(xy))
time.sleep(10)
else:
xy=[0xAA, 0xAF,0XF2,0x07,0x00,0x00,0x00,0x00,0x0c,0xf2,0x00,0x00]
uart.write(bytes(xy))
time.sleep(10)