我有 2 个带有值的 txt 文件,一个带有点 1 的 x 坐标,一个带有点 2 的 x 坐标,点 2 需要跟随点 1,在通过调节器运行点之间的差异后,我需要获得点 2 的速度由于系统是离散的,因此我们假设每 8ms 调节一次调节器
简而言之:从point1和point2的txt读取行通过调节器对point2的写入速度,每8ms或更慢,没关系
点的坐标逐行列在 txt 文件中,我已经有调节器的代码(如下),但不知道如何使用 txt 文件来执行此操作,因为我刚开始使用 python
class PID:
"""
Discrete PID control
"""
def __init__(self, P=1.3, I=0.3, D=0.0, Derivator=0, Integrator=0, Integrator_max=500, Integrator_min=-500):
self.Kp=P
self.Ki=I
self.Kd=D
self.Derivator=Derivator
self.Integrator=Integrator
self.Integrator_max=Integrator_max
self.Integrator_min=Integrator_min
self.set_point=0.0
self.error=0.0
def update(self,current_value):
"""
Calculate PID output value for given reference input and feedback
"""
self.error = self.set_point - current_value
self.P_value = self.Kp * self.error
self.D_value = self.Kd * ( self.error - self.Derivator)
self.Derivator = self.error
self.Integrator = self.Integrator + self.error
if self.Integrator > self.Integrator_max:
self.Integrator = self.Integrator_max
elif self.Integrator < self.Integrator_min:
self.Integrator = self.Integrator_min
self.I_value = self.Integrator * self.Ki
PID = self.P_value + self.I_value + self.D_value
return PID
def setPoint(self,set_point):
"""
Initilize the setpoint of PID
"""
self.set_point = set_point
self.Integrator=0
self.Derivator=0
def setIntegrator(self, Integrator):
self.Integrator = Integrator
def setDerivator(self, Derivator):
self.Derivator = Derivator
def setKp(self,P):
self.Kp=P
def setKi(self,I):
self.Ki=I
def setKd(self,D):
self.Kd=D
def getPoint(self):
return self.set_point
def getError(self):
return self.error
def getIntegrator(self):
return self.Integrator
def getDerivator(self):
return self.Derivator