文章目录
- 追踪人脸的云台
- 追踪AprilTags的云台
- 追踪圆形的云台
和追踪小车的原理是一样的
首先获得目标物体的x,y坐标,然后通过目标物体的xy坐标来控制我们云台的两个舵机的pid运动
无论追踪什么物体,都是通过物体的x,y坐标来控制云台的运动,对于云台的舵机来说,它只知道传给它的是x,y坐标,并不知道OpenMV传给它的是小球的xy坐标还是人脸的xy坐标
所以我们只需要修改main.py中的函数即可
追踪人脸的云台
搜索函数:objects = img.find_features(face_cascade,threshold ,scale)
记得要载入haar算子
,并且寻找小球的函数find_blob()
和寻找人脸的函数find_feartures()
的返回值是不一样的!
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)
tilt_pid = PID(p=0.05, i=0, imax=90)
sensor.reset()
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_vflip(True)
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()
face_cascade = image.HaarCascade("frontalface" , stages = 25)
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()
img = sensor.snapshot()
objects = img.find_features(face_cascade,threshold = 0.75,scale = 1.35)
if objects:
max_object = find_max(objects)
pan_error = max_object[0] + max_object[2]/2 - img.width()/2
tilt_error = max_object[1] + max_object[3]/2 - img.height()/2
print("pan_error: ", pan_error)
img.draw_rectangle( max_object.rect())
img.draw_cross(int( max_object[0] + max_object[2]/2),int( max_object[1] + max_object[3]/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)
追踪AprilTags的云台
搜索函数: objects = img.find_apriltags()
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)
tilt_pid = PID(p=0.05, i=0, imax=90)
sensor.reset()
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()
face_cascade = image.HaarCascade("frontalface" , stages = 25)
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()
img = sensor.snapshot()
objects = img.find_apriltags()
if objects:
max_object = find_max(objects)
pan_error = max_object[0] + max_object[2]/2 - img.width()/2
tilt_error = max_object[1] + max_object[3]/2 - img.height()/2
print("pan_error: ", pan_error)
img.draw_rectangle( max_object.rect())
img.draw_cross(int( max_object[0] + max_object[2]/2),int( max_object[1] + max_object[3]/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)
追踪圆形的云台
搜索函数:objects = img.find_circles()
注意要用到畸变矫正
,加在我们的sensor.snapshot()之后img = sensor.snapshot().lens_corr(1,8)
对于圆来说,比较大小就不用if blob[2]*blob[3] > max_size:
了,而是直接比较半径cirle[2]
即可
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)
tilt_pid = PID(p=0.05, i=0, imax=90)
sensor.reset()
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()
def find_max(blobs):
max_size=0
for blob in blobs:
if blob[2]*blob[2] > max_size:
max_blob=blob
max_size = blob[2]*blob[2]
return max_blob
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(1,8)
objects = img.find_circles(threshold = 3500,x_margin = 10,y_margin = 10,r_margin = 10,r_min = 2,r_max = 100,r_step = 2)
if objects:
max_object = find_max(objects)
pan_error = max_object[0] - img.width()/2
tilt_error = max_object[1] - img.height()/2
print("pan_error: ", pan_error)
img.draw_rectangle( max_object.rect())
img.draw_circle(max_object[0],max_object[1],max_object[2],color = (255,0,0))
img.draw_cross(int(max_object[0]),int(max_object[1])
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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)