import time

class PID:
"""PID Controller
""" 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):
"""Clears PID computations and coefficients"""
self.SetPoint = 0.0 self.PTerm = 0.0
self.ITerm = 0.0
self.DTerm = 0.0
self.last_error = 0.0 # Windup Guard
self.int_error = 0.0
self.windup_guard = 20.0 self.output = 0.0 def update(self, feedback_value):
"""Calculates PID value for given reference feedback
.. math::
u(t) = K_p e(t) + K_i \int_{0}^{t} e(t)dt + K_d {de}/{dt}
.. figure:: images/pid_1.png
:align: center
Test PID with Kp=1.2, Ki=1, Kd=0.001 (test_pid.py)
"""
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 # Remember last time and last error for next calculation
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):
"""Determines how aggressively the PID reacts to the current error with setting Proportional Gain"""
self.Kp = proportional_gain def setKi(self, integral_gain):
"""Determines how aggressively the PID reacts to the current error with setting Integral Gain"""
self.Ki = integral_gain def setKd(self, derivative_gain):
"""Determines how aggressively the PID reacts to the current error with setting Derivative Gain"""
self.Kd = derivative_gain def setWindup(self, windup):
"""Integral windup, also known as integrator windup or reset windup,
refers to the situation in a PID feedback controller where
a large change in setpoint occurs (say a positive change)
and the integral terms accumulates a significant error
during the rise (windup), thus overshooting and continuing
to increase as this accumulated error is unwound
(offset by errors in the other direction).
The specific problem is the excess overshooting.
"""
self.windup_guard = windup def setSampleTime(self, sample_time):
"""PID that should be updated at a regular interval.
Based on a pre-determined sampe time, the PID decides if it should compute or return immediately.
"""
self.sample_time = sample_time

Python PID的更多相关文章

  1. 在 Python 中使用 GDB 来调试 转载

    2013/11/01 | Comments 大约一年前,我接触了 Java 中的 Btrace 能够不停机查看线上 JVM 运行情况的特性让我艳羡不已. 另外还有强悍的 jStack 和 jConso ...

  2. 在后台运行Python脚本服务

    在服务器,程序都是后台运行的,当写的python脚本时,需要:   你要是想python robot.py & 是不行的,一旦用户登出,脚本就自动退出了.用at, cron也可以实现不过我发现 ...

  3. GDB: advanced usages

    Sometimes running program in Unix will fail without any debugging info or warnings because of the la ...

  4. js实现无限级分类

    let arr = [ {id:1,name:"php",pid:0}, {id:2,name:"php基础",pid:1}, {id:3,name:" ...

  5. 监控系统信息模块psutil

    About psutil (python system and process utilities) is a cross-platform library for retrieving inform ...

  6. python3.6 新特性学习

    #支持类型提示 typing { def greeting(name: str) -> str: return 'Hello ' + name #在函数greeting中,参数名称的类型为str ...

  7. mysql8安装

    1.先卸载当前系统中已安装的mariadb rpm -qa | grep mariadb rpm -e --nodeps 文件名 2.安装mysql依赖包 yum install gcc gcc-c+ ...

  8. mysql5.6

    5.6 与之后版本有差别本文以5.6为例** 1.mysql5.6安装 本文采用2进制安装 mkdir /server/tools -p cd /server/tools 1.下载 wget http ...

  9. day58:Linux:BashShell&linux文件管理&linux文件下载上传

    目录 1.BashShell 2.Linux文件管理 3.Linux文件下载和上传 BashShell 1.什么是BeshShell? 命令的解释,用来翻译用户输入的指令 2.BashShell能做什 ...

随机推荐

  1. 【Docker】Ubuntu16.04将Docker升级至最新版

    1.使用curl升级到最新版 curl -fsSL https://get.docker.com/ | sh 2.重启Docker sudo systemctl restart docker 3.设置 ...

  2. 假期汇总表handler中涉及的基础知识

    1,stringBuffer 的常用方法,append () 方法, public StringBuffer append(boolean b) 追加内容到当前StringBuffer对象的末尾 ,我 ...

  3. Mysql的SQL优化指北

    概述 在一次和技术大佬的聊天中被问到,平时我是怎么做Mysql的优化的?在这个问题上我只回答出了几点,感觉回答的不够完美,所以我打算整理一次SQL的优化问题. 要知道怎么优化首先要知道一条SQL是怎么 ...

  4. $CF809C\ Find\ a\ car$ 数位$dp$

    正解:数位$dp$ 解题报告: 传送门! 然后因为没有翻译所以先放个翻译$QAQ$ 有一个无穷大的矩阵,第$i$行第$j$列的数是$(i-1)\ xor\ (j-1)+1$,有$q$次询问,每次询问一 ...

  5. MementoPattern(备忘录模式)-----Java/.Net

    备忘录模式(Memento Pattern)保存一个对象的某个状态,以便在适当的时候恢复对象.备忘录模式属于行为型模式.

  6. JS进阶——this绑定了谁?

    一.this的意义 二.寻找this绑定对象 经常听到这么一句话,找this只需要看谁是调用方.当函数被调用时会记录函数调用调用方式.传参包括this等各种属性.有时候this绑定对象情况太抽象,找到 ...

  7. NB的程序员,亮瞎了你的眼吗?

    郑重声明: 本文首发于人工博客 1.导读 你能想象到1K的代码能写出什么样的功能强大.效果炫酷的作品吗?来吧,今天小编带领大家认识下下面这位大神的作品. 西班牙程序员Roman Cortes用纯Jav ...

  8. Linux安装MySQL及基本操作(Centos)

    安装: 系统:CentOS-7-x86_64-DVD-1810.iso 安装命令: wget http://repo.mysql.com/mysql-community-release-el7-5.n ...

  9. kubelet--help-v1.15.4

    kubelet --help 官方文档   The kubelet is the primary "node agent" that runs on each node. It c ...

  10. Android/Unity大乱斗-完整双方集成交互指南

    这是一个很长很长的story!-芝麻粒儿创作 开篇 源码地址:GitHub 本文目的,将Unity集成到Android端,学完本文后你可以做到 Android任意布局加载Unity 3D场景 任意操作 ...