logo

PID控制舵机实现巡线小车问题

2018-12-01 10:05
K
Barret

我将巡线小车例程和追小球云台代码合在一起,可以初步实现巡线,但是舵机转向并不灵敏,转向的角度要么多大要么太小,小车无法实现巡线,求如何调节参数实现小车巡线。

d3ad1475-3822-4a05-9216-1edc0a7e3192.png

以下是main.py代码:

THRESHOLD = (59, 86, 15, 62, -15, 31) # Grayscale threshold for dark things...

import sensor, image, time

from pyb import LED

from pid import PID

from pyb import Servo

pan_servo=Servo(1) #p7

rho_pid = PID(p=0.4, i=0)

theta_pid = PID(p=0.001, i=0)

LED(1).off()

LED(2).off()

LED(3).off()

sensor.reset()

sensor.set_vflip(False)

sensor.set_hmirror(False)

sensor.set_pixformat(sensor.RGB565)

sensor.set_framesize(sensor.QQQVGA) # 80x60 (4,800 pixels) - O(N^2) max = 2,3040,000.

#sensor.set_windowing([0,20,80,40])

sensor.skip_frames(time = 2000) # WARNING: If you use QQVGA it may take seconds

clock = time.clock() # to process a frame sometimes.

while(True):

clock.tick()

img = sensor.snapshot().binary([THRESHOLD])

line = img.get_regression([(100,100,0,0,0,0)], robust = True)

if (line):

rho_err = abs(line.rho())-img.width()/2

if line.theta()>90:

theta_err = line.theta()-180

else:

theta_err = line.theta()

img.draw_line(line.line(), color = 127)

print(rho_err,line.magnitude(),rho_err)

if line.magnitude()>8:

#if -40<b_err<40 and -30<t_err<30:

rho_output = rho_pid.get_pid(rho_err,1)

theta_output = theta_pid.get_pid(theta_err,1)

output = rho_output+theta_output

#car.run(50+output, 50-output)

pan_servo.angle(pan_servo.angle()+output)

else:

pan_servo.angle(pan_servo.angle())

else:

pan_servo.angle(pan_servo.angle())

pass

#print(clock.fps())

2018-12-01 13:33
I
网红小智智 星瞳科技

PID调节靠经验,我没有你的硬件,帮不了你。

2018-12-01 16:09
K
Barret

代码我这样改理论上只需要调pid参数,逻辑上控制舵机转向没什么问题吧

2018-12-01 21:22
I
网红小智智 星瞳科技

应该没什么问题吧。

代码我没法运行,我保证不了。:face_with_tears_of_joy: