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


Posted in Python onAugust 08, 2019

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算法及测试的例子

以上这篇python实现PID算法及测试的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现的一只从百度开始不断搜索的小爬虫
Aug 13 Python
python中os模块详解
Oct 14 Python
Python计算时间间隔(精确到微妙)的代码实例
Feb 26 Python
Python学习笔记基本数据结构之序列类型list tuple range用法分析
Jun 08 Python
Puppeteer使用示例详解
Jun 20 Python
详解使用python绘制混淆矩阵(confusion_matrix)
Jul 14 Python
PyCharm 2019.3发布增加了新功能一览
Dec 08 Python
Django 后台带有字典的列表数据与页面js交互实例
Apr 03 Python
python将音频进行变速的操作方法
Apr 08 Python
pycharm的python_stubs问题
Apr 08 Python
python破解同事的压缩包密码
Oct 14 Python
Django Admin后台模型列表页面如何添加自定义操作按钮
Nov 11 Python
python开头的coding设置方法
Aug 08 #Python
pycharm 安装JPype的教程
Aug 08 #Python
Python学习笔记之lambda表达式用法详解
Aug 08 #Python
python读取大文件越来越慢的原因与解决
Aug 08 #Python
Python实现Singleton模式的方式详解
Aug 08 #Python
Python判断字符串是否xx开始或结尾的示例
Aug 08 #Python
详解解决Python memory error的问题(四种解决方案)
Aug 08 #Python
You might like
php5编程中的异常处理详细方法介绍
2008/07/29 PHP
解析csv数据导入mysql的方法
2013/07/01 PHP
使用PHP强制下载PDF文件示例
2014/01/17 PHP
php多种形式发送邮件(mail qmail邮件系统 phpmailer类)
2014/01/22 PHP
PHP whois查询类定义与用法示例
2019/04/03 PHP
thinkphp 5框架实现登陆,登出及session登陆状态检测功能示例
2019/10/10 PHP
jquery获取css中的选择器(实例讲解)
2013/12/02 Javascript
JQuery中使用on方法绑定hover事件实例
2014/12/09 Javascript
jquery简单实现网页层的展开与收缩效果
2015/08/07 Javascript
任意Json转成无序列表的方法示例
2016/12/09 Javascript
Node.js 8 中的重要新特性
2017/06/28 Javascript
React中常见的动画实现的几种方式
2018/01/10 Javascript
Router解决跨模块下的页面跳转示例
2018/01/11 Javascript
vue-better-scroll 的使用实例代码详解
2018/12/03 Javascript
如何为你的JS项目添加智能提示与类型检查详解
2019/03/12 Javascript
微信小程序实现时间戳格式转换
2020/07/20 Javascript
使用Python的web.py框架实现类似Django的ORM查询的教程
2015/05/02 Python
python求解水仙花数的方法
2015/05/11 Python
Python3通过Luhn算法快速验证信用卡卡号的方法
2015/05/14 Python
Python实现识别手写数字 Python图片读入与处理
2020/03/23 Python
python 创建一个空dataframe 然后添加行数据的实例
2018/06/07 Python
numpy.ndarray 交换多维数组(矩阵)的行/列方法
2018/08/02 Python
Python处理session的方法整理
2019/08/29 Python
python + selenium 刷B站播放量的实例代码
2020/06/12 Python
简单了解Django项目应用创建过程
2020/07/06 Python
Keras预训练的ImageNet模型实现分类操作
2020/07/07 Python
解决python pandas读取excel中多个不同sheet表格存在的问题
2020/07/14 Python
html5中localStorage本地存储的简单使用
2017/06/16 HTML / CSS
HTML5实现自带进度条和滑块滑杆效果
2018/04/17 HTML / CSS
RentCars.com巴西:汽车租赁网站
2016/08/22 全球购物
意大利买卖二手奢侈品网站:LAMPOO
2020/06/03 全球购物
main 主函数执行完毕后,是否可能会再执行一段代码,给出说明
2012/12/05 面试题
SQL SERVER面试资料
2013/03/30 面试题
专业实习自我鉴定
2013/10/29 职场文书
大学毕业感言100字
2014/02/03 职场文书
Css预编语言及区别详解
2021/04/25 HTML / CSS