增量式pid算法python
时间: 2023-10-24 17:33:30 浏览: 125
增量式PID算法的Python实现如下所示:
```python
class Pid():
def __init__(self, exp_val, kp, ki, kd):
self.KP = kp
self.KI = ki
self.KD = kd
self.exp_val = exp_val
self.now_val = 0
self.sum_err = 0
self.now_err = 0
self.last_err = 0
def cmd_pid(self):
self.last_err = self.now_err
self.now_err = self.exp_val - self.now_val
self.sum_err += self.now_err
# 增量式PID控制算法
delta_val = self.KP * (self.now_err - self.last_err) + self.KI * self.now_err + self.KD * (self.now_err - 2 * self.last_err + self.sum_err)
self.now_val += delta_val
return self.now_val
pid_val = []
my_Pid = Pid(1000, 0.1, 0.15, 0.01)
for i in range(0, 100):
pid_val.append(my_Pid.cmd_pid())
plt.plot(pid_val)
plt.show()
```
这段代码实现了增量式PID控制算法。在PID类的`cmd_pid`方法中,根据增量式PID控制算法的公式进行计算,得到控制器的输出值。然后通过循环迭代,在每次迭代中将输出值存入数组中。最后,使用matplotlib库将数组中的值绘制成曲线图显示出来。
阅读全文