python基礎之 python實現PID演算法及測試的例子
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)
得出結果
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69945560/viewspace-2680034/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python介面自動化測試之python基礎語法Python
- python+requests介面測試基礎Python
- 自動化測試基礎之Python常見問題Python
- 【圖論】Python [ numpy, pandas] 實現 基礎能力以及基礎演算法 [ dfs bfs spfa ] 經過較為嚴格測試圖論Python演算法
- Python基礎之openpyxl如何實現vlookup函式Python函式
- Pytest 實踐:Python 測試技術基礎知識Python
- 神經網路理論基礎及 Python 實現神經網路Python
- 內部UI自動化測試培訓之python基礎UIPython
- fixtrue基礎之測試初始化及測試後清理操作
- Python測試框架pytest入門基礎Python框架
- Python基礎之:Python中的類Python
- Python基礎之:Python中的IOPython
- Python基礎之模組Python
- python基礎之字串Python字串
- 基於Python實現的口罩佩戴檢測Python
- 實現Python壓力測試工具|Python 主題月Python
- Python基礎之:Python中的內部物件Python物件
- Python基礎之:Python的資料結構Python資料結構
- Python基礎之:Python中的流程控制Python
- Python基礎之(三)之字典Python
- Python基礎之Python資料世界Python
- 軟體測試學習教程——【大蟒蛇】python基礎Python
- 基於Python的測試驅動開發實戰Python
- 【深度學習基礎-12】多元迴歸分析基礎及進階-python程式碼實現深度學習Python
- Python實現工廠模式的兩個例子Python模式
- python 基礎之檔案Python
- python 基礎之物件管理Python物件
- python基礎之函式Python函式
- Python之matplotlib基礎Python
- Python 的單元測試之 unittestPython
- python自動化測試之異常及日誌Python
- 移動 APP 測試之基礎功能測試流程APP
- 介面測試之基礎知識
- Python基礎之四:Python3 基礎資料型別Python資料型別
- python基礎之資料型別及相關方法Python資料型別
- 機器學習之kNN演算法(純python實現)機器學習KNN演算法Python
- Python演算法:基礎知識Python演算法
- 基於Python的滲透測試資訊收集系統的設計和實現Python