亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python實現PID算法及測試的例子

發布時間:2020-10-18 06:41:40 來源:腳本之家 閱讀:572 作者:qq3163438396 欄目:開發技術

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)

結果

python實現PID算法及測試的例子

以上這篇python實現PID算法及測試的例子就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

东宁县| 安塞县| 古丈县| 崇文区| 佛冈县| 古交市| 绿春县| 蓬溪县| 淄博市| 汕尾市| 马鞍山市| 乾安县| 宁河县| 灌阳县| 乳山市| 三门县| 固原市| 太康县| 临泽县| 山西省| 政和县| 宜丰县| 铜川市| 华阴市| 榆中县| 平和县| 惠州市| 万州区| 阿坝县| 马尔康县| 沐川县| 雅江县| 汉川市| 阜阳市| 保定市| 临夏县| 中山市| 旌德县| 高台县| 桐柏县| 清水河县|