急求位置式PID控制算法 MATLAB 代码
答案:1 悬赏:50 手机版
解决时间 2021-03-29 04:26
- 提问者网友:容嬷嬷拿针来
- 2021-03-28 16:20
急求位置式PID控制算法 MATLAB 代码
最佳答案
- 五星知识达人网友:逃夭
- 2021-03-28 17:17
刚好前不久搞过PID,部分程序如下,仅供参考
#include
#include
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 // 微分项
);
}
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控制结构 PID Control Structure
double rOut; // PID响应(输出) PID Response (Output)
double rIn; // PID反馈(输入) PID Feedback (Input)
PIDInit ( &sPID ); // 初始化结构 Initialize Structure
sPID.Proportion = 0.5; // 设置PID系数 Set PID Coefficients
sPID.Integral = 0.5;
sPID.Derivative = 0.0;
sPID.SetPoint = 100.0; // 设置PID设定 Set PID Setpoint
for (;;)
{ // 模拟最多的PID处理 Mock Up of PID Processing
rIn = sensor (); // 读取输入 Read Input
rOut = PIDCalc ( &sPID,rIn ); // 执行的PID迭代 Perform PID Interation
actuator ( rOut ); // 所需的更改的影响 Effect Needed Changes
}
#include
#include
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 // 微分项
);
}
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控制结构 PID Control Structure
double rOut; // PID响应(输出) PID Response (Output)
double rIn; // PID反馈(输入) PID Feedback (Input)
PIDInit ( &sPID ); // 初始化结构 Initialize Structure
sPID.Proportion = 0.5; // 设置PID系数 Set PID Coefficients
sPID.Integral = 0.5;
sPID.Derivative = 0.0;
sPID.SetPoint = 100.0; // 设置PID设定 Set PID Setpoint
for (;;)
{ // 模拟最多的PID处理 Mock Up of PID Processing
rIn = sensor (); // 读取输入 Read Input
rOut = PIDCalc ( &sPID,rIn ); // 执行的PID迭代 Perform PID Interation
actuator ( rOut ); // 所需的更改的影响 Effect Needed Changes
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯