root/pmsm/sim_var.cpp @ 224

Revision 224, 4.0 kB (checked in by smidl, 15 years ago)

doc

  • Property svn:eol-style set to native
RevLine 
[105]1/*!
[81]2  \file
[223]3  \brief Simulation of disturbances in PMSM model, EKF runs with simulated covariances
[81]4  \author Vaclav Smidl.
5
[224]6  \ingroup PMSM
[81]7  -----------------------------------
8  BDM++ - C++ library for Bayesian Decision Making under Uncertainty
9
10  Using IT++ for numerical operations
11  -----------------------------------
12*/
13
14#include <itpp/itbase.h>
15#include <stat/libFN.h>
16#include <estim/libKF.h>
[108]17//#include <estim/libPF.h>
18#include <math/chmat.h>
[81]19
20#include "pmsm.h"
21#include "simulator.h"
[117]22#include "sim_profiles.h"
[81]23
[94]24#include <stat/loggers.h>
[81]25
26using namespace itpp;
27
28int main() {
29        // Kalman filter
30        int Ndat = 90000;
31        double h = 1e-6;
32        int Nsimstep = 125;
33
[94]34        dirfilelog L("exp/sim_var",1000);
[105]35        //memlog L(Ndat);
[94]36       
[81]37        // SET SIMULATOR
38        pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h );
39        double Ww = 0.0;
40        vec dt ( 2 );
41        vec ut ( 2 );
[135]42        vec dut ( 2 );
[131]43        vec dit (2);
[81]44        vec xtm=zeros ( 4 );
[135]45        vec xte=zeros ( 4 );
[81]46        vec xdif=zeros ( 4 );
47        vec xt ( 4 );
[94]48        vec ddif=zeros(2);
[81]49        IMpmsm fxu;
50        //                  Rs    Ls        dt           Fmag(Ypm) kp   p    J     Bf(Mz)
51        fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989,   1.5 ,4.0, 0.04, 0.0 );
52        OMpmsm hxu;
53        mat Qt=zeros ( 4,4 );
[94]54        mat Rt=zeros ( 2,2 );
[81]55
56        // ESTIMATORS
57        vec mu0= "0.0 0.0 0.0 0.0";
58        vec Qdiag ( "62 66 454 0.03" ); //zdenek: 0.01 0.01 0.0001 0.0001
59        vec Rdiag ( "100 100" ); //var(diff(xth)) = "0.034 0.034"
60        mat Q =diag( Qdiag );
61        mat R =diag ( Rdiag );
62        EKFfull Efix ( rx,ry,ru );
63        Efix.set_est ( mu0, 1*eye ( 4 )  );
64        Efix.set_parameters ( &fxu,&hxu,Q,R);
65
66        EKFfull Eop ( rx,ry,ru );
67        Eop.set_est ( mu0, 1*eye ( 4 ) );
68        Eop.set_parameters ( &fxu,&hxu,Q,R);
69
[94]70        EKFfull Edi ( rx,ry,ru );
71        Edi.set_est ( mu0, 1*eye ( 4 ) );
72        Edi.set_parameters ( &fxu,&hxu,Q,R);
73
[170]74        const epdf& Efix_ep = Efix._epdf();
75        const epdf& Eop_ep = Eop._epdf();
76        const epdf& Edi_ep = Edi._epdf();
[105]77       
[94]78        //LOG
[162]79        RV rQ( "{Q }", "16");
80        RV rR( "{R }", "4");
81        RV rUD( "{u_isa u_isb i_isa i_isb }", ones_i(4));
82        RV rDu("{dux duy duxf duyf }",ones_i(4));
83        RV rDi("{disa disb }",ones_i(2));
[94]84        int X_log = L.add(rx,"X");
85        int Efix_log = L.add(rx,"XF");
86        int Eop_log = L.add(rx,"XO");
87        int Edi_log = L.add(rx,"XD");
[105]88        int Q_log = L.add(rQ,"Q");
89        int R_log = L.add(rR,"R");
90        int D_log = L.add(rUD,"D");
[131]91        int Du_log = L.add(rDu,"Du");
92        int Di_log = L.add(rDi,"Di");
[94]93        L.init();
[81]94
95        for ( int tK=1;tK<Ndat;tK++ ) {
96                //Number of steps of a simulator for one step of Kalman
97                for ( int ii=0; ii<Nsimstep;ii++ ) {
[135]98                        sim_profile_steps1 ( Ww , false);
[81]99                        pmsmsim_step ( Ww );
100                };
101                // simulation via deterministic model
102                ut ( 0 ) = KalmanObs[0];
103                ut ( 1 ) = KalmanObs[1];
104                dt ( 0 ) = KalmanObs[2];
105                dt ( 1 ) = KalmanObs[3];
[131]106                dut ( 0 ) = KalmanObs[4];
107                dut ( 1 ) = KalmanObs[5];
108                dit ( 0 ) = KalmanObs[8];
109                dit ( 1 ) = KalmanObs[9];
[81]110
[135]111                xte = fxu.eval ( xtm,ut );
[81]112                //Results:
[135]113                // in xt we have simulation according to the model
[81]114                // in x we have "reality"
[135]115                xt ( 0 ) =x[0];xt ( 1 ) =x[1];xt ( 2 ) =x[2];xt ( 3 ) =x[3];
116                xdif = xt-xte; //xtm is a copy of x[]
[131]117                if (xdif(0)>3.0){
118                        cout << "here" <<endl;
119                        }
[81]120                if ( xdif ( 3 ) >pi ) xdif ( 3 )-=2*pi;
121                if ( xdif ( 3 ) <-pi ) xdif ( 3 ) +=2*pi;
[94]122               
[135]123                ddif = hxu.eval(xt,ut) - dit;
[81]124
125                //Rt = 0.9*Rt + xdif^2
[135]126                Qt*=0.1;
127                Qt += 10*outer_product ( xdif,xdif ); //(x-xt)^2
[81]128
[135]129                if (Qt(0,0)>3.0){
130                        cout << "here" <<endl;
131                        }
132                // For future ref.
133                xtm = xt;
134
135                Rt*=0.1;
136//              Rt += 10*outer_product ( ddif,ddif ); //(x-xt)^2
137
[81]138                //ESTIMATE
139                Efix.bayes(concat(dt,ut));
140                //
[135]141                Eop.set_parameters ( &fxu,&hxu,(Qt+1e-8*eye(4)),(Rt+1e-6*eye(2)));
[81]142                Eop.bayes(concat(dt,ut));
[94]143                //
[105]144                Edi.set_parameters ( &fxu,&hxu,(diag(diag(Qt))+1e-16*eye(4)), (diag(diag(Rt))+1e-3*eye(2)));
[94]145                Edi.bayes(concat(dt,ut));
[81]146               
[94]147                //LOG
[105]148                L.logit(X_log,  vec(x,4)); //vec from C-array
[94]149                L.logit(Efix_log, Efix_ep.mean() ); 
[105]150                L.logit(Eop_log,        Eop_ep.mean() ); 
151                L.logit(Edi_log,        Edi_ep.mean() ); 
152                L.logit(Q_log,  vec(Qt._data(),16) ); 
153                L.logit(R_log,          vec(Rt._data(),4) ); 
154                L.logit(D_log,  vec(KalmanObs,4) ); 
[131]155                L.logit(Du_log, dut ); 
156                L.logit(Di_log, dit ); 
[94]157               
[162]158                L.step();
[81]159        }
160
[162]161        L.finalize();
[135]162        //L.itsave("sim_var.it");       
[105]163       
[81]164
165        return 0;
166}
Note: See TracBrowser for help on using the browser.