时间:2021-05-23
PID算法实现
import timeclass 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测试PID算法
import PIDimport timeimport matplotlibmatplotlib.use("TkAgg")import matplotlib.pyplot as pltimport numpy as npfrom scipy.interpolate import spline#这个程序的实质就是在前九秒保持零输出,在后面的操作中在传递函数为某某的系统中输出1def test_pid(P = 0.2, I = 0.0, D= 0.0, L=100): """Self-test PID class .. note:: ... for i in range(1, END): pid.update(feedback) output = pid.output if pid.SetPoint > 0: feedback += (output - (1/i)) if i>9: pid.SetPoint = 1 time.sleep(0.02) --- """ pid = PID.PID(P, I, D) pid.SetPoint=0.0 pid.setSampleTime(0.01) END = L feedback = 0 feedback_list = [] time_list = [] setpoint_list = [] for i in range(1, END): pid.update(feedback) output = pid.output if pid.SetPoint > 0: feedback +=output# (output - (1/i))控制系统的函数 if i>9: pid.SetPoint = 1 time.sleep(0.01) feedback_list.append(feedback) setpoint_list.append(pid.SetPoint) time_list.append(i) time_sm = np.array(time_list) time_smooth = np.linspace(time_sm.min(), time_sm.max(), 300) feedback_smooth = spline(time_list, feedback_list, time_smooth) plt.figure(0) plt.plot(time_smooth, feedback_smooth) plt.plot(time_list, setpoint_list) plt.xlim((0, L)) plt.ylim((min(feedback_list)-0.5, max(feedback_list)+0.5)) plt.xlabel('time (s)') plt.ylabel('PID (PV)') plt.title('TEST PID') plt.ylim((1-0.5, 1+0.5)) plt.grid(True) plt.show()if __name__ == "__main__": test_pid(1.2, 1, 0.001, L=80)# test_pid(0.8, L=50)结果
以上这篇python实现PID算法及测试的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
应用OpenCV和Python进行SIFT算法的实现如下图为进行测试的gakki101和gakki102,分别验证基于BFmatcher、FlannBasedM
用python语言实现根据pid杀死相应进程kill_process.py代码如下#!/usr/bin/python# -*-coding:utf-8-*-im
这篇文章并不是介绍排序算法原理的,纯粹是想比较一下各种排序算法在真实场景下的运行速度。算法由Python实现,可能会和其他语言有些区别,仅当参考就好。测试的数据
之所以说”使用”而不是”实现”,是因为python的相关类库已经帮我们实现了具体算法,而我们只要学会使用就可以了。随着对技术的逐渐掌握及积累,当类库中的算法已经
Python中的单元测试我们先来回顾一下Python中的单元测试方法。下面是一个Python的单元测试简单的例子:假如我们开发一个除法的功能,有的同学可能觉得很