python實現(xiàn)PID算法及測試的例子
更新時間:2019年08月08日 11:53:31 作者:qq3163438396
今天小編就為大家分享一篇python實現(xiàn)PID算法及測試的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
PID算法實現(xiàn)
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 #這個程序的實質(zhì)就是在前九秒保持零輸出,在后面的操作中在傳遞函數(shù)為某某的系統(tǒng)中輸出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))控制系統(tǒng)的函數(shù) 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)
結(jié)果
以上這篇python實現(xiàn)PID算法及測試的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
python利用openpyxl拆分多個工作表的工作簿的方法
這篇文章主要介紹了python利用openpyxl拆分多個工作表的工作簿的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09opencv深入淺出了解機(jī)器學(xué)習(xí)和深度學(xué)習(xí)
機(jī)器學(xué)習(xí)是人工智能的核心,專門研究如何讓計算機(jī)模擬和學(xué)習(xí)人類的行為。?深度學(xué)習(xí)是機(jī)器學(xué)習(xí)中的一個熱門研究方向,它主要研究樣本數(shù)據(jù)的內(nèi)在規(guī)律和表示層次,讓計算機(jī)能夠讓人一樣具有分析與學(xué)習(xí)能力2022-03-03使用Python3?Boto3包刪除AWS?CloudFormation的棧(Stacks)
這篇文章主要介紹了如何使用Python3?Boto3刪除AWS?CloudFormation的棧(Stacks),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-01-01tesseract庫及訓(xùn)練數(shù)據(jù)下載安裝方式
這篇文章主要介紹了tesseract庫及訓(xùn)練數(shù)據(jù)下載安裝方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02tensorflow模型繼續(xù)訓(xùn)練 fineturn實例
今天小編就為大家分享一篇tensorflow模型繼續(xù)訓(xùn)練 fineturn實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python GUI庫圖形界面開發(fā)之PyQt5布局控件QHBoxLayout詳細(xì)使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5布局控件QHBoxLayout詳細(xì)使用方法與實例,需要的朋友可以參考下2020-03-03