-
Notifications
You must be signed in to change notification settings - Fork 0
/
PID.py
68 lines (55 loc) · 2.43 KB
/
PID.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# PID控制一階慣性系統測試程序
import numpy as np
import pid_GUI
# *****************************************************************#
# 增量式PID系統 #
# *****************************************************************#
class IncrementalPID:
def __init__(self, P, I, D):
self.Kp = P
self.Ki = I
self.Kd = D
self.PIDOutput = 0.0 # PID控制器輸出
self.SystemOutput = 0.0 # 系統輸出值
self.LastSystemOutput = 0.0 # 上次系統輸出值
self.Error = 0.0 # 輸出值與輸入值的偏差
self.LastError = 0.0
self.LastLastError = 0.0
# 設置PID控制器參數
def SetStepSignal(self, StepSignal):
self.Error = StepSignal - self.SystemOutput
IncrementValue = self.Kp * (self.Error - self.LastError) + self.Ki * self.Error + self.Kd * (
self.Error - 2 * self.LastError + self.LastLastError)
self.PIDOutput += IncrementValue
self.LastLastError = self.LastError
self.LastError = self.Error
# 設置一階慣性環節系統 其中InertiaTime為慣性時間常數
def SetInertiaTime(self, InertiaTime, SampleTime):
self.SystemOutput = (InertiaTime * self.LastSystemOutput + SampleTime * self.PIDOutput) / (
SampleTime + InertiaTime)
self.LastSystemOutput = self.SystemOutput
# *****************************************************************#
# 位置式PID系統 #
# *****************************************************************#
class PositionalPID:
def __init__(self, P, I, D):
self.Kp = P
self.Ki = I
self.Kd = D
self.SystemOutput = 0.0
self.ResultValueBack = 0.0
self.PidOutput = 0.0
self.PIDErrADD = 0.0
self.ErrBack = 0.0
def SetInertiaTime(self, InertiaTime, SampleTime):
self.SystemOutput = (InertiaTime * self.ResultValueBack + SampleTime * self.PidOutput) / (
SampleTime + InertiaTime)
self.ResultValueBack = self.SystemOutput
def SetStepSignal(self, StepSignal):
Err = StepSignal - self.SystemOutput
KpWork = self.Kp * Err
KiWork = self.Ki * self.PIDErrADD
KdWork = self.Kd * (Err - self.ErrBack)
self.PidOutput = KpWork + KiWork + KdWork
self.PIDErrADD += Err
self.ErrBack = Err