增量式PID控制的C程序: 以下为一个使用C语言编写的基本增量式PID控制程序:
#include <stdio.h>
//Define Variables
int target;
int KP;
int KI;
int KD;
//Function definition
void pid_control(int Error){
int Proportional_Control, Integral_Control, Derivative_Control, Output;
Proportional_Control = Error * KP; //Proportional Control
Integral_Control = Error * KI; //Integral Control
Derivative_Control = Error * KD; //Derivative Control
Output = Proportional_Control + Integral_Control + Derivative_Control; //Calculate the Output
if (Output > 0){ //Limit the output
printf("Increase output"); //Do appropriate action
}
else {
printf("Decrease output"); //Do appropriate action
}
}
int main(){
printf("Enter the Target Value:");
scanf("%d", &target); //Read the target value
//Calculate PID coefficients
KP = 2.5;
KI = 0.5;
KD = 0.1;
pid_control(target); //Call the function to control the system
return 0;
}
然后我让它再写一份
#include <stdio.h>
int main()
{
float error, prevError;
float Kp, Ki, Kd;
float output;
// Proportional gain
Kp = 0.5;
// Integral gain
Ki = 0.1;
// Derivative gain
Kd = 0.05;
while (1)
{
// Compute Error
prevError = error;
error = getError(); // This should return the process error as a float
// Compute output from PID algorithm
float pTerm = Kp * error;
float iTerm = Ki * (error + prevError);
float dTerm = Kd * (error - prevError);
output = pTerm + iTerm + dTerm;
// Send output to the system to be controlled
outputControl(output); // This should send the processed output to the system being controlled
}
return 0;
}
我怎么看第一个不对,大家说它写的对吗?
|