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. C++Review7_STL、容器、迭代器

    我之前的博文中有专门的5篇整理并介绍了STL的概念: STL1——整体介绍:https://www.cnblogs.com/grooovvve/p/10467794.html STL2——泛型编程(模 ...

  2. Cortex-A9 Timing

    在Cortex-A9的文档<Cortex-A9 NEON Media Processing Engine>Technical Reference Manual中有关于Instruction ...

  3. 前端——JS

    目录 JavaScript概述 ECMAScript和JavaScript的关系 ECMAScript的历史 JavaScript引入方式 Script标签内写代码 引入额外的JS文件 JavaScr ...

  4. redis 本地安装

    1.redis介绍 Redis是有名的NoSql数据库,一般Linux都会默认支持.但在Windows环境中,可能需要手动安装设置才能有效使用.简单介绍一下Windows下Redis服务的安装方法. ...

  5. 侠说java8-行为参数化(开山篇)

    啥是行为参数化 行为参数化的本质是不执行复杂的代码块,让逻辑清晰可用. 相信使用过js的你肯定知道,js是可以传递函数的,而在 java中也有类似的特性,那就是匿名函数. 理解:行为参数化是一种方法, ...

  6. Eclipse直接运行算法第4版例子(重定向和读取指定路径文件)

    Eclipse直接运行算法第4版例子(重定向和读取指定路径文件)   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://b ...

  7. C++ | C++ 基础知识 | 指针、数组与引用

    1.指针 在 C++ 语言中存放及使用内存地址是通过指针和引用完成的. char c = 'a'; // 声明 c 变量,c 变量存储的是 'a' 的值. char* p = &c; // 声 ...

  8. JS获取时间戳的几种方法与区别

    var today = new Date() Date.now()) Date.parse(today) today.valueOf()) today.getTime() 可以看出,第二种是精确到秒的 ...

  9. Java项目之客户信息管理软件

    模拟实现基于文本界面的客户信息管理软件,该软件能够实现对客户对象的插入. 修改和删除(用数组实现),并能够打印客户明细表. 项目采用分级菜单方式.主菜单如下: “添加客户”的界面及操作过程如下所示: ...

  10. Android学习进度一

    在解决了电脑产生的一系列问题之后成功安装了Android Studio,并在其自带的手机模拟器上成功运行了第一个App(Hello World!),通过这个最简单的App研究了App基本的工程结构,为 ...