pid.py >>https://github.com/wagnerc4/flight_controller/blob/master/pid.py
openmv 官网:http://book.openmv.cc/project/zhui-xiao-qiu-de-xiao-8f665d28-project-pan-tilt-md.html
缘由:官网上只调PID中p参数,让当时小白的我有点误解,学过PID,但不熟悉应用人就会一头雾水,所以就有本文咯,实际上我们可以充分利用强大的PID。
本文正确标题:如何用PID去控制追球小车的方向和速度控制。
硬件:两轮小车+OpenMV主控
目标:摄像头找红色小球色块,如果没有,小车绕圆运动寻找,发现小球后小车直线运动到距离球20cm。
内容:本文主要讲述如何用PID算法去实现对电机的控制。
注意:博主电机并无编码盘,输入误差是摄像头判断的数据。假设大家已经用官网的car.py作过了对电机的处理。
代码解释:
整定,就是修改PID,两个,一个方向,一个速度,因为旋转找小球
x_pid = PID(p=0.8, i=0.1, d=0.1,imax=10)#2018年10月26日更新
h_pid = PID(p=0.03, i=0.005,d=0.1, imax=40)
while(True):
pass
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs([green_threshold])
if blobs:
max_blob = find_max(blobs)
x_error = max_blob[5]-img.width()/2
h_error = max_blob[2]*max_blob[3]-size_threshold
if -20
print("x error: ", x_error)
img.draw_rectangle(max_blob[0:4]) # rect
img.draw_cross(max_blob[5], max_blob[6]) # cx, cy
x_output=x_pid.get_pid(x_error,1)
h_output=h_pid.get_pid(h_error,1)
print("h_output",h_output)
car.run(-h_output-x_output,-h_output+x_output)
else:
car.run(40,-40)#绕圆旋转
附官网PID.py文件:
from pyb import millis
from math import pi, isnan
class PID:
_kp = _ki = _kd = _integrator = _imax = 0
_last_error = _last_derivative = _last_t = 0
_RC = 1/(2 * pi * 20)
def __init__(self, p=0, i=0, d=0, imax=0):
self._kp = float(p) #比例
self._ki = float(i) #积分
self._kd = float(d) #微分
self._imax = abs(imax)
self._last_derivative = float('nan')
def get_pid(self, error, scaler):
tnow = millis()
dt = tnow - self._last_t
output = 0
if self._last_t == 0 or dt > 1000:
dt = 0
self.reset_I()
self._last_t = tnow
delta_time = float(dt) / float(1000)
output += error * self._kp
if abs(self._kd) > 0 and dt > 0:
if isnan(self._last_derivative):
derivative = 0
self._last_derivative = 0
else:
derivative = (error - self._last_error) / delta_time
derivative = self._last_derivative + \
((delta_time / (self._RC + delta_time)) * \
(derivative - self._last_derivative))
self._last_error = error
self._last_derivative = derivative
output += self._kd * derivative
output *= scaler
if abs(self._ki) > 0 and dt > 0:
self._integrator += (error * self._ki) * scaler * delta_time
if self._integrator < -self._imax: self._integrator = -self._imax
elif self._integrator > self._imax: self._integrator = self._imax
output += self._integrator
return output
def reset_I(self):
self._integrator = 0
self._last_derivative = float('nan')
附博主main.py代码
# Blob Detection Example
#
# This example shows off how to use the find_blobs function to find color
# blobs in the image. This example in particular looks for dark green objects.
import sensor, image, time
import car
from pid import PID
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # 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.
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
green_threshold = (18, 69, 40, 86, 0, 72)
size_threshold = 2300
x_pid = PID(p=0.8, i=0.1, d=0.1,imax=10)#2018年10月26日更新
h_pid = PID(p=0.03, i=0.005,d=0.1, imax=40)
#x_pid = PID(p=0.8, i=0.1, imax=10)
#h_pid = PID(p=0.03, i=0.005, imax=40)
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):
while(True):
pass
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs([green_threshold])
if blobs:
max_blob = find_max(blobs)
x_error = max_blob[5]-img.width()/2
h_error = max_blob[2]*max_blob[3]-size_threshold
if -20