Python PID
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的更多相关文章
- 在 Python 中使用 GDB 来调试 转载
2013/11/01 | Comments 大约一年前,我接触了 Java 中的 Btrace 能够不停机查看线上 JVM 运行情况的特性让我艳羡不已. 另外还有强悍的 jStack 和 jConso ...
- 在后台运行Python脚本服务
在服务器,程序都是后台运行的,当写的python脚本时,需要: 你要是想python robot.py & 是不行的,一旦用户登出,脚本就自动退出了.用at, cron也可以实现不过我发现 ...
- GDB: advanced usages
Sometimes running program in Unix will fail without any debugging info or warnings because of the la ...
- js实现无限级分类
let arr = [ {id:1,name:"php",pid:0}, {id:2,name:"php基础",pid:1}, {id:3,name:" ...
- 监控系统信息模块psutil
About psutil (python system and process utilities) is a cross-platform library for retrieving inform ...
- python3.6 新特性学习
#支持类型提示 typing { def greeting(name: str) -> str: return 'Hello ' + name #在函数greeting中,参数名称的类型为str ...
- mysql8安装
1.先卸载当前系统中已安装的mariadb rpm -qa | grep mariadb rpm -e --nodeps 文件名 2.安装mysql依赖包 yum install gcc gcc-c+ ...
- mysql5.6
5.6 与之后版本有差别本文以5.6为例** 1.mysql5.6安装 本文采用2进制安装 mkdir /server/tools -p cd /server/tools 1.下载 wget http ...
- day58:Linux:BashShell&linux文件管理&linux文件下载上传
目录 1.BashShell 2.Linux文件管理 3.Linux文件下载和上传 BashShell 1.什么是BeshShell? 命令的解释,用来翻译用户输入的指令 2.BashShell能做什 ...
随机推荐
- ASP.NET WebForm Ajax请求Handler的经验
ajax代码 $.ajax({ type: "GET", url: "/AjaxHandler/GetPluginCode.ashx", data: " ...
- 使用原生JDBC方式对数据库进行操作
使用原生JDBC方式对数据库进行操作,包括六个步骤: 1.加载JDBC驱动程序 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM.可以通过java.lang.Class类的静态方法forNa ...
- System类StringBuilder小结
- 对sql server查询速度的优化
处理百万级以上的数据提高查询速度的方法: 1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考 ...
- Python综合应用:教你用字符打印一张怀旧风格的照片
1. 前言第一次在学校机房里见到计算机,还是上古时期.计算机型号大概是LASER-310吧,有点记不清了.那会儿,显示器还是单色的,只能显示文本,每行最多显示80个字符.想看图片,印象中只能用针式打印 ...
- 探索 模块打包 exports和require 与 export和import 的用法和区别
菜单快捷导航: CommonJS 之 exports和require用法 ES6 Module 之 export 和 import 用法 CommonJS和ES6 Module的区别 循环依赖 和 解 ...
- Django 项目目录重构
原因 一个完整的项目下来, 会涉及很多模块, 文件和资源, 对Django默认的文件目录结构基础上进行重构, 会使得我们的项目结构更加清晰, 便于后期管理 重构 """ ...
- Linux 下解压.tar.gz文件报错 gzip:stdin:not in gzip format 的解决办法!
[root@hzp124 opt]# tar xzvf 1577255462-qypt.tar gzip: stdin: not in gzip formattar: Child returned s ...
- 【转载】解决KindEditor图片上传对话框位置异常问题(浏览器放大缩小时对话框不见了)
今早在整理文件上传模块的时候,发现富文本编辑器 kindeditor 上传图片的对话框无法显示,其实对话框已经生成了,但是它没有top值,所以在页面上看不见. 捣鼓了一个多小时,代码看了一大串,没解决 ...
- 图解kubernetes scheduler基于map/reduce模式实现优选阶段
优选阶段通过分map/reduce模式来实现多个node和多种算法的并行计算,并且通过基于二级索引来设计最终的存储结果,从而达到整个计算过程中的无锁设计,同时为了保证分配的随机性,针对同等优先级的采用 ...