필터 지우기
필터 지우기

S-function Restart Capability

조회 수: 9 (최근 30일)
Nicholas Olson
Nicholas Olson 2018년 4월 25일
답변: Mark McBroom 2018년 4월 30일
I'm working with an aerospace vehicle simulation tool based in Simulink. There is a guidance subsystem that is an S-function. The functionality we'd like to add is the ability to restart the simulation at various points in order to run a lower fidelity version of the simulation (to improve speed during phases with very little going on). This removes the ability to use simstate to save and restart the states of the simulation and s-function. The s-function depends on running from the start of the sim in order to initialize itself properly. Is there a way to have the sfunction initialize by feeding it saved inputs from another run? I.e. We do a full run and save the inputs to the sfunction. Then we reconfigure the sim to the lower fidelity model and start the sim. The s-function would then use those saved inputs to initialize itself. I don't know if that's possible, but I've heard it mentioned by others as a possibility. Is that possible with s-functions? Is there another method that could be used?

답변 (3개)

Jim Riggs
Jim Riggs 2018년 4월 25일
I do this sort of thing all the time. You can do anything in an S-function, all you have to do is code it.
The S-function can open and read an external file. The only thing I don't know how to do is pass the name and path of the input file to the S-function, so this needs to be hard-coded. You can pass in instruction/logic commands to tell the S-function whether to use the input file, or to select from multiple input files, etc. Simply code the logic of when to open/read the input file and use the file contents as you like.
  댓글 수: 2
Nicholas Olson
Nicholas Olson 2018년 4월 25일
I'm not terribly familiar with S-functions, but I know they are incredibly powerful.
I assume that most of this would be coded in the MDL_START section of code such that it runs once during initialization? I'm fairly confident I can get the input file read in, but I'm not certain how I can run the S-function during initialization to get the S-function to the desired time for running it with the simplified simulation.
Jim Riggs
Jim Riggs 2018년 4월 26일
편집: Jim Riggs 2018년 4월 26일
An S-function is a Simulink block that executes C code which you provide. You can write the C code directly in the S-function block, or refer to an external .c file. (I use the "S-function builder" block which has a "build" button which you execute. This is when Simulink compiles the C code).
Since you are writing the C code, it will do whatever you program. You write a function to read the data from a file, then write a function to select the portion of the data that you need for initialization, then you write the rest of the function. You program all of the logic yourself, so it can do whatever you want. The Simulink S-function block allows you to define the input and output parameters, which you connect to external blocks in your model.
You can also write code in Matlab within a Simulink S-function or "MATLAB function" block.

댓글을 달려면 로그인하십시오.


Mark McBroom
Mark McBroom 2018년 4월 28일
편집: Mark McBroom 2018년 4월 28일
Simulink S-Functions can particpate in simstate. This link provides details and an example. I would recommend this approach rather than trying write/read to a file because it leverages the simstate capability already in Simulink. Note how the s-function allocates memory in mdlStart() to hold state information for your guidance code. A "PWork" holds the address for this allocated data. The comments at the top of the example code describes the 2 new functions, mdlGetSimState() and mdlSetSimstate(). These functions are called after mdlStart and give you control over the initial value of the variables used in your guidance code. The tricky part is that you have to convert the state data pointed to by PWork to MATLAB mex data structures. Simulink uses these 2 functions as when simstate is saved/restored.
  댓글 수: 1
Nicholas Olson
Nicholas Olson 2018년 4월 30일
One thing I forgot to mention in my original post is that we quite often would like to initialize the sim using data from other simulation tools (c-based tools). I'm not sure if that makes a difference, but I thought it worth mentioning.
Is there a way to see what states the S-function has? The guidance c-code was not written by me and it is very complex. I'm not sure how many states are contained within it.
The reason we were looking at feeding the pre-recorded inputs (I believe there are 33 inputs) was that it was the most straight forward approach in terms of handling multiple simulation tools. Does the simstate initialization feature of S-functions work when not using a simstate to initialize the rest of the sim? If we were to initialize off of another tool's final state, we would not be using a previously saved simstate, but the guidance function would still need to use that custom simstate.

댓글을 달려면 로그인하십시오.


Mark McBroom
Mark McBroom 2018년 4월 30일
to determine states in the guidance code you'll need to inspect the code to look for any global or static data. These variables will hold their value between simulation steps and would need to be saved/restored with simstate or save/restored to a file as described by Jim Riggs. Does your guidance code have an initialization function called from the mdlStart() or mdlInitializeSizes() function? If so, this function might do the initialization of all global/static variables for you and therefore help you identify which variables you need to save/restore.

Community Treasure Hunt

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

Start Hunting!

Translated by