library for implementing continuous-time linear time invariant systems. for PID controller, filters etc.

Dependents:   ltisys_test

ltisys

Library for implementing PID controllers / continuous-time filters / etc.

All codes are inlined to enhance the performance.

Code generation from MATLAB

The following small script is useful for implementing controllers designed in MATLAB.

Example

As for the usage, see the sample program

Import programltisys_test

test program for ltisys library which implements continuous-time linear time-invariant systems

Files at this revision

API Documentation at this revision

Comitter:
maruta
Date:
Sat May 09 02:48:46 2015 +0000
Parent:
3:e093e92b7039
Commit message:
updated to use const A, B, C, D matrices

Changed in this revision

ltisys.h Show annotated file Show diff for this revision Revisions of this file
--- a/ltisys.h	Fri May 08 17:23:03 2015 +0000
+++ b/ltisys.h	Sat May 09 02:48:46 2015 +0000
@@ -28,7 +28,7 @@
      *
      * @note All matrices are row major in this library. Different from MATLAB/FORTRAN.
      */
-    ltisys(double *matA, double *matB, double *matC, double *matD);
+    ltisys(const double *matA, const double *matB, const double *matC, const double *matD);
     virtual ~ltisys();
 
     /** Update states and outputs
@@ -48,26 +48,22 @@
     ///  system outputs
     double y[ny];
     
-    double A[nx][nx], B[nx][nu], C[ny][nx], D[ny][nu];
+    const double *A, *B, *C, *D;
 
 private:
     inline void matcopy(int nrow, int ncol, const double *src,
             double *dst);
     inline void mataddprod(int nrowDst, int ncolA, int ncolDst,
-            double *matA, double *matB,
+            const double *matA, const double *matB,
             double *dst);
     inline void matprod(int nrowDst, int ncolA, int ncolDst,
-            double *matA, double *matB,
+            const double *matA, const double *matB,
             double *dst);
     inline void calc_dx(double *x, double *dx);
 };
 
 template <int nx, int nu, int ny>
-ltisys<nx,nu,ny>::ltisys(double *matA, double *matB, double *matC, double *matD) {
-    matcopy(nx,nx,matA,&A[0][0]);
-    matcopy(nx,nu,matB,&B[0][0]);
-    matcopy(ny,nx,matC,&C[0][0]);
-    matcopy(ny,nu,matD,&D[0][0]);
+ltisys<nx,nu,ny>::ltisys(const double *matA, const double *matB, const double *matC, const double *matD): A(matA), B(matB), C(matC), D(matD){
     for(int i=0;i<nx;i++) x[i]=0.0;
     for(int i=0;i<nu;i++) u[i]=0.0;
     for(int i=0;i<ny;i++) y[i]=0.0;
@@ -109,9 +105,9 @@
     }
 
     // update I/O
-    matprod(ny,nx,1,&C[0][0],&x[0],&y[0]);
+    matprod(ny,nx,1,C,&x[0],&y[0]);
     matcopy(nu,1,new_u,u);
-    mataddprod(ny,nu,1,&D[0][0],&u[0],&y[0]);
+    mataddprod(ny,nu,1,D,&u[0],&y[0]);
 }
 
 template <int nx, int nu, int ny>
@@ -123,7 +119,7 @@
 
 template <int nx, int nu, int ny>
 inline void ltisys<nx,nu,ny>::mataddprod(int nrowDst, int ncolA, int ncolDst,
-        double *matA, double *matB,
+        const double *matA, const double *matB,
         double *dst) {
     for (int i = 0; i < nrowDst; i++) {
         for (int j = 0; j < ncolDst; j++) {
@@ -138,7 +134,7 @@
 
 template <int nx, int nu, int ny>
 inline void ltisys<nx,nu,ny>::matprod(int nrowDst, int ncolA, int ncolDst,
-        double *matA, double *matB,
+        const double *matA, const double *matB,
         double *dst) {
     for (int i = 0; i < nrowDst; i++) {
         for (int j = 0; j < ncolDst; j++) {
@@ -156,10 +152,10 @@
     for (int i = 0; i < nx; i++) {
         dx[i] = 0.0;
         for (int j = 0; j < nx; j++) {
-            dx[i] += A[i][j] * x[j];
+            dx[i] += A[i*nx+j] * x[j];
         }
         for (int j = 0; j < nu; j++) {
-            dx[i] += B[i][j] * u[j];
+            dx[i] += B[i*nu+j] * u[j];
         }
     }
 }