-2

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.

Mr Dog
  • 396
  • 1
  • 3
  • 22
  • Please show us what you tried and what error message you get. – Daniel Mar 11 '16 at 10:26
  • @Daniel I have tried using inports but they are not reference able in the Parameter Block. I have also tried creating parameters in the model workspace but they do not show up in the code generation. – Mr Dog Mar 11 '16 at 16:16
  • So you have a model which is working properly but does not support code generation? Please share a minimal example for that model which does what you want. Describe what no longer works after generating code. Is the problem that it does not work at all, or that you can't modify the parameter. – Daniel Mar 11 '16 at 16:21
  • @Daniel Everything works but I am unable to change the values – Mr Dog Mar 11 '16 at 16:37

2 Answers2

3

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).

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
  • "Note that the Parameters of a block a typically tunable." Is this also true when the variable is part of the model workspace? Thought it would only be tunable by default when the variable part of the base workspace. – Daniel Mar 11 '16 at 17:12
  • 1
    You're right in that the [doc](http://www.mathworks.com/help/simulink/ug/using-model-workspaces.html) does say that Model Workspace Variables aren't *generally* tunable, however (in some instances) a model_name_data.c file is still generated. – Phil Goddard Mar 11 '16 at 18:09
  • Thanks! I ended up just building the state-space model myself as it wasn't a very big model. – Mr Dog Mar 12 '16 at 22:32
1

You can not tune the parameter after generating code, because it is inlined with a constant value, this is typically done because it results in the fastest code. To have full control over the behaviour, you have to use tunable parameters. There is a table with different code versions, depending on what you want you can choose the right type of parameter.

Another lazy way to achieve this in many cases is using base workspace variables, very simple to achieve and works fine in the most cases.

Daniel
  • 36,610
  • 3
  • 36
  • 69