EasyLink
EasyLink is a C++ software development kit for developing blocks for SIMULINK (S-functions)
/Users/guillaume.laurent/Documents/Prog/easylinkEigen/easylink/sfunStateSpace.cpp
1 //------------------------------------------------------------------------------
2 /* C++ S-function for defining a continuous system with EasyLink and Eigen.
3  *
4  * x' = Ax + Bu
5  * y = Cx + Du
6  *
7  * To compile this C++ S-function, enter the following command in MATLAB:
8  *
9  * >>make sfunStateSpace.cpp
10  *
11  * Then open the file "testStateSpace.mdl/slx" and start the simulation.
12  */
13 //------------------------------------------------------------------------------
14 #define S_FUNCTION_NAME sfunStateSpace
15 
16 enum inputPortName {
17  U
18 };
19 
20 enum outputPortName {
21  Y
22 };
23 
24 enum parameterName {
25  A, B, C, D
26 };
27 
28 
29 //------------------------------------------------------------------------------
30 #include "EasyLink.h"
31 
32 #include <Eigen/Dense>
33 
34 #define EIGEN_INPUT_MATRIX(type, name, port) Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic> > name((type*)getInputData(port), getInputNRows(port), getInputNCols(port))
35 #define EIGEN_OUTPUT_MATRIX(type, name, port) Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic> > name((type*)getOutputData(port), getOutputNRows(port), getOutputNCols(port))
36 #define EIGEN_PARAMETER_MATRIX(type, name, port) name= Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic> >((type*)getParameterData(port), getParameterNRows(port), getParameterNCols(port))
37 #define EIGEN_STATE_MATRIX(type, name) Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic> > name((type*)getContinuousStateData(), getContinuousStateWidth(), 1)
38 #define EIGEN_DERIVATIVE_STATE_MATRIX(type, name) Eigen::Map<Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic> > name((type*)getDerivativeStateData(), getContinuousStateWidth(), 1)
39 
40 
41 //------------------------------------------------------------------------------
42 
43 class Block : public BaseBlock {
44 private:
45 
46  Eigen::MatrixXd a, b, c, d;
47 
48 public:
49 
50  static void checkParametersSizes() {
52  assertParameterPort(A, false, -1, -1, mxDOUBLE_CLASS);
53  assertParameterPort(B, false, -1, -1, mxDOUBLE_CLASS);
54  assertParameterPort(C, false, -1, -1, mxDOUBLE_CLASS);
55  assertParameterPort(D, false, -1, -1, mxDOUBLE_CLASS);
56  }
57 
58  static void initializeInputPortSizes() {
60  setInputPort(U, getParameterNCols(B), 1, SS_DOUBLE);
61  }
62 
63  static void initializeOutputPortSizes() {
65  setOutputPort(Y, getParameterNRows(C), 1, SS_DOUBLE);
66  }
67 
68  static void initializeStatePortSizes() {
71  }
72 
73  static void initializeSampleTimes() {
74  ssSetSampleTime(simStruct, 0, CONTINUOUS_SAMPLE_TIME);
75  ssSetOffsetTime(simStruct, 0, 0.0);
76  // ssSetModelReferenceSampleTimeDefaultInheritance(simStruct);
77  }
78 
79  void start() {
80  EIGEN_PARAMETER_MATRIX(double, a, A);
81  EIGEN_PARAMETER_MATRIX(double, b, B);
82  EIGEN_PARAMETER_MATRIX(double, c, C);
83  EIGEN_PARAMETER_MATRIX(double, d, D);
84  }
85 
86  void outputs() {
87  EIGEN_INPUT_MATRIX(double, u, U);
88  EIGEN_OUTPUT_MATRIX(double, y, Y);
89  EIGEN_STATE_MATRIX(double, x);
90 
91  y = c * x + d*u;
92  }
93 
94  void derivatives() {
95  EIGEN_INPUT_MATRIX(double, u, U);
96  EIGEN_STATE_MATRIX(double, x);
97  EIGEN_DERIVATIVE_STATE_MATRIX(double, dx);
98 
99  dx = a * x + b*u;
100  }
101 
102 };
103 
104 //------------------------------------------------------------------------------
105 #include "sfunDefinitions.h"
106 
107 //------------------------------------------------------------------------------
static void setInputPortsCount(int portsCount)
Definition: BaseBlock.h:104
static int getParameterNRows(int port)
Definition: BaseBlock.h:596
static void setOutputPortsCount(int portsCount)
Definition: BaseBlock.h:152
void outputs()
Definition: BaseBlock.h:340
static void setInputPort(int port, int nRows, int nCols, DTypeId type=SS_DOUBLE, bool directFeedThrough=true)
Definition: BaseBlock.h:116
static void initializeInputPortSizes()
Definition: BaseBlock.h:145
static void setContinuousStatesWidth(int num)
Definition: BaseBlock.h:197
void derivatives()
Definition: BaseBlock.h:352
static void initializeSampleTimes()
Definition: BaseBlock.h:312
static void assertParameterPort(int port, bool tunable, int nRows, int nCols, mxClassID type=mxDOUBLE_CLASS, mxComplexity complexFlag=mxREAL)
Definition: BaseBlock.h:57
static void checkParametersSizes()
Definition: BaseBlock.h:97
static void initializeOutputPortSizes()
Definition: BaseBlock.h:189
void start()
Definition: BaseBlock.h:327
static void setDiscreteStatesWidth(int num)
Definition: BaseBlock.h:205
static void initializeStatePortSizes()
Definition: BaseBlock.h:218
static int getParameterNCols(int port)
Definition: BaseBlock.h:604
static void assertParameterPortsCount(int portsCount)
Definition: BaseBlock.h:42
static void setOutputPort(int port, int nRows, int nCols, DTypeId type=SS_DOUBLE)
Definition: BaseBlock.h:164

Copyright (c) 2014 FEMTO-ST / ENSMM / UFC / UTBM, Besançon, France. Generated by Doxygen.