利用增量式PID
并利用Python中的matplotlib库输出打印
import matplotlib.pyplot as plt
class Pid:
def __init__(self, target, p, i, d):
self.target = target
self.kp = p
self.ki = i
self.kd = d
self.target_val = target
self.now_val = 0
self.error_new = 0
self.error_last = 0
self.error_last_last = 0
self.d = 0
def myfunc(self):
self.error_last_last = self.error_last
self.error_last = self.error_new
self.error_new = self.target_val - self.now_val
self.d = self.kp * (self.error_new - self.error_last) + self.ki * self.error_new + self.kd * (self.error_new - 2 * self.error_last + self.error_last_last )
self.now_val += self.d
return self.now_val
p = Pid(100, 0.02, 0.4, 0)
val = []
for i in range(0, 100):
val.append(p.myfunc())
plt.plot(val)
plt.grid()
plt.xlim(0, 100)
plt.show()
输出图像: