class _pid:
def __init__(self,setSpeed,actualSpeed,err,err_last,Kp,Ki,Kd,voltage,intergral):
self.setSpeed = setSpeed;
self.actualSpeed = actualSpeed;
self.err = err;
self.err_last = err_last;
self.Kp = Kp;
self.Ki = Ki;
self.Kd = Kd;
self.voltage = voltage;
self.intergral = intergral;
def PID_init():
pass;
def PID_realize(speed):
pID.setSpeed = speed;
pID.err = pID.setSpeed-pID.actualSpeed;
pID.intergral += pID.err;
pID.voltage = pID.Kp * pID.err + pID.Ki * pID.intergral + pID.Kd * (pID.err-pID.err_last);
pID.err_last = pID.err;
pID.actualSpeed = pID.voltage * 1.0;
return pID.actualSpeed;
if __name__ == '__main__':
pID = _pid(0.0, 0.0, 0.0, 0.0, 0.2, 0.015, 0.2, 0.0, 0.0);
count = 0;
while(count<450):
speed = PID_realize(200.0);
count = count+1;
print(speed)
最基本的算法实现形式,没有考虑死区问题,没有设定上下限,只是对公式的一种直接的实现。