[384] | 1 | #include <estim/kalman.h> |
---|
[61] | 2 | #include "ekf_obj.h" |
---|
[1380] | 3 | //#include "../simulator.h" |
---|
[61] | 4 | |
---|
| 5 | double minQ(double Q){if (Q>1.0){ return 1.0;} else {return Q;};}; |
---|
| 6 | |
---|
[1240] | 7 | void mat_to_int16(const imat &M, int16 *I){ |
---|
| 8 | for (int16 i=0;i<M.rows(); i++){ |
---|
| 9 | for (int16 j=0;j<M.cols(); j++){ |
---|
[1174] | 10 | *I++ = M(i,j); |
---|
| 11 | } |
---|
| 12 | } |
---|
| 13 | } |
---|
[1466] | 14 | void int16_to_mat(int16 *I, imat &M, int rows, int cols){ |
---|
| 15 | M.set_size(rows,cols); |
---|
| 16 | for (int16 i=0;i<M.rows(); i++){ |
---|
| 17 | for (int16 j=0;j<M.cols(); j++){ |
---|
| 18 | M(i,j) = *I++; |
---|
| 19 | } |
---|
| 20 | } |
---|
| 21 | } |
---|
| 22 | void int16_to_vec(int16 *I, ivec &v, int len){ |
---|
| 23 | v.set_size(len); |
---|
| 24 | for (int16 i=0;i<v.length(); i++){ |
---|
| 25 | v(i) = *I++; |
---|
| 26 | } |
---|
| 27 | } |
---|
[1240] | 28 | void vec_to_int16(const ivec &v, int16 *I){ |
---|
| 29 | for (int16 i=0;i<v.length(); i++){ |
---|
[1174] | 30 | *I++ = v(i); |
---|
| 31 | } |
---|
| 32 | } |
---|
| 33 | |
---|
[1240] | 34 | #ifdef XXX |
---|
[61] | 35 | /////////////// |
---|
[1168] | 36 | void EKFfixed::bayes(const vec &yt, const vec &ut){ |
---|
| 37 | ekf(yt(0),yt(1),ut(0),ut(1)); |
---|
| 38 | |
---|
[66] | 39 | vec xhat(4); |
---|
| 40 | //UGLY HACK!!! reliance on a predictor!! |
---|
| 41 | xhat(0)=zprevod(x_est[0],Qm)*Iref; |
---|
| 42 | xhat(1)=zprevod(x_est[1],Qm)*Iref; |
---|
| 43 | xhat(2)=zprevod(x_est[2],Qm)*Wref; |
---|
[1168] | 44 | xhat(3)=zprevod(x_est[3],15)*Thetaref; |
---|
[61] | 45 | |
---|
[66] | 46 | E.set_mu(xhat); |
---|
| 47 | |
---|
[61] | 48 | if ( BM::evalll ) { |
---|
[1201] | 49 | /* //from enorm |
---|
[66] | 50 | vec xdif(x,4);//first 4 of x |
---|
| 51 | //UGLY HACK!!! reliance on a predictor!! |
---|
| 52 | /* xdif(0)=x[0]-zprevod(x_pred[0],Qm)*Iref; |
---|
| 53 | xdif(1)=x[1]-zprevod(x_pred[1],Qm)*Iref; |
---|
| 54 | xdif(2)=x[2]-zprevod(x_pred[2],Qm)*Wref; |
---|
| 55 | xdif(3)=x[3]-zprevod(x_pred[3],15);*/ |
---|
[61] | 56 | |
---|
[1201] | 57 | // xdif -=xhat; //(xdif=x-xhat) |
---|
[66] | 58 | |
---|
| 59 | mat Pfull(4,4); |
---|
| 60 | double* Pp=Pfull._data(); |
---|
[1240] | 61 | for(int16 i=0;i<16;i++){*(Pp++) = zprevod(P_est[i],15);} |
---|
[66] | 62 | |
---|
[1168] | 63 | E._R()._M()=Pfull; |
---|
| 64 | |
---|
| 65 | |
---|
[1226] | 66 | // BM::ll = -0.5* ( 4 * 1.83787706640935 +log ( det ( Pfull ) ) +xdif* ( inv(Pfull)*xdif ) );*/ |
---|
[61] | 67 | } |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | void EKFfixed::update_psi(void) |
---|
| 72 | { |
---|
[1240] | 73 | int16 t_sin,t_cos,tmp; |
---|
[61] | 74 | |
---|
| 75 | // implementace v PC |
---|
| 76 | t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
| 77 | t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15); |
---|
| 78 | |
---|
[1240] | 79 | PSI[2]=((int32)cB*t_sin)>>15; |
---|
| 80 | tmp=((int32)cH*x_est[2])>>15; |
---|
| 81 | PSI[3]=((int32)tmp*t_cos)>>15; |
---|
| 82 | PSI[6]=-((int32)cB*t_cos)>>15; |
---|
| 83 | PSI[7]=((int32)tmp*t_sin)>>15; |
---|
[61] | 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
[1240] | 87 | void EKFfixed::prediction(int16 *ux) |
---|
[61] | 88 | { |
---|
[1240] | 89 | int16 t_sin,t_cos, tmp; |
---|
[61] | 90 | |
---|
| 91 | // implementace v PC |
---|
[1201] | 92 | //t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
| 93 | //t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15); |
---|
[61] | 94 | |
---|
[1201] | 95 | t_sin=prevod(sin(Thetaref*x_est[3]/Qm),15); |
---|
| 96 | t_cos=prevod(cos(Thetaref*x_est[3]/Qm),15); |
---|
| 97 | |
---|
[1240] | 98 | tmp=((int32)cB*x_est[2])>>15; |
---|
| 99 | x_pred[0]=((int32)cA*x_est[0]+(int32)tmp*t_sin+(int32)cC*ux[0])>>15; |
---|
| 100 | x_pred[1]=((int32)cA*x_est[1]-(int32)tmp*t_cos+(int32)cC*ux[1])>>15; |
---|
[61] | 101 | x_pred[2]=x_est[2]; |
---|
[1240] | 102 | x_pred[3]=(((int32)x_est[3]<<15)+(int32)cG*x_est[2])>>15; |
---|
[61] | 103 | |
---|
| 104 | update_psi(); |
---|
| 105 | |
---|
| 106 | mmult(PSI,P_est,temp15a,3,3,3); |
---|
| 107 | // mtrans(PSI,temp15b,5,5); |
---|
| 108 | mmultt(temp15a,PSI,P_pred,3,3,3); |
---|
| 109 | maddD(P_pred,Q,3,3); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | void EKFfixed::correction(void) |
---|
| 113 | { |
---|
[1240] | 114 | int16 Y_error[2]; |
---|
| 115 | int32 temp30a[4]; /* matrix [2,2] - temporary matrix for inversion */ |
---|
[61] | 116 | |
---|
| 117 | choice_P(P_pred,temp15a,3); |
---|
| 118 | maddD(temp15a,R,1,1); |
---|
| 119 | minv2(temp15a,temp30a); |
---|
| 120 | Ry(0,0)=zprevod(temp15a[0],15); |
---|
| 121 | Ry(0,1)=zprevod(temp15a[1],15); |
---|
| 122 | Ry(1,0)=zprevod(temp15a[2],15); |
---|
| 123 | Ry(1,1)=zprevod(temp15a[3],15); |
---|
| 124 | |
---|
| 125 | mmultDr(P_pred,temp15a,3,3,1,1); |
---|
| 126 | mmult1530(temp15a,temp30a,Kalm,3,1,1); |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | /* estimate the state system */ |
---|
| 130 | choice_x(x_pred, temp15a); |
---|
| 131 | msub(Y_mes,temp15a,Y_error,1,0); |
---|
| 132 | mmult(Kalm,Y_error,temp15a,3,1,0); |
---|
| 133 | madd(x_pred,temp15a,x_est,3,0); |
---|
| 134 | |
---|
| 135 | /* matrix of covariances - version without MMULTDL() */ |
---|
| 136 | |
---|
| 137 | /* Version with MMULTDL() */ |
---|
| 138 | mmultDl(P_pred,temp15a,1,3,3,1); |
---|
| 139 | |
---|
| 140 | mmult(Kalm,temp15a,P_est,3,1,3); |
---|
| 141 | msub(P_pred,P_est,P_est,3,3); |
---|
| 142 | /* END */ |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | void EKFfixed::ekf(double ux, double uy, double isxd, double isyd) |
---|
| 147 | { |
---|
| 148 | // vypocet napeti v systemu (x,y) |
---|
| 149 | ukalm[0]=prevod(ux/Uref,Qm); |
---|
| 150 | ukalm[1]=prevod(uy/Uref,Qm); |
---|
| 151 | |
---|
| 152 | // zadani mereni |
---|
| 153 | Y_mes[0]=prevod(isxd/Iref,Qm); |
---|
| 154 | Y_mes[1]=prevod(isyd/Iref,Qm); |
---|
| 155 | |
---|
| 156 | ////// vlastni rutina EKF ///////////////////////// |
---|
| 157 | prediction(ukalm); |
---|
| 158 | correction(); |
---|
| 159 | |
---|
| 160 | // navrat estimovanych hodnot regulatoru |
---|
[80] | 161 | vec& mu = E._mu(); |
---|
| 162 | (mu)(0)=zprevod(x_est[0],Qm)*Iref; |
---|
| 163 | (mu)(1)=zprevod(x_est[1],Qm)*Iref; |
---|
| 164 | (mu)(2)=zprevod(x_est[2],Qm)*Wref; |
---|
| 165 | (mu)(3)=zprevod(x_est[3],15)*Thetaref; |
---|
[61] | 166 | } |
---|
| 167 | |
---|
| 168 | void EKFfixed::init_ekf(double Tv) |
---|
| 169 | { |
---|
| 170 | // Tuning of matrix Q |
---|
[1201] | 171 | Q[0]=prevod(.05,15); // 0.05 |
---|
[61] | 172 | Q[5]=Q[0]; |
---|
[1201] | 173 | Q[10]=prevod(0.0002,15); // 1e-3 |
---|
| 174 | Q[15]=prevod(0.001,15); // 1e-3 |
---|
[61] | 175 | |
---|
| 176 | // Tuning of matrix R |
---|
[1201] | 177 | R[0]=prevod(0.1,15); // 0.05 |
---|
[61] | 178 | R[3]=R[0]; |
---|
| 179 | |
---|
| 180 | // Motor model parameters |
---|
| 181 | cA=prevod(1-Tv*Rs/Ls,15); |
---|
| 182 | cB=prevod(Tv*Wref*Fmag/Iref/Ls,15); |
---|
| 183 | cC=prevod(Tv/Ls/Iref*Uref,15); |
---|
| 184 | // cD=prevod(1-Tv*Bf/J,15); |
---|
| 185 | // cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); |
---|
| 186 | // cF=prevod(p*Tv*Mref/J/Wref,15); |
---|
| 187 | cG=prevod(Tv*Wref*4/Thetaref,15); |
---|
[1201] | 188 | // cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); |
---|
[1226] | 189 | cH=prevod(Tv*Wref*Fmag/Iref/Ls,17); //cB in q |
---|
[1201] | 190 | // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); |
---|
[61] | 191 | |
---|
| 192 | /* Init matrix PSI with permanently constant terms */ |
---|
| 193 | PSI[0]=cA; |
---|
| 194 | PSI[5]=PSI[0]; |
---|
| 195 | PSI[10]=0x7FFF; |
---|
| 196 | PSI[14]=cG; |
---|
| 197 | PSI[15]=0x7FFF; |
---|
[1201] | 198 | |
---|
| 199 | P_est[0]=0x7FFF; |
---|
| 200 | P_est[5]=0x7FFF; |
---|
| 201 | P_est[10]=0x7FFF; |
---|
| 202 | P_est[15]=0x7FFF; |
---|
[61] | 203 | } |
---|
[1240] | 204 | #endif |
---|
[1174] | 205 | |
---|
| 206 | void EKF_UDfix::set_parameters ( const shared_ptr<diffbifn> &pfxu0, const shared_ptr<diffbifn> &phxu0, const mat Q0, const vec R0 ) { |
---|
| 207 | pfxu = pfxu0; |
---|
| 208 | phxu = phxu0; |
---|
| 209 | |
---|
| 210 | set_dim ( pfxu0->_dimx() ); |
---|
| 211 | dimy = phxu0->dimension(); |
---|
| 212 | dimc = pfxu0->_dimu(); |
---|
| 213 | |
---|
| 214 | vec &_mu = est._mu(); |
---|
| 215 | // if mu is not set, set it to zeros, just for constant terms of A and C |
---|
| 216 | if ( _mu.length() != dimension() ) _mu = zeros ( dimension() ); |
---|
| 217 | A = zeros ( dimension(), dimension() ); |
---|
| 218 | C = zeros ( dimy, dimension() ); |
---|
| 219 | |
---|
| 220 | //initialize matrices A C, later, these will be only updated! |
---|
| 221 | pfxu->dfdx_cond ( _mu, zeros ( dimc ), A, true ); |
---|
| 222 | // pfxu->dfdu_cond ( *_mu,zeros ( dimu ),B,true ); |
---|
| 223 | phxu->dfdx_cond ( _mu, zeros ( dimc ), C, true ); |
---|
| 224 | // phxu->dfdu_cond ( *_mu,zeros ( dimu ),D,true ); |
---|
| 225 | |
---|
| 226 | R = R0; |
---|
| 227 | Q = Q0; |
---|
| 228 | |
---|
| 229 | // |
---|
| 230 | } |
---|
| 231 | // aux fnc |
---|
| 232 | void UDtof(const mat &U, const vec &D, imat &Uf, ivec &Df, const vec &xref){ |
---|
| 233 | mat P= U*diag(D)*U.T(); |
---|
| 234 | mat T = diag(1.0/(xref)); |
---|
| 235 | mat Pf = T*P*T; |
---|
| 236 | |
---|
| 237 | ldmat Pld(Pf); |
---|
| 238 | |
---|
| 239 | mat Ut=Pld._L().T()*(1<<15); // U is in q15 -- diagonal is 0!!! |
---|
| 240 | Uf=round_i(Ut); |
---|
| 241 | Df=round_i(Pld._D()*(1<<15)); |
---|
| 242 | ivec zer=find(Df==0); |
---|
[1240] | 243 | for(int16 i=0; i<zer.length(); i++) Df(zer(i))=1; |
---|
[1174] | 244 | } |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | void EKF_UDfix::bayes ( const vec &yt, const vec &cond ) { |
---|
| 248 | //preparatory |
---|
| 249 | vec &_mu=est._mu(); |
---|
| 250 | const vec &u=cond; |
---|
[1240] | 251 | int16 dim = dimension(); |
---|
[1182] | 252 | ///// !!!!!!!!!!!!!!!! |
---|
[1174] | 253 | U = est._R()._L().T(); |
---|
| 254 | D = est._R()._D(); |
---|
[1183] | 255 | |
---|
[1174] | 256 | //////////// |
---|
| 257 | |
---|
| 258 | pfxu->dfdx_cond ( _mu, u, A, false ); //update A by a derivative of fx |
---|
| 259 | phxu->dfdx_cond ( _mu, u, C, false ); //update A by a derivative of fx |
---|
| 260 | |
---|
| 261 | mat PhiU = A*U; |
---|
| 262 | |
---|
| 263 | ////// |
---|
[1201] | 264 | /* vec xref(4); |
---|
[1174] | 265 | xref(0)= 30.0*1.4142; |
---|
| 266 | xref(1)= 30.0*1.4142; |
---|
| 267 | xref(2)= 6.283185*200.; |
---|
[1201] | 268 | xref(3) = 3.141593;*/ |
---|
| 269 | //xref(4) = 34.0; |
---|
[1174] | 270 | |
---|
[1182] | 271 | |
---|
[1174] | 272 | vec Din = D; |
---|
[1240] | 273 | int16 i,j,k; |
---|
[1174] | 274 | double sigma; |
---|
| 275 | mat G = eye(dim); |
---|
| 276 | //////// thorton |
---|
| 277 | |
---|
| 278 | //move mean; |
---|
| 279 | _mu = pfxu->eval(_mu,u); |
---|
| 280 | |
---|
| 281 | for (i=dim-1; i>=0;i--){ |
---|
| 282 | sigma = 0.0; |
---|
| 283 | for (j=0; j<dim; j++) { |
---|
| 284 | sigma += PhiU(i,j)*PhiU(i,j) *Din(j); |
---|
| 285 | sigma += G(i,j)*G(i,j) * Q(j,j); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | /* double sigma2= 0.0; |
---|
| 289 | for (j=0; j<dim; j++) { |
---|
| 290 | sigma2 += PhiU(i,j)*PhiU(i,j) *Din(j); |
---|
| 291 | } |
---|
| 292 | sigma2 +=Q(i,i);//*G(i,i)=1.0 |
---|
| 293 | for (j=i+1; j<dim; j++) { |
---|
| 294 | sigma2 += G(i,j)*G(i,j) * Q(j,j); |
---|
| 295 | }*/ |
---|
[1182] | 296 | D(i) = sigma; |
---|
[1174] | 297 | |
---|
[1182] | 298 | /* UDtof(U,D,Utf,Dtf,xref); |
---|
[1174] | 299 | cout << "d=sig"<<endl; |
---|
[1182] | 300 | cout << Dtf << endl; |
---|
| 301 | */ |
---|
[1174] | 302 | for (j=0;j<i;j++){ |
---|
| 303 | // cout << i << "," << j << endl; |
---|
| 304 | sigma = 0.0; |
---|
| 305 | for (k=0;k<dim;k++){ |
---|
| 306 | sigma += PhiU(i,k)*Din(k)*PhiU(j,k); |
---|
| 307 | } |
---|
| 308 | for (k=0;k<dim;k++){ |
---|
| 309 | sigma += G(i,k)*Q(k,k)*G(j,k); |
---|
| 310 | } |
---|
| 311 | // |
---|
| 312 | U(j,i) = sigma/D(i); |
---|
| 313 | |
---|
| 314 | /* cout << "U=sig/D"<<endl; |
---|
| 315 | UDtof(U,D,Utf,Dtf,xref); |
---|
| 316 | cout << Utf << endl << Dtf << endl; |
---|
| 317 | cout << G << endl << Din << endl<<endl;*/ |
---|
| 318 | |
---|
| 319 | for (k=0;k<dim;k++){ |
---|
| 320 | PhiU(j,k) = PhiU(j,k) - U(j,i)*PhiU(i,k); |
---|
| 321 | } |
---|
| 322 | for (k=0;k<dim;k++){ |
---|
| 323 | G(j,k) = G(j,k) - U(j,i)*G(i,k); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | } |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | // bierman |
---|
| 330 | |
---|
| 331 | double dz,alpha,gamma,beta,lambda; |
---|
| 332 | vec a; |
---|
| 333 | vec b; |
---|
| 334 | vec yp = phxu->eval(_mu,u); |
---|
| 335 | vec xp=_mu; // used in bierman |
---|
| 336 | |
---|
[1183] | 337 | |
---|
[1240] | 338 | for (int16 iy=0; iy<dimy; iy++){ |
---|
[1174] | 339 | a = U.T()*C.get_row(iy); // a is not modified, but |
---|
| 340 | b = elem_mult(D,a); // b is modified to become unscaled Kalman gain. |
---|
| 341 | dz = yt(iy) - yp(iy); |
---|
| 342 | |
---|
| 343 | alpha = R(iy); |
---|
| 344 | gamma = 1/alpha; |
---|
| 345 | for (j=0;j<dim;j++){ |
---|
| 346 | beta = alpha; |
---|
| 347 | alpha = alpha + a(j)*b(j); |
---|
| 348 | lambda = -a(j)*gamma; |
---|
| 349 | gamma = 1.0/alpha; |
---|
| 350 | D(j) = beta*gamma*D(j); |
---|
| 351 | |
---|
| 352 | // cout << "a: " << alpha << "g: " << gamma << endl; |
---|
| 353 | for (i=0;i<j;i++){ |
---|
| 354 | beta = U(i,j); |
---|
| 355 | U(i,j) = beta + b(i)*lambda; |
---|
| 356 | b(i) = b(i) + b(j)*beta; |
---|
| 357 | } |
---|
| 358 | } |
---|
| 359 | double dzs = gamma*dz; // apply scaling to innovations |
---|
| 360 | _mu = _mu + dzs*b; // multiply by unscaled Kalman gain |
---|
| 361 | //cout << "Ub: " << U << endl; |
---|
| 362 | //cout << "Db: " << D << endl <<endl; |
---|
| 363 | |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | |
---|
| 367 | ///// |
---|
| 368 | est._R().__L()=U.T(); |
---|
| 369 | est._R().__D()=D; |
---|
| 370 | |
---|
| 371 | if ( evalll == true ) { //likelihood of observation y |
---|
| 372 | } |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | void EKF_UDfix::from_setting ( const Setting &set ) { |
---|
| 376 | BM::from_setting ( set ); |
---|
| 377 | shared_ptr<diffbifn> IM = UI::build<diffbifn> ( set, "IM", UI::compulsory ); |
---|
| 378 | shared_ptr<diffbifn> OM = UI::build<diffbifn> ( set, "OM", UI::compulsory ); |
---|
| 379 | |
---|
| 380 | //statistics |
---|
[1240] | 381 | int16 dim = IM->dimension(); |
---|
[1174] | 382 | vec mu0; |
---|
| 383 | if ( !UI::get ( mu0, set, "mu0" ) ) |
---|
| 384 | mu0 = zeros ( dim ); |
---|
| 385 | |
---|
| 386 | mat P0; |
---|
| 387 | vec dP0; |
---|
| 388 | if ( UI::get ( dP0, set, "dP0" ) ) |
---|
| 389 | P0 = diag ( dP0 ); |
---|
| 390 | else if ( !UI::get ( P0, set, "P0" ) ) |
---|
| 391 | P0 = eye ( dim ); |
---|
| 392 | |
---|
| 393 | est._mu()=mu0; |
---|
| 394 | est._R()=ldmat(P0); |
---|
| 395 | |
---|
| 396 | //parameters |
---|
| 397 | vec dQ, dR; |
---|
| 398 | UI::get ( dQ, set, "dQ", UI::compulsory ); |
---|
| 399 | UI::get ( dR, set, "dR", UI::compulsory ); |
---|
| 400 | set_parameters ( IM, OM, diag ( dQ ), dR ); |
---|
| 401 | |
---|
| 402 | UI::get(log_level, set, "log_level", UI::optional); |
---|
| 403 | } |
---|
[1179] | 404 | |
---|
| 405 | |
---|
| 406 | void EKFfixedUD::bayes(const itpp::vec& yt, const itpp::vec& ut) |
---|
| 407 | { |
---|
| 408 | ekf(ut[0],ut[1],yt[0],yt[1]); |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | |
---|
| 412 | void EKFfixedUD::ekf(double ux, double uy, double isxd, double isyd) |
---|
| 413 | { |
---|
| 414 | // vypocet napeti v systemu (x,y) |
---|
[1240] | 415 | int16 uf[2]; |
---|
[1226] | 416 | uf[0]=prevod(ux/Uref,15); |
---|
| 417 | uf[1]=prevod(uy/Uref,15); |
---|
[1179] | 418 | |
---|
[1240] | 419 | int16 Y_mes[2]; |
---|
[1179] | 420 | // zadani mereni |
---|
[1226] | 421 | Y_mes[0]=prevod(isxd/Iref,15); |
---|
| 422 | Y_mes[1]=prevod(isyd/Iref,15); |
---|
| 423 | |
---|
[1179] | 424 | ////// vlastni rutina EKF -- ///////////////////////// |
---|
[1240] | 425 | int16 t_sin,t_cos; |
---|
| 426 | int32 tmp; |
---|
[1179] | 427 | |
---|
| 428 | // implementace v PC |
---|
[1201] | 429 | /* t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
| 430 | t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);*/ |
---|
[1240] | 431 | tmp=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
| 432 | if (tmp>32767) t_sin =32767; else t_sin=tmp; |
---|
| 433 | tmp=prevod(cos(Thetaref*x_est[3]/32768.),15); |
---|
| 434 | if (tmp>32767) t_cos =32767; else t_cos=tmp; |
---|
[1179] | 435 | |
---|
[1240] | 436 | tmp=((int32)cB*x_est[2])>>15; // q15*q13 -> q13 |
---|
[1226] | 437 | // q15*q13 + q13*q15 + q15*q13?? |
---|
[1240] | 438 | x_est[0]=((int32)cA*x_est[0]+(int32)tmp*t_sin+(int32)cC*(uf[0]))>>15; // in q13 |
---|
| 439 | x_est[1]=((int32)cA*x_est[1]-(int32)tmp*t_cos+(int32)cC*(uf[1]))>>15; // q13 |
---|
[1179] | 440 | x_est[2]=x_est[2]; |
---|
[1226] | 441 | // q15 + q15*q13 |
---|
[1240] | 442 | x_est[3]=(((int32)x_est[3]<<15)+(int32)cG*x_est[2])>>15; |
---|
[1179] | 443 | |
---|
[1201] | 444 | if(x_est[3]>(1<<15)) x_est[3]-=2*(1<<15); |
---|
| 445 | if(x_est[3]<-(1<<15)) x_est[3]+=2*(1<<15); |
---|
| 446 | |
---|
[1179] | 447 | //void EKFfixed::update_psi(void) |
---|
[1226] | 448 | { |
---|
[1240] | 449 | PSI[2]=((int32)cB*t_sin)>>15; |
---|
| 450 | tmp=((int32)cH*x_est[2])>>15; // ! cH =cB with different scale!! |
---|
| 451 | PSI[3]=((int32)tmp*t_cos)>>15; |
---|
| 452 | PSI[6]=-((int32)cB*t_cos)>>15; |
---|
| 453 | PSI[7]=((int32)tmp*t_sin)>>15; |
---|
[1179] | 454 | } |
---|
[1201] | 455 | { |
---|
[1240] | 456 | int Ai[16]; |
---|
| 457 | for (int i=0;i<16; i++) Ai[i]=(int)(PSI[i]); |
---|
| 458 | ivec Ad(Ai,16); |
---|
[1201] | 459 | log_level.store(logA,get_from_ivec(Ad)); |
---|
| 460 | } |
---|
[1179] | 461 | |
---|
| 462 | ///////// end of copy /////////////// |
---|
| 463 | mmultAU(PSI,Uf,PSIU,4,4); |
---|
[1240] | 464 | //thorton(int16 *U, int16 *D, int16 *PSIU, int16 *Q, int16 *G, int16 *Dold, unsigned int16 dimx); |
---|
[1230] | 465 | thorton_fast(Uf,Df,PSIU,Q,G,Dfold,4); |
---|
[1179] | 466 | |
---|
[1201] | 467 | { |
---|
[1240] | 468 | int Ui[16]; for (int i=0;i<16; i++) Ui[i]=(int)Uf[i]; |
---|
| 469 | |
---|
| 470 | ivec Ud(Ui,16); |
---|
[1201] | 471 | log_level.store(logU,get_from_ivec(Ud)); |
---|
[1240] | 472 | |
---|
| 473 | int di[16]; for (int i=0;i<16; i++) di[i]=(int)Df[i]; |
---|
| 474 | ivec dd(di,4); |
---|
| 475 | imat U(Ui,4,4); |
---|
[1236] | 476 | mat U2=to_mat(U)/32768; |
---|
| 477 | mat PP=U2*diag(to_vec(dd))*U2.T(); |
---|
| 478 | vec pp(PP._data(),16); |
---|
| 479 | log_level.store(logP,pp); |
---|
| 480 | } |
---|
[1201] | 481 | { |
---|
[1240] | 482 | int Gi[16]; for (int i=0;i<16; i++) Gi[i]=(int)G[i]; |
---|
| 483 | ivec Gd(Gi,16); |
---|
[1201] | 484 | log_level.store(logG,get_from_ivec(Gd)); |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | |
---|
[1240] | 488 | int16 difz[2]; |
---|
[1226] | 489 | difz[0]=(Y_mes[0]-x_est[0]); // shift to q15!! |
---|
| 490 | difz[1]=(Y_mes[1]-x_est[1]);//<<2; |
---|
[1201] | 491 | |
---|
| 492 | { |
---|
[1240] | 493 | int Di[4]; for (int i=0;i<4; i++) Di[i]=(int)Df[i]; |
---|
| 494 | ivec dd(Di,4);//;dd(0)=Y_mes[0];dd(1)=Y_mes[1]; dd(2)=difz[0]; dd(3)=difz[1]; |
---|
[1226] | 495 | log_level.store(logD,get_from_ivec(dd)); |
---|
[1201] | 496 | } |
---|
| 497 | |
---|
[1240] | 498 | //bierman(int16 *difz, int16 *xp, int16 *U, int16 *D, int16 *R, unsigned int16 dimy, unsigned int16 dimx ); |
---|
| 499 | int16 dR[2];dR[0]=R[0];dR[1]=R[3]; |
---|
| 500 | //int16 xb[4]; xb[0]=x_est[0]<<2; xb[1]=x_est[1]<<2; xb[2]=x_est[2]<<2; xb[3]=x_est[3]; |
---|
[1230] | 501 | bierman_fast(difz,x_est,Uf,Df,dR,2,4); |
---|
[1226] | 502 | //x_est[0] = xb[0]>>2; x_est[1]=xb[1]>>2; x_est[2]=xb[2]>>2; x_est[3]=xb[3]; |
---|
[1179] | 503 | |
---|
| 504 | // navrat estimovanych hodnot regulatoru |
---|
| 505 | vec& mu = E._mu(); |
---|
[1201] | 506 | (mu)(0)=zprevod(x_est[0],15)*Iref; |
---|
| 507 | (mu)(1)=zprevod(x_est[1],15)*Iref; |
---|
| 508 | (mu)(2)=zprevod(x_est[2],15)*Wref; |
---|
[1179] | 509 | (mu)(3)=zprevod(x_est[3],15)*Thetaref; |
---|
| 510 | |
---|
[1226] | 511 | //x_est[2]=x[2]*32768/Wref; |
---|
| 512 | //x_est[3]=x[3]*32768/Thetaref; |
---|
[1179] | 513 | // mat T=diag(concat(vec_2(Iref,Iref), vec_2(Wref,Thetaref))); |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | void EKFfixedUD::init_ekf(double Tv) |
---|
| 517 | { |
---|
| 518 | // Tuning of matrix Q |
---|
[1294] | 519 | Q[0]=prevod(.001,15); // 0.05 |
---|
[1179] | 520 | Q[5]=Q[0]; |
---|
[1240] | 521 | Q[10]=prevod(0.0005,15); // 1e-3 |
---|
[1294] | 522 | Q[15]=prevod(0.0001,15); // 1e-3 |
---|
[1179] | 523 | |
---|
[1226] | 524 | Uf[0]=0x7FFF;// ! // 0.05 |
---|
[1201] | 525 | Uf[1]=Uf[2]=Uf[3]=Uf[4]=0; |
---|
[1226] | 526 | Uf[5]=0x7FFF;//! |
---|
[1201] | 527 | Uf[6]=Uf[6]=Uf[8]=Uf[9]=0; |
---|
[1226] | 528 | Uf[10]=0x7FFF;//! // 1e-3 |
---|
[1201] | 529 | Uf[11]=Uf[12]=Uf[13]=Uf[4]=0; |
---|
[1179] | 530 | Uf[15]=0x7FFF; // 1e-3 |
---|
| 531 | |
---|
[1226] | 532 | Df[0]=1<<14; |
---|
| 533 | Df[1]=1<<14; |
---|
| 534 | Df[2]=1<<14; |
---|
| 535 | Df[3]=1<<14; |
---|
[1179] | 536 | |
---|
| 537 | // Tuning of matrix R |
---|
| 538 | R[0]=prevod(0.05,15); // 0.05 |
---|
| 539 | R[3]=R[0]; |
---|
| 540 | |
---|
| 541 | // Motor model parameters |
---|
| 542 | cA=prevod(1-Tv*Rs/Ls,15); |
---|
| 543 | cB=prevod(Tv*Wref*Fmag/Iref/Ls,15); |
---|
| 544 | cC=prevod(Tv/Ls/Iref*Uref,15); |
---|
| 545 | // cD=prevod(1-Tv*Bf/J,15); |
---|
| 546 | // cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); |
---|
| 547 | // cF=prevod(p*Tv*Mref/J/Wref,15); |
---|
[1226] | 548 | cG=prevod(Tv*Wref/Thetaref,15); //in q15! |
---|
[1201] | 549 | //cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); |
---|
[1240] | 550 | // cH=prevod(Tv*Wref*Thetaref*Fmag/Iref/Ls,15); // < =========== |
---|
[1226] | 551 | cH=prevod(Tv*Wref*Thetaref*Fmag/Iref/Ls,15); // |
---|
[1179] | 552 | // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); |
---|
| 553 | |
---|
| 554 | /* Init matrix PSI with permanently constant terms */ |
---|
| 555 | PSI[0]=cA; |
---|
| 556 | PSI[5]=PSI[0]; |
---|
| 557 | PSI[10]=0x7FFF; |
---|
| 558 | PSI[14]=cG; |
---|
| 559 | PSI[15]=0x7FFF; |
---|
| 560 | |
---|
[1226] | 561 | } |
---|
| 562 | |
---|
[1305] | 563 | void EKFfixedUD2::bayes(const itpp::vec& yt, const itpp::vec& ut) |
---|
| 564 | { |
---|
| 565 | ekf2(ut[0],ut[1],yt[0],yt[1]); |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | |
---|
| 569 | void EKFfixedUD2::ekf2(double ux, double uy, double isxd, double isyd) |
---|
| 570 | { |
---|
| 571 | // vypocet napeti v systemu (x,y) |
---|
| 572 | int16 uf[2]; |
---|
| 573 | uf[0]=prevod(ux/Uref,15); |
---|
| 574 | uf[1]=prevod(uy/Uref,15); |
---|
| 575 | |
---|
| 576 | int16 Y_mes[2]; |
---|
| 577 | // zadani mereni |
---|
| 578 | Y_mes[0]=prevod(isxd/Iref,15); |
---|
| 579 | Y_mes[1]=prevod(isyd/Iref,15); |
---|
| 580 | |
---|
| 581 | ////// vlastni rutina EKF -- ///////////////////////// |
---|
| 582 | int16 t_sin,t_cos; |
---|
| 583 | int32 tmp; |
---|
| 584 | |
---|
[1311] | 585 | x_est[0]=x_est[0]; |
---|
[1305] | 586 | // q15 + q15*q13 |
---|
[1341] | 587 | x_est[1]=(((int32)x_est[1]<<15)+(int32)cG*x_est[0]+(1<<14))>>15; |
---|
[1305] | 588 | |
---|
[1311] | 589 | // if(x_est[1]>(1<<15)) x_est[1]-=2*(1<<15); |
---|
| 590 | // if(x_est[1]<-(1<<15)) x_est[1]+=2*(1<<15); |
---|
[1305] | 591 | |
---|
[1311] | 592 | |
---|
| 593 | ///////// end of copy /////////////// |
---|
| 594 | mmultAU(PSI,Uf,PSIU,2,2); |
---|
| 595 | |
---|
[1305] | 596 | //void EKFfixed::update_psi(void) |
---|
| 597 | { |
---|
[1311] | 598 | int Ai[4]; |
---|
[1326] | 599 | for (int i=0;i<4; i++) Ai[i]=(int)(PSI[i]); |
---|
[1311] | 600 | ivec Ad(Ai,4); |
---|
[1305] | 601 | log_level.store(logA,get_from_ivec(Ad)); |
---|
| 602 | } |
---|
| 603 | |
---|
| 604 | //thorton(int16 *U, int16 *D, int16 *PSIU, int16 *Q, int16 *G, int16 *Dold, unsigned int16 dimx); |
---|
[1311] | 605 | thorton_fast(Uf,Df,PSIU,Q,G,Dfold,2); |
---|
[1305] | 606 | |
---|
[1311] | 607 | // implementace v PC |
---|
| 608 | /* t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
| 609 | * t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);*/ |
---|
| 610 | tmp=prevod(sin(Thetaref*x_est[1]/32768.),15); |
---|
| 611 | if (tmp>32767) t_sin =32767; else t_sin=tmp; |
---|
| 612 | tmp=prevod(cos(Thetaref*x_est[1]/32768.),15); |
---|
| 613 | if (tmp>32767) t_cos =32767; else t_cos=tmp; |
---|
| 614 | |
---|
| 615 | { |
---|
| 616 | C[0]=((int32)cB*t_sin)>>15; |
---|
| 617 | tmp=((int32)cH*x_est[0])>>15; // ! cH =cB with different scale!! |
---|
| 618 | C[1]=((int32)tmp*t_cos)>>15; |
---|
| 619 | C[2]=-((int32)cB*t_cos)>>15; |
---|
| 620 | C[3]=((int32)tmp*t_sin)>>15; |
---|
| 621 | } |
---|
| 622 | |
---|
| 623 | |
---|
| 624 | { |
---|
| 625 | int Ui[4]; for (int i=0;i<4; i++) Ui[i]=(int)Uf[i]; |
---|
| 626 | int Ci[4]; for (int i=0;i<4; i++) Ci[i]=(int)C[i]; |
---|
[1305] | 627 | |
---|
[1311] | 628 | imat Ud(Ui,2,2); |
---|
| 629 | imat Cd(Ci,2,2); |
---|
[1326] | 630 | // imat CU = Cd*Ud/(1<<15); |
---|
| 631 | log_level.store(logU,get_from_ivec(ivec(Ud._data(),Ud._datasize()))); |
---|
[1305] | 632 | |
---|
[1311] | 633 | int di[2]; for (int i=0;i<2; i++) di[i]=(int)Df[i]; |
---|
| 634 | ivec dd(di,2); |
---|
| 635 | imat U(Ui,2,2); |
---|
| 636 | mat U2=to_mat(U)/32768; |
---|
| 637 | mat PP=U2*diag(to_vec(dd))*U2.T(); |
---|
| 638 | vec pp(PP._data(),4); |
---|
| 639 | log_level.store(logP,pp); |
---|
| 640 | } |
---|
| 641 | { |
---|
| 642 | int Gi[4]; for (int i=0;i<4; i++) Gi[i]=(int)G[i]; |
---|
| 643 | ivec Gd(Gi,4); |
---|
| 644 | log_level.store(logG,get_from_ivec(Gd)); |
---|
| 645 | } |
---|
| 646 | |
---|
| 647 | |
---|
| 648 | tmp=((int32)cB*x_est[0])>>15; // q15*q13 -> q13 |
---|
| 649 | // q15*q13 + q13*q15 + q15*q13?? |
---|
[1341] | 650 | y_est[0]=((int32)cA*y_old[0]+(int32)tmp*t_sin+(int32)cC*(uf[0])+(1<<14))>>15; // in q13 |
---|
| 651 | y_est[1]=((int32)cA*y_old[1]-(int32)tmp*t_cos+(int32)cC*(uf[1])+(1<<14))>>15; // q13 |
---|
[1311] | 652 | |
---|
| 653 | |
---|
[1305] | 654 | int16 difz[2]; |
---|
[1311] | 655 | difz[0]=(Y_mes[0]-y_est[0]); // shift to q15!! |
---|
| 656 | difz[1]=(Y_mes[1]-y_est[1]);//<<2; |
---|
[1305] | 657 | |
---|
[1311] | 658 | y_old[0] = Y_mes[0]; |
---|
| 659 | y_old[1] = Y_mes[1]; |
---|
[1305] | 660 | { |
---|
[1311] | 661 | int Di[2]; for (int i=0;i<2; i++) Di[i]=(int)Df[i]; |
---|
| 662 | ivec dd(Di,2);//;dd(0)=Y_mes[0];dd(1)=Y_mes[1]; dd(2)=difz[0]; dd(3)=difz[1]; |
---|
[1305] | 663 | log_level.store(logD,get_from_ivec(dd)); |
---|
[1311] | 664 | int Ci[4]; for (int i=0;i<4; i++) Ci[i]=(int)C[i]; |
---|
| 665 | ivec CC(Ci,4);//;dd(0)=Y_mes[0];dd(1)=Y_mes[1]; dd(2)=difz[0]; dd(3)=difz[1]; |
---|
| 666 | log_level.store(logC,get_from_ivec(CC)); |
---|
[1305] | 667 | } |
---|
| 668 | |
---|
| 669 | //bierman(int16 *difz, int16 *xp, int16 *U, int16 *D, int16 *R, unsigned int16 dimy, unsigned int16 dimx ); |
---|
| 670 | int16 dR[2];dR[0]=R[0];dR[1]=R[3]; |
---|
| 671 | //int16 xb[4]; xb[0]=x_est[0]<<2; xb[1]=x_est[1]<<2; xb[2]=x_est[2]<<2; xb[3]=x_est[3]; |
---|
[1311] | 672 | bierman_fastC(difz,x_est,Uf,Df,C,dR,2,2); |
---|
[1305] | 673 | //x_est[0] = xb[0]>>2; x_est[1]=xb[1]>>2; x_est[2]=xb[2]>>2; x_est[3]=xb[3]; |
---|
| 674 | |
---|
| 675 | // navrat estimovanych hodnot regulatoru |
---|
| 676 | vec& mu = E._mu(); |
---|
[1311] | 677 | (mu)(0)=zprevod(x_est[0],15)*Wref; |
---|
| 678 | (mu)(1)=zprevod(x_est[1],15)*Thetaref; |
---|
[1305] | 679 | |
---|
| 680 | //x_est[2]=x[2]*32768/Wref; |
---|
| 681 | //x_est[3]=x[3]*32768/Thetaref; |
---|
| 682 | // mat T=diag(concat(vec_2(Iref,Iref), vec_2(Wref,Thetaref))); |
---|
| 683 | } |
---|
| 684 | |
---|
| 685 | void EKFfixedUD2::init_ekf2(double Tv) |
---|
| 686 | { |
---|
| 687 | // Tuning of matrix Q |
---|
[1341] | 688 | Q[0]=prevod(0.01,15); // 1e-3 |
---|
| 689 | Q[3]=prevod(0.01,15); // 1e-3 |
---|
[1305] | 690 | |
---|
| 691 | Uf[0]=0x7FFF;// ! // 0.05 |
---|
| 692 | Uf[1]=Uf[2]=0; |
---|
[1311] | 693 | Uf[3]=0x7FFF;//! |
---|
[1305] | 694 | |
---|
| 695 | Df[0]=1<<14; |
---|
| 696 | Df[1]=1<<14; |
---|
| 697 | |
---|
| 698 | // Tuning of matrix R |
---|
[1341] | 699 | R[0]=prevod(0.01,15); // 0.05 |
---|
[1305] | 700 | R[3]=R[0]; |
---|
| 701 | |
---|
| 702 | // Motor model parameters |
---|
| 703 | cA=prevod(1-Tv*Rs/Ls,15); |
---|
| 704 | cB=prevod(Tv*Wref*Fmag/Iref/Ls,15); |
---|
| 705 | cC=prevod(Tv/Ls/Iref*Uref,15); |
---|
| 706 | // cD=prevod(1-Tv*Bf/J,15); |
---|
| 707 | // cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); |
---|
| 708 | // cF=prevod(p*Tv*Mref/J/Wref,15); |
---|
| 709 | cG=prevod(Tv*Wref/Thetaref,15); //in q15! |
---|
| 710 | //cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); |
---|
| 711 | // cH=prevod(Tv*Wref*Thetaref*Fmag/Iref/Ls,15); // < =========== |
---|
[1326] | 712 | cH=prevod(Tv*Wref*Fmag*Thetaref/Iref/Ls,15); // |
---|
[1305] | 713 | // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); |
---|
| 714 | |
---|
| 715 | /* Init matrix PSI with permanently constant terms */ |
---|
| 716 | PSI[0]=0x7FFF; |
---|
[1326] | 717 | PSI[1]=0; |
---|
[1305] | 718 | PSI[2]=cG; |
---|
| 719 | PSI[3]=0x7FFF; |
---|
| 720 | } |
---|
| 721 | |
---|
[1341] | 722 | void EKFfixedUD3::bayes(const itpp::vec& yt, const itpp::vec& ut) |
---|
| 723 | { |
---|
| 724 | ekf3(ut[0],ut[1],yt[0],yt[1]); |
---|
| 725 | } |
---|
[1305] | 726 | |
---|
[1341] | 727 | |
---|
| 728 | void EKFfixedUD3::ekf3(double ux, double uy, double isxd, double isyd) |
---|
| 729 | { |
---|
| 730 | // vypocet napeti v systemu (x,y) |
---|
| 731 | int16 uf[2]; |
---|
| 732 | uf[0]=prevod(ux/Uref,15); |
---|
| 733 | uf[1]=prevod(uy/Uref,15); |
---|
| 734 | |
---|
| 735 | int16 Y_mes[2]; |
---|
| 736 | // zadani mereni |
---|
| 737 | Y_mes[0]=prevod(isxd/Iref,15); |
---|
| 738 | Y_mes[1]=prevod(isyd/Iref,15); |
---|
| 739 | |
---|
| 740 | ////// vlastni rutina EKF -- ///////////////////////// |
---|
| 741 | int16 t_sin,t_cos; |
---|
| 742 | int32 tmp; |
---|
| 743 | |
---|
| 744 | int16 Mm=x_est[2]; |
---|
| 745 | x_est[0]=(((int32)x_est[0]<<15) + (int32)cF*x_est[2] + (1<<14))>>15; |
---|
| 746 | // q15 + q15*q13 |
---|
| 747 | x_est[1]=(((int32)x_est[1]<<15)+(int32)cG*x_est[0] + (1<<14))>>15; |
---|
| 748 | x_est[2]=x_est[2]; |
---|
| 749 | |
---|
| 750 | // if(x_est[1]>(1<<15)) x_est[1]-=2*(1<<15); |
---|
| 751 | // if(x_est[1]<-(1<<15)) x_est[1]+=2*(1<<15); |
---|
| 752 | |
---|
| 753 | |
---|
| 754 | ///////// end of copy /////////////// |
---|
| 755 | mmultAU(PSI,Uf,PSIU,3,3); |
---|
| 756 | |
---|
| 757 | |
---|
| 758 | //thorton(int16 *U, int16 *D, int16 *PSIU, int16 *Q, int16 *G, int16 *Dold, unsigned int16 dimx); |
---|
| 759 | thorton_fast(Uf,Df,PSIU,Q,G,Dfold,3); |
---|
| 760 | |
---|
| 761 | // implementace v PC |
---|
| 762 | /* t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
| 763 | * t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);*/ |
---|
| 764 | tmp=prevod(sin(Thetaref*x_est[1]/32768.),15); |
---|
| 765 | if (tmp>32767) t_sin =32767; else t_sin=tmp; |
---|
| 766 | tmp=prevod(cos(Thetaref*x_est[1]/32768.),15); |
---|
| 767 | if (tmp>32767) t_cos =32767; else t_cos=tmp; |
---|
| 768 | |
---|
| 769 | { |
---|
| 770 | C[0]=((int32)cB*t_sin)>>15; |
---|
| 771 | tmp=((int32)cH*x_est[0])>>15; // ! cH =cB with different scale!! |
---|
| 772 | C[1]=((int32)tmp*t_cos)>>15; |
---|
| 773 | C[2] = 0; |
---|
| 774 | |
---|
| 775 | C[3]=-((int32)cB*t_cos)>>15; |
---|
| 776 | C[4]=((int32)tmp*t_sin)>>15; |
---|
| 777 | C[5]=0; |
---|
| 778 | } |
---|
| 779 | |
---|
| 780 | tmp=((int32)cB*x_est[0])>>15; // q15*q13 -> q13 |
---|
| 781 | // q15*q13 + q13*q15 + q15*q13?? |
---|
| 782 | y_est[0]=((int32)cA*y_old[0]+(int32)tmp*t_sin+(int32)cC*(uf[0])+(1<<14))>>15; // in q13 |
---|
| 783 | y_est[1]=((int32)cA*y_old[1]-(int32)tmp*t_cos+(int32)cC*(uf[1])+(1<<14))>>15; // q13 |
---|
| 784 | |
---|
| 785 | |
---|
| 786 | int16 difz[2]; |
---|
| 787 | difz[0]=(Y_mes[0]-y_est[0]); // shift to q15!! |
---|
| 788 | difz[1]=(Y_mes[1]-y_est[1]);//<<2; |
---|
| 789 | |
---|
| 790 | y_old[0] = Y_mes[0]; |
---|
| 791 | y_old[1] = Y_mes[1]; |
---|
| 792 | |
---|
| 793 | //bierman(int16 *difz, int16 *xp, int16 *U, int16 *D, int16 *R, unsigned int16 dimy, unsigned int16 dimx ); |
---|
| 794 | int16 dR[2];dR[0]=R[0];dR[1]=R[3]; |
---|
| 795 | //int16 xb[4]; xb[0]=x_est[0]<<2; xb[1]=x_est[1]<<2; xb[2]=x_est[2]<<2; xb[3]=x_est[3]; |
---|
| 796 | bierman_fastC(difz,x_est,Uf,Df,C,dR,2,3); |
---|
| 797 | //x_est[0] = xb[0]>>2; x_est[1]=xb[1]>>2; x_est[2]=xb[2]>>2; x_est[3]=xb[3]; |
---|
| 798 | |
---|
| 799 | // navrat estimovanych hodnot regulatoru |
---|
| 800 | vec& mu = E._mu(); |
---|
| 801 | (mu)(0)=zprevod(x_est[0],15)*Wref; |
---|
| 802 | (mu)(1)=zprevod(x_est[1],15)*Thetaref; |
---|
| 803 | (mu)(2)=zprevod(x_est[2],15)*Mref; |
---|
| 804 | |
---|
| 805 | //x_est[2]=x[2]*32768/Wref; |
---|
| 806 | //x_est[3]=x[3]*32768/Thetaref; |
---|
| 807 | // mat T=diag(concat(vec_2(Iref,Iref), vec_2(Wref,Thetaref))); |
---|
| 808 | } |
---|
| 809 | |
---|
| 810 | void EKFfixedUD3::init_ekf3(double Tv) |
---|
| 811 | { |
---|
| 812 | // Tuning of matrix Q |
---|
| 813 | Q[0]=prevod(0.001,15); // 1e-3 |
---|
| 814 | Q[4]=prevod(0.001,15); // 1e-3 |
---|
| 815 | Q[8]=prevod(0.001,15); // 1e-3 |
---|
| 816 | |
---|
| 817 | Uf[0]=0x7FFF;// ! // 0.05 |
---|
| 818 | Uf[1]=Uf[2]=Uf[3]=0; |
---|
| 819 | Uf[4]=0x7FFF;//! |
---|
| 820 | Uf[5]=Uf[6]=Uf[7]=0; |
---|
| 821 | Uf[8]=0x7FFF;//! |
---|
| 822 | |
---|
| 823 | Df[0]=1<<14; |
---|
| 824 | Df[1]=1<<14; |
---|
| 825 | Df[2]=1<<14; |
---|
| 826 | |
---|
| 827 | // Tuning of matrix R |
---|
| 828 | R[0]=prevod(0.01,15); // 0.05 |
---|
| 829 | R[3]=R[0]; |
---|
| 830 | |
---|
| 831 | // Motor model parameters |
---|
| 832 | cA=prevod(1-Tv*Rs/Ls,15); |
---|
| 833 | cB=prevod(Tv*Wref*Fmag/Iref/Ls,15); |
---|
| 834 | cC=prevod(Tv/Ls/Iref*Uref,15); |
---|
| 835 | // cD=prevod(1-Tv*Bf/J,15); |
---|
| 836 | // cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); |
---|
| 837 | cF=prevod(-p*Tv*Mref/J/Wref,15); |
---|
| 838 | cG=prevod(Tv*Wref/Thetaref,15); //in q15! |
---|
| 839 | //cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); |
---|
| 840 | // cH=prevod(Tv*Wref*Thetaref*Fmag/Iref/Ls,15); // < =========== |
---|
| 841 | cH=prevod(Tv*Wref*Fmag*Thetaref/Iref/Ls,15); // |
---|
| 842 | // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); |
---|
| 843 | |
---|
| 844 | /* Init matrix PSI with permanently constant terms */ |
---|
| 845 | PSI[0]=0x7FFF; |
---|
| 846 | PSI[1]=0; |
---|
| 847 | PSI[2]= cF; |
---|
| 848 | |
---|
| 849 | PSI[3]=cG; |
---|
| 850 | PSI[4]=0x7FFF; |
---|
| 851 | PSI[5]=0; |
---|
| 852 | |
---|
| 853 | PSI[6]=0; |
---|
| 854 | PSI[7]=0; |
---|
| 855 | PSI[8]=0x7FFF; |
---|
| 856 | } |
---|
| 857 | |
---|
| 858 | |
---|
[1226] | 859 | void EKFfixedCh::bayes(const itpp::vec& yt, const itpp::vec& ut) |
---|
| 860 | { |
---|
| 861 | ekf(ut[0],ut[1],yt[0],yt[1]); |
---|
| 862 | } |
---|
| 863 | |
---|
| 864 | |
---|
| 865 | void EKFfixedCh::ekf(double ux, double uy, double isxd, double isyd) |
---|
| 866 | { |
---|
| 867 | // vypocet napeti v systemu (x,y) |
---|
[1240] | 868 | int16 uf[2]; |
---|
[1226] | 869 | uf[0]=prevod(ux/Uref,15); |
---|
| 870 | uf[1]=prevod(uy/Uref,15); |
---|
[1179] | 871 | |
---|
[1240] | 872 | int16 Y_mes[2]; |
---|
[1226] | 873 | // zadani mereni |
---|
| 874 | Y_mes[0]=prevod(isxd/Iref,15); |
---|
| 875 | Y_mes[1]=prevod(isyd/Iref,15); |
---|
[1179] | 876 | |
---|
[1226] | 877 | ////// vlastni rutina EKF -- ///////////////////////// |
---|
[1240] | 878 | int16 t_sin,t_cos, tmp; |
---|
[1226] | 879 | |
---|
| 880 | // implementace v PC |
---|
| 881 | /* t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
| 882 | * t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);*/ |
---|
| 883 | t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
| 884 | t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15); |
---|
| 885 | |
---|
[1240] | 886 | tmp=((int32)cB*x_est[2])>>15; // q15*q13 -> q13 |
---|
[1226] | 887 | // q15*q13 + q13*q15 + q15*q13?? |
---|
[1240] | 888 | x_est[0]=((int32)cA*x_est[0]+(int32)tmp*t_sin+(int32)cC*(uf[0]))>>15; // in q13 |
---|
| 889 | x_est[1]=((int32)cA*x_est[1]-(int32)tmp*t_cos+(int32)cC*(uf[1]))>>15; // q13 |
---|
[1226] | 890 | x_est[2]=x_est[2]; |
---|
| 891 | // q15 + q15*q13 |
---|
[1240] | 892 | x_est[3]=(((int32)x_est[3]<<15)+(int32)cG*x_est[2])>>15; |
---|
[1226] | 893 | |
---|
| 894 | if(x_est[3]>(1<<15)) x_est[3]-=2*(1<<15); |
---|
| 895 | if(x_est[3]<-(1<<15)) x_est[3]+=2*(1<<15); |
---|
| 896 | |
---|
| 897 | //void EKFfixed::update_psi(void) |
---|
| 898 | { |
---|
[1240] | 899 | PSI[2]=((int32)cB*t_sin)>>15; |
---|
| 900 | tmp=((int32)cH*x_est[2])>>15; // ! cH =cB with different scale!! |
---|
| 901 | PSI[3]=((int32)tmp*t_cos)>>15; |
---|
| 902 | PSI[6]=-((int32)cB*t_cos)>>15; |
---|
| 903 | PSI[7]=((int32)tmp*t_sin)>>15; |
---|
[1226] | 904 | } |
---|
| 905 | { |
---|
[1240] | 906 | /* ivec Ad(PSI,16); |
---|
| 907 | log_level.store(logA,get_from_ivec(Ad));*/ |
---|
[1226] | 908 | } |
---|
| 909 | |
---|
| 910 | ///////// end of copy /////////////// |
---|
| 911 | mmultACh(PSI,Chf,PSICh,4,4); |
---|
[1240] | 912 | //thorton(int16 *U, int16 *D, int16 *PSIU, int16 *Q, int16 *G, int16 *Dold, unsigned int16 dimx); |
---|
[1294] | 913 | //householder(PSICh,Q,4); |
---|
| 914 | givens_fast(PSICh,Q,4); |
---|
[1230] | 915 | // COPY |
---|
[1240] | 916 | for (int16 ii=0; ii<16; ii++){Chf[ii]=PSICh[ii];} |
---|
[1226] | 917 | |
---|
| 918 | { |
---|
[1241] | 919 | |
---|
| 920 | int Chi[16]; for (int i=0;i<16;i++) Chi[i]=Chf[i]; |
---|
| 921 | ivec Chd(Chi,16); |
---|
| 922 | log_level.store(logCh,get_from_ivec(Chd)); |
---|
| 923 | imat mCh(Chd._data(), 4,4); |
---|
| 924 | imat P = mCh*mCh.T(); |
---|
| 925 | ivec iP(P._data(),16); |
---|
| 926 | log_level.store(logP,get_from_ivec(iP)); |
---|
[1226] | 927 | } |
---|
| 928 | |
---|
| 929 | |
---|
[1240] | 930 | int16 difz[2]; |
---|
[1226] | 931 | difz[0]=(Y_mes[0]-x_est[0]); // shift to q15!! |
---|
| 932 | difz[1]=(Y_mes[1]-x_est[1]);//<<2; |
---|
| 933 | |
---|
| 934 | |
---|
| 935 | // |
---|
[1240] | 936 | int16 dR[2];dR[0]=R[0];dR[1]=R[3]; |
---|
| 937 | //int16 xb[4]; xb[0]=x_est[0]<<2; xb[1]=x_est[1]<<2; xb[2]=x_est[2]<<2; xb[3]=x_est[3]; |
---|
[1230] | 938 | |
---|
[1294] | 939 | carlson_fast(difz,x_est,Chf,dR,2,4); |
---|
[1226] | 940 | //x_est[0] = xb[0]>>2; x_est[1]=xb[1]>>2; x_est[2]=xb[2]>>2; x_est[3]=xb[3]; |
---|
| 941 | |
---|
| 942 | // navrat estimovanych hodnot regulatoru |
---|
| 943 | vec& mu = E._mu(); |
---|
| 944 | (mu)(0)=zprevod(x_est[0],15)*Iref; |
---|
| 945 | (mu)(1)=zprevod(x_est[1],15)*Iref; |
---|
| 946 | (mu)(2)=zprevod(x_est[2],15)*Wref; |
---|
| 947 | (mu)(3)=zprevod(x_est[3],15)*Thetaref; |
---|
| 948 | |
---|
| 949 | //x_est[2]=x[2]*32768/Wref; |
---|
| 950 | //x_est[3]=x[3]*32768/Thetaref; |
---|
| 951 | // mat T=diag(concat(vec_2(Iref,Iref), vec_2(Wref,Thetaref))); |
---|
[1179] | 952 | } |
---|
| 953 | |
---|
[1226] | 954 | void EKFfixedCh::init_ekf(double Tv) |
---|
| 955 | { |
---|
| 956 | // Tuning of matrix Q |
---|
[1241] | 957 | Q[0]=prevod(.01,15); // 0.05 |
---|
[1226] | 958 | Q[5]=Q[0]; |
---|
[1294] | 959 | Q[10]=prevod(0.0005,15); // 1e-3 |
---|
| 960 | Q[15]=prevod(0.001,15); // 1e-3 |
---|
[1226] | 961 | |
---|
[1230] | 962 | Chf[0]=0x3FFF;// ! // 0.05 |
---|
[1226] | 963 | Chf[1]=Chf[2]=Chf[3]=Chf[4]=0; |
---|
[1230] | 964 | Chf[5]=0x3FFF;//! |
---|
[1226] | 965 | Chf[6]=Chf[6]=Chf[8]=Chf[9]=0; |
---|
[1230] | 966 | Chf[10]=0x3FFF;//! // 1e-3 |
---|
[1226] | 967 | Chf[11]=Chf[12]=Chf[13]=Chf[4]=0; |
---|
[1230] | 968 | Chf[15]=0x3FFF; // 1e-3 |
---|
[1226] | 969 | |
---|
| 970 | // Tuning of matrix R |
---|
[1294] | 971 | R[0]=prevod(0.05,15); // 0.05 |
---|
[1226] | 972 | R[3]=R[0]; |
---|
| 973 | |
---|
| 974 | // Motor model parameters |
---|
| 975 | cA=prevod(1-Tv*Rs/Ls,15); |
---|
| 976 | cB=prevod(Tv*Wref*Fmag/Iref/Ls,15); |
---|
| 977 | cC=prevod(Tv/Ls/Iref*Uref,15); |
---|
| 978 | // cD=prevod(1-Tv*Bf/J,15); |
---|
| 979 | // cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); |
---|
| 980 | // cF=prevod(p*Tv*Mref/J/Wref,15); |
---|
| 981 | cG=prevod(Tv*Wref/Thetaref,15); //in q15! |
---|
| 982 | //cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); |
---|
[1321] | 983 | cH=prevod(Tv*Wref*Thetaref*Fmag/Iref/Ls,15); // <======= |
---|
| 984 | //cH=prevod(Tv*Wref*Fmag/Iref/Ls,15); // |
---|
[1226] | 985 | // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); |
---|
| 986 | |
---|
| 987 | /* Init matrix PSI with permanently constant terms */ |
---|
| 988 | PSI[0]=cA; |
---|
| 989 | PSI[5]=PSI[0]; |
---|
| 990 | PSI[10]=0x7FFF; |
---|
| 991 | PSI[14]=cG; |
---|
| 992 | PSI[15]=0x7FFF; |
---|
| 993 | |
---|
| 994 | } |
---|
| 995 | |
---|
[1326] | 996 | void EKFfixedCh2::bayes(const itpp::vec& yt, const itpp::vec& ut) |
---|
| 997 | { |
---|
| 998 | ekf2(ut[0],ut[1],yt[0],yt[1]); |
---|
| 999 | } |
---|
| 1000 | |
---|
| 1001 | |
---|
| 1002 | void EKFfixedCh2::ekf2(double ux, double uy, double isxd, double isyd) |
---|
| 1003 | { |
---|
| 1004 | // vypocet napeti v systemu (x,y) |
---|
| 1005 | int16 uf[2]; |
---|
| 1006 | uf[0]=prevod(ux/Uref,15); |
---|
| 1007 | uf[1]=prevod(uy/Uref,15); |
---|
| 1008 | |
---|
| 1009 | int16 Y_mes[2]; |
---|
| 1010 | // zadani mereni |
---|
| 1011 | Y_mes[0]=prevod(isxd/Iref,15); |
---|
| 1012 | Y_mes[1]=prevod(isyd/Iref,15); |
---|
| 1013 | |
---|
| 1014 | ////// vlastni rutina EKF -- ///////////////////////// |
---|
| 1015 | int16 t_sin,t_cos; |
---|
| 1016 | int32 tmp; |
---|
| 1017 | |
---|
| 1018 | x_est[0]=x_est[0]; |
---|
| 1019 | // q15 + q15*q13 |
---|
| 1020 | x_est[1]=(((int32)x_est[1]<<15)+(int32)cG*x_est[0])>>15; |
---|
| 1021 | |
---|
| 1022 | // if(x_est[1]>(1<<15)) x_est[1]-=2*(1<<15); |
---|
| 1023 | // if(x_est[1]<-(1<<15)) x_est[1]+=2*(1<<15); |
---|
| 1024 | |
---|
| 1025 | |
---|
| 1026 | ///////// end of copy /////////////// |
---|
| 1027 | mmultACh(PSI,Chf,PSICh,2,2); |
---|
| 1028 | |
---|
| 1029 | //void EKFfixed::update_psi(void) |
---|
| 1030 | { |
---|
| 1031 | int Ai[4]; |
---|
| 1032 | for (int i=0;i<4; i++) Ai[i]=(int)(PSICh[i]); |
---|
| 1033 | ivec Ad(Ai,4); |
---|
| 1034 | log_level.store(logA,get_from_ivec(Ad)); |
---|
| 1035 | } |
---|
| 1036 | |
---|
| 1037 | //thorton(int16 *U, int16 *D, int16 *PSIU, int16 *Q, int16 *G, int16 *Dold, unsigned int16 dimx); |
---|
| 1038 | givens_fast(PSICh,Q,2); |
---|
| 1039 | for (int i=0;i<4;i++) Chf[i]=PSICh[i]; |
---|
| 1040 | |
---|
| 1041 | // implementace v PC |
---|
| 1042 | /* t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
| 1043 | * t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);*/ |
---|
| 1044 | tmp=prevod(sin(Thetaref*x_est[1]/32768.),15); |
---|
| 1045 | if (tmp>32767) t_sin =32767; else t_sin=tmp; |
---|
| 1046 | tmp=prevod(cos(Thetaref*x_est[1]/32768.),15); |
---|
| 1047 | if (tmp>32767) t_cos =32767; else t_cos=tmp; |
---|
| 1048 | |
---|
| 1049 | { |
---|
| 1050 | C[0]=((int32)cB*t_sin)>>15; |
---|
| 1051 | tmp=((int32)cH*x_est[0])>>15; // ! cH =cB with different scale!! |
---|
| 1052 | C[1]=((int32)tmp*t_cos)>>15; |
---|
| 1053 | C[2]=-((int32)cB*t_cos)>>15; |
---|
| 1054 | C[3]=((int32)tmp*t_sin)>>15; |
---|
| 1055 | } |
---|
| 1056 | |
---|
| 1057 | { |
---|
| 1058 | int Chi[4]; for (int i=0;i<4; i++) Chi[i]=(int)Chf[i]; |
---|
| 1059 | // int Ci[4]; for (int i=0;i<4; i++) Ci[i]=(int)C[i]; |
---|
| 1060 | |
---|
| 1061 | imat Chd(Chi,2,2); |
---|
| 1062 | // imat Cd(Ci,2,2); |
---|
| 1063 | imat CCh = Chd; |
---|
| 1064 | log_level.store(logCh,get_from_ivec(ivec(CCh._data(),CCh._datasize()))); |
---|
| 1065 | } |
---|
| 1066 | |
---|
| 1067 | |
---|
| 1068 | tmp=((int32)cB*x_est[0])>>15; // q15*q13 -> q13 |
---|
| 1069 | // q15*q13 + q13*q15 + q15*q13?? |
---|
| 1070 | y_est[0]=((int32)cA*y_old[0]+(int32)tmp*t_sin+(int32)cC*(uf[0]))>>15; // in q13 |
---|
| 1071 | y_est[1]=((int32)cA*y_old[1]-(int32)tmp*t_cos+(int32)cC*(uf[1]))>>15; // q13 |
---|
| 1072 | |
---|
| 1073 | |
---|
| 1074 | int16 difz[2]; |
---|
| 1075 | difz[0]=(Y_mes[0]-y_est[0]); // shift to q15!! |
---|
| 1076 | difz[1]=(Y_mes[1]-y_est[1]);//<<2; |
---|
| 1077 | |
---|
| 1078 | y_old[0] = Y_mes[0]; |
---|
| 1079 | y_old[1] = Y_mes[1]; |
---|
| 1080 | { |
---|
| 1081 | int Ci[4]; for (int i=0;i<4; i++) Ci[i]=(int)C[i]; |
---|
| 1082 | ivec CC(Ci,4);//;dd(0)=Y_mes[0];dd(1)=Y_mes[1]; dd(2)=difz[0]; dd(3)=difz[1]; |
---|
| 1083 | log_level.store(logC,get_from_ivec(CC)); |
---|
| 1084 | } |
---|
| 1085 | |
---|
| 1086 | //bierman(int16 *difz, int16 *xp, int16 *U, int16 *D, int16 *R, unsigned int16 dimy, unsigned int16 dimx ); |
---|
| 1087 | int16 dR[2];dR[0]=R[0];dR[1]=R[3]; |
---|
| 1088 | //int16 xb[4]; xb[0]=x_est[0]<<2; xb[1]=x_est[1]<<2; xb[2]=x_est[2]<<2; xb[3]=x_est[3]; |
---|
[1468] | 1089 | int16 rem; |
---|
| 1090 | int32 detS; |
---|
[1464] | 1091 | carlson_fastC(difz,x_est,Chf,C,dR,2,2, &detS, &rem); |
---|
[1326] | 1092 | //x_est[0] = xb[0]>>2; x_est[1]=xb[1]>>2; x_est[2]=xb[2]>>2; x_est[3]=xb[3]; |
---|
| 1093 | |
---|
[1464] | 1094 | log_level.store(logDet,vec_1((double)detS)); |
---|
| 1095 | log_level.store(logRem,vec_1((double)rem)); |
---|
| 1096 | |
---|
[1326] | 1097 | // navrat estimovanych hodnot regulatoru |
---|
| 1098 | vec& mu = E._mu(); |
---|
| 1099 | (mu)(0)=zprevod(x_est[0],15)*Wref; |
---|
| 1100 | (mu)(1)=zprevod(x_est[1],15)*Thetaref; |
---|
| 1101 | |
---|
| 1102 | //x_est[2]=x[2]*32768/Wref; |
---|
| 1103 | //x_est[3]=x[3]*32768/Thetaref; |
---|
| 1104 | // mat T=diag(concat(vec_2(Iref,Iref), vec_2(Wref,Thetaref))); |
---|
| 1105 | } |
---|
| 1106 | |
---|
| 1107 | void EKFfixedCh2::init_ekf2(double Tv) |
---|
| 1108 | { |
---|
| 1109 | // Tuning of matrix Q |
---|
[1330] | 1110 | Q[0]=prevod(0.005,15); // 1e-3 |
---|
| 1111 | Q[3]=prevod(0.001,15); // 1e-3 |
---|
[1326] | 1112 | |
---|
| 1113 | Chf[0]=0x1FFF;// ! // 0.05 |
---|
| 1114 | Chf[1]=Chf[2]=0; |
---|
| 1115 | Chf[3]=0x1FFF;//! |
---|
| 1116 | |
---|
| 1117 | // Tuning of matrix R |
---|
[1330] | 1118 | R[0]=prevod(0.001,15); // 0.05 |
---|
[1326] | 1119 | R[3]=R[0]; |
---|
| 1120 | |
---|
| 1121 | // Motor model parameters |
---|
| 1122 | cA=prevod(1-Tv*Rs/Ls,15); |
---|
| 1123 | cB=prevod(Tv*Wref*Fmag/Iref/Ls,15); |
---|
| 1124 | cC=prevod(Tv/Ls/Iref*Uref,15); |
---|
| 1125 | // cD=prevod(1-Tv*Bf/J,15); |
---|
| 1126 | // cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); |
---|
| 1127 | // cF=prevod(p*Tv*Mref/J/Wref,15); |
---|
| 1128 | cG=prevod(Tv*Wref/Thetaref,15); //in q15! |
---|
| 1129 | //cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); |
---|
| 1130 | // cH=prevod(Tv*Wref*Thetaref*Fmag/Iref/Ls,15); // < =========== |
---|
| 1131 | cH=prevod(Tv*Wref*Fmag/Iref/Ls,15); // |
---|
| 1132 | // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); |
---|
| 1133 | |
---|
| 1134 | /* Init matrix PSI with permanently constant terms */ |
---|
| 1135 | PSI[0]=0x7FFF; |
---|
| 1136 | PSI[2]=cG; |
---|
| 1137 | PSI[3]=0x7FFF; |
---|
| 1138 | } |
---|