Search code examples
c++matlabsimulinkstate-space

Use value from Input Port in Parameter of block - Simulink


I have a simulink model that I plan on converting to C code and using elsewhere. I have defined 'input ports' in order to set variables in the simulink model.

I am trying to find a way to use the input variables as part of a State Space block but have tried everything and not sure how else to go about it.

As mentioned this will be converted to C/C++ code so there is no option to use matlab in anyway.

Say I use matrix A in the state-space block parameter. Matrix A is defined liek so A= [Input1 0; Input2 0; 0 Input3]

I want to be able to change the values of the inputs through the code by setting the values of Input1 2 3 etc.


Solution

  • There is a very clear distinction in Simulink between Parameters and Signals. A parameter is something entered into a dialog, while a signal is something fed into or coming out of a block.

    The matrices in the State-Space block are defined as parameters, and hence you will never be able to feed your signals into them.

    You have two options.

    1. Don't use the State-Space block. Rather develop the state-space model yourself using more fundamental blocks (i.e. integrators, sums and product blocks). This is feasible for small models, but not really recommended.

    2. Note that the Parameters of a block a typically tunable. When you generate code, one of the files will be model_name_data.c and this will contain a parameter structure allowing you to change, the parameters.

    Note that in either case, merely from a model design perspective, it'll be up to you to ensure that the changes to the model make sense (for instance don't make any loop, etc. go unstable).