标准的PID处理例程 (转载的一个工业控制中常用算法)
2012-12-28 12:38
381 查看
工业控制中常用算法
标准的PID处理例程
转自:http://www.xiao-qi.com/mcu/PID.html
标准的PID处理例程
转自:http://www.xiao-qi.com/mcu/PID.html
/*==================================================================================================== 这是从网上找来的一个比较典型的PID处理程序,在使用单片机作为控制cpu时,请稍作简化,具体的PID 参数必须由具体对象通过实验确定。由于单片机的处理速度和ram资源的限制,一般不采用浮点数运算, 而将所有参数全部用整数,运算到最后再除以一个2的N次方数据(相当于移位),作类似定点数运算,可 大大提高运算速度,根据控制精度的不同要求,当精度要求很高时,注意保留移位引起的“余数”,做好余 数补偿。这个程序只是一般常用pid算法的基本架构,没有包含输入输出处理部分。 =====================================================================================================*/ #include <string.h> #include <stdio.h> /*==================================================================================================== PID Function The PID (比例、积分、微分) function is used in mainly control applications. PIDCalc performs one iteration of the PID algorithm. While the PID function works, main is just a dummy program showing a typical usage. =====================================================================================================*/ typedef struct PID { double SetPoint; // 设定目标 Desired Value double Proportion; // 比例常数 Proportional Const double Integral; // 积分常数 Integral Const double Derivative; // 微分常数 Derivative Const double LastError; // Error[-1] double PrevError; // Error[-2] double SumError; // Sums of Errors } PID; /*==================================================================================================== PID计算部分 =====================================================================================================*/ double PIDCalc( PID *pp, double NextPoint ) { double dError, Error; Error = pp->SetPoint - NextPoint; // 偏差 pp->SumError += Error; // 积分 dError = pp->LastError - pp->PrevError; // 当前微分 pp->PrevError = pp->LastError; pp->LastError = Error; return (pp->Proportion * Error // 比例项 + pp->Integral * pp->SumError // 积分项 + pp->Derivative * dError // 微分项 ); } /*==================================================================================================== Initialize PID Structure =====================================================================================================*/ void PIDInit (PID *pp) { memset ( pp,0,sizeof(PID)); } /*==================================================================================================== Main Program =====================================================================================================*/ double sensor (void) // Dummy Sensor Function { return 100.0; } void actuator(double rDelta) // Dummy Actuator Function {} void main(void) { PID sPID; // PID Control Structure double rOut; // PID Response (Output) double rIn; // PID Feedback (Input) PIDInit ( &sPID ); // Initialize Structure sPID.Proportion = 0.5; // Set PID Coefficients sPID.Integral = 0.5; sPID.Derivative = 0.0; sPID.SetPoint = 100.0; // Set PID Setpoint for (;;) { // Mock Up of PID Processing rIn = sensor (); // Read Input rOut = PIDCalc ( &sPID,rIn ); // Perform PID Interation actuator ( rOut ); // Effect Needed Changes } }
相关文章推荐
- 【转载】常用 大数据量、海量数据处理 方法 / 算法总结
- 标准的PID处理例程
- 【转载】一个关于VC++6.0与标准C++差异处理的小技巧
- 疏失误差处理常用的算法
- 【转载】备战腾讯社交广告算法大赛 · 七种常用特征工程
- 评估图像质量评价算法性能的几个常用的标准
- 2018年全国多校算法寒假训练营练习比赛(第二场) B TaoTao要吃鸡 (0 1背包 特殊处理一个物品)
- 安装一个新的int 9中断例程【在DOS下,按Tab建后改变当前屏幕的显示颜色,其它键照常处理】
- 字符串处理算法(四)现在一个给定字符串中寻找子串的功能(不能使用库函数)[2014百度笔试题]
- 一个处理多值Map集合的算法
- (转)一个关于VC++6.0与标准C++差异处理的小技巧
- Java实现常用权限控制算法
- JS控制div的样式(窗口4:3处理算法)
- 一个支持各种交叉等形状工业控制管道控件的实现
- 一个分组查询的SQL 常用算法(附源码可直接执行)
- STL常用算法(转载)
- [转载]工程上常用的三种计算实时CPU占用率算法
- 图像处理(验证码识别)程序中常用算法
- nginx学习笔记(7)Nginx如何处理一个请求---转载
- (转载)视频编解码学习之四:视频处理及编码标准