|
发表于 2017-4-11 16:28:32
|
显示全部楼层
我帮lz贴出来,
- unit U_SMotor;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, ExtCtrls, StdCtrls, ComCtrls;
- type
- TLocation = record
- X: Real;
- Y: Real;
- end;
- TForm1 = class(TForm)
- PageCont1: TPageControl;
- Main: TTabSheet;
- DataOut: TTabSheet;
- Image1: TImage;
- Label1: TLabel;
- Edit1: TEdit;
- Label2: TLabel;
- Edit2: TEdit;
- Timer1: TTimer;
- Edit3: TEdit;
- Label3: TLabel;
- Edit4: TEdit;
- Label4: TLabel;
- Edit5: TEdit;
- Label5: TLabel;
- Label6: TLabel;
- Edit6: TEdit;
- Memo1: TMemo;
- Label7: TLabel;
- Edit7: TEdit;
- Button1: TButton;
- Button2: TButton;
- SaveDialog1: TSaveDialog;
- Label8: TLabel;
- Edit8: TEdit;
- RdGMode: TRadioGroup;
- Edit9: TEdit;
- Label9: TLabel;
- TabSheet1: TTabSheet;
- Memo2: TMemo;
- procedure Timer1Timer(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- AccT: array[1..10000] of Real; //加速度数组
- VT: array[1..10000] of Real; //速度数组
- Tpwm: array[1..10000] of Real; //Pwm周期数组
- T_His: array[1..10000] of Real; //时间数组
- nT_Idea: array[1..10000] of Real; //理论的Pwm值(机器周期整数倍)
- nT_Real: array[1..10000] of Real; //实际的Pwm值(机器周期整数倍)
- Inc_nT: array[1..10000] of Real; //Pwm增量(机器周期整数倍)
- AccelPointMatrix: array[1..10000] of TLocation;
- SpeedPointMatrix: array[1..10000] of TLocation;
- Act_ArrayLim: integer;
- FreqOsc: Real;
- TOsc : Real;
- DivTOsc: Real;
- TMac : Real;
- InitPwmCount: Real;
- MaxSpeedPwmC: Real;
- TSpeedUp: Real;
- AccK,w_v: Real; //a(t)=k*t; or a(t)=k*sin(wt) k系数和角速度
- MaxAcc: Real;
- MaxSpeed: Real;
- implementation
- {$R *.dfm}
- Procedure DrawXYControlPicture;
- var
- i,j,temx,temy: integer;
- begin
- for i:=1 to Act_ArrayLim do //计算曲线数据
- begin
- AccelPointMatrix[i].X:=T_His[i]*Form1.Image1.Width /T_His[Act_ArrayLim];
- AccelPointMatrix[i].Y:=AccT[i]*Form1.Image1.Height/MaxAcc;
- SpeedPointMatrix[i].X:=T_His[i]*Form1.Image1.Width /T_His[Act_ArrayLim];
- SpeedPointMatrix[i].Y:=VT[i]*Form1.Image1.Height/MaxSpeed;
- end;
- for i:=1 to Form1.Image1.Width do //将绘图区清空
- begin
- Form1.Image1.Canvas.Pen.Color:=clWhite;
- Form1.Image1.Canvas.MoveTo(i,0);
- Form1.Image1.Canvas.Lineto(i,Form1.Image1.Height);
- end;
- for i:=2 to Act_ArrayLim do
- begin
- temx:=trunc(AccelPointMatrix[i-1].X);
- temy:=Form1.Image1.Height-(trunc(AccelPointMatrix[i-1].Y*0.95)+5);
- if i=1 then Form1.Image1.Canvas.MoveTo(0,0)
- else Form1.Image1.Canvas.MoveTo(temx,temy);
- Form1.Image1.Canvas.Pen.Color:=clBlue;
- temx:=trunc(AccelPointMatrix[i].X);
- temy:=Form1.Image1.Height-(trunc(AccelPointMatrix[i].Y*0.95)+5);
- Form1.Image1.Canvas.LineTo(temx,temy);
- temx:=trunc(SpeedPointMatrix[i-1].X);
- temy:=Form1.Image1.Height-(trunc(SpeedPointMatrix[i-1].Y*0.95)+5);
- if i=1 then Form1.Image1.Canvas.MoveTo(0,0)
- else Form1.Image1.Canvas.MoveTo(temx,temy);
- Form1.Image1.Canvas.Pen.Color:=clRed;
- temx:=trunc(SpeedPointMatrix[i].X);
- temy:=Form1.Image1.Height-(trunc(SpeedPointMatrix[i].Y*0.95)+5);
- Form1.Image1.Canvas.LineTo(temx,temy);
- end;
- end;
- procedure TForm1.Timer1Timer(Sender: TObject);
- begin
- if ABS(strtofloat(Edit1.Text))>0.0001 then Edit2.Text:=floattostr(1/strtofloat(Edit1.Text));
- Edit4.Text:=floattostr(strtofloat(Edit2.Text)*strtofloat(Edit3.Text));
- if (ABS(strtofloat(Edit6.Text))>0.0001) and (ABS(strtofloat(Edit5.Text))>0.0001)
- and (ABS(strtofloat(Edit6.Text)-strtofloat(Edit5.Text))>0.0001) and (strtofloat(Edit7.Text)>0.001)
- then
- begin
- if RdGMode.ItemIndex=0 then
- Edit8.Text:=floattostr(4*(1/(strtofloat(Edit6.Text)*strtofloat(Edit3.Text))-1/(strtofloat(Edit5.Text)*strtofloat(Edit3.Text)))/((strtofloat(Edit7.Text)/1000)*(strtofloat(Edit7.Text)/1000)))
- else
- begin
- if ABS(strtofloat(Edit7.Text))>0.01 then
- Edit9.Text:=floattostr(2*Pi/(2*strtofloat(Edit7.Text)/1000));
- Edit8.Text:=floattostr((1/strtofloat(Edit6.Text)-1/strtofloat(Edit5.Text))*strtofloat(Edit9.Text)/2);
- end;
- end;
- end;
- procedure TForm1.Button2Click(Sender: TObject);
- begin
- if SaveDialog1.Execute then
- begin
- memo1.Lines.SaveToFile(SaveDialog1.FileName);
- end;
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- Var
- LoopI: integer;
- Str_Line,s_Format: String;
- begin
- MaxAcc:=0;
- MaxSpeed:=0;
- FreqOsc:=strtofloat(Edit1.text);
- TOsc:=1/FreqOsc;
- DivTOsc:=strtofloat(Edit3.text);
- TMac:=TOsc*DivTOsc;
- InitPwmCount:=strtofloat(Edit5.text);
- MaxSpeedPwmC:=strtofloat(Edit6.text);
- TSpeedUp:=strtofloat(Edit7.text);
- AccK:=strtofloat(Edit8.text);
- w_v:=strtofloat(Edit9.text);
- nT_Idea[1]:=InitPwmCount; //6000
- Tpwm[1]:=nT_Idea[1]*TMac; //6000/8000000
- T_His[1]:=Tpwm[1];
- nT_Real[1]:=Int(nT_Idea[1]);
- AccT[1]:=6E-6;
- VT[1]:=1/nT_Idea[1];
- Inc_nT[1]:=0;
- Str_Line:='';
- s_Format:=' '+ '时间 ';
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+'加速度 ';
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+'速度 ';
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+'PWM周期 ';
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+'PWM周期变化量';
- Str_Line:=Str_Line+s_Format;
- Memo1.Lines.Append(Str_Line);
- Str_Line:='';
- s_Format:=' '+ Format('%.9f',[T_His[1]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+Format('%.9f',[AccT[1]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+Format('%.9f',[VT[1]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+ Format('%.4e',[nT_Real[1]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+ Format('%.3e',[Inc_nT[1]]);
- Str_Line:=Str_Line+s_Format;
- Memo1.Lines.Append(Str_Line);
- Str_Line:='';
- if RdGMode.ItemIndex=0 then //三角波加速度
- begin
- for LoopI:=2 to 10000 do
- begin
- AccT[LoopI]:=AccT[LoopI-1]+AccK*Tpwm[LoopI-1];
- VT[LoopI]:=VT[LoopI-1]+AccT[LoopI-1]*Tpwm[LoopI-1];
- nT_Idea[LoopI]:=1/VT[LoopI];
- nT_Real[LoopI]:=Int(nT_Idea[LoopI]);
- Inc_nT[LoopI]:=nT_Real[LoopI-1]-nT_Real[LoopI];
- Tpwm[LoopI]:=nT_Idea[LoopI]*DivTOsc/FreqOsc;
- T_His[LoopI]:=T_His[LoopI-1]+Tpwm[LoopI];
- s_Format:=' '+ Format('%.9f',[T_His[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+ Format('%.9f',[AccT[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+ Format('%.9f',[VT[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+ Format('%.4e',[nT_Real[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+ Format('%.3e',[Inc_nT[LoopI]]);
- Str_Line:=Str_Line+s_Format;
- Memo1.Lines.Append(Str_Line);
- Str_Line:='';
- if (T_His[LoopI]*1000)>(TSpeedUp/2) then break;
- if AccT[LoopI]>MaxAcc then MaxAcc:=AccT[LoopI];
- end;
- repeat
- inc(LoopI);
- AccT[LoopI]:=AccT[LoopI-1]-AccK*Tpwm[LoopI-1];
- VT[LoopI]:=VT[LoopI-1]+AccT[LoopI-1]*Tpwm[LoopI-1];
- nT_Idea[LoopI]:=1/VT[LoopI];
- nT_Real[LoopI]:=Int(nT_Idea[LoopI]);
- Inc_nT[LoopI]:=nT_Real[LoopI-1]-nT_Real[LoopI];
- Tpwm[LoopI]:=nT_Idea[LoopI]*DivTOsc/FreqOsc;
- T_His[LoopI]:=T_His[LoopI-1]+Tpwm[LoopI];
- s_Format:=' '+ Format('%.9f',[T_His[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+Format('%.9f',[AccT[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+Format('%.9f',[VT[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+ Format('%.4e',[nT_Real[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+ Format('%.3e',[Inc_nT[LoopI]]);
- Str_Line:=Str_Line+s_Format;
- Memo1.Lines.Append(Str_Line);
- Str_Line:='';
- if VT[LoopI]>MaxSpeed then MaxSpeed:=VT[LoopI];
- until (AccT[LoopI]<0);
- end
- else //正弦波加速度
- begin
- for LoopI:=2 to 10000 do
- begin
- VT[LoopI]:=VT[LoopI-1]+AccT[LoopI-1]*Tpwm[LoopI-1];
- nT_Idea[LoopI]:=1/VT[LoopI];
- nT_Real[LoopI]:=Int(nT_Idea[LoopI]);
- Inc_nT[LoopI]:=nT_Real[LoopI-1]-nT_Real[LoopI];
- Tpwm[LoopI]:=nT_Idea[LoopI]*DivTOsc/FreqOsc;
- T_His[LoopI]:=T_His[LoopI-1]+Tpwm[LoopI];
- AccT[LoopI]:=AccK*SIN(w_v*T_His[LoopI]);
- s_Format:=' '+ Format('%.9f',[T_His[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+ Format('%.9f',[AccT[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+ Format('%.9f',[VT[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+ Format('%.4e',[nT_Real[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+ Format('%.3e',[Inc_nT[LoopI]]);
- Str_Line:=Str_Line+s_Format;
- Memo1.Lines.Append(Str_Line);
- Str_Line:='';
- if (T_His[LoopI]*1000)>(TSpeedUp/2) then break;
- if AccT[LoopI]>MaxAcc then MaxAcc:=AccT[LoopI];
- end;
- repeat
- inc(LoopI);
- VT[LoopI]:=VT[LoopI-1]+AccT[LoopI-1]*Tpwm[LoopI-1];
- nT_Idea[LoopI]:=1/VT[LoopI];
- nT_Real[LoopI]:=Int(nT_Idea[LoopI]);
- Inc_nT[LoopI]:=nT_Real[LoopI-1]-nT_Real[LoopI];
- Tpwm[LoopI]:=nT_Idea[LoopI]*DivTOsc/FreqOsc;
- T_His[LoopI]:=T_His[LoopI-1]+Tpwm[LoopI];
- AccT[LoopI]:=AccK*SIN(w_v*T_His[LoopI]);
- s_Format:=' '+ Format('%.9f',[T_His[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+Format('%.9f',[AccT[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+Format('%.9f',[VT[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+ Format('%.4e',[nT_Real[LoopI]]);
- Str_Line:=Str_Line+s_Format+',';
- s_Format:=' '+ Format('%.3e',[Inc_nT[LoopI]]);
- Str_Line:=Str_Line+s_Format;
- Memo1.Lines.Append(Str_Line);
- Str_Line:='';
- if VT[LoopI]>MaxSpeed then MaxSpeed:=VT[LoopI];
- until (AccT[LoopI]<0);
- end;
- Act_ArrayLim:=LoopI-1;
- DrawXYControlPicture;
- end;
- end.
复制代码 |
|