Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 class Pid2:
00026 def __init__( self, gain_p = 0.0, gain_i = 0.0, gain_d = 0.0,
00027 time_constant = 0.0, limit = -1.0 ):
00028 self.gain_p = gain_p
00029 self.gain_i = gain_i
00030 self.gain_d = gain_d
00031 self.time_constant = time_constant
00032 self.limit = limit
00033
00034 self.input = 0.0
00035 self.dinput = 0.0
00036 self.output = 0.0
00037 self.p = 0.0
00038 self.i = 0.0
00039 self.d = 0.0
00040
00041 def update( self, new_input, x, dx, dt ):
00042 if self.limit > 0.0 and abs( new_input ) > self.limit:
00043 if new_input < 0:
00044 new_input = - self.limit
00045 else:
00046 new_input = self.limit
00047
00048 if dt + self.time_constant > 0.0:
00049 self.dinput = ( new_input - self.input ) / ( dt + self.time_constant )
00050 self.input = ( dt * new_input + self.time_constant * self.input ) / ( dt + self.time_constant )
00051
00052 self.p = self.input - x
00053 self.d = self.dinput - dx
00054 self.i = self.i + dt * self.p
00055
00056
00057
00058 self.output = self.gain_p * self.p + self.gain_d * self.d + self.gain_i * self.i
00059 return self.output
00060
00061 def reset( self ):
00062 self.input = self.dinput = 0.0
00063 self.p = self.i = self.d = 0.0
00064
00065
00066 class Pid:
00067 def __init__( self, Kp = 0.0, Ti = 0.0, Td = 0.0, outputLimit = None ):
00068 self.Kp = Kp
00069 self.Ti = Ti
00070 self.Td = Td
00071
00072 self.prev_error = 0.0
00073 self.integral = 0.0
00074
00075 self.outputLimit = outputLimit
00076 self.setPointMin = self.setPointMax = 0.0
00077
00078 def get_output( self, pv, dt ):
00079 if( pv < self.setPointMax and pv > self.setPointMin ):
00080 error = 0
00081 else:
00082 errorFromMax = self.setPointMax - pv
00083 errorFromMin = self.setPointMin - pv
00084
00085 if abs( errorFromMax ) > abs( errorFromMin ):
00086 error = errorFromMin
00087 else:
00088 error = errorFromMax
00089
00090 self.integral += error * dt
00091
00092 if dt != 0:
00093 derivative = ( error - self.prev_error ) / dt
00094 else:
00095 derivative = 0
00096
00097 self.prev_error = error
00098
00099 if self.Ti == 0.0:
00100 TiInv = 0
00101 else:
00102 TiInv = 1/self.Ti
00103
00104 output = self.Kp * ( error + TiInv * self.integral + self.Td * derivative )
00105 if self.outputLimit != None:
00106 limitedOutput = min( max( output, -self.outputLimit ),
00107 self.outputLimit )
00108 return limitedOutput
00109 else:
00110 return output