Professional Documents
Culture Documents
KHIN CC PHN T KH NN
TIM1
1000
1000
1000
TIM1
#0020
1000
1001
#0020
//Thit lp 2 trng thi y ra v thu vo l ngc nhau
LD NOT 1000
OUT
1001
2)Th nghim 1E:
Vit chng trnh iu khin sao cho khi nhn SW2 ,th 2 xi lanh I v J lun
phin y ra v vo vi chu k 4s,khi nhn SW3 th dng
Chng trnh LADDER
00004
00005
1007
1007
1007
TIM0
1000
TIM0
1002
1000
1007
1001
1007
TIM1
TIM0
TIM0
#0040
TIM1
#0040
SW3
00004
00005
AUTO
1007
1007
CB1
1007
0001
CB2
0002
TIM0
1000
1000
1000
TIM0
#0020
1000
1007
1001
1007
0001
0002
CNT 1
CNT1
R
12
TIM2
CNT1
1002
1002
1002
TIM2
#0040
1007
TIM3
0001
TIM3
#0300
HORN
1004
OR
AND
AND
AND
OUT
LD
TIM
1000
0001
0002
NOT TIM 0
1000
1000
000
#0020
LD NOT 1000
AND 1007
OUT 1001
//khi bng chuyn hot ng,cm bin quang bo c chai i ngang(CB1 ON)
v cm bin dung khng bo li chai th ta bt u m s chai xung.Khi s
chai m xung bng 0( 12 chai) th reset s chai m v 12
LD 1007
AND 0001
AND NOT 0002
LD CNT1
CNT 001
12
//Khi 12 chai th cho xi lanh J hot ng (trong 4s) ng np chai
LD CNT1
OR 1002
AND NOT TIM 002
OUT 1002
LD 1002
TIM 002
#0040
//Thit lp ch bo ng ci khi c li kt chai:khi bng chuyn hot
ng,v cm bin quang cha xc nh c chai ti th ta s cho timer 3
chy,nu sau 30s m khng c chai i ngang (tc b kt chai) timer 3 s on
v h ci bo
LD 1007
AND NOT 0001
TIM 003
#0300
LD TIM3
OUT 1004
III) Thc hin phng n c giao:
Khi nhn Start (SW2) , h thng s thc hin mt cch tun t cc
bc sau:
1/ xi lanh I y ra
6
2/ xi lanh J y ra
3/ xi lanh K y ra
4/ xi lanh I,J thu vo
5/ xi lanh K thu vo
6/ xi lanh K y ra
7/ xi lanh J y ra
8/ xi lanh I y ra
9/ xi lanh J,K thu vo
10/ xi lanh I thu vo
11/ tt h thng
Mi bc cch nhau 1s
Khi nhn Stop (SW3) h thng dng li.
SW3
00004
00005
AUTO
TIM11
1007
1007
1007
1007
1007
1007
1007
1007
1007
1007
1007
TIM1
#0010
TIM2
#0020
TIM3
#0030
TIM4
#0040
TIM5
#0050
TIM6
#0060
TIM7
#0070
TIM8
#0080
TIM9
#0090
1007
TIM10
#0100
1007
TIM11
#0110
TIM1
TIM4
1000
TIM8
1000
TIM10
1007
1001
TIM2
TIM4
1002
TIM7
TIM9
TIM3
TIM5
1003
TIM6
TIM9
LD 1007
TIM 002
#0020
..
LD 1007
TIM 011
#0110
//da vo yu cu ta c xi lanh I s y ra khi timer 1 ON v thu vo khi
timer 4 ON,v li y ra khi timer 8 ON thu vo khi timer 10 ON
LD TIM1
AND NOT TIM4
OUT 1000
LD TIM8
AND NOT TIM10
OUT 1000
LD 1007
AND NOT 1000
OUT 1001
//Tng t ta c xi lanh J y ra khi timer 2 ON v thu vo khi timer 4
ON,tip tc y ra khi timer 7 ON,thu vo khi timer 9 ON
LD TIM2
AND NOT TIM4
OUT 1002
LD TIM7
AND NOT TIM9
OUT 1002
//Tng t ta c xi lanh K y ra khi timer 3 ON v thu vo khi timer 5
ON,tip tc y ra khi timer 6 ON,thu vo khi timer 9 ON
LD TIM3
AND NOT TIM5
OUT 1003
LD TIM6
AND NOT TIM9
OUT 1003
Nhn xt: h thng hot ng ng theo yu cu.
10
STAR
00001
00002
AUTO
20001
20000
20000
20000
TIM1
#0050
PAUSE
TIM1
00003
20002
KEEP(11)
20002
20002
IL(02)
10001
TIM1
10000
11
00000
CNT 0
00007
CNT0
#0010
TIM1
10001
00007
CNT0
25314
CNTR(12)
#0010
20000
25313
CMP(20)
#0010
25506
20001
IL(03)
END
OR 20000
AND NOT 00002
OUT 20000
//Cho timer 1 chy delay 5s sau khi nhn STAR
LD 20000
TIM 001
#0050
//thit lp ch PAUSE dng a ch 20002 lm bin trung gian
LD TIM1
AND 00003
LD TIM1
AND 20002
KEEP
20002
//S dng a ch 20002 lm iu kin cho hm kha on chng trnh
LD 20002
IL
//Bt u on chng trnh trong hm kha
//Nu delay 5s v bng ti thng dng th cho bng ti to chy
LD TIM1
AND NOT 10001
OUT 10000
//khi cng tc hnh trnh ON,bo thng n ng v tr th reset counter
m to v dng cm bin to m s to i qua
LD 00000
LD NOT 00007
CNT 000
#0010
//bng ti thng s chy khi timer 5s ON v cng tc hnh trnh khng
ON(tc thng cha n ng v tr b to vo) hoc bng ti thng s
chy khi to b (CNT0 ON),
LD CNT0
OR NOT 00007
AND TIM1
OUT 10001
//Mi ln b xong 1 thng to (CNT0 ON) th cho counter m thng
tng thm 1 v dng bin trung gian chy chng trnh (20000) reset
counter m thng(mi ln h thng hot ng li th reset counter)
LD CNT0
LD 25314
LD 20000
CNTR
0000 #0010
//thit lp gi tr so snh lin tc cho counter m thng
LD 25313
13
14
15
16
17
AND LD
OUT 100.00
//thit lp cc Counter m s th t thng ti (1..4) v Counter m s to
cho mi thng:
LD 000.00
LD NOT
000.07
CNT 007 005
LD CNT020
AND 000.00
LD NOT
000.07
CNT 008 003
LD CNT002
AND 000.00
LD NOT
000.07
CNT 009 002
LD CNT003
AND 000.00
LD NOT
000.07
CNT 010 001
LD 000.07
LD 000.01
CNT 020 002
LD 000.07
LD 000.01
CNT 002 003
LD 000.07
//Counter 3 ON khi c 4 thng qua
LD 000.01
CNT 003 004
//Thit lp iu kin chy cho bng ti thng: gm 5 iu kin tng ng
cho tng trng hp
LD NOT
200.01
AND CNT007
OR NOT 000.07
OR CNT008
OR CNT009
OR CNT010
19
AND 200.00
OUT 100.01
m_Db = 0.5;
m_T0 = 1000;
m_Temp = 80;
//}}AFX_DATA_INIT
}
void CSetting::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSetting)
DDX_Text(pDX, IDC_DB, m_Db);
DDX_Text(pDX, IDC_T0, m_T0);
DDX_Text(pDX, IDC_TEMP, m_Temp);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSetting, CDialog)
//{{AFX_MSG_MAP(CSetting)
ON_BN_CLICKED(IDC_ACCEPT, OnAccept)
ON_EN_CHANGE(IDC_DB, OnChangeDb)
ON_EN_CHANGE(IDC_T0, OnChangeT0)
//}}AFX_MSG_MAP
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);
// Set big icon
SetIcon(m_hIcon, FALSE);
// Set small icon
// TODO: Add extra initialization here
VCL_InitControls(m_hWnd);
Scope2.Open(m_Scope2.m_hWnd);
Scope2.SizeLimit = 1000;
Scope2.Channels.Add(0);
Scope2.Title.Text="Control Power realtime trend";
Scope2.Channels[0].Name="Control Power";
Scope2.YAxis.AxisLabel.Text="Power(%)";
Scope2.YAxis.Max=100;
Scope2.YAxis.Min=0;
Scope1.Open(m_Scope1.m_hWnd);
Scope1.SizeLimit = 1000;
Scope1.Channels.Add(0);
Scope1.Channels.Add(1);
Scope1.Title.Text="Measuring temperature realtime trend";
Scope1.Channels[0].Name="Meas.Temp";
Scope1.Channels[1].Name="Set Temp";
Scope1.YAxis.AxisLabel.Text="Temperature(C)";
Scope1.YAxis.Max=200;
Scope1.YAxis.Min=0;
return TRUE; // return TRUE unless you set the focus to a control
}
22
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CBai3_3Dlg::OnPaint()
{
if (IsIconic())
{
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSetting message handlers
Pwr=0;
pw=0;
ptAOConfig.chan
= gwChannelOutput ;
ptAOVoltageOut.OutputValue = 0;
DRV_AOVoltageOut(DriverHandle,(LPT_AOVoltageOut)&ptAOVoltageOut);
}
s.Format("%0d", dwCurrentTime/1000);
this->SetDlgItemText(IDC_RTIME, s);
s.Format("%.2f",Temp);
this->SetDlgItemText(IDC_STEMP, s);
s.Format("%.2f",Temp - Input*62.5);
this->SetDlgItemText(IDC_ERROR, s);
// write output data
if ((Temp-Input*62.5)<(Db))//db duoi
{
Output = 5;
Pwr = 100 ;
pw = 100 ;
}
if ((Input*62.5-Temp)>(Db))//db tren
{
Output = 0;
Pwr = 0 ;
pw = 0 ;
}
ptAOConfig.chan
= gwChannelOutput ;
ptAOVoltageOut.OutputValue = Output;
DRV_AOVoltageOut(DriverHandle,(LPT_AOVoltageOut)&ptAOVoltageOut);
CDialog::OnTimer(nIDEvent);
}
void CSetting::OnChangeDb()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code her
}
void CSetting::OnChangeT0()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
1) iu khin PID:
- on c tn hiu nhit :
DRV_AIVoltageIn(DriverHandle,(LPT_AIVoltageIn)&ptAIVoltageIn);
- on tnh v xut tn hiu iu khin:
Error = Temp - 62.5*Input;
ek2 = ek1;
ek1 = ek;
ek = Error;
uk1 = uk;
uk = uk1 + A0*ek + A1*ek1 + A2*ek2;
if(uk>MaxValueOut)
{
uk = MaxValueOut;
}
if(uk<MinValueOut)
{
uk = MinValueOut;
}
Output = (float)uk;
2) iu khin ON-OFF:
- on c tn hiu nhit :
// read input data
DRV_AIVoltageIn(DriverHandle,(LPT_AIVoltageIn)&ptAIVoltageIn);
- on tnh v xut tn hiu iu khin:
// write output data
if ((Temp-Input*62.5)<(Db) ;
{
Output = 5;
Pwr = 100 ;
pw = 100 ;
}
if ((Input*62.5-Temp)>(Db))
{
Output = 0;
Pwr = 0 ;
pw = 0 ;
}
ptAOConfig.chan = gwChannelOutput ;
26
Output =(float)Pwr*5/100;
ptAOVoltageOut.OutputValue = Output;
DRV_AOVoltageOut(DriverHandle,(LPT_AOVoltageOut)&ptAOVoltageOut);
V) Tin hnh th nghim
1) Kho st vng h:
p ng thu c:
Xc nh cc h s Kp,Ki,Kd:
Ta xc nh c cc h s K=100-32=68
L=50
T=450
a=KL/T=7.5
Ti=2L=100
Td=0.5L=25
Kp=1,20/a=0.16
Ki=Kp/Ti=1.6*10-3 Kd=Kp*Td=4
2) Kho st vng kn vi b iu khin PID:
Kho st vi cc h s PID tnh c t vng h:
p ng thu c:
27
Thi gian ln
(s)
75
70
Thi gian xc
lp (s)
550
450
vt l
(%)
122.5
112.5
Sai s xc lp
(0C)
9
5
28
29
Bng gi tr:
B iu khin
ON-OFF
=10C
=0.20C
Thi gian ln
(s)
60
120
Thi gian xc
lp (s)
420
420
vt l
(%)
140
140
Sai s xc lp
(0C)
15
10
if ((Temp-Input*62.5)>(Db+a)
if ((Input*62.5-Temp)>(Db-b))
....
vi a,b l cc s dng ta chn thch hp sai s phn trn v phn di u
nhau (da vo thc nghim v phng php th v sai).
30
Bi 4: IU KHIN TC V V TR NG C DC
I) Cc on chng trnh mu :
// on chng trnh c xung:
PT_DioReadPortByte ptDioReadPortByte0,ptDioReadPortByte1;
USHORT DataLo,DataHi;
double Pulse;
double Delta_Pulse;
static double PrevPulse;
// read low byte
ptDioReadPortByte0.port = 0;
ptDioReadPortByte0.value = (USHORT far *)&DataLo;
// read high byte
ptDioReadPortByte1.port = 1;
ptDioReadPortByte1.value = (USHORT far *)&DataHi;
DRV_DioReadPortByte(DriverHandle,(LPT_DioReadPortByte)&ptDioReadPortByte0);
DRV_DioReadPortByte(DriverHandle,(LPT_DioReadPortByte)&ptDioReadPortByte1);
Pulse = DataHi*256 + DataLo;
Delta_Pulse = Pulse - PrevPulse;
PrevPulse = Pulse;
// processing counter overflow
if(Delta_Pulse>+32768) Delta_Pulse = Delta_Pulse - 65536;
if(Delta_Pulse<-32768) Delta_Pulse = Delta_Pulse + 65536;
return (Delta_Pulse);
// on x l tn hiu:
void CBAI4Dlg::OnRun()
{
// TODO: Add your control notification handler code here
//------------------------------------------------------------//configure input and output of the PCI card
if(!flag) DRV_SelectDevice(m_hWnd,FALSE,&m_DeviceNume, m_DeviceName);
DRV_DeviceOpen(m_DeviceNume,&DriverHandle);
// configures the gain for the specifed analog input channel
ptAIConfig.DasGain = DevFeatures.glGainList[gwGain].usGainCde;
ptAIConfig.DasChan = gwChannelInput;
DRV_AIConfig(DriverHandle,(LPT_AIConfig)&ptAIConfig);
// reads an analog input channel
ptAIVoltageIn.chan = gwChannelInput;
ptAIVoltageIn.gain = ptAIConfig.DasGain;
ptAIVoltageIn.TrigMode = 0; // internal trigger
ptAIVoltageIn.voltage = (FLOAT far *)&Input;
31
double m_speed;
static double updatespeed[10];
double avrspeed =0;
if(m_nTimer)
{
m_speed = DataPulse()*speedratio/T0;
updatespeed[9] = m_speed;
for(i=0;i<10;i++){
avrspeed+= updatespeed[i];}
avrspeed = avrspeed/10;
for (i=0;i<9;i++)
updatespeed[i] = updatespeed[i+1];
Output = (float)PID(avrspeed);
Output = 2.5 + Output;
if(Output> MaxValueOut)
Output = MaxValueOut;
if(Output < MinValueOut)
Output = MinValueOut;
ptAOVoltageOut.OutputValue = (Output);
DRV_AOVoltageOut(DriverHandle,(LPT_AOVoltageOut)&ptAOVoltageOut);
if(i<1000)
{
CurrentSpeed[i] = m_speed;
i++;
}
//plot the measure speed, set speed, control voltage
Scope1.Channels[0].Data.AddYPoint(SetSpeed);
Scope1.Channels[1].Data.AddYPoint(avrspeed);
Scope2.Channels[0].Data.AddYPoint(Output);
}
dwCurrentTime = GetTickCount()- gdwStartTime;
s.Format("%0d", dwCurrentTime/1000);
this->SetDlgItemText(IDC_STATIC_RUN, s);
s.Format("%2.2f",Output);
this->SetDlgItemText(IDC_STATIC_VOLTAGE, s);
s.Format("%2.2f",SetSpeed - m_speed);
this->SetDlgItemText(IDC_STATIC_ERROR, s);
33
s.Format("%4d",m_speed);
this->SetDlgItemText(IDC_STATIC_MEASPEED, s);
s.Format("%4d",SetSpeed);
this->SetDlgItemText(IDC_STATIC_SETSPEED, s);
CDialog::OnTimer(nIDEvent);
}
0.001
0.002
0.003
0.004
0.005
0.006
0.007
14
6.2
5.5
4.1
3.6
3.2
vt l (%)
Sai s xc lp (v/p)
26
2
27
5
28
6
30
9
32
11
34
13
35
16
34
p ng thu c:
+ Trng hp Kp=0.001:
+ Trng hp Kp=0.002:
35
0.0025
10
75
0
+ Trng hp Ki=0.002:
3.10^(-4)
5.9s
23.2%
2
36
+ Trng hp Kd=3.10^(-4):
2) iu khin v tr ng c:
Th Nghim vi Ki=0.05,Kp=0.002,Kd=0.025
Thi gian ln
0.07s
Thi gian xl (s)
1.6s
vt l (%)
15%
Sai s xl (v/p)
0.41
37
38
if(SetSpeed > 0)
USetSpeed = (2952.0 - SetSpeed)/1135.4; //QUY DOI TOC DO RA DIEN AP
else
USetSpeed = (2720.0 - SetSpeed)/1133.8;
a[0][0] = a0;
a[1][0] = a1;
a[2][0] = a2;
//on tnh tn hiu iu khin:
double CBAI4Dlg::PID(double currentspeed)
{
static double u[2]; //u[0]: u(k-1); u[1]: u(k)
static double e[3]; //e[0] la` e1(k), e[1]: e1(k-1), e[2]: e1(k-2)
double etemp;
double e2;
int i1;
/*
*/
/* Tinh' e2
//*/
if(Output<=2.4) e2 = 2.5-(2695.0-SetSpeed-25)/1133.8+Output; // 50ms (0V->2.5V)
//if(u<2.4) e = 2.4-(2696.5-dat)/1135.9-u; // 50ms (0V->2.5V) (them 2.3V)
if(Output>2.4 & Output<2.6) e2 = 2.5;
if(Output>=2.6) e2 = 2.6-(2897.7- SetSpeed)/1135.4+Output; // 50ms (2.5V->5V)
//sai lech dau vao tinh theo ap
//
e[2] = USetSpeed - etemp;
/* Tinh e1(k) tuc la e[2]*/
if(Output<=2.4) e[2] = 2.4-(2695.0-(SetSpeed - currentspeed-25))/1133.8;
// 50ms (0V->2.5V)
//
if(u<2.4) e_omega0 = 2.4-(2696.5-(dat - s/T1))/1135.9; // 50ms (0V->2.5V)
if(Output>2.4 & Output<2.6) e[2] = 2.5;
40
//
//
//sai lech tinh theo ap giua toc do dat va tin hieu dieu khien
Output = 2.5 - Output;
e2 = -USetSpeed + Output;
for(i1 =0;i1<3; i1++)
//Cong thuc cap nhat thong so: an[k+1] = an[k] +beta*e2[k]*e1[k-n]
a[i1][1] = a[i1][0] + beta*e[2-i1]*e2;
u[1] = u[0] + a[0][1]*e[2] + a[1][1]*e[1] + a[2][1]*e[0];
u[0] = u[1];
e[0] = e[1];
e[1] = e[2];
for (i1 = 0; i1<3; i1++)
a[i1][0] = a[i1][1];
return u[1];
}
//on xut tn hiu iu khin:
void CBAI4Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CString s;
double m_speed;
//
static double updatespeed[MAXUPDATE];
double avrspeed =0;
if(m_nTimer)
{
m_speed = DataPulse()*speedratio/T0;
updatespeed[MAXUPDATE-1] = m_speed;
for(i=0; i<MAXUPDATE; i++){
avrspeed+= updatespeed[i];}
avrspeed = avrspeed/MAXUPDATE; //TINH TRUNG BINH DE TRANH' NHIEU
for (i=0; i<MAXUPDATE-1; i++)
updatespeed[i] = updatespeed[i+1];
41
Output = (float)PID(avrspeed);//********************************
Output = 2.5 - Output;
if(Output> MaxValueOut)
Output = MaxValueOut;
if(Output < MinValueOut)
Output = MinValueOut;
ptAOVoltageOut.OutputValue = (Output);
DRV_AOVoltageOut(DriverHandle,(LPT_AOVoltageOut)&ptAOVoltage
Out);
if(i<1000)
{
CurrentSpeed[i] = avrspeed;
i++;
//
}
//plot the measure speed, set speed, control voltage
Scope1.Channels[0].Data.AddYPoint(SetSpeed);
Scope1.Channels[1].Data.AddYPoint(avrspeed);
Scope2.Channels[0].Data.AddYPoint(Output);
}
dwCurrentTime = GetTickCount()- gdwStartTime;
s.Format("%0d", dwCurrentTime/1000);
this->SetDlgItemText(IDC_STATIC_RUN, s);
s.Format("%2.2f",Output);
this->SetDlgItemText(IDC_STATIC_VOLTAGE, s);
s.Format("%2.2f",SetSpeed - m_speed);
this->SetDlgItemText(IDC_STATIC_ERROR, s);
s.Format("%4d",m_speed);
this->SetDlgItemText(IDC_STATIC_MEASPEED, s);
s.Format("%4d",SetSpeed);
this->SetDlgItemText(IDC_STATIC_SETSPEED, s);
CDialog::OnTimer(nIDEvent);
}
42
vt l(%)
12
4
Sai s
Xc lp(v/p)
10
0
43
Th nghim vi tc t 1000(v/p):
B
Thi gian
Thi gian
iu Khin
ln(s)
xc lp(s)
PID
10
27
PID thch nghi 4.5
12
vt l(%)
4.4
8.5
Sai s
Xc lp(v/p)
0
0
44
45
Th nghim vi tc t 2000(v/p):
B
Thi gian
Thi gian
iu Khin
ln(s)
xc lp(s)
PID
10
50
PID thch nghi 2.5
16
vt l(%)
25
10
Sai s
Xc lp(v/p)
0
0
Th nghim vi tc t -200(v/p):
B
Thi gian
Thi gian
iu Khin
ln(s)
xc lp(s)
PID
14
40
PID thch nghi 2
12
vt l(%)
15
30
Sai s
Xc lp(v/p)
3
0
46
Th nghim vi tc t -2000(v/p):
B
Thi gian
Thi gian
iu Khin
ln(s)
xc lp(s)
vt l(%)
Sai s
Xc lp(v/p)
47
PID
10
PID thch nghi 3
50
6
25
53
0
0
49