simulink coder, initialize Simulink.Bus top level input ports

6 views (last 30 days)
I have a simulink model that I am autocoding to C++ (via Simulink coder). The subsystem I want to build has inputs and outputs that are buses. So I defined Simulink.Bus data types for the autocode to treat the buses as C structs. Unfortunately, my input bus gets inizialized to a const struct where all the values are 0. This results in division by 0 and NaNs quickly propagate everywhere.
Is it possible to define an initial value of an input port to replace the default zeros?
In my full simulink model there are no top level inputs or outputs -- the initial values are specified at the integrators and unit delays which are all connected in a loop. I've broken my model into atomic subsystems and I'm builing the subsystems independently. So the autocoded algorithm starts at the input port rather than a block that has an initial state.

Answers (2)

Kaustubha Govind
Kaustubha Govind on 29 Apr 2013
I haven't tried this, but I wonder if you could use Simulink.Signal objects to specify initial values for your signal. Right-click on the signal exiting the Inport block and select Properties in the context menu. Enter the signal name as the name of a Simulink.Signal object (DataType set to your Simulink.Bus type and the appropriate InitialValue) in the base workspace, and check the box "Signal name must resolve to Simulink signal object".
  2 Comments
Roman Luz
Roman Luz on 29 Apr 2013
Thanks for your suggestion.
I have tried to use a Simulink.Signal object, and it does not appear to do anything but rename the variable in the autocode. I don't see the initial value that I defined (as a Matlab struct) used anywhere in the generated code.
I applied this signal object to the output of the input port that I wanted to initialize: rw_in_signal_init = Simulink.Signal; rw_in_signal_init.DataType = 'fsw_out'; rw_in_signal_init.InitialValue = 'fsw_out_struct';
Kaustubha Govind
Kaustubha Govind on 30 Apr 2013
Ah ok. I'm not sure how else you can achieve what you need. Could you try contacting MathWorks Technical Support about this?

Sign in to comment.


Roman Luz
Roman Luz on 29 Apr 2013
Thanks.
I'm also looking for a good mechanism to allow users to (re)define parameter values after the auto-code is compiled. The only technique I've noticed that will allow me to do that is using tunable parameters.
I think one option I have with tunable parameters is to define the variables as extern, which gives me access to define their values.
Might it be possible to combine tunable parameters with your suggested approach?

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!