| 153 | class PMSM_LQCtrl: public PMSMCtrl{ |
| 154 | public: |
| 155 | /* |
| 156 | PMSMCtrl: |
| 157 | double isa; |
| 158 | double isb; |
| 159 | double ome; |
| 160 | double the; |
| 161 | double Ww; |
| 162 | */ |
| 163 | |
| 164 | //PMSM parameters |
| 165 | const double a; |
| 166 | const double b; |
| 167 | const double c; |
| 168 | const double d; |
| 169 | const double e; |
| 170 | |
| 171 | //input penalty |
| 172 | double r; |
| 173 | |
| 174 | //time step |
| 175 | const double Dt; |
| 176 | |
| 177 | //receding horizon |
| 178 | const int rec_hor; |
| 179 | |
| 180 | //system matrices |
| 181 | mat A; //5x5 |
| 182 | mat B; //5x2 |
| 183 | //mat C; //2x5 |
| 184 | mat Q; //5x5 |
| 185 | mat R; //2x2 |
| 186 | |
| 187 | //control matrix |
| 188 | mat L; |
| 189 | vec uab; |
| 190 | vec icond; |
| 191 | |
| 192 | //control maximum |
| 193 | const double MAXu; |
| 194 | |
| 195 | //lqg controler class |
| 196 | LQG_universal lq; |
| 197 | |
| 198 | //prediction |
| 199 | vec p_isa, p_isb, p_ome, p_the; |
| 200 | |
| 201 | PMSM_LQCtrl():PMSMCtrl(), a(0.9898), b(0.0072), c(0.0361), d(1.0), e(0.0149), |
| 202 | r(0.005), Dt(0.000125), rec_hor(10), //for r is a default value rewrited by pcp.txt file value |
| 203 | A(5, 5), B(5, 2), Q(5, 5), R(2, 2), |
| 204 | uab(2), icond(6), MAXu(100.0), |
| 205 | p_isa(rec_hor+1), p_isb(rec_hor+1), p_ome(rec_hor+1), p_the(rec_hor+1) |
| 206 | { |
| 207 | //set fix matrices elements & dimensions |
| 208 | A.zeros(); |
| 209 | A(0, 0) = A(1, 1) = a; |
| 210 | A(2, 2) = d; |
| 211 | A(2, 4) = d - 1; |
| 212 | A(3, 2) = A(3, 4) = Dt; |
| 213 | A(3, 3) = A(4, 4) = 1.0; |
| 214 | B.zeros(); |
| 215 | B(0, 0) = B(1, 1) = c; |
| 216 | //C.zeros(); |
| 217 | // C(0, 0) = C(1, 1) = 1.0; |
| 218 | Q.zeros(); |
| 219 | Q(2, 2) = 1.0; |
| 220 | R.zeros(); |
| 221 | //load input penalty r from file |
| 222 | ifstream pcpfile; |
| 223 | double pcp; |
| 224 | |
| 225 | pcpfile.open("pcp.txt"); |
| 226 | if(pcpfile){ |
| 227 | if(pcpfile >> pcp) r = pcp; |
| 228 | pcpfile.close(); |
| 229 | } |
| 230 | R(0, 0) = R(1, 1) = r; |
| 231 | |
| 232 | //set lq |
| 233 | lq.rv = RV("u", 2, 0); |
| 234 | lq.set_rvc(RV("x", 5, 0)); |
| 235 | lq.horizon = rec_hor; |
| 236 | |
| 237 | Array<linfnEx> model(2); |
| 238 | model(0).A = A; |
| 239 | model(0).B = vec("0 0 0 0 0"); |
| 240 | model(0).rv = RV("x", 5, 0); |
| 241 | model(0).rv_ret = RV("x", 5, 1); |
| 242 | model(1).A = B; |
| 243 | model(1).B = vec("0 0"); |
| 244 | model(1).rv = RV("u", 2, 0); |
| 245 | model(1).rv_ret = RV("x", 5, 1); |
| 246 | lq.Models = model; |
| 247 | |
| 248 | Array<quadraticfn> qloss(2); |
| 249 | qloss(0).Q.setCh(Q); |
| 250 | qloss(0).rv = RV("x", 5, 1); |
| 251 | qloss(1).Q.setCh(R); |
| 252 | qloss(1).rv = RV("u", 2, 0); |
| 253 | lq.Losses = qloss; |
| 254 | |
| 255 | lq.finalLoss.Q.setCh(Q); |
| 256 | lq.finalLoss.rv = RV("x", 5, 1); |
| 257 | |
| 258 | lq.validate(); |
| 259 | |
| 260 | uab.zeros(); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | virtual vec ctrlaction(const itpp::vec& cond) { |
| 265 | PMSMCtrl::ctrlaction(cond); // fills isa,isb,ome,the,Ww |
| 266 | |
| 267 | int i; |
| 268 | lq.resetTime(); |
| 269 | |
| 270 | //create prediction |
| 271 | p_isa.zeros(); |
| 272 | p_isb.zeros(); |
| 273 | p_ome.zeros(); |
| 274 | p_the.zeros(); |
| 275 | p_isa(0) = isa; |
| 276 | p_isb(0) = isb; |
| 277 | p_ome(0) = ome; |
| 278 | p_the(0) = the; |
| 279 | |
| 280 | for(i = 0; i < rec_hor-1; i++){ |
| 281 | p_isa(i+1) = a*p_isa(i) + b*p_ome(i)*sin(p_the(i)) + c*0;//uab(0); //use last used control |
| 282 | p_isb(i+1) = a*p_isb(i) - b*p_ome(i)*cos(p_the(i)) + c*0;//uab(1); |
| 283 | p_ome(i+1) = d*p_ome(i) + e*(p_isb(i)*cos(p_the(i)) - p_isa(i)*sin(p_the(i))); |
| 284 | p_the(i+1) = p_the(i) + Dt*p_ome(i); |
| 285 | } |
| 286 | |
| 287 | //create control matrix |
| 288 | for(i = rec_hor; i > 0; i--){ |
| 289 | //set variable matrices elements (A matrix only) |
| 290 | A(0, 2) = b*sin(p_the(i)); |
| 291 | A(0, 3) = b*(p_ome(i))*cos(p_the(i)); |
| 292 | A(0, 4) = b*sin(p_the(i)); |
| 293 | A(1, 2) = -b*cos(p_the(i)); |
| 294 | A(1, 3) = b*(p_ome(i))*sin(p_the(i)); |
| 295 | A(1, 4) = -b*cos(p_the(i)); |
| 296 | A(2, 0) = -e*sin(p_the(i)); |
| 297 | A(2, 1) = e*cos(p_the(i)); |
| 298 | A(2, 3) = -e*(p_isa(i)*cos(p_the(i)) + p_isb(i)*sin(p_the(i))); |
| 299 | |
| 300 | lq.Models(0).A = A; |
| 301 | lq.redesign(); |
| 302 | } |
| 303 | lq.redesign(); |
| 304 | |
| 305 | L = lq.getL(); |
| 306 | icond(0) = isa; |
| 307 | icond(1) = isb; |
| 308 | icond(2) = ome - Ww; |
| 309 | icond(3) = the; |
| 310 | icond(4) = Ww; |
| 311 | icond(5) = 0; |
| 312 | vec tmp = L*icond; |
| 313 | |
| 314 | uab = tmp(0,1); |
| 315 | |
| 316 | if(uab(0) > MAXu) uab(0) = MAXu; |
| 317 | else if(uab(0) < -MAXu) uab(0) = -MAXu; |
| 318 | if(uab(1) > MAXu) uab(1) = MAXu; |
| 319 | else if(uab(1) < -MAXu) uab(1) = -MAXu; |
| 320 | |
| 321 | return uab; |
| 322 | }; |
| 323 | }; |
| 324 | UIREGISTER(PMSM_LQCtrl); |
| 325 | |