root/applications/pmsm/pmsmDS.h @ 1239

Revision 1198, 8.3 kB (checked in by smidl, 14 years ago)

do not discretize U

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