- Timestamp:
- 12/02/13 13:54:59 (10 years ago)
- 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> 2 2 #include <stdio.h> 3 3 #include "fast_exp.h" 4 4 #include "fastexp.h" 5 5 #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. 6 15 7 16 static floatx om[N]; … … 14 23 static floatx w[N]; 15 24 static floatx lwi[N]; 25 static int i_best; 16 26 17 27 static floatx qth; … … 20 30 static floatx rinv; 21 31 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 33 static float _ad; 34 static float _aq; 35 static float _b; 36 static float _cd; 37 static float _cq; 38 static float _dd; 39 static float _dq; 66 40 67 41 … … 95 69 96 70 while ( i_from<N ) { 71 w[i_from]=1.0/N; 97 72 while ( N_babies[i_from]>1 ) { // 1 baby stays where it is 98 73 … … 139 114 floatx ydC; 140 115 141 floatx maxlwi, sumlwi ;116 floatx maxlwi, sumlwi,sumw2,neff_th; 142 117 int i; 143 118 … … 145 120 floatx *w_i, *lw_i; 146 121 floatx *sth_i, *cth_i; 122 floatx sumw_inv; 147 123 // implementation starts here 148 124 th_i = &th[0]; … … 155 131 for ( i=0; i<N; i++ ) { 156 132 133 /////////////////////////// HACK!!!!!!!!!!! 134 //om[i] = 0; 135 /////////////////////////// HACK!!!!!!!!!!! 136 157 137 // time update 158 138 *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; 160 142 while ( *th_i>M_PI ) *th_i -=2*M_PI; 161 143 while ( *th_i<-M_PI ) *th_i+=2*M_PI; … … 170 152 usq = -(*sth_i)*usa+(*cth_i)*usb; 171 153 172 Cd = isq*_d t;173 Cq = -_b - isd* _d t;154 Cd = isq*_dd; 155 Cq = -_b - isd* _dq; 174 156 175 157 difid=isd- _ad * (*isdm_i) - _cd *(usd); … … 211 193 212 194 } 213 /*maxlwi=-1e10;195 maxlwi=-1e10; 214 196 lw_i = &lwi[0]; 197 i_best = 0; 215 198 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 /*!!*/ 218 202 lw_i = &lwi[0]; 219 203 w_i = &w[0]; 220 204 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 223 207 } 224 208 225 209 sumlwi=0.0; 210 sumw2=0.0; 226 211 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! 228 214 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(); 237 220 } 238 221 … … 240 223 void mpf_init(floatx qom0, floatx qth0, floatx r0) { 241 224 242 rng_init();225 // rng_init(); 243 226 244 227 int i; … … 259 242 w[i]=1.0/N; 260 243 } 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 261 265 262 266 } … … 277 281 278 282 } 283 284 void 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 279 292 void mpf_th(floatx th1[N]){ 280 293 int i; … … 330 343 usq = -sth*usa+cth*usb; 331 344 332 Cd = isq*_d t;333 Cq = -_b - isd* _d t;345 Cd = isq*_dd; 346 Cq = -_b - isd* _dq; 334 347 335 348 difid=isd- _ad *isdm - _cd *usd;