-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathadrc.py
62 lines (48 loc) · 2.05 KB
/
adrc.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
"""
Active Disturbance Rejection Control (ADRC) is a robust control strategy
that estimates and compensates for disturbances in real-time without needing
an explicit mathematical model of the system.
It consists of:
1. Tracking Differentiator (TD) - Smooths the reference signal
2. Extended State Observer (ESO) - Estimates system states and disturbances
3. Nonlinear State Error Feedback (NLSEF) - Generates the control signal
Refer - https://en.wikipedia.org/wiki/Active_disturbance_rejection_control
"""
class ADRC:
def __init__(self, beta1: float, beta2: float, beta3: float, setpoint: float = 0.0):
"""
Initialize the ADRC controller.
:param beta1: Gain for error correction in ESO
:param beta2: Gain for disturbance estimation in ESO
:param beta3: Gain for acceleration estimation in ESO
:param setpoint: Desired target value
"""
self.beta1 = beta1
self.beta2 = beta2
self.beta3 = beta3
self.setpoint = setpoint
self.z1 = 0.0 # Estimated system output
self.z2 = 0.0 # Estimated system velocity
self.z3 = 0.0 # Estimated total disturbance
def compute(self, measured_value: float, dt: float) -> float:
"""
Compute the control signal based on error estimation and disturbance rejection.
:param measured_value: The current process variable
:param dt: Time difference since the last update
:return: Control output
"""
# Extended State Observer (ESO) Update
self.z1 += dt * (self.z2 - self.beta1 * (self.z1 - measured_value))
self.z2 += dt * (self.z3 - self.beta2 * (self.z1 - measured_value))
self.z3 -= self.beta3 * (self.z1 - measured_value)
# Control Law (Nonlinear State Error Feedback - NLSEF)
control_output = self.z2 - self.z3
return control_output
def reset(self):
"""Reset the estimated states."""
self.z1 = 0.0
self.z2 = 0.0
self.z3 = 0.0
if __name__ == "__main__":
import doctest
doctest.testmod()