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;
/*=========================================================================*\
Perform One PID Iteration
\*=========================================================================*/
double PIDCalc ( PID *pp, double NextPoint )
{ double dError, Error;
pp->SumError += (Error = pp->SetPoint - NextPoint);
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调节C51程序(3)
下一篇:PID调节C51程序(5)
推荐阅读最新更新时间:2024-03-16 15:20
设计资源 培训 开发板 精华推荐
- 小弟迷茫,请各位指点迷津
- 测评汇总:博流BL606P音视频开发板
- 测评汇总:Sipeed 博流BL808全能板,RISC-V三核、无线三模、AIoT+Linux+RTOS
- 又是一年,回顾2022,确立2023目标,一起重新出发吧!
- 直播回顾:12月7日福禄克数据采集器的基础知识及其应用和校准
- 直播回顾:12月8日MPS 新一代磁角度传感器MA600介绍与应用
- 【博流BL606P音视频开发板】一、环境搭建及HelloWord Demo运行
- 回顾2022展望2023+忙忙碌碌的一年
- 【Sipeed 博流BL808全能板】- 开发环境二 烧录软件
- [ ST NUCLEO-U575ZI-Q 测评] STM32Cube MCU 包示例