#include "stdio.h"
typedef struct pid{
float set_speed;
float real_speed;
float Kp,Ki,Kd;
float err[3];
}t_pid;
t_pid pid;
float inc_speed;
void PID_Init()
{
pid.set_speed = 0;
pid.real_speed = 0;
pid.Kp = 0.1;
pid.Ki = 0.1;
pid.Kd = 0.1;
pid.err[0] = 0;
pid.err[1] = 0;
pid.err[2] = 0;
}
float PID_Realize(speed)
{
pid.set_speed = speed;
pid.err[0] = pid.set_speed - pid.real_speed;
inc_speed = pid.Kp * (pid.err[0] - pid.err[1]) + pid.Ki * pid.err[0] + pid.Kd * (pid.err[0] - pid.err[1] * 2 + pid.err[1]);
pid.real_speed += inc_speed;
pid.err[2] = pid.err[1];
pid.err[1] = pid.err[0];
return pid.real_speed;
}
void main()
{
int count = 1000;
PID_Init();
while(count--){
float speed = PID_Realize(200);
printf("%f ", speed);
if(count%5 == 0)
printf("\n");
}
}
typedef struct pid{
float set_speed;
float real_speed;
float Kp,Ki,Kd;
float err[3];
}t_pid;
t_pid pid;
float inc_speed;
void PID_Init()
{
pid.set_speed = 0;
pid.real_speed = 0;
pid.Kp = 0.1;
pid.Ki = 0.1;
pid.Kd = 0.1;
pid.err[0] = 0;
pid.err[1] = 0;
pid.err[2] = 0;
}
float PID_Realize(speed)
{
pid.set_speed = speed;
pid.err[0] = pid.set_speed - pid.real_speed;
inc_speed = pid.Kp * (pid.err[0] - pid.err[1]) + pid.Ki * pid.err[0] + pid.Kd * (pid.err[0] - pid.err[1] * 2 + pid.err[1]);
pid.real_speed += inc_speed;
pid.err[2] = pid.err[1];
pid.err[1] = pid.err[0];
return pid.real_speed;
}
void main()
{
int count = 1000;
PID_Init();
while(count--){
float speed = PID_Realize(200);
printf("%f ", speed);
if(count%5 == 0)
printf("\n");
}
}