root/applications/pmsm/pmsmDS.h @ 357

Revision 357, 9.3 kB (checked in by mido, 15 years ago)

mnoho zmen:
1) presun FindXXX modulu do \system
2) zalozeni dokumentace \doc\local\library_structure.dox
3) presun obsahu \tests\UI primo do \tests
4) namisto \INSTALL zalozen \install.html, je to vhodnejsi pro uzivatele WINDOWS, a snad i obecne
5) snaha o predelani veskerych UI podle nove koncepce, soubory pmsm_ui.h, arx_ui.h, KF_ui.h, libDS_ui.h, libEF_ui.h a loggers_ui.h ponechavam
jen zdokumentacnich duvodu, nic by na nich jiz nemelo zaviset, a po zkontrolovani spravnosti provedenych uprav by mely byt smazany
6) predelani estimatoru tak, aby fungoval s novym UI konceptem
7) vytazeni tridy bdmroot do samostatneho souboru \bdm\bdmroot.h
8) pridana dokumentace pro zacleneni programu ASTYLE do Visual studia, ASTYLE pridan do instalacniho balicku pro Windows

  • Property svn:eol-style set to native
Line 
1/*!
2  \file
3  \brief DataSource for experiments with realistic simulator of the PMSM model
4  \author Vaclav Smidl.
5
6  -----------------------------------
7  BDM++ - C++ library for Bayesian Decision Making under Uncertainty
8
9  Using IT++ for numerical operations
10  -----------------------------------
11*/
12
13#include <stat/loggers.h>
14#include <estim/libKF.h>
15#include "simulator_zdenek/simulator.h"
16#include "pmsm.h"
17
18//! Simulator of PMSM machine with predefined profile on omega
19class pmsmDS : public DS
20{
21
22protected:
23    //! indeces of logged variables
24    int L_x, L_ou, L_oy, L_iu, L_optu;
25    //! Setpoints of omega in timespans given by dt_prof
26    vec profileWw;
27    //! Setpoints of Mz in timespans given by dt_prof
28    vec profileMz;
29    //! time-step for profiles
30    double dt_prof;
31    //! Number of miliseconds per discrete time step
32    int Dt;
33    //! options for logging, - log predictions of 'true' voltage
34    bool opt_modu;
35    //! options for logging, -
36public:
37    //! Constructor with fixed sampling period
38    pmsmDS ()
39    {
40        Dt=125;
41        Drv=RV ( "{o_ua o_ub o_ia o_ib t_ua t_ub o_om o_th Mz }" );
42    }
43    void set_parameters ( double Rs0, double Ls0, double Fmag0, double Bf0, double p0, double kp0, double J0, double Uc0, double DT0, double dt0 )
44    {
45        pmsmsim_set_parameters ( Rs0, Ls0, Fmag0, Bf0, p0, kp0, J0, Uc0, DT0, dt0 );
46    }
47    //! parse options: "modelu" => opt_modu=true;
48    void set_options ( string &opt )
49    {
50        opt_modu = ( opt.find ( "modelu" ) !=string::npos );
51    }
52    void getdata ( vec &dt )
53    {
54        dt.set_subvector(0,vec ( KalmanObs,6 ));
55        dt(6)=x[2];
56        dt(7)=x[3];
57        dt(8)=x[8];
58    }
59    void write ( vec &ut ) {}
60
61    void step()
62    {
63        static int ind=0;
64        static double dW; // increase of W
65        static double Ww; // W
66        static double Mz; // W
67        if ( t>=dt_prof*ind )
68        {
69            ind++;
70            // check omega profile and set dW
71            if ( ind<profileWw.length() )
72            {
73                //linear increase
74                if ( profileWw.length() ==1 )
75                {
76                    Ww=profileWw ( 0 );
77                    dW=0.0;
78                }
79                else
80                {
81                    dW = profileWw ( ind )-profileWw ( ind-1 );
82                    dW *=125e-6/dt_prof;
83                }
84            }
85            else
86            {
87                dW = 0;
88            }
89            // Check Mz profile and set Mz
90            if ( ind<profileMz.length() )
91            {
92                //sudden increase
93                Mz = profileMz(ind);
94            }
95            else
96            {
97                Mz = 0;
98            }
99        }
100        Ww += dW;
101        //Simulate Dt seconds!
102        for ( int i=0; i<Dt; i++ )
103        {
104            pmsmsim_step ( Ww , Mz);
105        }
106//              for ( int i=0;i<Dt;i++ ) {      pmsmsim_noreg_step ( Ww , Mz);}
107
108        //discretization
109        double ustep=1.2;
110        KalmanObs [ 0 ] = ustep*itpp::round( KalmanObs [ 0 ]/ ustep) ;
111        KalmanObs [ 1 ] = ustep*itpp::round(KalmanObs [ 1 ]/ ustep);
112        double istep=0.085;
113        KalmanObs [ 2 ] = istep*itpp::round( KalmanObs [ 2 ]/ istep) ;
114        KalmanObs [ 3 ] = istep*itpp::round(KalmanObs [ 3 ]/ istep);
115
116    };
117
118    void log_add ( logger &L )
119    {
120        L_x = L.add ( rx, "x" );
121        L_oy = L.add ( ry, "o" );
122        L_ou = L.add ( ru, "o" );
123        L_iu = L.add ( ru, "t" );
124        // log differences
125        if ( opt_modu )
126        {
127            L_optu = L.add ( ru, "model" );
128        }
129    }
130
131    void logit ( logger &L )
132    {
133        L.logit ( L_x, vec ( x,4 )      );
134        L.logit ( L_oy, vec_2 ( KalmanObs[2],KalmanObs[3] ) );
135        L.logit ( L_ou, vec_2 ( KalmanObs[0],KalmanObs[1] ) );
136        L.logit ( L_iu, vec_2 ( KalmanObs[4],KalmanObs[5] ) );
137        if ( opt_modu )
138        {
139            double sq3=sqrt ( 3.0 );
140            double ua,ub;
141            double i1=x[0];
142            double i2=0.5* ( -i1+sq3*x[1] );
143            double i3=0.5* ( -i1-sq3*x[1] );
144            double u1=KalmanObs[0];
145            double u2=0.5* ( -u1+sq3*KalmanObs[1] );
146            double u3=0.5* ( -u1-sq3*KalmanObs[1] );
147
148            double du1=1.4* ( double ( i1>0.3 ) - double ( i1<-0.3 ) ) +0.2*i1;
149            double du2=1.4* ( double ( i2>0.3 ) - double ( i2<-0.3 ) ) +0.2*i2;
150            double du3=1.4* ( double ( i3>0.3 ) - double ( i3<-0.3 ) ) +0.2*i3;
151            ua = ( 2.0* ( u1-du1 )- ( u2-du2 )- ( u3-du3 ) ) /3.0;
152            ub = ( ( u2-du2 )- ( u3-du3 ) ) /sq3;
153            L.logit ( L_optu , vec_2 ( ua,ub ) );
154        }
155
156    }
157
158    void set_profile ( double dt, const vec &Ww, const vec &Mz )
159    {
160        dt_prof=dt;
161        profileWw=Ww;
162        profileMz=Mz;
163    }
164
165    void from_setting( const Setting &root )
166    {
167        set_parameters ( root["params"]["Rs"], root["params"]["Ls"], root["params"]["Fmag"], \
168                         root["params"]["Bf"], root["params"]["p"], root["params"]["kp"], \
169                         root["params"]["J"], root["params"]["Uc"], root["params"]["DT"], 1.0e-6 );
170
171        // Default values of profiles for omega and Mz
172        vec profW=vec("1.0");
173        vec profM=vec("0.0");
174        double tstep=1.0;
175        root.lookupValue( "tstep", tstep );
176        UI::get( profW, root, "profileW" );
177        UI::get( profM, root, "profileM" );
178        set_profile (tstep , profW, profM);
179
180        string opts;
181        if ( root.lookupValue( "options", opts ) )
182            set_options(opts);
183    }
184
185    // TODO dodelat void to_setting( Setting &root ) const;
186};
187
188UIREGISTER ( pmsmDS );
189
190
191//! This class behaves like BM but it is evaluating EKF
192class pmsmCRB : public EKFfull
193{
194protected:
195    vec interr;
196    vec old_true;
197    vec secder;
198    int L_CRB;
199    int L_err;
200    int L_sec;
201public:
202    //! constructor
203    pmsmCRB():EKFfull()
204    {
205        old_true=zeros(6);
206    }
207
208    void bayes(const vec &dt)
209    {
210        static vec umin(2);
211        vec u(2);
212        //assume we know state exactly:
213        vec true_state=vec(x,4); // read from pmsm
214        E.set_mu(true_state);
215        mu=true_state;
216
217        //integration error
218        old_true(4)=KalmanObs[4];
219        old_true(5)=KalmanObs[5];// add U
220        u(0) = KalmanObs[0]; // use the required value for derivatives
221        u(1) = KalmanObs[1];
222        interr = (true_state - pfxu->eval(old_true));
223
224        //second derivative
225        IMpmsm2o* pf = dynamic_cast<IMpmsm2o*>(pfxu);
226        if (pf)
227        {
228            secder=pf->eval2o(u-umin);
229        }
230
231        umin =u;
232        EKFfull::bayes(dt);
233        old_true.set_subvector(0,true_state);
234    }
235
236    void log_add(logger &L, const string &name="" )
237    {
238        L_CRB=L.add(rx,"crb");
239        L_err=L.add(rx,"err");
240        L_sec=L.add(rx,"d2");
241    }
242    void logit(logger &L)
243    {
244        L.logit(L_err, interr);
245        L.logit(L_CRB,diag(_R()));
246        L.logit(L_sec,secder);
247    }
248
249    void from_setting( const Setting &root )
250    {
251        diffbifn* IM = UI::build<diffbifn>(root, "IM");
252        diffbifn* OM = UI::build<diffbifn>(root, "OM");
253
254        //parameters
255
256        //statistics
257        int dim=IM->dimension();
258        vec mu0;
259        if (!UI::get( mu0, root, "mu0") )
260            mu0=zeros(dim);
261        mat P0;
262        vec dP0;
263
264        if (UI::get(dP0,root, "dP0") )
265            P0=diag(dP0);
266        else if (!UI::get(P0,root, "P0") )
267            P0=eye(dim);
268
269        set_statistics(mu0,P0);
270
271        vec dQ;
272        UI::get( dQ, root, "dQ");
273        vec dR;
274        UI::get( dR, root, "dR");
275        set_parameters(IM, OM, diag(dQ) , diag(dR));
276
277        //connect
278        RV* drv = UI::build<RV>(root, "drv");
279        set_drv(*drv);
280        RV* rv = UI::build<RV>(root, "rv");
281        set_rv(*rv);
282    }
283
284    // TODO dodelat void to_setting( Setting &root ) const;
285};
286
287UIREGISTER ( pmsmCRB );
288
289
290//! This class behaves like BM but it is evaluating EKF
291class pmsmCRBMz : public EKFfull
292{
293protected:
294    int L_CRB;
295public:
296    //! constructor
297    pmsmCRBMz():EKFfull() {}
298
299    void bayes(const vec &dt)
300    {
301//assume we know state exactly:
302        vec true_state(5);
303        true_state.set_subvector(0,vec(x,4)); // read from pmsm
304        true_state(4)=x[8];
305
306        E.set_mu(true_state);
307        mu = true_state;
308        //hack for ut
309        EKFfull::bayes(dt);
310    }
311
312    void log_add(logger &L, const string &name="" )
313    {
314        L_CRB=L.add(concat(rx,RV("Mz",1,0)),"crbz");
315    }
316    void logit(logger &L)
317    {
318        L.logit(L_CRB,diag(_R()));
319    }
320
321    void from_setting( const Setting &root )
322    {
323        diffbifn* IM = UI::build<diffbifn>(root,"IM");
324        diffbifn* OM = UI::build<diffbifn>(root,"OM");
325
326        //statistics
327        int dim=IM->dimension();
328        vec mu0;
329        vec dP0;
330        mat P0;
331        if (!UI::get(mu0, root, "mu0"))
332            mu0=zeros(dim);
333        if (!UI::get( P0, root, "P0" ))
334            if (UI::get(dP0, root, "dP0"))
335                P0=diag(dP0);
336            else
337                P0=eye(dim);
338
339        set_statistics(mu0,P0);
340
341        vec dQ;
342        UI::get(dQ, root, "dQ");
343        vec dR;
344        UI::get(dR, root, "dR");
345        set_parameters(IM, OM, diag(dQ), diag(dR));
346
347        //connect
348        RV* drv = UI::build<RV>(root, "drv");
349        set_drv(*drv);
350        RV* rv = UI::build<RV>(root, "rv");
351        set_rv(*rv);
352    }
353
354    // TODO dodelat void to_setting( Setting &root ) const;
355};
356
357UIREGISTER ( pmsmCRBMz );
Note: See TracBrowser for help on using the browser.