Show
Ignore:
Timestamp:
12/02/13 13:54:59 (10 years ago)
Author:
smidl
Message:

upravy rand

Files:
1 modified

Legend:

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

    r1468 r1469  
    1 #include "math.h" 
     1#include <math.h> 
    22#include <stdio.h> 
    33#include "fast_exp.h" 
    44#include "fastexp.h" 
    55#include "mpf_double.h" 
     6 
     7//Parameters of PMSM 
     8#define Lsd (3.465e-3*0.9)      //      *0.9)//H        ... zajimave - chodi to i pri Lsd = Lsq 
     9#define Lsq 3.465e-3//H 
     10//#define Ls  3.465e-3//H 
     11#define Rs 0.28//Ohm 
     12#define Fpm (0.1989*1.1)        //*1.15)        //V.s           // !!xx!! 
     13#define pp 4 
     14#define Ism 35.//40. 
    615 
    716static floatx om[N]; 
     
    1423static floatx w[N]; 
    1524static floatx lwi[N]; 
     25static int i_best; 
    1626 
    1727static floatx qth; 
     
    2030static floatx rinv; 
    2131 
    22 #if 1 
    23 #include "RArandn.h" 
    24 floatx qrandu() { 
    25         if ( randu_i==randu_last ) 
    26                 randu_i =&RArandu[0]; 
    27         else 
    28                 randu_i++; 
    29  
    30         return *randu_i; 
    31 } 
    32  
    33 floatx qrandn() { 
    34         if ( randn_i==randn_last ) 
    35                 randn_i =&RArandn[0]; 
    36         else 
    37                 randn_i++; 
    38  
    39         return *randn_i; 
    40 } 
    41  
    42 void rng_init() 
    43 { 
    44 randu_i=&RArandu[0]; 
    45 randn_i=&RArandn[0]; 
    46 } 
    47  
    48 #else 
    49  
    50 #include "rnor.h" 
    51 floatx qrandu() { 
    52         return UNI; 
    53 } 
    54  
    55 floatx qrandn() { 
    56         return PolarRNG(); 
    57 } 
    58  
    59 void rng_init() 
    60 { 
    61 zigset(86947731 ); 
    62 } 
    63  
    64  
    65 #endif 
     32// staticke promenne pro model 
     33static float _ad; 
     34static float _aq; 
     35static float _b; 
     36static float _cd; 
     37static float _cq; 
     38static float _dd; 
     39static float _dq; 
    6640 
    6741 
     
    9569 
    9670        while ( i_from<N ) { 
     71                w[i_from]=1.0/N; 
    9772                while ( N_babies[i_from]>1 ) { // 1 baby stays where it is 
    9873                         
     
    139114        floatx ydC; 
    140115 
    141         floatx maxlwi, sumlwi; 
     116        floatx maxlwi, sumlwi,sumw2,neff_th; 
    142117        int i; 
    143118         
     
    145120        floatx *w_i, *lw_i; 
    146121        floatx *sth_i, *cth_i; 
     122        floatx sumw_inv; 
    147123// implementation starts here 
    148124        th_i = &th[0]; 
     
    155131        for ( i=0; i<N; i++ ) { 
    156132 
     133          /////////////////////////// HACK!!!!!!!!!!! 
     134          //om[i] = 0; 
     135          /////////////////////////// HACK!!!!!!!!!!! 
     136           
    157137                // time update 
    158138                *Pt_i = 1.0*1.0*(*Pt_i)+qom; // Pt is now predictive variance 
    159                 *th_i += *om_i*_dt+qth *  qrandn() ; 
     139                floatx ff =qrandn() ; 
     140//              printf("%f\n", ff); 
     141                *th_i += *om_i*_dt+qth * ff;  
    160142                while ( *th_i>M_PI ) *th_i -=2*M_PI; 
    161143                while ( *th_i<-M_PI ) *th_i+=2*M_PI; 
     
    170152                usq = -(*sth_i)*usa+(*cth_i)*usb; 
    171153 
    172                 Cd = isq*_dt; 
    173                 Cq = -_b - isd* _dt; 
     154                Cd = isq*_dd; 
     155                Cq = -_b - isd* _dq; 
    174156 
    175157                difid=isd- _ad  * (*isdm_i) - _cd *(usd); 
     
    211193 
    212194        } 
    213 /*      maxlwi=-1e10; 
     195        maxlwi=-1e10; 
    214196        lw_i = &lwi[0]; 
     197        i_best = 0; 
    215198        for ( i=0;i<N;i++,lw_i++ ) { 
    216                 if ( *lw_i>maxlwi ) maxlwi=*lw_i; 
    217         }*/ 
     199                if ( *lw_i>maxlwi ) {maxlwi=*lw_i;i_best=i;} 
     200        } 
     201/*!!*/ 
    218202        lw_i = &lwi[0]; 
    219203        w_i = &w[0]; 
    220204        for ( i=0;i<N;i++,lw_i++,w_i++ ) { 
    221 //              *lw_i-=maxlwi; 
    222                 (*w_i)=fasterexp ( *lw_i ); // always resample -> wmin=1/n; 
     205                *lw_i-=maxlwi; 
     206                (*w_i)*=fasterexp ( *lw_i ); // do not always resample  
    223207        } 
    224208 
    225209        sumlwi=0.0; 
     210        sumw2=0.0; 
    226211        w_i = &w[0]; 
    227         for ( i=0;i<N;i++,w_i++ ) sumlwi+=*w_i; 
     212        for ( i=0;i<N;i++,w_i++ ) {sumlwi+=*w_i;sumw2+=*w_i**w_i;} 
     213        sumw_inv = 1.0/sumlwi; // multiplication is faster than division! 
    228214        w_i = &w[0]; 
    229         for ( i=0;i<N;i++,w_i++ ) *w_i/=sumlwi; 
    230          
    231         // NEFF 
    232         floatx sw2=0.0; 
    233         for ( i=0;i<N;i++ ) sw2+=w[i]*w[i]; 
    234         printf("%f\n",1./sw2*N); 
    235          
    236         resample(); 
     215        for ( i=0;i<N;i++,w_i++ ) *w_i*=sumw_inv; 
     216         
     217        neff_th = 0.8*N*sumw2*sumw_inv*sumw_inv; 
     218        if (1<neff_th) 
     219          resample(); 
    237220} 
    238221 
     
    240223void mpf_init(floatx qom0, floatx qth0, floatx r0) { 
    241224         
    242         rng_init(); 
     225//      rng_init(); 
    243226         
    244227        int i; 
     
    259242                w[i]=1.0/N; 
    260243        } 
     244 
     245   // inicializace promennych 
     246   #if (VYBER_MODELU==0) 
     247    // plny model s respektovanim Lsd/Lsq 
     248    _ad = (1.-Rs*_dt/(Lsd)); 
     249    _aq = (1.-Rs*_dt/(Lsq)); 
     250    _b = Fpm/Lsq*_dt; 
     251    _cd = _dt/(Lsd); 
     252    _cq = _dt/(Lsq); 
     253    _dd = _dt*Lsq/Lsd; 
     254    _dq = _dt*Lsd/Lsq; /**/ 
     255  #else 
     256  // parametry modelu identifikovane z namerenych dat z prototypu v EL204 
     257    _ad = 0.9880; 
     258    _aq = 0.9817; 
     259    _b = 0.0076; 
     260    _cd = 0.0289; 
     261    _cq = 0.0292; 
     262    _dd = _dt; 
     263    _dq = _dt; 
     264  #endif    
    261265 
    262266} 
     
    277281 
    278282} 
     283 
     284void mpf_best( floatx *Ecosth, floatx *Esinth, floatx *Eome )  
     285{ 
     286        *Ecosth=cth[i_best]; 
     287        *Esinth=sth[i_best]; 
     288        *Eome=om[i_best]; 
     289} 
     290 
     291 
    279292void mpf_th(floatx th1[N]){ 
    280293        int i; 
     
    330343                usq = -sth*usa+cth*usb; 
    331344 
    332                 Cd = isq*_dt; 
    333                 Cq = -_b - isd* _dt; 
     345                Cd = isq*_dd; 
     346                Cq = -_b - isd* _dq; 
    334347 
    335348                difid=isd- _ad  *isdm - _cd *usd;