PID调节基础程序模版
2011-01-16 11:46
239 查看
Code:
#include <string.h>
#include <stdio.h>
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;
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 // 微分项
);
}
/*====================================================================================================
PID初始化
=====================================================================================================*/
void PIDInit (PID *pp)
{
memset ( pp,0,sizeof(PID));
}
/*====================================================================================================
主函数
=====================================================================================================*/
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
}
}
相关文章推荐
- 第八回 基础才是重中之重~用好“逻辑与”和“逻辑或”,可以让你的程序更优美!
- Java基础4:程序结构以及注意事项
- 【文章汇总】J2ME程序开发全方位基础讲解
- 关于程序注释的基础知识[三]
- Java基础----一个有问题的多线程程序
- 汇编练习2:用一个根据年月日计算发生天数的程序练习参数传递和对基础命令的认识
- C语言基础程序 查找篇
- 【廖雪峰python3.0】-课后习题:第2章:第一个python程序 and 第三章:python基础
- 7.Spark Streaming:输入DStream之基础数据源以及基于HDFS的实时wordcount程序
- 程序设计基础(Python语言)”编程练习(1)
- 欢迎报名参加Delphi教学研习营, 在一个星期内学会Delphi语言基础程序开发
- Java基础复习:abstract关键字&抽象类&模版模式
- 由mysql数据库基础上的php程序实现单词的查询、删除、更改和查询
- php面试题集-基础程序题
- 基础程序题
- 零基础Android学习笔记-02 安卓程序生命周期
- 总结:程序设计基础
- Eclipse4.7基础 新建工程 编写HelloWorld程序
- 程序设计基础
- C#编程点滴之基础编程1:第1个程序Hello World