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 相关文章推荐
node.js获取参数的常用方法(总结)
May 29 Python
python中使用正则表达式的连接符示例代码
Oct 10 Python
Python Nose框架编写测试用例方法
Oct 26 Python
Python Unittest自动化单元测试框架详解
Apr 04 Python
Python3基于sax解析xml操作示例
May 22 Python
Sanic框架异常处理与中间件操作实例分析
Jul 16 Python
python打包生成的exe文件运行时提示缺少模块的解决方法
Oct 31 Python
解决Python3下map函数的显示问题
Dec 04 Python
python实现高斯判别分析算法的例子
Dec 09 Python
Win下PyInstaller 安装和使用教程
Dec 25 Python
python 8种必备的gui库
Aug 27 Python
Python读取和写入Excel数据
Apr 20 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
第九节--绑定
2006/11/16 PHP
php获取后台Job管理的实现代码
2011/06/10 PHP
php cli模式学习(PHP命令行模式)
2013/06/03 PHP
在Linux系统的服务器上隐藏PHP版本号的方法
2015/06/06 PHP
Ajax+PHP实现的分类列表框功能示例
2019/02/11 PHP
jquery里的正则表达式说明
2011/08/03 Javascript
jQuery 快速结束当前正在执行的动画
2013/11/20 Javascript
js实现简单折叠、展开菜单的方法
2015/08/28 Javascript
js+html5实现的自由落体运动效果代码
2016/01/28 Javascript
全面了解js中的script标签
2016/07/04 Javascript
JavaScript职责链模式概述
2016/09/17 Javascript
js完整倒计时代码分享
2016/09/18 Javascript
JavaScript事件用法浅析
2016/10/31 Javascript
JS使用ActiveXObject实现用户提交表单时屏蔽敏感词功能
2017/06/20 Javascript
scrapyd schedule.json setting 传入多个值问题
2019/08/07 Javascript
js基础之事件捕获与冒泡原理
2019/10/09 Javascript
javascript 对象 与 prototype 原型用法实例分析
2019/11/11 Javascript
Python贪吃蛇游戏编写代码
2020/10/26 Python
python使用logging模块发送邮件代码示例
2018/01/18 Python
python控制nao机器人身体动作实例详解
2019/04/29 Python
Pandas之DataFrame对象的列和索引之间的转化
2019/06/25 Python
python GUI库图形界面开发之PyQt5美化窗体与控件(异形窗体)实例
2020/02/25 Python
基于OpenCV的路面质量检测的实现
2020/11/04 Python
HTML5之SVG 2D入门8—文档结构及相关元素总结
2013/01/30 HTML / CSS
英国高街奥特莱斯:Highstreet Outlet
2019/11/21 全球购物
信息部岗位职责
2013/11/12 职场文书
客服工作职责
2013/12/11 职场文书
《大禹治水》教学反思
2014/04/27 职场文书
大班上学期幼儿评语
2014/04/30 职场文书
体育节口号
2014/06/19 职场文书
学校标语大全
2014/06/19 职场文书
医生个人自我剖析材料
2014/10/08 职场文书
幼儿园五一劳动节活动总结
2015/02/09 职场文书
同意转租证明
2015/06/24 职场文书
大队委员竞选演讲稿
2015/11/20 职场文书
Nginx反向代理配置的全过程记录
2021/06/22 Servers