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
MathWorks Support Team 2009년 6월 27일
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에 대해 자세히 알아보기

제품


릴리스

R14SP2

Community Treasure Hunt

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

Start Hunting!

Translated by