root/applications/pmsm/pmsmDS.h @ 894

Revision 894, 9.2 kB (checked in by smidl, 14 years ago)

pmsm stuff

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