import sensor, image, time
import car
from pid import PID
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(30)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
size_threshold = 2000
x_pid = PID(p=0.2, i=1, imax=100)
h_pid = PID(p=0.05, i=0.1, imax=50)
clock = time.clock()
while(True):
clock.tick()
img = sensor.snapshot()
car.run(35,-35)
for tag in img.find_apriltags():#没扫描出来,则不运行
if tag[4]==0:
x_error =tag[0]+tag[3]/2-80
h_error = tag[2]*tag[3]-size_threshold
img.draw_rectangle(tag[0:4],color=(255,0,0)) # rect
x_output=x_pid.get_pid(x_error,1)/6
h_output=h_pid.get_pid(h_error,1)
car.run((-h_output-30)/2-x_output,(-h_output-30)/2+x_output)
print((-h_output-30)/2-x_output,(-h_output-30)/2+x_output)
time.sleep(50)
else:
car.run(35,-35)什么错误?
哪里和哪里不符?
能加下您的QQ吗?这边交流不是很方便。
@kidswong999 左边电压输出正常,能根据摄像头获取的信息进行改变(-h_output-30)/2-x_output从而改变电压,但右边同样也可以获取(-h_output-30)/2+x_output的数据,但是右边对应p8的pwm输出电压则是变成不按参数变化的。
我认为你的公式不对
x_error =tag[0]+tag[3]/2-80
h_error = tag[2]*tag[3]-size_thresholdx_error应该是tag[0]+tag[2]/2-80
改成
x_error = tag.cx()-img.width()/2更好一些。
@kidswong999 感谢感谢,那个算法只是简化了一些,自己解决了,不是算法的问题。