Show
Ignore:
Timestamp:
03/16/12 16:46:40 (12 years ago)
Author:
smidl
Message:

Funkcni 1D Kalman

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • applications/pmsm/simulator_zdenek/ekf_example/mpf_double.cpp

    r1438 r1439  
    22#include "math.h" 
    33#include <stdio.h> 
    4  
    5 static double om[N]; 
    6 static double Pt[N]; 
    7 static double sth[N]; 
    8 static double cth[N]; 
    9 static double isdm[N]; 
    10 static double isqm[N]; 
    11 static double th[N]; 
    12 static double w[N]; 
    13 static double lwi[N]; 
    14  
    15 static double qth; 
    16 static double qom; 
    17 static double r; 
     4//#include <libconfig/lib/grammar.y> 
     5 
     6static floatx om[N]; 
     7static floatx Pt[N]; 
     8static floatx sth[N]; 
     9static floatx cth[N]; 
     10static floatx isdm[N]; 
     11static floatx isqm[N]; 
     12static floatx th[N]; 
     13static floatx w[N]; 
     14static floatx lwi[N]; 
     15 
     16static floatx qth; 
     17static floatx qom; 
     18static floatx r; 
     19 
    1820 
    1921#define MAXrand 100 
    2022#define MAXrandn 100 
    21 static const double RArandu[MAXrand]={0.8147, 0.9058, 0.1270, 0.9134, 0.6324, 0.0975, 0.2785, 0.5469, 0.9575, 0.9649, 
     23static const floatx RArandu[MAXrand]={0.8147, 0.9058, 0.1270, 0.9134, 0.6324, 0.0975, 0.2785, 0.5469, 0.9575, 0.9649, 
    2224                                      0.1576, 0.9706, 0.9572, 0.4854, 0.8003, 0.1419, 0.4218, 0.9157, 0.7922, 0.9595, 0.6557, 0.0357, 
    2325                                      0.8491, 0.9340, 0.6787, 0.7577, 0.7431, 0.3922, 0.6555, 0.1712, 0.7060, 0.0318, 0.2769, 0.0462, 
     
    2931                                      0.9340, 0.1299, 0.5688, 0.4694, 0.0119, 0.3371 
    3032                                     }; 
    31 static const double *randu_i=&RArandu[0]; 
    32 const static double *randu_last=&RArandu[MAXrand-1]; 
    33  
    34 static const double RArandn[MAXrandn]={ 
     33static const floatx *randu_i=&RArandu[0]; 
     34const static floatx *randu_last=&RArandu[MAXrand-1]; 
     35 
     36static const floatx RArandn[MAXrandn]={ 
    3537        0.6353, -0.6313, -1.0181,  0.4801,  0.4855,  0.9901, -0.5412, -1.0290,  1.0641, -0.0132, 
    3638        -0.6014, -2.3252, -0.1821,  0.6682, -0.0050,  0.2189, -1.3335,  0.2431, -0.2454, -0.5803, 
     
    4446        1.2366, -0.7043, -0.2512, -1.1071, -2.3646,  0.2809, -1.6640, -0.1161,  0.2608,  1.2698 
    4547}; 
    46 static const double *randn_i=&RArandn[0]; 
    47 const static double *randn_last=&RArandn[MAXrandn-1]; 
    48  
    49 double randu() { 
     48static const floatx *randn_i=&RArandn[0]; 
     49const static floatx *randn_last=&RArandn[MAXrandn-1]; 
     50 
     51floatx qrandu() { 
    5052        if ( randu_i==randu_last ) 
    5153                randu_i =&RArandu[0]; 
     
    5658} 
    5759 
    58 double randn() { 
     60floatx qrandn() { 
    5961        if ( randn_i==randn_last ) 
    6062                randn_i =&RArandn[0]; 
     
    6769void resample() { 
    6870        int N_babies[N]; 
    69         double cumdist; 
    70         int i; 
    71         double ui; 
    72  
    73         double u0 = randu(); 
     71        floatx cumdist; 
     72        int i; 
     73        floatx ui; 
     74        int i_from=0; 
     75        int i_to=0; 
     76 
     77        floatx u0 = qrandu(); 
    7478 
    7579        int j=0; 
     
    9094 
    9195        // COPY 
    92         int i_from=0; 
    93         int i_to=0; 
     96 
    9497        while ( i_from<N ) { 
    9598                while ( N_babies[i_from]>1 ) { // 1 baby stays where it is 
     
    111114} 
    112115 
    113 void mpf_bayes ( const double &isa, const double &isb , const double &usa, const double &usb ) { 
    114  
    115         double isd; 
    116         double isq; 
    117         double usd; 
    118         double usq; 
    119  
    120         double Cd; 
    121         double Cq; 
    122         double CC; 
    123         double oCC; 
    124  
    125         double difid; 
    126         double difiq; 
    127  
    128         double zeta; 
    129         double Kd; 
    130         double Kq; 
    131         double ro; 
    132         double ypd; 
    133         double ypq; 
    134         double detRy; 
    135         double ydiffd; 
    136         double ydiffq; 
    137         double ydC; 
    138  
    139         double maxlwi, sumlwi; 
    140         int i; 
     116void mpf_bayes ( const floatx isa, const floatx isb , const floatx usa, const floatx usb ) { 
     117 
     118        floatx isd; 
     119        floatx isq; 
     120        floatx usd; 
     121        floatx usq; 
     122 
     123        floatx Cd; 
     124        floatx Cq; 
     125        floatx CC; 
     126        floatx oCC; 
     127 
     128        floatx difid; 
     129        floatx difiq; 
     130 
     131        floatx zeta; 
     132        floatx Kd; 
     133        floatx Kq; 
     134        floatx ro; 
     135        floatx ypd; 
     136        floatx ypq; 
     137        floatx detRy; 
     138        floatx ydiffd; 
     139        floatx ydiffq; 
     140        floatx ydC; 
     141 
     142        floatx maxlwi, sumlwi; 
     143        int i; 
     144         
     145        floatx *th_i, *Pt_i, *om_i, *isdm_i, *isqm_i; 
     146        floatx *w_i, *lw_i; 
     147        floatx *sth_i, *cth_i; 
    141148// implementation starts here 
     149        th_i = &th[0]; 
     150        Pt_i = &Pt[0]; 
     151        om_i = &om[0]; 
     152        isdm_i = &isdm[0]; 
     153        isqm_i = &isqm[0]; 
     154        sth_i = &sth[0]; 
     155        cth_i = &cth[0]; 
    142156        for ( i=0; i<N; i++ ) { 
    143                 Pt[i] = 1.0*1.0*Pt[i]+qom; // Pt is now predictive variance 
    144                 th[i] =th[i] + om[i]*_dt+sqrt ( qth+_dt*_dt*Pt[i] ) *  randn() ; 
    145                 while ( th[i]>M_PI ) th[i]=th[i]-2*M_PI; 
    146                 while ( th[i]<-M_PI ) th[i]=th[i]+2*M_PI; 
    147  
    148                 sth[i]=sin ( th[i] ); 
    149                 cth[i]=cos ( th[i] ); 
    150  
    151                 isd = cth[i]*isa+sth[i]*isb; 
    152                 isq = -sth[i]*isa+cth[i]*isb; 
    153                 usd = cth[i]*usa+sth[i]*usb; 
    154                 usq = -sth[i]*usa+cth[i]*usb; 
     157 
     158                // time update 
     159                *Pt_i = 1.0*1.0*(*Pt_i)+qom; // Pt is now predictive variance 
     160                *th_i += *om_i*_dt+qth *  qrandn() ; 
     161                while ( *th_i>M_PI ) *th_i -=2*M_PI; 
     162                while ( *th_i<-M_PI ) *th_i+=2*M_PI; 
     163 
     164                *sth_i=sin ( *th_i ); 
     165                *cth_i=cos ( *th_i ); 
     166                 
     167                isd = *cth_i*isa+*sth_i*isb; 
     168                isq = -*sth_i*isa+*cth_i*isb; 
     169                // process old voltage 
     170                usd = (*cth_i)*usa+(*sth_i)*usb; 
     171                usq = -(*sth_i)*usa+(*cth_i)*usb; 
    155172 
    156173                Cd = isq*_dt; 
    157174                Cq = -_b - isd* _dt; 
    158175 
    159                 difid=isd- ( 1.0- ( _ad ) ) *isdm[i] - _cd *usd; 
    160                 difiq=isq- ( 1.0- ( _aq ) ) *isqm[i] - _cq *usq; 
     176                difid=isd- _ad  * (*isdm_i) - _cd *(usd); 
     177                difiq=isq- _aq  * (*isqm_i) - _cq *(usq); 
    161178 
    162179                CC=Cd*Cd+Cq*Cq; 
    163                 zeta = Pt[i]/ ( r+Pt[i]*CC ); 
     180                zeta = *Pt_i/ ( r+*Pt_i*CC ); 
    164181                oCC = ( 1-zeta*CC ); 
    165182                ro = oCC/r; 
    166183 
    167                 Kd = Pt[i]*Cd*ro; 
    168                 Kq = Pt[i]*Cq*ro; 
    169  
    170                 Pt[i]=Pt[i]* ( 1- ( Kd*Cd+Kq*Cq ) ); 
    171  
    172                 ypd = Cd*om[i]; 
    173                 ypq = Cq*om[i]; 
    174  
    175                 detRy = ro/r; 
    176                 om[i] = om[i] + Kd* ( difid - ypd ) +Kq* ( difiq-ypq ); 
     184                Kd = *Pt_i*Cd*ro; 
     185                Kq = *Pt_i*Cq*ro; 
     186 
     187                (*Pt_i)=(*Pt_i)*( 1.0- ( Kd*Cd+Kq*Cq ) ); 
     188 
     189                ypd = Cd**om_i; 
     190                ypq = Cq**om_i; 
     191 
     192                //detRy = ro/r; 
     193                *om_i += Kd* ( difid - ypd ) +Kq* ( difiq-ypq ); 
    177194                ydiffd = ( ypd-difid ); 
    178195                ydiffq = ( ypq-difiq ); 
    179196                ydC = ydiffd*Cd + ydiffq*Cq; 
    180197 
    181                 lwi[i] = 0.5* ( log ( detRy ) + ( ydC*ydC*zeta -(ydiffd*ydiffd+ydiffq*ydiffq) ) /r ) ; 
    182                  
    183                 isdm[i]=isd; 
    184                 isqm[i]=isq; 
     198                //lwi[i] = 0.5* ( log ( detRy ) + ( ydC*ydC*zeta -(ydiffd*ydiffd+ydiffq*ydiffq) ) /r ) ; 
     199                lwi[i] = 0.5*( ydC*ydC*zeta -(ydiffd*ydiffd+ydiffq*ydiffq) ) /r ; 
     200                 
     201                *isdm_i=isd; 
     202                *isqm_i=isq; 
     203                 
     204                // shift all counters 
     205                th_i++; 
     206                Pt_i++; 
     207                om_i++; 
     208                isdm_i++; 
     209                isqm_i++; 
     210                sth_i++; 
     211                cth_i++; 
     212 
    185213        } 
    186214        maxlwi=-1e10; 
    187         for ( i=0;i<N;i++ ) { 
    188                 if ( lwi[i]>maxlwi ) maxlwi=lwi[i]; 
    189         } 
    190         for ( i=0;i<N;i++ ) lwi[i]-=maxlwi; 
    191         for ( i=0;i<N;i++ ) w[i]=exp ( lwi[i] ); // always resample -> wmin=1/n; 
     215        lw_i = &lwi[0]; 
     216        for ( i=0;i<N;i++,lw_i++ ) { 
     217                if ( *lw_i>maxlwi ) maxlwi=*lw_i; 
     218        } 
     219        lw_i = &lwi[0]; 
     220        w_i = &w[0]; 
     221        for ( i=0;i<N;i++,lw_i++,w_i++ ) { 
     222                *lw_i-=maxlwi; 
     223                (*w_i)=exp ( *lw_i ); // always resample -> wmin=1/n; 
     224        } 
    192225 
    193226        sumlwi=0.0; 
    194         for ( i=0;i<N;i++ ) sumlwi+=w[i]; 
    195         for ( i=0;i<N;i++ ) w[i]/=sumlwi; 
     227        w_i = &w[0]; 
     228        for ( i=0;i<N;i++,w_i++ ) sumlwi+=*w_i; 
     229        w_i = &w[0]; 
     230        for ( i=0;i<N;i++,w_i++ ) *w_i/=sumlwi; 
     231         
     232        // NEFF 
     233        floatx sw2=0.0; 
     234        for ( i=0;i<N;i++ ) sw2+=w[i]*w[i]; 
     235        printf("%f\n",1./sw2*N); 
     236         
    196237        resample(); 
    197238} 
    198239 
    199240 
    200 void mpf_init(double qom0, double qth0, double r0) { 
     241void mpf_init(floatx qom0, floatx qth0, floatx r0) { 
     242        int i; 
    201243        r=r0; 
    202244        qth=qth0; 
    203245        qom=qom0; 
    204         int i; 
     246 
    205247        for ( i=0; i<N; i++ ) { 
    206                 th[i]=randu() *2*M_PI - M_PI; 
     248                th[i]=qrandu() *2*M_PI - M_PI; 
     249                sth[i]=sin ( th[i] ); 
     250                cth[i]=cos ( th[i] ); 
     251                 
    207252                om[i]=0; 
    208                 Pt[i]=1; 
     253                Pt[i]=10.; 
    209254                isdm[i]=0; 
    210255                isqm[i]=0; 
     
    213258 
    214259} 
    215 void mpf_mean ( double *Ecosth, double *Esinth, double *Eome ) { 
     260void mpf_mean ( floatx *Ecosth, floatx *Esinth, floatx *Eome ) { 
    216261        int i; 
    217262        *Ecosth=0.0; 
     
    225270 
    226271} 
    227 void mpf_th(double th1[N]){ 
    228         for (int i=0;i<N;i++) th1[i]=th[i]; 
    229 } 
    230  
    231 double kalman_om( const double &isa, const double &isb , const double &usa, const double &usb, const double &th ) { 
    232  
    233         double isd; 
    234         double isq; 
    235         double usd; 
    236         double usq; 
    237  
    238         double Cd; 
    239         double Cq; 
    240         double CC; 
    241         double oCC; 
    242  
    243         double difid; 
    244         double difiq; 
    245  
    246         double zeta; 
    247         double Kd; 
    248         double Kq; 
    249         double ro; 
    250         double ypd; 
    251         double ypq; 
    252         double detRy; 
    253         double ydiffd; 
    254         double ydiffq; 
    255         double ydC; 
    256  
    257         static double Pt; 
    258         static double om; 
    259         double cth, sth; 
    260         static double isdm, isqm; 
     272void mpf_th(floatx th1[N]){ 
     273        int i; 
     274        for (i=0;i<N;i++) th1[i]=th[i]; 
     275} 
     276 
     277floatx kalman_om( const floatx isa, const floatx isb , const floatx usa, const floatx usb, const floatx th ) { 
     278 
     279        floatx isd; 
     280        floatx isq; 
     281        floatx usd; 
     282        floatx usq; 
     283 
     284        floatx Cd; 
     285        floatx Cq; 
     286        floatx CC; 
     287        floatx oCC; 
     288 
     289        floatx difid; 
     290        floatx difiq; 
     291 
     292        floatx zeta; 
     293        floatx Kd; 
     294        floatx Kq; 
     295        floatx ro; 
     296        floatx ypd; 
     297        floatx ypq; 
     298        floatx detRy; 
     299        floatx ydiffd; 
     300        floatx ydiffq; 
     301        floatx ydC; 
     302 
     303        static floatx Pt; 
     304        static floatx om; 
     305        floatx cth, sth; 
     306        static floatx isdm, isqm; 
    261307 
    262308