root/applications/pmsm/pmsmDS.h @ 676

Revision 676, 9.3 kB (checked in by smidl, 15 years ago)

logger refactoring

  • 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 <base/loggers.h>
14#include <estim/kalman.h>
15#include "simulator_zdenek/simulator.h"
16#include "pmsm.h"
17
18using namespace bdm;
19
20//! Simulator of PMSM machine with predefined profile on omega
21class pmsmDS : public DS
22{
23
24protected:
25    //! indeces of logged variables
26    int L_x, L_ou, L_oy, L_iu, L_optu;
27    //! Setpoints of omega in timespans given by dt_prof
28    vec profileWw;
29    //! Setpoints of Mz in timespans given by dt_prof
30    vec profileMz;
31    //! time-step for profiles
32    double dt_prof;
33    //! Number of miliseconds per discrete time step
34    int Dt;
35    //! options for logging, - log predictions of 'true' voltage
36    bool opt_modu;
37    //! options for logging, -
38public:
39    //! Constructor with fixed sampling period
40    pmsmDS ()
41    {
42        Dt=125;
43        Drv=RV ( "{o_ua o_ub o_ia o_ib t_ua t_ub o_om o_th Mz }" );
44    }
45    void set_parameters ( double Rs0, double Ls0, double Fmag0, double Bf0, double p0, double kp0, double J0, double Uc0, double DT0, double dt0 )
46    {
47        pmsmsim_set_parameters ( Rs0, Ls0, Fmag0, Bf0, p0, kp0, J0, Uc0, DT0, dt0 );
48    }
49    //! parse options: "modelu" => opt_modu=true;
50    void set_options ( string &opt )
51    {
52        opt_modu = ( opt.find ( "modelu" ) !=string::npos );
53    }
54    void getdata ( vec &dt ) const
55    {
56        dt.set_subvector(0,vec ( KalmanObs,6 ));
57        dt(6)=x[2];
58        dt(7)=x[3];
59        dt(8)=x[8];
60    }
61    void write ( vec &ut ) {}
62
63    void step()
64    {
65        static int ind=0;
66        static double dW; // increase of W
67        static double Ww; // W
68        static double Mz; // W
69        if ( t>=dt_prof*ind )
70        {
71            ind++;
72            // check omega profile and set dW
73                        if ( ind <2 && profileWw.length() ==1 )
74                        {
75                                Ww=profileWw ( 0 );
76                                dW=0.0;
77                        }
78                        if ( ind<profileWw.length() )
79            {
80                    dW = profileWw ( ind )-profileWw ( ind-1 );
81                    dW *=125e-6/dt_prof;
82            }
83            else
84            {
85                dW = 0;
86            }
87            // Check Mz profile and set Mz
88            if ( ind<profileMz.length() )
89            {
90                //sudden increase
91                Mz = profileMz(ind);
92            }
93            else
94            {
95                Mz = 0;
96            }
97        }
98        Ww += dW;
99        //Simulate Dt seconds!
100        for ( int i=0; i<Dt; i++ )
101        {
102            pmsmsim_step ( Ww , Mz);
103        }
104//              for ( int i=0;i<Dt;i++ ) {      pmsmsim_noreg_step ( Ww , Mz);}
105
106        //discretization
107        double ustep=1.2;
108        KalmanObs [ 0 ] = ustep*itpp::round( KalmanObs [ 0 ]/ ustep) ;
109        KalmanObs [ 1 ] = ustep*itpp::round(KalmanObs [ 1 ]/ ustep);
110        double istep=0.085;
111        KalmanObs [ 2 ] = istep*itpp::round( KalmanObs [ 2 ]/ istep) ;
112        KalmanObs [ 3 ] = istep*itpp::round(KalmanObs [ 3 ]/ istep);
113
114    };
115
116    void log_register ( logger &L )
117    {
118        L_x = L.add ( rx, "x" );
119        L_oy = L.add ( ry, "o" );
120        L_ou = L.add ( ru, "o" );
121        L_iu = L.add ( ru, "t" );
122        // log differences
123        if ( opt_modu )
124        {
125            L_optu = L.add ( ru, "model" );
126        }
127    }
128
129    void log_write ( logger &L )
130    {
131        L.logit ( L_x, vec ( x,4 )      );
132        L.logit ( L_oy, vec_2 ( KalmanObs[2],KalmanObs[3] ) );
133        L.logit ( L_ou, vec_2 ( KalmanObs[0],KalmanObs[1] ) );
134        L.logit ( L_iu, vec_2 ( KalmanObs[4],KalmanObs[5] ) );
135        if ( opt_modu )
136        {
137            double sq3=sqrt ( 3.0 );
138            double ua,ub;
139            double i1=x[0];
140            double i2=0.5* ( -i1+sq3*x[1] );
141            double i3=0.5* ( -i1-sq3*x[1] );
142            double u1=KalmanObs[0];
143            double u2=0.5* ( -u1+sq3*KalmanObs[1] );
144            double u3=0.5* ( -u1-sq3*KalmanObs[1] );
145
146            double du1=1.4* ( double ( i1>0.3 ) - double ( i1<-0.3 ) ) +0.2*i1;
147            double du2=1.4* ( double ( i2>0.3 ) - double ( i2<-0.3 ) ) +0.2*i2;
148            double du3=1.4* ( double ( i3>0.3 ) - double ( i3<-0.3 ) ) +0.2*i3;
149            ua = ( 2.0* ( u1-du1 )- ( u2-du2 )- ( u3-du3 ) ) /3.0;
150            ub = ( ( u2-du2 )- ( u3-du3 ) ) /sq3;
151            L.logit ( L_optu , vec_2 ( ua,ub ) );
152        }
153
154    }
155
156    void set_profile ( double dt, const vec &Ww, const vec &Mz )
157    {
158        dt_prof=dt;
159        profileWw=Ww;
160        profileMz=Mz;
161    }
162
163    void from_setting( const Setting &root )
164    {
165                const SettingResolver& params_l(root["params"]);
166                const Setting &params = params_l.result;
167        set_parameters ( params["Rs"], params["Ls"], params["Fmag"], \
168                         params["Bf"], params["p"], params["kp"], \
169                         params["J"], params["Uc"], 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                vec &mu = est->_mu();
213                //assume we know state exactly:
214        vec true_state=vec(x,4); // read from pmsm
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.get());
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        shared_ptr<diffbifn> IM = UI::build<diffbifn>(root, "IM");
252        shared_ptr<diffbifn> OM = UI::build<diffbifn>(root, "OM");
253
254        //parameters
255
256        //statistics
257        int dim=IM->dimension();
258
259        vec mu0;
260        if(root.exists("mu0"))
261                        UI::get( mu0, root, "mu0");
262                else
263            mu0=zeros(dim);
264
265        mat P0;
266        if(root.exists( "dP0" ))
267                {
268                        vec dP0;       
269                        UI::get(dP0,root, "dP0");
270            P0=diag(dP0);
271                }
272                else if (root.exists("P0"))
273                        UI::get(P0,root, "P0");
274                else
275            P0=eye(dim);
276
277        set_statistics(mu0,P0);
278
279        vec dQ;
280        UI::get( dQ, root, "dQ");
281        vec dR;
282        UI::get( dR, root, "dR");
283        set_parameters(IM, OM, diag(dQ) , diag(dR));
284
285        //connect
286        shared_ptr<RV> drv = UI::build<RV>(root, "drv");
287        set_drv(*drv);
288        shared_ptr<RV> rv = UI::build<RV>(root, "rv");
289        set_rv(*rv);
290    }
291
292    // TODO dodelat void to_setting( Setting &root ) const;
293};
294
295UIREGISTER ( pmsmCRB );
296
297
298//! This class behaves like BM but it is evaluating EKF
299class pmsmCRBMz : public EKFfull
300{
301protected:
302    int L_CRB;
303public:
304    //! constructor
305    pmsmCRBMz():EKFfull() {}
306
307    void bayes(const vec &dt)
308    {
309//assume we know state exactly:
310        vec true_state(5);
311        true_state.set_subvector(0,vec(x,4)); // read from pmsm
312        true_state(4)=x[8];
313
314        vec &mu = est->_mu();
315        mu = true_state;
316        //hack for ut
317        EKFfull::bayes(dt);
318    }
319
320    void log_add(logger &L, const string &name="" )
321    {
322        L_CRB=L.add(concat(rx,RV("Mz",1,0)),"crbz");
323    }
324    void logit(logger &L)
325    {
326        L.logit(L_CRB,diag(_R()));
327    }
328
329    void from_setting( const Setting &root )
330    {
331        shared_ptr<diffbifn> IM = UI::build<diffbifn>(root,"IM");
332                shared_ptr<diffbifn> OM = UI::build<diffbifn>(root,"OM");
333
334        //statistics
335        int dim=IM->dimension();
336        vec mu0;
337        if( root.exists( "mu0"))
338                        UI::get(mu0, root, "mu0");
339                else
340            mu0=zeros(dim);
341
342        mat P0;
343
344                if(root.exists("dP0"))
345                {
346                        vec dP0;                               
347                        UI::get(dP0, root, "dP0");
348                        P0=diag(dP0);
349                }
350                else if(root.exists("P0"))
351                        UI::get( P0, root, "P0" );
352                else
353                        P0=eye(dim);
354
355        set_statistics(mu0,P0);
356
357        vec dQ;
358        UI::get(dQ, root, "dQ");
359        vec dR;
360        UI::get(dR, root, "dR");
361        set_parameters(IM, OM, diag(dQ), diag(dR));
362
363        //connect
364                shared_ptr<RV> drv = UI::build<RV>(root, "drv");
365        set_drv(*drv);
366                shared_ptr<RV> rv = UI::build<RV>(root, "rv");
367        set_rv(*rv);
368    }
369
370    // TODO dodelat void to_setting( Setting &root ) const;
371};
372
373UIREGISTER ( pmsmCRBMz );
Note: See TracBrowser for help on using the browser.