Exclude Simulink Function block from code generation - matlab

I want to exclude the Simulink Function block from code generation. This means the Simulink Function blocks are in place during simulation (to keep the DiagnosticMonitorCaller block from the AUTOSAR library happy) but are excluded when Matlab is compiling the model for code generation. The reason for this is that the functions are already there and not generated from Matlab/Simulink.
The work around is to 'comment out' during compiling and building but this is not really user and subversion friendly.
Can I use another block to 'fake' the function in place during simulation or do some settings on the Simulink Function block I didn't find?

This is achieved using the Environment Controller block.

Related

Feeding initial states to a FMU block (FMIKit) in MATLAB

I am using Dymola 2020x to develop a thermal model and export the FMU to Simulink to simulate controllers.
For implementing advanced controllers, I require an iteratively run framework, which helps to initialize the states after every iteration to the values at end of every previous simulation. This can be done within Dymola through “import initial” and “Continue” commands in the Simulation tab of Dymola.
However, since I am designing the controller in MATLAB, I require a similar feature for that platform. The only way I know right now is to manually change the initial conditions in the FMU block, but since I have a lot of states, it would not be feasible to do it manually. Any scripting ideas are welcome as well.
All in all, I require a framework/method to be able to initialize states of my model through MATLAB/Simulink to the values I get after running a single iteration.
Some help would be appreciated.
Expose the initial conditions of the variables as parameters, and set them from MATLA Scripts with e.g.
FMIKit.setStartValue(gcb, 'step', 'true')
see https://github.com/CATIA-Systems/FMIKit-Simulink/blob/master/docs/fmu_import.md

How do I input variables into a Simulink model through MATLAB script (SimDriveline)

For my coursework project in MATLAB, I have decided to build a drive-line model within Simulink, using the SimDriveline toolbox. The idea is to get the user to input values for the various parameters that are associated with each part of the model, such as the engine or the transmission. I would like to able to write this in a MATLAB script, but I'm not sure how to assign the values that are input to the Simulink model. For instance, the stock sdl_vehicle example that comes with SimDriveline. I am aware of the sim() command, but I am still confused on how to use it properly.
Also at the end of the simulation, the program is supposed to display the graphs that are collected in the scope window. I know that in the window itself that the scope can be printed to a figure, but is it possible to print that scope to a figure through MATLAB script?
This is the first time I have ever used a program like MATLAB. I would appreciate any help I could get, many thanks in advance!
There is a simulink block called simin:
http://de.mathworks.com/help/simulink/slref/fromworkspace.html?searchHighlight=simin
I used it some days back and it worked quite well. You can use the block in your model and define some signals/varibles as input.
After that you may write a Matlab-Script with an input function to set all the previous defined input values.

Level1 Matlab S-function - "Work Vectors"

I'm using a Matlab level 1 S-function several times in a model but don't want the mutual overwriting of global/persistent variables.
A solution could be work vectors but there is little documentation about level 1 S-function work vectors. Where do i get the SimStruct S for the ssSetNumRWork function?
Thanks!
The right thing to do is upgrade the code to be a level-2 S-Function, which shouldn't be difficult.
Level 1 m-code S-functions don't have work vectors (and all of the ssGet/Set functions are for c-code S-Functions anyway). A reason they aren't recommended any more is that they have limited functionality -- there's no nice way of getting around that without using Level 2 functionality.
You can use persistent variables (within each subfunction of a level-1 S-Function). But the point of global variables to enable them to be seen everywhere, so trying to use them without wanting everyone to see them seems pointless. (Note: you should never use global variables anyway.)
Having said that, if you really want to use Level-1 functionality, then within each S-Function you can use the getappdata and setappdata functions to act like work vector storage. But if you're going to go to that trouble, upgrading to a level-2 S-function will be easier anyway.

clear all from Simulink S-function

I am trying to call clear all from a simulink Level 1 S-function. I've take a look over doc mexCallMATLAB but I do not know how to use it. I've wanted to clear the work space after my function is called.
Are you sure that you're writing a level-1 S-Function? If you are, then you shouldn't. These days both m-code and c-code S-Functions should all be written as level-2.
You haven't specifically indicated whether you are writing in m-code or c-code, but since you've mentioned mexCallMATLAB it's assumed that you are using c-code, in which case you should use
mexEvalString("clear('all');");
If you're using m-code, then just use
evalin('base','clear all');

Initialization of a Dymola FMU in Simulink

I have encountered an issue while trying to simulate a model using an fmu file in Simulink:
I am using the Modelon FMI toolbox for a Dymola/Simulink interface to import a Model-Exchange type fmu file (version 1.0) into the Simulink model. I have a Matlab m-file that programmatically simulates the model sequentially, saving the final internal states of the model for initialization in the next simulation. The first iteration of the simulation runs without errors, but on the second iteration, Matlab returns the following errors at the call for the simulation:
module = partial_run_sim2/BasicModels.System_model_final_Sim, log
level = ERROR: [][FMU status:Error] fmiInitialize: dsblock_ failed,
QiErr = 1
module = partial_run_sim2/BasicModels.System_model_final_Sim, log level =
ERROR: [][FMU status:Error] Possible errors (non-exhaustive): 1. The
license file was not found. Use the environment variable
"DYMOLA_RUNTIME_LICENSE" to specify your Dymola license file. 2. The
model references external data that is not present on the target
machine, at least not with the same location. Error reported by
S-function 'sfun_fmu_me_1_0' in 'partial_run_sim2/BasicModels.System_model_final_Sim': fmiInitialize
returned with an error in initialize_fmu_model_first_time. See the
Command Window for more information printed by the FMU model.
I know that my licenses for Dymola and the FMI toolbox are up-to-date.
The simulation call in the m-file is:
simOut_itr = sim(model, model_cs);
Where “model” is the Simulink .slx model file and “model_cs” is a Simulink Configuration Setting variable. I have set the Configuration such that the model loads initial states from the workspace, and I have saved the previous iterations final states to that workspace variable. This action occurs for the first iteration as well, though the initial states are the same as the default initial states in the fmu file. I suspect that the first iteration runs without issue because the initialization is the same as the default value, but the subsequent iteration has a different initialization value, which does not agree with the fmu block.
I am aware of the ability to simulate the fmu file in Matlab using the fmi functions, but these methods do not allow for as much transparency into the system states, which hinders the sequential simulation. I am open to any methods that allow this kind of simulation, however.
Any suggestions with regard to initializing the system states in fmu files in Matlab/Simulink are much appreciated. Thank you.
I think I have encountered this error before, but not with the FMI Toolbox.
I had issues initializing a Dymola generated FMU more than once in a for-loop very similar to what you described above with the output of one initialization as the input for the next.
Give some random (but reasonably good values) and initialize the model just once such that the initialization is not the same as the default value.
If this works, then in all likelihood, the problem might be in multiple-initializations. Perhaps, you can unload the model at the end of each initialization and then load it again at the beginning of the each loop?
If nothing works, perhaps u can use JModelica.org (open source platform from Modelon) to get the output of these initializations into a mat-file and then use it in MATLAB for subsequent work. I know this is not ideal, but might save you some time while you wait for some answers here.