python实现PID算法及测试的例子

作者:qq3163438396 时间:2021-08-30 00:57:33 

PID算法实现


import time

class PID:
 def __init__(self, P=0.2, I=0.0, D=0.0):
   self.Kp = P
   self.Ki = I
   self.Kd = D
   self.sample_time = 0.00
   self.current_time = time.time()
   self.last_time = self.current_time
   self.clear()
 def clear(self):
   self.SetPoint = 0.0
   self.PTerm = 0.0
   self.ITerm = 0.0
   self.DTerm = 0.0
   self.last_error = 0.0
   self.int_error = 0.0
   self.windup_guard = 20.0
   self.output = 0.0
 def update(self, feedback_value):
   error = self.SetPoint - feedback_value
   self.current_time = time.time()
   delta_time = self.current_time - self.last_time
   delta_error = error - self.last_error
   if (delta_time >= self.sample_time):
     self.PTerm = self.Kp * error#比例
     self.ITerm += error * delta_time#积分
     if (self.ITerm < -self.windup_guard):
       self.ITerm = -self.windup_guard
     elif (self.ITerm > self.windup_guard):
       self.ITerm = self.windup_guard
     self.DTerm = 0.0
     if delta_time > 0:
       self.DTerm = delta_error / delta_time
     self.last_time = self.current_time
     self.last_error = error
     self.output = self.PTerm + (self.Ki * self.ITerm) + (self.Kd * self.DTerm)
 def setKp(self, proportional_gain):
   self.Kp = proportional_gain
 def setKi(self, integral_gain):
   self.Ki = integral_gain
 def setKd(self, derivative_gain):
   self.Kd = derivative_gain
 def setWindup(self, windup):
   self.windup_guard = windup
 def setSampleTime(self, sample_time):
   self.sample_time = sample_time

测试PID算法


import PID
import time
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import spline
#这个程序的实质就是在前九秒保持零输出,在后面的操作中在传递函数为某某的系统中输出1

def test_pid(P = 0.2, I = 0.0, D= 0.0, L=100):
 """Self-test PID class

.. note::
   ...
   for i in range(1, END):
     pid.update(feedback)
     output = pid.output
     if pid.SetPoint > 0:
       feedback += (output - (1/i))
     if i>9:
       pid.SetPoint = 1
     time.sleep(0.02)
   ---
 """
 pid = PID.PID(P, I, D)

pid.SetPoint=0.0
 pid.setSampleTime(0.01)

END = L
 feedback = 0

feedback_list = []
 time_list = []
 setpoint_list = []

for i in range(1, END):
   pid.update(feedback)
   output = pid.output
   if pid.SetPoint > 0:
     feedback +=output# (output - (1/i))控制系统的函数
   if i>9:
     pid.SetPoint = 1
   time.sleep(0.01)

feedback_list.append(feedback)
   setpoint_list.append(pid.SetPoint)
   time_list.append(i)

time_sm = np.array(time_list)
 time_smooth = np.linspace(time_sm.min(), time_sm.max(), 300)
 feedback_smooth = spline(time_list, feedback_list, time_smooth)
 plt.figure(0)
 plt.plot(time_smooth, feedback_smooth)
 plt.plot(time_list, setpoint_list)
 plt.xlim((0, L))
 plt.ylim((min(feedback_list)-0.5, max(feedback_list)+0.5))
 plt.xlabel('time (s)')
 plt.ylabel('PID (PV)')
 plt.title('TEST PID')

plt.ylim((1-0.5, 1+0.5))

plt.grid(True)
 plt.show()

if __name__ == "__main__":
 test_pid(1.2, 1, 0.001, L=80)
#  test_pid(0.8, L=50)

结果

python实现PID算法及测试的例子

来源:https://blog.csdn.net/qq_26657001/article/details/83147510

标签:python,PID,算法
0
投稿

猜你喜欢

  • try finally 妙用,防止内存泄漏

    2008-03-26 12:52:00
  • Python的getattr函数方法学习使用示例

    2021-10-18 13:43:25
  • 简单谈谈Python中的模块导入

    2021-02-24 20:28:17
  • Oracle自定义脱敏函数的代码详解

    2024-01-26 17:33:46
  • Pytorch实现的手写数字mnist识别功能完整示例

    2022-10-15 23:38:22
  • php实现mysql同步的实现方法

    2023-11-24 13:58:56
  • 浅谈numpy数组的几种排序方式

    2022-04-24 12:48:15
  • 人工智能学习pyTorch自建数据集及可视化结果实现过程

    2022-08-04 14:54:33
  • 详解Python3.1版本带来的核心变化

    2021-02-14 01:31:00
  • python plotly画柱状图代码实例

    2023-11-29 13:41:31
  • Python趣味挑战之教你用pygame画进度条

    2022-08-13 15:02:49
  • Python 打印中文字符的三种方法

    2022-11-14 10:22:07
  • js中位数不足自动补位扩展padLeft、padRight实现代码

    2024-04-19 10:46:42
  • python配置mssql连接的方法

    2021-05-12 08:03:28
  • js动态创建、删除表格示例代码

    2024-04-19 09:54:37
  • Python获取CPU、内存使用率以及网络使用状态代码

    2023-03-14 22:43:36
  • .Net行为型设计模式之策略模式(Stragety)

    2024-05-13 09:18:07
  • python爬虫中PhantomJS加载页面的实例方法

    2021-09-12 09:44:33
  • vue权限路由实现的方法示例总结

    2024-06-05 15:29:41
  • 十大热门的JavaScript框架和库

    2024-05-02 17:25:36
  • asp之家 网络编程 m.aspxhome.com