資源簡介
python code for PID controller method
代碼片段和文件信息
import?time
##PID?Library
class?PID(object):
????##Constants?used?in?some?of?the?functions?below
????pid_AUTOMATIC?=?1
????pid_MANUAL?=?0
????pid_DIRECT?=?0
????pid_REVERSE?=?1
????direction?=?0
????def?__init__(self?Setpoint?Kp?Ki?Kd?ControllerDirection):
????????self.mySetpoint?=?Setpoint
????????self.inAuto?=?0
????????self.myOutput=0
????????self.Kp?=?Kp
????????self.Ki?=?Ki
????????self.Kd?=?Kd
????????self.ControllerDirection?=?ControllerDirection
????????self.SetOutputLimits(0?255)
????????self.SampleTime?=?100
????????self.myInput=0
????????self.SetControllerDirection(self.ControllerDirection)
????????self.lastTime?=?int(round(time.time()?*?1000))?-?self.SampleTime
????def?Compute(self):
????????if?(self.inAuto?==?0):
????????????return?0
????????now?=?int(round(time.time()?*?1000))
????????timeChange?=?(now?-?self.lastTime)
????????if?(timeChange?>=?self.SampleTime):
????????????#?/*Compute?all?the?working?error?variables*/
????????????Input?=?self.myInput
????????????error?=?self.mySetpoint?-?Input
????????????self.ITerm?+=?(self.Ki?*?error)
????????????if?(self.ITerm?>?self.outMax):
????????????????self.ITerm?=?self.outMax
????????????elif?(self.ITerm?????????????????self.ITerm?=?self.outMin
????????????dInput?=?(Input?-?self.lastInput)
????????????#?/*Compute?PID?Output*/
????????????self.output?=?self.Kp?*?error?+?self.ITerm?-?self.Kd?*?dInput
????????????if?(self.output?>?self.outMax):
????????????????self.output?=?self.outMax
????????????elif?(self.output?????????????????self.output?=?self.outMin
????????????self.myOutput?=?self.output
????????????????#?/*Remember?some?variables?for?next?time*/
????????????self.lastInput?=?Input
????????????self.lastTime?=?now
????????????return?1
????????else:
????????????return?0
????def?SetTunings(self?Kp?Ki?Kd):
評論
共有 條評論