mirror of
https://github.com/WallabyLester/RBF-aPID-Controller.git
synced 2026-05-31 19:46:46 +08:00
Created PID controller class for tensorflow implementation
This commit is contained in:
+22
-22
@@ -1,44 +1,44 @@
|
|||||||
import numpy as np
|
import tensorflow as tf
|
||||||
|
|
||||||
class AdaptivePIDNP:
|
class AdaptivePIDTf:
|
||||||
""" PID class implemented for numpy integration.
|
""" PID class implemented for TensorFlow integration.
|
||||||
|
|
||||||
...
|
...
|
||||||
|
|
||||||
Attributes
|
Attributes
|
||||||
----------
|
----------
|
||||||
Kp : float64
|
Kp : float
|
||||||
Proportional gain.
|
Proportional gain.
|
||||||
Ki : float64
|
Ki : float
|
||||||
Integral gain.
|
Integral gain.
|
||||||
Kd : float64
|
Kd : float
|
||||||
Derivative gain.
|
Derivative gain.
|
||||||
rbf_network : RBFNetwork object
|
rbf_model : RBFAdaptiveModel object
|
||||||
RBF network class instance.
|
RBF adaptive model class instance.
|
||||||
|
|
||||||
Methods
|
Methods
|
||||||
-------
|
-------
|
||||||
update(target, measured_value, dt):
|
update(target, measured_value, dt):
|
||||||
Updates the control signal.
|
Updates the control signal.
|
||||||
"""
|
"""
|
||||||
def __init__(self, Kp, Ki, Kd, rbf_network):
|
def __init__(self, Kp, Ki, Kd, rbf_model):
|
||||||
""" Constructs PID gains and RBF network.
|
""" Constructs PID gains, RBF model, and initial PID components.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
Kp : float64
|
Kp : float
|
||||||
Proportional gain.
|
Proportional gain.
|
||||||
Ki : float64
|
Ki : float
|
||||||
Integral gain.
|
Integral gain.
|
||||||
Kd : float64
|
Kd : float
|
||||||
Derivative gain.
|
Derivative gain.
|
||||||
rbf_network : RBFNetwork object
|
rbf_model : RBFAdaptiveModel object
|
||||||
RBF network class instance.
|
RBF adaptive model class instance.
|
||||||
"""
|
"""
|
||||||
self.Kp = Kp
|
self.Kp = Kp
|
||||||
self.Ki = Ki
|
self.Ki = Ki
|
||||||
self.Kd = Kd
|
self.Kd = Kd
|
||||||
self.rbf_network = rbf_network
|
self.rbf_model = rbf_model
|
||||||
self.prev_err = 0
|
self.prev_err = 0
|
||||||
self.error = 0
|
self.error = 0
|
||||||
self.integral = 0
|
self.integral = 0
|
||||||
@@ -46,15 +46,15 @@ class AdaptivePIDNP:
|
|||||||
|
|
||||||
def update(self, target, measured_value, dt):
|
def update(self, target, measured_value, dt):
|
||||||
""" Update the control signal according to error and adapt with RBF
|
""" Update the control signal according to error and adapt with RBF
|
||||||
network predictions.
|
model predictions.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
target : float64
|
target : float
|
||||||
Target setpoint.
|
Target setpoint.
|
||||||
measured_value : float64
|
measured_value : float
|
||||||
Actual value.
|
Actual value.
|
||||||
dt : float64
|
dt : float
|
||||||
Timestep.
|
Timestep.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
@@ -67,8 +67,8 @@ class AdaptivePIDNP:
|
|||||||
|
|
||||||
u = (self.Kp * self.error) + (self.Ki * self.integral) + (self.Kd*self.derivative)
|
u = (self.Kp * self.error) + (self.Ki * self.integral) + (self.Kd*self.derivative)
|
||||||
|
|
||||||
gain_adapt = self.rbf_network.predict(np.array([self.error, self.integral, self.derivative]))
|
control_signal_adapt = self.rbf_model(tf.constant([[self.error, self.integral, self.derivative]])).numpy().flatten()[0]
|
||||||
u += gain_adapt
|
u += control_signal_adapt
|
||||||
|
|
||||||
self.prev_err = self.error
|
self.prev_err = self.error
|
||||||
return u
|
return u
|
||||||
Reference in New Issue
Block a user