Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
RC回路のPI制御
/*
Vi(t) = RC*Vo(t)' + Vo(t)
*/
#include<stdio.h>
#include<math.h>
#define GAIN_P 1.0 //比例ゲイン
#define GAIN_I 1.0 //積分ゲイン
#define R 1.0
#define C 1.0
#define INT_TIME 0.01
double P,I,dTime;//比例動作、積分動作、時刻
double dSum;//偏差の積分
double FD( double dCommand, double dVal );
int main(void){
double dV,FDback;
double dQ,dI,dVo;
int j;
printf("\nfeed---Vi(t)=RCVo(t)'+Vo(t)---\n");
printf("C=%f\tR=%f\tGAIN_P=%f\tGAIN_I=%f\n\n",C,R,GAIN_P,GAIN_I);
printf("GAIN_P=%f\tGAIN_I=%f\n",GAIN_P,GAIN_I);
//初期値
dVo=0;//コンデンサーの電位差
dQ=0;//コンデンサーの電気量
dTime=0;//時刻
dSum=0;//偏差の積
for(j=0;j<500;j++){
//コントーラー
FDback = FD(1,dVo);
//抵抗の電位差
dV = FDback - dVo;
//電流
dI = dV/R;
//電気量
dQ = dQ + dI*INT_TIME;
//コンデンサーの電位差
dVo = dQ/C;
//時刻
dTime = dTime + INT_TIME;
printf("TIME=%.2f\tVo=%f\tP=%f\tI=%f\n",dTime,dVo,P,I);
}
return 0;
}
double FD( double dCommand, double dVal ){
double dRet;
double dErr;
//誤差
dErr = dCommand - dVal;
//偏差の積分
dSum = dSum + dErr*INT_TIME;
//P動作、偏差に比例
P = GAIN_P*dErr;
//I動作、偏差の積分に比例
I = GAIN_I*dSum;
//返り値=P動作+I動作
dRet = P+I;
return dRet;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment