root/library/bdm/design/ctrlbase.cpp @ 1187

Revision 1064, 2.2 kB (checked in by mido, 14 years ago)

astyle applied all over the library

  • Property svn:eol-style set to native
Line 
1#include "ctrlbase.h"
2
3namespace bdm {
4
5void LQG::set_system ( shared_ptr<StateSpace<chmat> > S0 ) {
6    S = S0;
7}
8
9void LQG::update_system() {
10    pr.set_submatrix ( 0, 0, S->_B() );
11    pr.set_submatrix ( 0, dimu, S->_A() );
12
13    //penalization
14    qux.set_submatrix ( 0, 0, Qu._Ch() );
15    qux.set_submatrix ( 0, dimx + dimu + dimy, -Qu._Ch() );
16
17    qyx.set_submatrix ( 0, 0, S->_C() );
18    qyx.set_submatrix ( 0, dimx, -eye ( dimy ) );
19
20    // parts of QR
21    hqy = Qy.to_mat() * qyx * pr;
22
23    // pre_qr
24    pre_qr = concat_vertical ( s * pr, concat_vertical ( hqy, qux ) );
25}
26
27void LQG::set_control_parameters ( const mat &Qy0, const mat &Qu0, const vec &y_req0, int horizon0 ) {
28    Qy = Qy0;
29    Qu = Qu0;
30    y_req = y_req0;
31    horizon = horizon0;
32}
33
34void LQG::validate() {
35    // set properties from S
36    dimx = S->_A().rows();
37    dimy = S->_C().rows();
38    dimu = S->_B().cols();
39    pr = zeros ( dimx + dimu + dimy,  dimu + dimx + dimu + dimy );
40    pr.set_submatrix ( dimx, dimu + dimx, eye ( dimu + dimy ) );
41
42    //penalization
43    bdm_assert ( Qy.cols() == dimy, "LQG: wrong dimensions of Qy " );
44    bdm_assert ( Qu.cols() == dimu, "LQG: wrong dimensions of Qu " );
45    bdm_assert ( y_req.length() == dimy, "LQG: wrong dimensions of y_req " );
46
47    qux = zeros ( dimu, dimx + 2 * dimu + dimy );
48    qyx = zeros ( dimy, dimx + dimy + dimu );
49
50    //
51    initial_belmann();
52    // parts of QR
53    post_qr = zeros ( pre_qr.rows(), pre_qr.cols() );
54
55    update_system();
56}
57
58void LQG::ricatti_step() {
59//      pre_qr.set_submatrix ( 0, 0, s*pr );
60//      pre_qr.set_submatrix ( dimx + dimu + dimy, dimu + dimx, -Qy.to_mat() *y_req );
61    if ( !qr ( pre_qr, post_qr ) ) {
62        bdm_warning ( "QR in LQG unstable" );
63    }
64    triu ( post_qr );
65    // hn(m+1:2*m+n+r,m+1:2*m+n+r);
66    s = post_qr.get ( dimu, 2 * dimu + dimx + dimy - 1, dimu, 2 * dimu + dimx + dimy - 1 );
67};
68
69void LQG::redesign() {
70    for ( td = horizon; td > 0; td-- ) {
71        update_system();
72        ricatti_step();
73    }
74    /*                  ws=hn(1:m,m+1:2*m+n+r);
75                        wsd=hn(1:m,1:m);
76                        Lklq=-inv(wsd)*ws;*/
77    L = -inv ( post_qr.get ( 0, dimu - 1, 0, dimu - 1 ) ) * post_qr.get ( 0, dimu - 1, dimu, 2 * dimu + dimx + dimy - 1 );
78}
79
80
81}
Note: See TracBrowser for help on using the browser.