import sensor, image, time
from pid import PID
from pyb import Servo
from pyb import UART
pan_servo=Servo(1)
tilt_servo=Servo(2)
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_contrast(3)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QVGA) # use QQVGA for speed.
sensor.set_windowing((320, 240))
sensor.set_pixformat(sensor.GRAYSCALE)
#ensor.skip_frames(10) # Let new settings take affect.
#sensor.set_auto_whitebal(False) # turn this off.
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False, value=100)
clock = time.clock() # Tracks FPS.
uart=UART(3,115200,timeout_char=10)
def draw_keypoints(img, kpts):
if kpts:
print(kpts)
img.draw_keypoints(kpts)
img = sensor.snapshot()
time.sleep(1)
kpts1 = None
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
if (kpts1 == None):
kpts1 = img.find_keypoints([106,68,125,87],max_keypoints=150, threshold=10, scale_factor=1.2)
draw_keypoints(img, kpts1)
else:
kpts2 = img.find_keypoints([106,68,125,87],max_keypoints=150, threshold=10, normalized=True)
if (kpts2):
match = image.match_descriptor(kpts1, kpts2, threshold=85)
if (match.count()>10):
img.draw_rectangle(match.rect())
img.draw_cross(match.cx(), match.cy(), size=10)
pan_error = match.cx()-img.width()/2
tilt_error = match.cy()-img.height()/2
pan_output=pan_pid.get_pid(pan_error,1)/2
tilt_output=tilt_pid.get_pid(tilt_error,1)
print("pan_output",pan_output)
pan_servo.angle(pan_servo.angle()+pan_output)
tilt_servo.angle(tilt_servo.angle()-tilt_output)
output_str="[%d,%d]" % (match.cx(),match.cy()) #方式1
#output_str=json.dumps([max_blob.cx(),max_blob.cy()]) #方式2
print('you send:',output_str)
uart.write(output_str+'\r\n')
以上是代码,目的是用特征的方法使云台跟随目标转动;并且发送目标的坐标值;
编译没问题;但是运行不行;除了发送坐标值可以,其他都不好;