Is there an example of using DWork vectors to store discrete states in Simulink?

3 views (last 30 days)
I am trying to use DWork vectors to store discrete states inside my S-function. I was wondering if there was an example of how to do this. Also, I want to write a TLC-file so I can inline the S-function.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
Attached below is a small example. In it, two discrete states are stored inside a DWork vector. Note you could also implement multiple work vectors, one for each state, if desired. Storing multiple states inside one vector could possibly simplify code, since it would require only one function parameter being passed to the wrapper functions. It could also possibly simplify the TLC code needed, as you would only have to point to one DWork vector.
In dwork_example.c, there are a few lines to note:
41: The number of discrete states is set to zero. This is because the discrete states are going to be stored in the DWork vectors.
64-68: Set up the DWork vectors. This includes using ssSetDWorkUsedAsDState to let Simulink and RTW know the DWork vector is being used to store discrete states. Also, the DWork name set is the same used in the TLC-file on line 37. This tells RTW to look for this particular DWork vector address when generating code.
148: Get the DWork vector for use in the Outputs function.
172: Get the DWork vector for use in the Update function. Note ssGetDWork is used instead of ssGetDiscStates
In dwork_example_wrapper.c, in the Update function, there is a simple double delay implemented. The Ouputs function outputs the original signal and the delayed signals.
You will have to run:
mex dwork_example.c dwork_example_wrapper.c
to create the S-function .dll file.
In the TLC-file, the basic necessities are the function arguments matching the declarations in the code. Also, the %assign statements must match the arguments being passed.
Also, in the InitializeConditions TLC-block, as mentioned above, line 37 must assign the name of the DWork vector to the roll variable.
  1 Comment
Sriram Narayanan
Sriram Narayanan on 19 Jun 2015
Hi Ahmad,
Please ensure that all the associated files for this solution are in the same directory or on the MATLAB path.
Please verify your compiler to make sure you are using a supported C/C++ compiler.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R14SP2

Community Treasure Hunt

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

Start Hunting!