System Controlling: Continous to discrete system
Processing math: 100%

Sunday, April 22, 2012

Continous to discrete system

In a real environment we are using discretized system,  so because of this we need to discretize our system. This page is contains a short description about the zoh and an example written in Python using numpy's.

Discretization with zero order hold element on the input

It's given the following continuous-time state space model:
˙x(t)=Ax(t)+Bu(t)
y(t)=Cx(t)+Du(t)

It can be shown that the following discrete-time state space model expresses how the state x evolves along a discrete time axis x[k+1]=Adx[k]+Bdu[k]
y[k]=Cdx[k]+Ddu[k]

Where: Ad=eATs
Bd=(Tsτ=0eAτdτ)B
Cd=C
Dd=D
Where Ts is the sampling time
There is a trick how can we calculate the Ad,Bd discrete matrices: M=[AB00]
[M11M120I]=eMTs
After the calculation we can get easily the Ad,Bd discrete matrices: Ad=M11,Bd=M21
The following code written in Python contains the theory above.
from numpy import hstack , vstack, zeros
from scipy.linalg import expm
from control.matlab import tf2ss, ss2tf

def c2d ( sys , Ts, method = 'ZOH' ) :
    """
    @summary: From continuous time convert discrete time
 
    @param sys:  an instance of the LTI class or a tuple describing the system.
    The following gives the number of elements in the tuple and
    the interpretation:

    2: (num, den)        
    4: (A, B, C, D)
    @param Ts: Sampling time
    @param method: The name of the discretization method
    
    @return: The discrete system    
    """
    if(len(sys) == 2):
        (A, B, C, D) = tf2ss( sys )
    else:
        (A, B, C, D) = sys
 
    n = A.shape[ 0 ]
    nb = B.shape[ 1 ]
    if(method == 'ZOH'):
        ztmp= zeros ( ( nb , n + nb ) ) 
        tmp = hstack ( ( A , B ) ) 
        tmp = vstack ( ( tmp , ztmp ) ) 
        tmp = expm( tmp * Ts )
 
        A = tmp [ 0 : n , 0 : n ]
        B = tmp [ 0 : n , n : n+nb ]
  
  
    sysd = ( A , B , C , D  ) 
    if(len(sys) == 2):
       return ss2tf ( sysd )
    return sysd
Also it's possible to download from the github.

No comments:

Post a Comment