• 大小: 4KB
    文件类型: .py
    金币: 1
    下载: 0 次
    发布日期: 2021-05-10
  • 语言: Python
  • 标签: pid  python  

资源简介

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.outMin):
                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.outMin):
                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):

评论

共有 条评论