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

1 次查看(过去 30 天)
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.

采纳的回答

MathWorks Support Team
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 个评论
Sriram Narayanan
Sriram Narayanan 2015-6-19
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Coder 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by