2

I am attempting to implement observer based state feedback in C and can't figure it out.

Here is pseudocode of the algorithm:

//Initialize Stuff

loop:

//read process variable                                                                                 
y = read(ch1)  

//compute control variable                                                                      
u = -K * xhat 

//set process input                                                                             
write(u) 

//update state estimate                                                                                  
xhat = xhat + Ts * (A*xhat + B*u + L*(y-C*xhat))

// repeat loop
  • xhat is the estimated state
  • K is the feedback gain
  • L is the observer gain
  • y is the system output
  • u is the system input
  • Ts is the sampling period
  • A, B, & C are the state space matrices

Is this how observer feedback is supposed to be implemented?

Here is the actual code I have for the loop:

// Get Process Output from Sensors:
y[0] = sensor1();
y[1] = sensor2();

// Compute Control Variable:
u = 0;  
for (i=0; i<4; i++)         
    u += K[i]*xhat[i];

// Set Process Input:
write_to_DAC(u);

// Calculate: A*xhat + B*u
for (i=0; i<4; i++) {
    Ax_Bu[i] = 0;
    for (j=0; j<4; j++)
        Ax_Bu[i] += A[i*4+j]*xhat[j];    
    Ax_Bu[i] += B[i]*u; 
}

// Calculate: y-y_hat
for (i=0; i<2; i++) {
    yhat[i]   = 0;
    y_yhat[i] = 0;  
    for (j=0; j<4; j++) {
        yhat[i] +=  C[i*4+j]*xhat[j]; // Calculate yhat = C*xhat
        y_yhat[i] = y[i]-yhat[i];     // Calculate the 'Innovation'
    }             
}

// Calculate: L*(y-y_hat)
for (i=0; i<4; i++) {
    L_y_yhat[i] = 0;
    for (j=0; j<2; j++) 
        L_y_yhat[i] += L[i*2+j]*y_yhat[j];    
}   

// Update State Estimate:
for (i=0; i<4; i++) 
    xhat[i] += Ts*(Ax_Bu[i] + L_y_yhat[i]);
Cosmo Kramer
  • 21
  • 1
  • 3

1 Answers1

1

Assuming $y$ and $u$ are scalars, then the first equation:

$$ u = -K \hat{x} $$

is just a vector-vector multiplication and can be implemented in C as:

u = 0.0;
for (i=0; i<N; i++)
{  
    u += xhat[i]*K[i];
}

assuming that $K$ is $1 \times N$ and that $\hat{x}$ is $N \times 1$.

The multiplication C*xhat can be implemented similarly (because I suspect that $C$ is really $1 \times N$ rather than $2 \times N$).

The multiplications L*(y-C*xhat) and B*u are vector-scalar multiplications which, for the latter, would look like:

Bu = 0.0;
for (i=0; i<N; i++)
{  
    Bu += B[i]*u;
}

The multiplication A*xhat will be slightly different because $A$ is $N \times N$.

Peter K.
  • 25,714
  • 9
  • 46
  • 91
  • Thanks for the explanation Peter. $C$ is 2x4. And does the order of the loop in my original post look correct? – Cosmo Kramer Mar 24 '13 at 00:12
  • OK, that means $y$ must be $2 \times 1$; I assumed it was scalar. No real matter.

    Regarding the loop order: I would have thought the LAST thing to do would be write(u)... but it depends on the indices in the original equations.

    – Peter K. Mar 24 '13 at 00:18
  • In the line where the state estimate is updated is it proper to multiply that term by the sampling period? I am referring to: Ts * (Axhat + Bu + L(y-Cxhat)) – Cosmo Kramer Mar 24 '13 at 00:49
  • That Ts term often shows up. You'd have to point me to the origin of the equations before I could say unequivocally. – Peter K. Mar 24 '13 at 18:24
  • I got the algorithm from pages 136-137 on this document: http://www.cds.caltech.edu/~murray/courses/cds101/fa04/caltech/am04_ch5-24oct04.pdf – Cosmo Kramer Mar 24 '13 at 18:55
  • I edited the original post to include the actual code of the algorithm. It does not seem to work. Is the code correct? – Cosmo Kramer Mar 24 '13 at 20:49
  • Yeah, I have tried everything I can think of. All of the estimated states just continually grow. Does anyone see anything wrong with that code? – Cosmo Kramer Mar 28 '13 at 18:01
  • Can you provide some numerical examples? How are you calculating K[i] and L[i] ? – Peter K. Mar 28 '13 at 19:14
  • I am designing $K$ and $L$ as described on this page (the project is an inverted pendulum on a cart): http://ctms.engin.umich.edu/CTMS/index.php?example=InvertedPendulum§ion=ControlStateSpace – Cosmo Kramer Mar 29 '13 at 20:29