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能做什 ...
随机推荐
- CodeForces - 786B -- 线段树优化建图
刚开始想了两个小时,打算把区间分块然后计算,但是这就很灵性了看了一个大佬的博客,侵删 #include<cstring> #include<iostream> #include ...
- 【他山之石】jenkins忘记初始化密码解决办法
没有太好的方式,网上有的是这样子的,找到 /var/lib/jenkins/users/username/config.xml, 修改为一个已知的 hash 值 #jbcrypt:$2a$10$Dda ...
- Git是什么?
Git是什么? Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控 ...
- 使用ASP.NET Core 3.x 构建 RESTful API - 4.3 HTTP 方法的安全性和幂等性
什么样的HTTP方法是安全的? 如果一个方法不会该表资源的表述,那么这个方法就被认为是安全的. 例如 HTTP GET 和 HTTP HEAD 就被认为是安全的,但需要注意的是,这并不意味着执行GET ...
- JVM性能优化系列-(1) Java内存区域
1. Java内存区域 1.1 运行时数据区 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.主要包括:程序计数器.虚拟机栈.本地方法栈.Java堆.方法区(运 ...
- Oracle 数据泵expdq,impdq
使用数据泵技术实现逻辑备份 数据泵概述 数据泵(DATA PUMP)是一种在数据库之间.数据库与操作系统之间,高速传输数据的技术(10g推出). 逻辑备份概述 逻辑备份是对数据库对象(如用户.表.存储 ...
- 实操教程丨如何在K8S集群中部署Traefik Ingress Controller
注:本文使用的Traefik为1.x的版本 在生产环境中,我们常常需要控制来自互联网的外部进入集群中,而这恰巧是Ingress的职责. Ingress的主要目的是将HTTP和HTTPS从集群外部暴露给 ...
- OpenJ_Bailian 4103 踩方格(搜索 动态规划 )
题目传送门OpenJ_Bailian 4103 描述 有一个方格矩阵,矩阵边界在无穷远处.我们做如下假设:a. 每走一步时,只能从当前方格移动一格,走到某个相邻的方格上:b. 走过的格子立 ...
- docker发布.net core程序的坑
docker发布遇到的两个问题 1:Could not resolve CoreCLR path. For more details, enable tracing by setting COREHO ...
- 【记】创建 VirtualBoxClient COM 对象失败. 应用程序将被中断
1. 在本地64位win7系统安装VirtualBox完,启动时提示错误 原因:兼容性造成的 按照下图显示修改VirtualBox快捷方式的兼容性 2. 启动虚拟机时,提示 点击弹出框的确定按钮后,接 ...