際際滷

際際滷Share a Scribd company logo
K畛 thu畉t l畉p tr狸nh
010101010101010110000101010101010101011000010101010101010101100001
010101010010101010010101010101001010101001010101010100101010100101
101001100011001001001010100110001100100100101010011000110010010010
110010110010001000001011001011001000100000101100101100100010000010
010101010101010110000101010101010101011000010101010101010101100001
010101010010101010010101010101001010101001010101010100101010100101
101001100011001001001010100110001100100100101010011000110010010010
110010110010001000001011001011001000100000101100101100100010000010
010101010101010110000101010101010101011000010101010101010101100001
010101010010101010010101010101001010101001010101010100101010100101
101001100011001001001010100110001100100100101010011000110010010010
110010110010001000001011001011001000100000101100101100100010000010
8/13/2007
y = A*x + B*u;
x = C*x + d*u;
StateController
start()
stop()
LQGController
start()
stop()
Ch動董ng 8: Ti畉n t畛i t動 duy l畉p tr狸nh
h動畛ng 畛i t動畛ng
2Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
N畛i dung ch動董ng 8
8.1 畉t v畉n 畛
8.2 Gi畛i thi畛u v鱈 d畛 ch動董ng tr狸nh m担 ph畛ng
8.3 T動 duy "r畉t" c畛 i畛n
8.4 T動 duy h動畛ng hm
8.5 T動 duy d畛a tr棚n 畛i t動畛ng (object-based)
8.6 T動 duy th畛c s畛 h動畛ng 畛i t動畛ng
3Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
8.1 畉t v畉n 畛
Designing object-oriented software is hard, and designing reusable
object-oriented software is even harder...It takes a long time for
novices to learn what object-oriented design is all about. Exprienced
designers evidently know something inexperienced ones don't...
One thing expert designers know not to do is solve every problem from
first principles. Rather, they reuse solutions that have worked for
them in the past. When they find a good solution, they use it again
and again. Such experience is part of what makes them experts.
Consequently, you'll find recurring patterns of classes and
communicating objects in many object-oriented systems. These
patterns solve specific design problems and make object-oriented
design more flexible, elegant, and ultimately reusable...
Erich Gamma et. al.: Design Patterns: Elements of Reusable Object-
Oriented Software, Addison-Wesley, 1995.
4Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
8.2 Ph畉n m畛m m担 ph畛ng ki畛u FBD
StaticGain Limiter IntegratorSum Scope1(t)
Nhi畛m v畛:
X但y d畛ng ph畉n m畛m 畛 h畛 tr畛 m担 ph畛ng th畛i gian th畛c m畛t
c叩ch linh ho畉t, m畛m d畉o, 叩p 畛ng 動畛c c叩c y棚u c畉u c畛a t畛ng
bi to叩n c畛 th畛
Tr動畛c m畉t ch動a c畉n h畛 tr畛 t畉o 畛ng d畛ng ki畛u k辿o th畉 b畉ng
c担ng c畛 畛 h畛a
5Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
8.3 T動 duy r畉t c畛 i畛n
// SimProg1.cpp
#include <iostream.h>
#include <conio.h>
#include <windows.h>
void main() {
double K =1,I=0, Ti = 5;
double Hi = 10, Lo = -10;
double Ts = 0.5;
double r =1, y=0, e, u, ub;
cout << "uty";
while (!kbhit()) {
e = r-y; // Sum block
u = K*e; // Static Gain
ub = max(min(u,Hi),Lo); // Limiter
I += ub*Ts/Ti; // Integrator state
y = I; // Integrator output
cout << 'n' << u << 't' << y;
cout.flush();
Sleep(long(Ts*1000));
}
}
6Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
V畉n 畛?
Ph畉n m畛m d動畛i d畉ng ch動董ng tr狸nh, kh担ng c坦 gi叩 tr畛
s畛 d畛ng l畉i
R畉t kh坦 thay 畛i ho畉c m畛 r畛ng theo y棚u c畉u c畛 th畛
c畛a t畛ng bi to叩n
Ton b畛 thu畉t to叩n 動畛c g坦i trong m畛t ch動董ng tr狸nh
=> kh坦 theo d探i, d畛 g但y l畛i, kh担ng b畉o v畛 動畛c ch畉t
x叩m
7Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
// SimProg2.cpp
#include <iostream.h>
#include <conio.h>
#include <windows.h>
#include "SimFun.h"
void main() {
double K = 5.0, double Ti = 5.0;
double Hi = 10, Lo = -10;
double Ts = 0.5;
double r =1, y=0, e, u, ub;
cout << "uty";
while (!kbhit()) {
e = sum(r,-y); // Sum block
u = gain(K,e); // Static Gain
ub= limit(Hi,Lo,u); // Limiter
y = integrate(Ti,Ts,ub); // Integrator output
cout << 'n' << u << 't' << y;
cout.flush();
Sleep(long(Ts*1000));
}
}
8.4 T動 duy h動畛ng hm
8Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
// SimFun.h
inline double sum(double x1, double x2) { return x1 + x2; }
inline double gain(double K, double x) { return K * x; }
double limit(double Hi, double Lo, double x);
double integrate(double Ti, double Ts, double x);
// SimFun.cpp
double limit(double Hi, double Lo, double x) {
if (x > Hi) x = Hi;
if (x < Lo) x = Lo;
return x;
}
double integrate(double Ti, double Ts, double x) {
static double I = 0;
I += x*Ts/Ti;
return I;
}
9Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
V畉n 畛?
V畉n ch動a 畛 t鱈nh linh ho畉t, m畛m d畉o c畉n thi畉t
Thay 畛i, m畛 r畛ng ch動董ng tr狸nh m担 ph畛ng r畉t kh坦
khn
C叩c kh但u c坦 tr畉ng th叩i nh動 kh但u t鱈ch ph但n, kh但u tr畛
kh坦 th畛c hi畛n m畛t c叩ch "s畉ch s畉" (tr畉ng th叩i l動u tr畛
d動畛i d畉ng no?)
R畉t kh坦 ph叩t tri畛n thnh ph畉n m畛m c坦 h畛 tr畛 畛 h畛a
ki畛u k辿o th畉
10Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
8.5 T動 duy d畛a 畛i t動畛ng
// SimClass.h
class Sum {
public:
double operator()(double x1, double x2) {
return x1 + x2;
}
};
class Gain {
double K;
public:
Gain(double k = 1) : K(k) {}
double operator()(double x){ return K * x; }
};
class Limiter {
double Hi, Lo;
public:
Limiter(double h=10.0, double l= -10.0);
double operator()(double x);
};
11Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
class Integrator {
double Ki, Ts;
double I;
public:
Integrator(double ti = 1.0, double ts = 0.5);
double operator()(double x);
};
class Delay {
double* bufPtr;
int bufSize;
double Td, Ts;
public:
Delay(double td = 0, double ts = 1);
Delay(const Delay&);
Delay& operator=(Delay&);
~Delay();
double operator()(double x);
private:
void createBuffer(int sz);
};
12Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
#include <math.h>
#include "SimClass.h"
Limiter::Limiter(double h, double l) : Hi(h), Lo(l) {
if (Hi < Lo) Hi = Lo;
}
double Limiter::operator()(double x) {
if (x > Hi) x = Hi;
if (x < Lo) x = Lo;
return x;
}
Integrator::Integrator(double ti, double ts)
: Ts(1), Ki(1), I(0) {
if (ts > 0)
Ts = ts;
if (ti > 0)
Ki = ts/ti;
}
double Integrator::operator()(double x) {
I += x*Ki;
return I;
}
13Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
Delay::Delay(double td, double ts) : Td(td), Ts(ts) {
if (Td < 0) Td = 0;
if (Ts < 0) Ts = 1;
createBuffer((int)ceil(Td/Ts));
}
double Delay::operator()(double x) {
if (bufSize > 0) {
double y = bufPtr[0];
for (int i=0; i < bufSize-1; ++i)
bufPtr[i] = bufPtr[i+1];
bufPtr[bufSize-1] = x;
return y;
}
return x;
}
void Delay::createBuffer(int sz) {
bufSize = sz;
bufPtr = new double[bufSize];
for (int i=0; i < bufSize; ++i)
bufPtr[i] = 0.0;
}
...
14Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
// SimProg3.cpp
#include <iostream.h>
#include <conio.h>
#include <windows.h>
#include "SimClass.h"
void main() {
double Ts = 0.5;
Sum sum;
Gain gain(2.0);
Limiter limit(10,-10);
Integrator integrate(5,Ts);
Delay delay(1.0);
double r =1, y=0, e, u, ub;
cout << "uty";
while (!kbhit()) {
e = sum(r,-y); // Sum block
u = gain(e); // Static Gain
ub= limit(u); // Limiter
y = integrate(ub);// Integrator output
y = delay(y);
cout << 'n' << u << 't' << y;
cout.flush();
Sleep(long(Ts*1000));
}
}
15Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
V畉n 畛?
Khi s畛 l動畛ng c叩c kh畛i l畛n l棚n th狸 qu畉n l箪 th畉 no?
Khi quan h畛 gi畛a c叩c kh畛i ph畛c t畉p h董n (nhi畛u vo,
nhi畛u ra) th狸 t畛 ch畛c quan h畛 gi畛a c叩c 畛i t動畛ng nh動
th畉 no?
Lm th畉 no 畛 t畉o v qu畉n l箪 c叩c 畛i t動畛ng m畛t c叩ch
畛ng (trong l炭c ch動董ng tr狸nh ang ch畉y)?
L畉p tr狸nh d畛a 畛i t動畛ng m畛i mang l畉i 動u i畛m v畛
m畉t an ton, tin c畉y, nh動ng ch動a mang l畉i 動u i畛m
v畛 t鱈nh linh ho畉t c畉n thi畉t c畛a ph畉n m畛m => gi叩 tr畛
s畛 d畛ng l畉i ch動a cao.
16Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
8.6 T動 duy h動畛ng 畛i t動畛ng
class FB {
public:
virtual void execute() = 0;
private:
virtual double* getOutputPort(int i=0) = 0;
virtual void setInputPort(double* pFromOutputPort,
int i=0)= 0;
friend class FBD;
};
Chi畛u d畛 li畛u
y0
px1=&y0y1
px0
px0
px1
px2
y0
Chi畛u li棚n k畉t
17Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
class Sum : public FB {
public:
Sum(bool plus_sign1 = true, bool plus_sign2 = false);
void execute();
private:
bool sign[2];
double *px[2];
double y;
double* getOutputPort(int i=0);
void setInputPort(double* pFromOutputPort, int i=0);
};
Sum::Sum(bool plus_sign1, bool plus_sign2): y(0) {
px[0] = px[1] = 0;
sign[0] = plus_sign1;
sign[1] = plus_sign2;
}
void Sum::execute() {
if (px[0] != 0) y = sign[0] ? *(px[0]) : - *(px[0]);
if (px[1] != 0) y += sign[1] ? *(px[1]) : - *(px[1]);
}
double* Sum::getOutputPort(int) {
return &y;
}
void Sum::setInputPort(double* pFromOutputPort, int i) {
if(i < 2)
px[i] = pFromOutputPort;
}
18Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
class Limiter: public FB {
public:
Limiter(double h=10.0, double l = -10.0);
void execute();
private:
double Hi, Lo;
double *px;
double y;
double* getOutputPort(int i=0);
void setInputPort(double* pFromOutputPort, int i=0);
};
Limiter::Limiter(double h, double l) : Hi(h), Lo(l), y(0), px(0) {
if (Hi < Lo) Hi = Lo; }
void Limiter::execute() {
if (px != 0) {
y = *px;
if (y > Hi) y = Hi;
if (y < Lo) y = Lo;
}
}
double* Limiter::getOutputPort(int) {
return &y;
}
void Limiter::setInputPort(double* pFromOutputPort, int i) {
px = pFromOutputPort;
}
19Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
#include <vector>
#include <windows.h>
class FBD : public std::vector<FB*> {
double Ts;
bool stopped;
public:
FBD(double ts = 0.5): Ts (ts > 0? ts : 1), stopped(true) {}
void addFB(FB* p) { push_back(p); }
void connect(int i1, int i2, int oport=0, int iport = 0) {
FB *fb1= at(i1), *fb2= at(i2);
fb2->setInputPort(fb1->getOutputPort(oport),iport);
}
void start();
~FBD();
};
FBD::~FBD() {
for (int i=0; i < size(); ++i)
delete at(i);
}
void FBD::start() {
while(!kbhit()) {
for (int i=0; i < size(); ++i)
at(i)->execute();
Sleep(long(Ts*1000));
}
}
20Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
#include <iostream>
#include "SimFB.h"
void main() {
double Ts=0.5;
FBD fbd(0.5);
fbd.addFB(new Step(1.0)); // 0
fbd.addFB(new Sum); // 1
fbd.addFB(new Gain(5.0)); // 2
fbd.addFB(new Limiter(10,-10)); // 3
fbd.addFB(new Integrator(5,Ts)); // 4
fbd.addFB(new Delay(0.0, Ts)); // 5
fbd.addFB(new Scope(std::cout)); // 6
for(int i=0; i < fbd.size()-1; ++i)
fbd.connect(i,i+1);
fbd.connect(5,1,0,1);
fbd.connect(3,6,0,1);
std::cout << "ytu";
fbd.start();
}
21Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng
Bi t畉p v畛 nh
Luy畛n t畉p l畉i tr棚n m叩y t鱈nh c叩c v鱈 d畛 t畛 ph畉n 8.3  8.5
D畛a tr棚n c叩c v鱈 d畛 l畛p 達 x但y d畛ng 畛 ph畉n 8.6 (Limiter, Sum),
b畛 sung c叩c l畛p c嘆n l畉i (Step, Scope, Gain, Integrator, Delay)
Ch畉y th畛 l畉i ch動董ng tr狸nh 畛 ph畉n 8.6 sau khi 達 hon thi畛n c叩c
l畛p c畉n thi畉t.
B畛 sung l畛p Pulse 畛 m担 ph畛ng t叩c 畛ng c畛a nhi畛u qu叩 tr狸nh
(d畉ng xung vu担ng bi棚n 畛 nh畛, chu k畛 畉t 動畛c). M畛 r畛ng
ch動董ng tr狸nh m担 ph畛ng nh動 minh h畛a tr棚n h狸nh v畉.
Gain Limiter IntegratorSum ScopeSumDelayStep
Pulse

More Related Content

C8 object-oriented thinking

  • 1. K畛 thu畉t l畉p tr狸nh 010101010101010110000101010101010101011000010101010101010101100001 010101010010101010010101010101001010101001010101010100101010100101 101001100011001001001010100110001100100100101010011000110010010010 110010110010001000001011001011001000100000101100101100100010000010 010101010101010110000101010101010101011000010101010101010101100001 010101010010101010010101010101001010101001010101010100101010100101 101001100011001001001010100110001100100100101010011000110010010010 110010110010001000001011001011001000100000101100101100100010000010 010101010101010110000101010101010101011000010101010101010101100001 010101010010101010010101010101001010101001010101010100101010100101 101001100011001001001010100110001100100100101010011000110010010010 110010110010001000001011001011001000100000101100101100100010000010 8/13/2007 y = A*x + B*u; x = C*x + d*u; StateController start() stop() LQGController start() stop() Ch動董ng 8: Ti畉n t畛i t動 duy l畉p tr狸nh h動畛ng 畛i t動畛ng
  • 2. 2Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng N畛i dung ch動董ng 8 8.1 畉t v畉n 畛 8.2 Gi畛i thi畛u v鱈 d畛 ch動董ng tr狸nh m担 ph畛ng 8.3 T動 duy "r畉t" c畛 i畛n 8.4 T動 duy h動畛ng hm 8.5 T動 duy d畛a tr棚n 畛i t動畛ng (object-based) 8.6 T動 duy th畛c s畛 h動畛ng 畛i t動畛ng
  • 3. 3Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng 8.1 畉t v畉n 畛 Designing object-oriented software is hard, and designing reusable object-oriented software is even harder...It takes a long time for novices to learn what object-oriented design is all about. Exprienced designers evidently know something inexperienced ones don't... One thing expert designers know not to do is solve every problem from first principles. Rather, they reuse solutions that have worked for them in the past. When they find a good solution, they use it again and again. Such experience is part of what makes them experts. Consequently, you'll find recurring patterns of classes and communicating objects in many object-oriented systems. These patterns solve specific design problems and make object-oriented design more flexible, elegant, and ultimately reusable... Erich Gamma et. al.: Design Patterns: Elements of Reusable Object- Oriented Software, Addison-Wesley, 1995.
  • 4. 4Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng 8.2 Ph畉n m畛m m担 ph畛ng ki畛u FBD StaticGain Limiter IntegratorSum Scope1(t) Nhi畛m v畛: X但y d畛ng ph畉n m畛m 畛 h畛 tr畛 m担 ph畛ng th畛i gian th畛c m畛t c叩ch linh ho畉t, m畛m d畉o, 叩p 畛ng 動畛c c叩c y棚u c畉u c畛a t畛ng bi to叩n c畛 th畛 Tr動畛c m畉t ch動a c畉n h畛 tr畛 t畉o 畛ng d畛ng ki畛u k辿o th畉 b畉ng c担ng c畛 畛 h畛a
  • 5. 5Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng 8.3 T動 duy r畉t c畛 i畛n // SimProg1.cpp #include <iostream.h> #include <conio.h> #include <windows.h> void main() { double K =1,I=0, Ti = 5; double Hi = 10, Lo = -10; double Ts = 0.5; double r =1, y=0, e, u, ub; cout << "uty"; while (!kbhit()) { e = r-y; // Sum block u = K*e; // Static Gain ub = max(min(u,Hi),Lo); // Limiter I += ub*Ts/Ti; // Integrator state y = I; // Integrator output cout << 'n' << u << 't' << y; cout.flush(); Sleep(long(Ts*1000)); } }
  • 6. 6Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng V畉n 畛? Ph畉n m畛m d動畛i d畉ng ch動董ng tr狸nh, kh担ng c坦 gi叩 tr畛 s畛 d畛ng l畉i R畉t kh坦 thay 畛i ho畉c m畛 r畛ng theo y棚u c畉u c畛 th畛 c畛a t畛ng bi to叩n Ton b畛 thu畉t to叩n 動畛c g坦i trong m畛t ch動董ng tr狸nh => kh坦 theo d探i, d畛 g但y l畛i, kh担ng b畉o v畛 動畛c ch畉t x叩m
  • 7. 7Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng // SimProg2.cpp #include <iostream.h> #include <conio.h> #include <windows.h> #include "SimFun.h" void main() { double K = 5.0, double Ti = 5.0; double Hi = 10, Lo = -10; double Ts = 0.5; double r =1, y=0, e, u, ub; cout << "uty"; while (!kbhit()) { e = sum(r,-y); // Sum block u = gain(K,e); // Static Gain ub= limit(Hi,Lo,u); // Limiter y = integrate(Ti,Ts,ub); // Integrator output cout << 'n' << u << 't' << y; cout.flush(); Sleep(long(Ts*1000)); } } 8.4 T動 duy h動畛ng hm
  • 8. 8Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng // SimFun.h inline double sum(double x1, double x2) { return x1 + x2; } inline double gain(double K, double x) { return K * x; } double limit(double Hi, double Lo, double x); double integrate(double Ti, double Ts, double x); // SimFun.cpp double limit(double Hi, double Lo, double x) { if (x > Hi) x = Hi; if (x < Lo) x = Lo; return x; } double integrate(double Ti, double Ts, double x) { static double I = 0; I += x*Ts/Ti; return I; }
  • 9. 9Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng V畉n 畛? V畉n ch動a 畛 t鱈nh linh ho畉t, m畛m d畉o c畉n thi畉t Thay 畛i, m畛 r畛ng ch動董ng tr狸nh m担 ph畛ng r畉t kh坦 khn C叩c kh但u c坦 tr畉ng th叩i nh動 kh但u t鱈ch ph但n, kh但u tr畛 kh坦 th畛c hi畛n m畛t c叩ch "s畉ch s畉" (tr畉ng th叩i l動u tr畛 d動畛i d畉ng no?) R畉t kh坦 ph叩t tri畛n thnh ph畉n m畛m c坦 h畛 tr畛 畛 h畛a ki畛u k辿o th畉
  • 10. 10Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng 8.5 T動 duy d畛a 畛i t動畛ng // SimClass.h class Sum { public: double operator()(double x1, double x2) { return x1 + x2; } }; class Gain { double K; public: Gain(double k = 1) : K(k) {} double operator()(double x){ return K * x; } }; class Limiter { double Hi, Lo; public: Limiter(double h=10.0, double l= -10.0); double operator()(double x); };
  • 11. 11Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng class Integrator { double Ki, Ts; double I; public: Integrator(double ti = 1.0, double ts = 0.5); double operator()(double x); }; class Delay { double* bufPtr; int bufSize; double Td, Ts; public: Delay(double td = 0, double ts = 1); Delay(const Delay&); Delay& operator=(Delay&); ~Delay(); double operator()(double x); private: void createBuffer(int sz); };
  • 12. 12Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng #include <math.h> #include "SimClass.h" Limiter::Limiter(double h, double l) : Hi(h), Lo(l) { if (Hi < Lo) Hi = Lo; } double Limiter::operator()(double x) { if (x > Hi) x = Hi; if (x < Lo) x = Lo; return x; } Integrator::Integrator(double ti, double ts) : Ts(1), Ki(1), I(0) { if (ts > 0) Ts = ts; if (ti > 0) Ki = ts/ti; } double Integrator::operator()(double x) { I += x*Ki; return I; }
  • 13. 13Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng Delay::Delay(double td, double ts) : Td(td), Ts(ts) { if (Td < 0) Td = 0; if (Ts < 0) Ts = 1; createBuffer((int)ceil(Td/Ts)); } double Delay::operator()(double x) { if (bufSize > 0) { double y = bufPtr[0]; for (int i=0; i < bufSize-1; ++i) bufPtr[i] = bufPtr[i+1]; bufPtr[bufSize-1] = x; return y; } return x; } void Delay::createBuffer(int sz) { bufSize = sz; bufPtr = new double[bufSize]; for (int i=0; i < bufSize; ++i) bufPtr[i] = 0.0; } ...
  • 14. 14Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng // SimProg3.cpp #include <iostream.h> #include <conio.h> #include <windows.h> #include "SimClass.h" void main() { double Ts = 0.5; Sum sum; Gain gain(2.0); Limiter limit(10,-10); Integrator integrate(5,Ts); Delay delay(1.0); double r =1, y=0, e, u, ub; cout << "uty"; while (!kbhit()) { e = sum(r,-y); // Sum block u = gain(e); // Static Gain ub= limit(u); // Limiter y = integrate(ub);// Integrator output y = delay(y); cout << 'n' << u << 't' << y; cout.flush(); Sleep(long(Ts*1000)); } }
  • 15. 15Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng V畉n 畛? Khi s畛 l動畛ng c叩c kh畛i l畛n l棚n th狸 qu畉n l箪 th畉 no? Khi quan h畛 gi畛a c叩c kh畛i ph畛c t畉p h董n (nhi畛u vo, nhi畛u ra) th狸 t畛 ch畛c quan h畛 gi畛a c叩c 畛i t動畛ng nh動 th畉 no? Lm th畉 no 畛 t畉o v qu畉n l箪 c叩c 畛i t動畛ng m畛t c叩ch 畛ng (trong l炭c ch動董ng tr狸nh ang ch畉y)? L畉p tr狸nh d畛a 畛i t動畛ng m畛i mang l畉i 動u i畛m v畛 m畉t an ton, tin c畉y, nh動ng ch動a mang l畉i 動u i畛m v畛 t鱈nh linh ho畉t c畉n thi畉t c畛a ph畉n m畛m => gi叩 tr畛 s畛 d畛ng l畉i ch動a cao.
  • 16. 16Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng 8.6 T動 duy h動畛ng 畛i t動畛ng class FB { public: virtual void execute() = 0; private: virtual double* getOutputPort(int i=0) = 0; virtual void setInputPort(double* pFromOutputPort, int i=0)= 0; friend class FBD; }; Chi畛u d畛 li畛u y0 px1=&y0y1 px0 px0 px1 px2 y0 Chi畛u li棚n k畉t
  • 17. 17Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng class Sum : public FB { public: Sum(bool plus_sign1 = true, bool plus_sign2 = false); void execute(); private: bool sign[2]; double *px[2]; double y; double* getOutputPort(int i=0); void setInputPort(double* pFromOutputPort, int i=0); }; Sum::Sum(bool plus_sign1, bool plus_sign2): y(0) { px[0] = px[1] = 0; sign[0] = plus_sign1; sign[1] = plus_sign2; } void Sum::execute() { if (px[0] != 0) y = sign[0] ? *(px[0]) : - *(px[0]); if (px[1] != 0) y += sign[1] ? *(px[1]) : - *(px[1]); } double* Sum::getOutputPort(int) { return &y; } void Sum::setInputPort(double* pFromOutputPort, int i) { if(i < 2) px[i] = pFromOutputPort; }
  • 18. 18Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng class Limiter: public FB { public: Limiter(double h=10.0, double l = -10.0); void execute(); private: double Hi, Lo; double *px; double y; double* getOutputPort(int i=0); void setInputPort(double* pFromOutputPort, int i=0); }; Limiter::Limiter(double h, double l) : Hi(h), Lo(l), y(0), px(0) { if (Hi < Lo) Hi = Lo; } void Limiter::execute() { if (px != 0) { y = *px; if (y > Hi) y = Hi; if (y < Lo) y = Lo; } } double* Limiter::getOutputPort(int) { return &y; } void Limiter::setInputPort(double* pFromOutputPort, int i) { px = pFromOutputPort; }
  • 19. 19Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng #include <vector> #include <windows.h> class FBD : public std::vector<FB*> { double Ts; bool stopped; public: FBD(double ts = 0.5): Ts (ts > 0? ts : 1), stopped(true) {} void addFB(FB* p) { push_back(p); } void connect(int i1, int i2, int oport=0, int iport = 0) { FB *fb1= at(i1), *fb2= at(i2); fb2->setInputPort(fb1->getOutputPort(oport),iport); } void start(); ~FBD(); }; FBD::~FBD() { for (int i=0; i < size(); ++i) delete at(i); } void FBD::start() { while(!kbhit()) { for (int i=0; i < size(); ++i) at(i)->execute(); Sleep(long(Ts*1000)); } }
  • 20. 20Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng #include <iostream> #include "SimFB.h" void main() { double Ts=0.5; FBD fbd(0.5); fbd.addFB(new Step(1.0)); // 0 fbd.addFB(new Sum); // 1 fbd.addFB(new Gain(5.0)); // 2 fbd.addFB(new Limiter(10,-10)); // 3 fbd.addFB(new Integrator(5,Ts)); // 4 fbd.addFB(new Delay(0.0, Ts)); // 5 fbd.addFB(new Scope(std::cout)); // 6 for(int i=0; i < fbd.size()-1; ++i) fbd.connect(i,i+1); fbd.connect(5,1,0,1); fbd.connect(3,6,0,1); std::cout << "ytu"; fbd.start(); }
  • 21. 21Ch動董ng 8: Ti畉n t畛i t動 duy h動畛ng 畛i t動畛ng Bi t畉p v畛 nh Luy畛n t畉p l畉i tr棚n m叩y t鱈nh c叩c v鱈 d畛 t畛 ph畉n 8.3 8.5 D畛a tr棚n c叩c v鱈 d畛 l畛p 達 x但y d畛ng 畛 ph畉n 8.6 (Limiter, Sum), b畛 sung c叩c l畛p c嘆n l畉i (Step, Scope, Gain, Integrator, Delay) Ch畉y th畛 l畉i ch動董ng tr狸nh 畛 ph畉n 8.6 sau khi 達 hon thi畛n c叩c l畛p c畉n thi畉t. B畛 sung l畛p Pulse 畛 m担 ph畛ng t叩c 畛ng c畛a nhi畛u qu叩 tr狸nh (d畉ng xung vu担ng bi棚n 畛 nh畛, chu k畛 畉t 動畛c). M畛 r畛ng ch動董ng tr狸nh m担 ph畛ng nh動 minh h畛a tr棚n h狸nh v畉. Gain Limiter IntegratorSum ScopeSumDelayStep Pulse