/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
typedef struct Integrator{
float val;
float K;
}Integrator;
typedef struct P{
float K;
float val;
}P;
typedef struct PI{
Integrator I;
P P;
float val;
}PI;
void config_Integrator(Integrator *data,float K){
data->K = K;
}
void add_val_Integrator(Integrator *data,float newval,float dt){
data->val += newval * data->K * dt;
}
void config_P(P *data,float K){
data->K=K;
}
void recall_P(P *data,float val){
data->val= val * data->K;
}
void config_PI(PI *data,float KP,float KI){
config_P(&data->P,KP);
config_Integrator(&data->I,KI);
}
void add_val_PI(PI *data,float newval, float dt){
add_val_Integrator(&data->I,newval,dt);
recall_P(&data->P,newval);
data->val=data->P.val+data->I.val;
}
int main() {
PI PI_Glied1;
float x=0.1f;
float y=0.0f;
config_PI(&PI_Glied1,0.6f,0.1f);
for (int i = 0; i < 100; ++i) {
y=1.0f-x;
add_val_PI(&PI_Glied1,y,0.01f);
x=PI_Glied1.val/5.0f+0.001f;
printf("%f \n" ,y);
}
return 0;
}