import sensor,image, time
from servo import Servos
from pid import PID
from machine import I2C, Pin
i2c = I2C(sda=Pin('P5'), scl=Pin('P4'))
tilt_servo = Servos(i2c, address=0x40, freq=50, min_us=500, max_us=2500, degrees=180) #通道1
pan_servo = Servos(i2c, address=0x40, freq=50, min_us=500, max_us=2500, degrees=180) #通道2
red_threshold = (46, 57, 46, 72, 9, 51)
#pan_pid = PID(p=0.02, i=0, imax=90,d=0.001) #脱机运行或者禁用图像传输,使用这个PID
#tilt_pid = PID(p=0.019, i=0, imax=90,d=0.001) #脱机运行或者禁用图像传输,使用这个PID
pan_pid = PID (p=0.018, i=0, imax=90,d=0)#在线调试使用这个PID
tilt_pid = PID(p=0.018, i=0, imax=90,d=0)#在线调试使用这个PID
pan=50#X初角度 70
tilt=50#Y初角度 50
pan_servo.position(1, pan)
tilt_servo.position(0,tilt)
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_hmirror(True)
sensor.set_vflip(True)
sensor.set_framesize(sensor.QVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.
# blob[2]=blob.width
# blob[3]=blob.height
def find_max(blobs):
max_size=0
for blob in blobs:
ratio = blob[2] / blob[3]
if (ratio >= 0.7) and (ratio <= 1.3):
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([red_threshold])
if blobs:
max_blob = find_max(blobs)
pan_error = max_blob.cx()-img.width()/2
tilt_error = max_blob.cy()-img.height()/2
print("pan_error: ", pan_error)
img.draw_rectangle(max_blob.rect()) # rect=返回一个矩形元组(x, y, w, h)
img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy 画一个十字
pan_output=pan_pid.get_pid(pan_error,1)
tilt_output=tilt_pid.get_pid(tilt_error,1)
print("pan_output",pan_output)
pan_servo.position(1, pan-pan_output)
tilt_servo.position(0,tilt+tilt_output)
pan=pan-pan_output
tilt=tilt+tilt_output
print(clock.fps())
C
cg6i
@cg6i
0
声望
6
楼层
821
资料浏览
0
粉丝
0
关注
cg6i 发布的帖子
-
NameError:local variable referenced before assignment
-
RE: 在定时器里调用PWM程序出现错误
我在前面加了 还是不行
def tick(timer): global tim4 tim4.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width_percent=8)
-
在定时器里调用PWM程序出现错误
import time from pyb import Pin, Timer def tick(timer): print(timer) tim4.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width_percent=8) tim4 = Timer(4, freq=50) tim2 = Timer(2, freq=1) tim2.callback(tick) while (True): time.sleep(1000)