#当检测出是红色时亮红灯或者绿色时亮绿灯或者蓝色时亮蓝灯。
# Single Color RGB565 Blob Tracking Example
#
# This example shows off single color RGB565 tracking using the OpenMV Cam.
import sensor, image, time, math
from pyb import LED
red=LED(1)
green=LED(2)
bule=LED(3)
threshold_index = 0 # 0 for red, 1 for green, 2 for blue
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green/blue things. You may wish to tune them...
thresholds = [(30, 100, 15, 127, 15, 127), # 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()
# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. "merge=True" merges all overlapping blobs in the image.
while(True):
clock.tick()
img = sensor.snapshot()
for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200, merge=True):
# These values are stable all the time.
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
if blob.code()==1:red.on()
if blob.code()==2:green.on()
if blob.code()==3:bule.on()
print(blob)
#运行舵机的代码 通过修改range(x,y)来改变舵机的转动范围 修改angle(i,t)来改变舵机转动的快慢。 增加s2 s3可添加两个或多个舵机
# Servo Control Example
#
# This example shows how to use your OpenMV Cam to control servos.
import time
from pyb import Servo
s1 = Servo(1) # P7
#s2 = Servo(2) # P8
#s3 = Servo(3) # P9 Only for OpenMV3 M7
while(True):
for i in range(-50,90):
s1.angle(i,1500)
#s2.angle(i)
time.sleep(10)
for i in range(90,-50):
s1.angle(i,1500)
#s2.angle(i)
time.sleep(10)