import sensor, image, time
from pid import PID
from pyb import Servo
pan_servo=Servo(1)
tilt_servo=Servo(2)
pan_servo.calibration(500,2500,500)
tilt_servo.calibration(500,2500,500)
red_threshold = (13, 49, 18, 61, 6, 47)
pan_pid = PID(p=0.07, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
tilt_pid = PID(p=0.05, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
#pan_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
#tilt_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
sensor.reset() # Initialize the camera sensor.
sensor.set_auto_gain(False)
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.RGB565
sensor.set_framesize(sensor. QVGA) # or sensor.QVGA (or others)
sensor.skip_frames(time=900) # Let new settings take affect.
sensor.set_auto_exposure(False, 1000)#在这里调节曝光度,调节完可以比较清晰地看清激光点
sensor.set_auto_whitebal(False) # turn this off.
sensor.set_auto_gain(False) # 关闭增益(色块识别时必须要关)
def find_max(blobs):
max_size=0
for blob in blobs:
if blob[2]*blob[3] > max_size:
max_blob=blob
max_size = blob[2]*blob[3]
return max_blob
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs(threshold,x_stride=1, y_stride=1, area_threshold=0, pixels_threshold=0,merge=False,margin=1)
if len(blobs)>=1 :#有色块
# Draw a rect around the blob.
b = blobs[0]
#img.draw_rectangle(b[0:4]) # rect
cx = b[5]
cy = b[6]
for i in range(len(blobs)-1):
#img.draw_rectangle(b[0:4]) # rect
cx = blobs[i][5]+cx
cy = blobs[i][6]+cy
cx=int(cx/len(blobs))
cy=int(cy/len(blobs))
#img.draw_cross(cx, cy) # cx, cy
print(cx,cy)
return int(cx), int(cy)
return -1, -1 #表示没有找到
while True不是函数,是主循环,不能返回