PID算法實(shí)現(xiàn)
創(chuàng)新互聯(lián)從2013年開(kāi)始,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元靜樂(lè)做網(wǎng)站,已為上家服務(wù),為靜樂(lè)各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108import time class PID: def __init__(self, P=0.2, I=0.0, D=0.0): self.Kp = P self.Ki = I self.Kd = D self.sample_time = 0.00 self.current_time = time.time() self.last_time = self.current_time self.clear() def clear(self): self.SetPoint = 0.0 self.PTerm = 0.0 self.ITerm = 0.0 self.DTerm = 0.0 self.last_error = 0.0 self.int_error = 0.0 self.windup_guard = 20.0 self.output = 0.0 def update(self, feedback_value): error = self.SetPoint - feedback_value self.current_time = time.time() delta_time = self.current_time - self.last_time delta_error = error - self.last_error if (delta_time >= self.sample_time): self.PTerm = self.Kp * error#比例 self.ITerm += error * delta_time#積分 if (self.ITerm < -self.windup_guard): self.ITerm = -self.windup_guard elif (self.ITerm > self.windup_guard): self.ITerm = self.windup_guard self.DTerm = 0.0 if delta_time > 0: self.DTerm = delta_error / delta_time self.last_time = self.current_time self.last_error = error self.output = self.PTerm + (self.Ki * self.ITerm) + (self.Kd * self.DTerm) def setKp(self, proportional_gain): self.Kp = proportional_gain def setKi(self, integral_gain): self.Ki = integral_gain def setKd(self, derivative_gain): self.Kd = derivative_gain def setWindup(self, windup): self.windup_guard = windup def setSampleTime(self, sample_time): self.sample_time = sample_time