在追小球的云台这一例程里,我加了一个旋转摄像头的函数,舵机的控制就变成反着旋转,请问应该怎么修改?
import sensor, image, time
from pid import PID
from pyb import Servo
pan_servo=Servo(1)
tilt_servo=Servo(2)
orange_threshold = (52, 75, 9, 127, 32, 95)
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()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
sensor.set_vflip(True)
clock = time.clock()
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([orange_threshold])
if blobs:
max_blob = find_max(blobs) #寻找最大色块
pan_error = max_blob.cx()-img.width()/2 #左右偏差=色块中心x-色块宽度/2
tilt_error = max_blob.cy()-img.height()/2 #上下偏差=色块中心y-色块长度/2
print("pan_error: ", pan_error)
img.draw_rectangle(max_blob.rect()) #画矩形
img.draw_cross(max_blob.cx(), max_blob.cy()) #色块中心画十字
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)旋转摄像头的函数为sensor.set_vflip(True)
https://forum.singtown.com/topic/50/%E5%9B%BE%E5%83%8F%E6%98%AF%E5%80%92%E7%9D%80%E7%9A%84-%E6%80%8E%E4%B9%88%E6%AD%A3%E8%BF%87%E6%9D%A5
@kidswong999 多谢,问题已解决,前面加sensor.set_hmirror(True)和sensor.set_vflip(True),然后 将 pan_error 和 tilt_error 算出来的值加个负号就行了