Changeset 1439
- Timestamp:
- 03/16/12 16:46:40 (13 years ago)
- Location:
- applications/pmsm/simulator_zdenek/ekf_example
- Files:
-
- 6 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/pmsm/simulator_zdenek/ekf_example/data/kresli_odhad.m
r1382 r1439 1 load graf1.txt -ascii1 %load graf1.txt -ascii 2 2 load graf2.txt -ascii 3 3 -
applications/pmsm/simulator_zdenek/ekf_example/ekf_obj.cpp
r1380 r1439 1121 1121 PSI[3]=0x7FFF; 1122 1122 } 1123 -
applications/pmsm/simulator_zdenek/ekf_example/ekf_obj.h
r1381 r1439 578 578 }; 579 579 void bayes ( const vec &val, const vec &cond ) { 580 const double &isa = val(0);580 /* const double &isa = val(0); 581 581 const double &isb = val(1); 582 582 const double &usa = cond(0); 583 const double &usb = cond(1); 584 mpf_bayes( isa,isb,usa,usb);583 const double &usb = cond(1);*/ 584 mpf_bayes((floatx)val(0),(floatx)val(1),(floatx)cond(0), (floatx)cond(1));//isa,isb,usa,usb); 585 585 } 586 586 587 587 class mp:public epdf{ 588 LOG_LEVEL(mp,logth); 588 589 public: 589 mp():epdf(){set_dim(3);} 590 mp():epdf(){set_dim(3); log_level[logth]=true; 591 } 590 592 vec sample() const {return zeros(3);} 591 593 double evallog(const vec &v) const {return 0.0;} 592 vec mean() const {vec tmp(3); mpf_mean(tmp._data(), tmp._data()+1, tmp._data()+2); return tmp;} 594 vec mean() const {vec tmp(3); 595 floatx es,ec,eo; 596 mpf_mean(&es, &ec, &eo); 597 tmp(0)=es;tmp(1)=ec;tmp(2)=eo; 598 return tmp; 599 } 593 600 vec variance() const {return zeros(3);} 601 void log_register ( bdm::logger& L, const string& prefix ) { 602 epdf::log_register ( L, prefix ); 603 if ( log_level[logth] ) { 604 int th_dim = N; // dimension - dimension of cov 605 L.add_vector( log_level, logth, RV ( th_dim ), prefix ); 606 } 607 } 608 void log_write() const { 609 epdf::log_write(); 610 if ( log_level[logth] ) { 611 floatx Th[N]; 612 mpf_th(Th); 613 vec th(N); 614 for(int i=0;i<N;i++){th(i)=Th[i];} 615 log_level.store( logth, th ); 616 } 617 } 594 618 }; 595 619 … … 598 622 599 623 void from_setting(const Setting &set){ 600 BM::from_setting(set); 624 BM::from_setting(set); 625 UI::get ( log_level, set, "log_level", UI::optional ); 626 601 627 UI::get(qom,set,"qom",UI::optional); 602 628 UI::get(qth,set,"qth",UI::optional); … … 604 630 } 605 631 void validate(){ 606 mpf_init( qom,qth,r);632 mpf_init((floatx)qom,(floatx)qth,(floatx)r); 607 633 608 634 } -
applications/pmsm/simulator_zdenek/ekf_example/mpf_double.cpp
r1438 r1439 2 2 #include "math.h" 3 3 #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 6 static floatx om[N]; 7 static floatx Pt[N]; 8 static floatx sth[N]; 9 static floatx cth[N]; 10 static floatx isdm[N]; 11 static floatx isqm[N]; 12 static floatx th[N]; 13 static floatx w[N]; 14 static floatx lwi[N]; 15 16 static floatx qth; 17 static floatx qom; 18 static floatx r; 19 18 20 19 21 #define MAXrand 100 20 22 #define MAXrandn 100 21 static const doubleRArandu[MAXrand]={0.8147, 0.9058, 0.1270, 0.9134, 0.6324, 0.0975, 0.2785, 0.5469, 0.9575, 0.9649,23 static const floatx RArandu[MAXrand]={0.8147, 0.9058, 0.1270, 0.9134, 0.6324, 0.0975, 0.2785, 0.5469, 0.9575, 0.9649, 22 24 0.1576, 0.9706, 0.9572, 0.4854, 0.8003, 0.1419, 0.4218, 0.9157, 0.7922, 0.9595, 0.6557, 0.0357, 23 25 0.8491, 0.9340, 0.6787, 0.7577, 0.7431, 0.3922, 0.6555, 0.1712, 0.7060, 0.0318, 0.2769, 0.0462, … … 29 31 0.9340, 0.1299, 0.5688, 0.4694, 0.0119, 0.3371 30 32 }; 31 static const double*randu_i=&RArandu[0];32 const static double*randu_last=&RArandu[MAXrand-1];33 34 static const doubleRArandn[MAXrandn]={33 static const floatx *randu_i=&RArandu[0]; 34 const static floatx *randu_last=&RArandu[MAXrand-1]; 35 36 static const floatx RArandn[MAXrandn]={ 35 37 0.6353, -0.6313, -1.0181, 0.4801, 0.4855, 0.9901, -0.5412, -1.0290, 1.0641, -0.0132, 36 38 -0.6014, -2.3252, -0.1821, 0.6682, -0.0050, 0.2189, -1.3335, 0.2431, -0.2454, -0.5803, … … 44 46 1.2366, -0.7043, -0.2512, -1.1071, -2.3646, 0.2809, -1.6640, -0.1161, 0.2608, 1.2698 45 47 }; 46 static const double*randn_i=&RArandn[0];47 const static double*randn_last=&RArandn[MAXrandn-1];48 49 doublerandu() {48 static const floatx *randn_i=&RArandn[0]; 49 const static floatx *randn_last=&RArandn[MAXrandn-1]; 50 51 floatx qrandu() { 50 52 if ( randu_i==randu_last ) 51 53 randu_i =&RArandu[0]; … … 56 58 } 57 59 58 doublerandn() {60 floatx qrandn() { 59 61 if ( randn_i==randn_last ) 60 62 randn_i =&RArandn[0]; … … 67 69 void resample() { 68 70 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(); 74 78 75 79 int j=0; … … 90 94 91 95 // COPY 92 int i_from=0; 93 int i_to=0; 96 94 97 while ( i_from<N ) { 95 98 while ( N_babies[i_from]>1 ) { // 1 baby stays where it is … … 111 114 } 112 115 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; 116 void 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; 141 148 // 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]; 142 156 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; 155 172 156 173 Cd = isq*_dt; 157 174 Cq = -_b - isd* _dt; 158 175 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); 161 178 162 179 CC=Cd*Cd+Cq*Cq; 163 zeta = Pt[i]/ ( r+Pt[i]*CC );180 zeta = *Pt_i/ ( r+*Pt_i*CC ); 164 181 oCC = ( 1-zeta*CC ); 165 182 ro = oCC/r; 166 183 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 ); 177 194 ydiffd = ( ypd-difid ); 178 195 ydiffq = ( ypq-difiq ); 179 196 ydC = ydiffd*Cd + ydiffq*Cq; 180 197 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 185 213 } 186 214 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 } 192 225 193 226 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 196 237 resample(); 197 238 } 198 239 199 240 200 void mpf_init(double qom0, double qth0, double r0) { 241 void mpf_init(floatx qom0, floatx qth0, floatx r0) { 242 int i; 201 243 r=r0; 202 244 qth=qth0; 203 245 qom=qom0; 204 int i; 246 205 247 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 207 252 om[i]=0; 208 Pt[i]=1 ;253 Pt[i]=10.; 209 254 isdm[i]=0; 210 255 isqm[i]=0; … … 213 258 214 259 } 215 void mpf_mean ( double *Ecosth, double *Esinth, double*Eome ) {260 void mpf_mean ( floatx *Ecosth, floatx *Esinth, floatx *Eome ) { 216 261 int i; 217 262 *Ecosth=0.0; … … 225 270 226 271 } 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; 272 void mpf_th(floatx th1[N]){ 273 int i; 274 for (i=0;i<N;i++) th1[i]=th[i]; 275 } 276 277 floatx 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; 261 307 262 308 -
applications/pmsm/simulator_zdenek/ekf_example/mpf_double.h
r1438 r1439 2 2 3 3 #include "parametry_motoru.h" 4 #define Fpm 0.1989 5 //#define pi 3.141596 4 6 5 #define N 207 #define N 1 6 8 7 9 #define Lsd Ls*0.9 8 10 #define Lsq Ls 9 11 #define _dt 0.000125 10 #define _ad (1.-Rs*_dt/ Lsd)11 #define _aq (1.-Rs*_dt/ Lsq)12 #define _b F mag/Lsq*_dt13 #define _cd _dt/ Lsd14 #define _cq _dt/ Lsq12 #define _ad (1.-Rs*_dt/(Lsd)) 13 #define _aq (1.-Rs*_dt/(Lsq)) 14 #define _b Fpm/Lsq*_dt 15 #define _cd _dt/(Lsd) 16 #define _cq _dt/(Lsq) 15 17 16 double randn();17 double randu();18 18 19 void mpf_bayes ( const double &isa, const double &isb , const double &usa, const double &usb ); 20 void mpf_init(double qom0, double qth0, double r0); 21 void mpf_mean(double *Ecosth, double *Esinth, double *Eome); 22 void mpf_th(double th1[N]); 19 # define floatx float 23 20 24 double kalman_om ( const double &isa, const double &isb , const double &usa, const double &usb, const double &th ); 21 floatx qrandn(); 22 floatx qrandu(); 23 24 void mpf_bayes ( const floatx isa, const floatx isb , const floatx usa, const floatx usb ); 25 void mpf_init(floatx qom0, floatx qth0, floatx r0); 26 void mpf_mean(floatx *Ecosth, floatx *Esinth, floatx *Eome); 27 void mpf_th(floatx th1[N]); 28 29 floatx kalman_om ( const floatx isa, const floatx isb , const floatx usa, const floatx usb, const floatx th ); -
applications/pmsm/simulator_zdenek/ekf_example/simulmpf.cpp
r1437 r1439 65 65 66 66 // EKF - vysledek estimace 67 static doublempf_estim[2]={0.,0.}; // w_est, theta_est68 static doubleEsinth, Ecosth;67 static floatx mpf_estim[2]={0.,0.}; // w_est, theta_est 68 static floatx Esinth, Ecosth; 69 69 70 70 FILE *fw; … … 115 115 init_pmsm(param, REL1); 116 116 init_regulace(param,h_reg); 117 mpf_init(1e0, 1e- 5, 1e0);//VS:117 mpf_init(1e0, 1e-3, 1e0);//VS: 118 118 119 119 fw=fopen("data/graf2.txt","w"); … … 161 161 162 162 if (t>0.7) Ww=2.*M_PI*10.; 163 if (t>1.0) x[8]=1.2; // 1A164 if (t>1.2) x[8]=10.8; // 9A165 if (t>1.4) x[8]=25.2; // 21A163 // if (t>1.0) x[8]=1.2; // 1A 164 // if (t>1.2) x[8]=10.8; // 9A 165 // if (t>1.4) x[8]=25.2; // 21A 166 166 167 167 if (t>1.6) Ww=2.*M_PI*50.; 168 if (t>1.9) x[8]=1.2; // 1A169 if (t>2.1) x[8]=10.8; // 9A170 if (t>2.3) x[8]=25.2; // 21A168 // if (t>1.9) x[8]=1.2; // 1A 169 // if (t>2.1) x[8]=10.8; // 9A 170 // if (t>2.3) x[8]=25.2; // 21A 171 171 172 172 if (t>2.5) Ww=2.*M_PI*100; 173 if (t>2.8) x[8]=1.2; // 1A174 if (t>3.0) x[8]=10.8; // 9A175 if (t>3.2) x[8]=25.2; // 21A173 // if (t>2.8) x[8]=1.2; // 1A 174 // if (t>3.0) x[8]=10.8; // 9A 175 // if (t>3.2) x[8]=25.2; // 21A 176 176 177 177 if (t>3.4) Ww=2.*M_PI*150; 178 if (t>3.7) x[8]=1.2; // 1A179 if (t>3.9) x[8]=10.8; // 9A180 if (t>4.1) x[8]=25.2; // 21A178 // if (t>3.7) x[8]=1.2; // 1A 179 // if (t>3.9) x[8]=10.8; // 9A 180 // if (t>4.1) x[8]=25.2; // 21A 181 181 182 182 if (t>4.3) Ww=2.*M_PI*0; 183 if (t>4.8) x[8]=-1.2; // 1A184 if (t>5.0) x[8]=-10.8; // 9A185 if (t>5.2) x[8]=-25.2; // 21A183 // if (t>4.8) x[8]=-1.2; // 1A 184 // if (t>5.0) x[8]=-10.8; // 9A 185 // if (t>5.2) x[8]=-25.2; // 21A 186 186 187 187 if (t>5.4) Ww=2.*M_PI*(-10.); 188 if (t>5.7) x[8]=-1.2; // 1A189 if (t>5.9) x[8]=-10.8; // 9A190 if (t>6.1) x[8]=-25.2; // 21A188 // if (t>5.7) x[8]=-1.2; // 1A 189 // if (t>5.9) x[8]=-10.8; // 9A 190 // if (t>6.1) x[8]=-25.2; // 21A 191 191 192 192 if (t>6.3) Ww=2.*M_PI*(-50.); 193 if (t>6.7) x[8]=-1.2; // 1A194 if (t>6.9) x[8]=-10.8; // 9A195 if (t>7.1) x[8]=-25.2; // 21A193 // if (t>6.7) x[8]=-1.2; // 1A 194 // if (t>6.9) x[8]=-10.8; // 9A 195 // if (t>7.1) x[8]=-25.2; // 21A 196 196 197 197 if (t>7.3) Ww=2.*M_PI*(-100.); 198 if (t>7.7) x[8]=-1.2; // 1A199 if (t>7.9) x[8]=-10.8; // 9A200 if (t>8.1) x[8]=-25.2; // 21A201 if (t>8.3) x[8]=10.8; // 9A202 if (t>8.5) x[8]=25.2; // 21A198 // if (t>7.7) x[8]=-1.2; // 1A 199 // if (t>7.9) x[8]=-10.8; // 9A 200 // if (t>8.1) x[8]=-25.2; // 21A 201 // if (t>8.3) x[8]=10.8; // 9A 202 // if (t>8.5) x[8]=25.2; // 21A 203 203 204 204 … … 218 218 if (x[2]<-1615) x[2]=-1615; /**/ 219 219 220 // KALMAN 221 Umk=*u*Uc/Ucn; 220 221 // KALMAN (pouzij stara Usx, Usy!! 222 if (0){ 223 mpf_bayes((floatx)Isx,(floatx)Isy,(floatx)Usx,(floatx)Usy); 224 mpf_mean(&Esinth,&Ecosth,&mpf_estim[0]); 225 mpf_estim[1]=atan2((floatx)Esinth,(floatx)Ecosth); 226 } else { 227 mpf_estim[0]=kalman_om((floatx)Isx,(floatx)Isy,(floatx)Usx,(floatx)Usy,(floatx)x[3]); 228 } 229 230 Umk=*u*Uc/Ucn; 222 231 ua=Umk*cos(*(u+1)); 223 232 ub=Umk*cos(*(u+1)-2./3.*M_PI); 224 233 Usx=ua; // usx 225 234 Usy=(ua+2.*ub)/sqrt(3.); // usy 226 227 mpf_bayes(Isx,Isy,Usx,Usy);228 mpf_mean(&Esinth,&Ecosth,&mpf_estim[0]);229 mpf_estim[1]=atan2(Esinth,Ecosth);230 231 235 // vystup z EKF zaveden do regulatoru 232 236 // rychlost=ekf_estim[0]; theta=ekf_estim[1]; //VS