//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include #include //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Edit4->Text = 50; // Set Temperature Edit5->Text = 10; // Delay Edit1->Text = 10; // Proportion Edit2->Text = 100; // Integration Edit3->Text = 0; // Differential Edit7->Text = 3; // Proportion Edit8->Text = 0.2; // Integration Edit9->Text = 0; // Differential Edit6->Text = 0; // External disorder Temp = 273; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { Timer1->OnTimer = Button1Click; Timer1->Enabled = true; for(int i=0;i<10;i++) { double out; double deltat = SetTemp - Temp; double xtemp = (sqrt(Temp)-sqrt(5))*6; S2 += deltat*0.01; if(deltat>0) out=15*deltat+S2+xtemp; if(deltat<0) out=40*deltat+S2+xtemp; out = (out>=0)*(out<256)*out+(out>255)*255; Hyouji(out); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { // P = 9.4 // I = 0.3 // D = 20 // convergence 193 // Deley 10 Timer1->Enabled = true; Timer1->OnTimer = Button2Click; for(int i=0;i<10;i++) { double dt = Temp - SetTemp; Temp2[1]=Temp2[0]; // Temp2[1] 一つ前の温度 Temp2[0]=Temp; // Temp2[0] 現在の温度 Dt2[1] = Dt2[0]; Dt2[0] = Temp2[0]-Temp2[1]; // Dt2[] 温度の上昇率 Out += -Dt2[0]*P2-dt*I2-(Dt2[0]-Dt2[1])*D2; Out = (Out>=0)*(Out<256)*Out+(Out>255)*255; Hyouji(Out); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button4Click(TObject *Sender) { // P = 0.93 // I = 0.1 // D = 0.9 // convergence 285 Timer1->Enabled = true; Timer1->OnTimer = Button4Click; for(int i=0;i<10;i++) { double dt = SetTemp - Temp; S2 += dt; S2 = S2*(S2>=0); Temp2[1]=Temp2[0]; Temp2[0]=Temp; double out = 100/P*(dt + S2/I + (Temp2[1]-Temp)*D); out = (out>=0)*(out<256)*out+(out>255)*255; Hyouji(out); Application->ProcessMessages(); } } //--------------------------------------------------------------------------- void TForm1::Hyouji(double out) { if(F != 1) Time++; Panel8->Caption = FloatToStrF(Time,ffFixed,5,2); double dt = Temp - SetTemp ; PaintBox1->Canvas->Pixels[X][-dt*5+120] = clRed; Panel1->Caption = FloatToStrF(dt,ffFixed,5,2); out /=25.5; // 255 が10V Panel2->Caption = FloatToStrF(out,ffFixed,5,2); X+=0.5; Tempd[0] = out*out; // P=V*V/R Temp += Tempd[DELAY]/50.0 - (Temp-10)/100 +(random(ExtInp)-ExtInp/2)/10.0; Panel5->Caption = FloatToStrF(Temp,ffFixed,5,2); for(int i=49;i>0;i--) { Tempd[i]=Tempd[i-1]; Dt[i]=Dt[i-1]; } Dt[0]= -dt; // PIDのdtに合わせるため if(MaxValue(Dt,49)<0.005 && MinValue(Dt,49)>-0.005) F=1; if(X>PaintBox1->Width) Clear(); } //--------------------------------------------------------------------------- void TForm1::Clear() { X=0; PaintBox1->Canvas->FillRect(Rect(0,0,PaintBox1->Width,Height)); } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit1Change(TObject *Sender) { double p = P; try { P = Edit1->Text.ToDouble(); } catch(...){} if(P==0) P=p; } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit2Change(TObject *Sender) { double i = I; try { I = Edit2->Text.ToDouble(); } catch(...){} if(I==0) I=i; S2 = 0; } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit3Change(TObject *Sender) { if(Edit3->Text!="") D = Edit3->Text.ToDouble(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit4Change(TObject *Sender) { if(Edit4->Text != "") SetTemp = Edit4->Text.ToInt(); Time=0; F=0; S2=0; } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit5Change(TObject *Sender) { if(Edit5->Text != "" && Edit5->Text>"0" && Edit5->Text<"50") { DELAY = Edit5->Text.ToInt(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit6Change(TObject *Sender) { if(Edit6->Text != "") ExtInp = Edit6->Text.ToIntDef(0); } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1Click(TObject *Sender) { Timer1->Enabled = false; } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit7Change(TObject *Sender) { if(Edit7->Text!="") P2 = Edit7->Text.ToDouble(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit8Change(TObject *Sender) { if(Edit8->Text!="") I2 = Edit8->Text.ToDouble(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit9Change(TObject *Sender) { if(Edit9->Text!="") D2 = Edit9->Text.ToDouble(); } //---------------------------------------------------------------------------