How to generate code for Simulink model which contains an export-function submodel? - code-generation

I'm using matlab 2019b to build this Simulink model: A function call generator calls an export-fuction submodel.
An export-function model is a model with function call inputs.
I can generate code for the submodel using embedded coder, as well as run simulation of the top model. But generating code for the top model gives this error message:
Model 'main' contains a reference model 'main/Model' that is set up as a multi-instance export-function, but model 'main' does not follow export-function rules.
where 'main' is the top model name.
The export-function model document https://www.mathworks.com/help/simulink/ug/export-function-models.html writes:
For function-call Model blocks, Periodic sample time constraint for the referenced model set to Ensure sample time independent
Following this rule throws another errors. If the top model's periodic sample time constraint is changed, the error is:
This model will not inherit a sample time because the block 'main/Model' disallows it from doing so. If this model does not need to inherit a sample time, change the "Periodic sample time constraint" to something other than "Ensure sample time independent" on the Solver tab of the Configuration Parameters dialog.
And if the submodel's periodic sample time constraint is changed, the error is:
The current diagnostic settings for model 'instance' are not valid because the model contains root-level Inport block 'instance/f' that outputs a function-call signal. See errors reported below for details.
Caused by:
In the export-function model 'instance', the parameter 'Periodic sample time constraint' in 'Configuration Parameters' > 'Solver' > 'Tasking and sample time options' must be set to 'Unconstrained'.
Suggested Actions
Set the 'Periodic sample time constraint' to 'Unconstrained' in 'instance'.
How can I generate code for the top model?

Related

configure entry points in simulink model to make it callable

I've been handed a simulink model. I'd like to use simulink's code generation features to compile it into a binary and then run it on an embedded system (a beaglebone black). From there, I want to query it for output as a stand-alone component of a more complicated system.
I am having trouble finding where to specify the interface behavior to the compiled model.
I want to start the simulink model (with a python os.system() call, probably) with some default arguments (like starting parameter values for the model blocks), and then capture its output, which should be the values reported at each outport at each tick (the model has an internal clock and reports values at given intervals).
I have been able to blindly compile it into an *.elf binary and deploy it to the embedded device, and it runs, but the only thing that happens is i get the string **** model running **** on STDOUT.
What's the easiest way to specify command line arguments into this compiled binary?
If the answer is "generate the code and then edit it by hand to pass arguments in", how do I persist the hand edits between re-generations of the code when the simulink model changes?
How do i make simulink outports report on STDOUT ?

Simulink function in Stateflow error

I have multiple subsystems each of which have State machines "Stateflow". One subsystem has FSM, inside one state there is a simulink function that is supposed to start "simulink model representing an ODE" when in that state , a solution is observed until it reaches a threshold , the value of which shall trigger a transition of a state in a different subsystem
I used the Simulink function inside the stateflow as indicated in numerous tutorial, but the error I'm getting is the following:
'MatlabFunInChart/Chart/VelocityProfile/Cart/Integrator' has sample time '0'. Only constant (inf) or inherited (-1) sample times are allowed in 'function call' subsystem 'MatlabFunInChart/Chart/VelocityProfile'.
It looks like I'm missing something inside the simulink model that has to do with the integration.
Any ideas ?

Cannot change the dimensions of run-time parameter in Model block

I have a Simulink model with a Model block I am using to avoid duplication of some functionality.
When I try and run the parent model, I get the following errors:
Cannot change the dimensions of run-time parameter 'Gain' in
'TranslationChannel/First-Order Filter1/Model/Continuous/A' from
[1x1] to [0x0] while model is executing
Invalid setting in
'TranslationChannel/Second-Order Filter/Model/Continuous/A*x/A11' for
parameter 'Gain'
Error evaluating parameter 'Gain' in
'TranslationChannel/Second-Order Filter/Model/Continuous/A*x/A11'
Reference to non-existent field 'A11'.
Invalid setting in
'TranslationChannel/Tilt/Model/Continuous/A*x/A11' for parameter
'Gain'
Error evaluating parameter 'Gain' in
'TranslationChannel/Tilt/Model/Continuous/A*x/A11'
Reference to
non-existent field 'A11'.
The sub-model is below:
The block the error refers to is First Order Fliter 1, the parameters are which are:
How do I resolve this error, or, is there a better way of calling the same series of blocks multiple times in a model?
The parent model is below:
EDIT:
After my discussion with Ander, I tried connecting a step source directly to the model block to eliminated any possibility of a null signal and got the same error, suggesting the problem is due to calling the second model. If anyone can assist further, that would be great.
EDIT 2: I have confirmed that data is being passed into the model. Removing the filters from the sub-model makes it work fine. The error occurs in the masked portion of the filters.
I have resolved this my using a library instead of a model, and putting the filter blocks into a subsystem inside the library that I then drag to my main model.
This allows me to tune the parameters once and have it change all the blocks at once.

Set Filename of ToFile Block after Model has Finished

I have a series of ToFile blocks in my Simulink model that each have a unique filename (e.g. "Pulse.mat". I want to store the results of my simulation in timestamped folders based roughly on when I hit the run button / use the sim command.
My solution was to write two scripts, one called during the InitFcn callback and one for the StopFcn/CloseFcn callback (and PreSaveFcn callback).
The InitFcn callback would find all ToFile blocks and change the filename from "Pulse.mat" to something like "../runs//Pulse.mat", and the StopFcn/CloseFcn/PreSaveFcn to revert them to their original ".mat" names. This worked on my small test model, however when I attempted to integrate it into my actual model I receive the following error:
Error evaluating 'StopFcn' callback of block_diagram '<model_name>'.
Caused by:
Cannot change parameter 'Filename' of '<ToFile Block>' while simulation is running. The block was made virtual as it was optimized for simulation
From my research I discovered that the StopFcn actually executes before the simulation is "done" (for whatever reason), but why did it work in my initial test model? Do my ToFile blocks in my actual model have some property set that causes this error to occur?
The block has been virtualised in your larger model; which seems to be changing the execution point. One option might be to untick 'Block Reduction' in the 'Optimisation' pane of the model configuration parameters dialogue.
Alternatively, there might be another approach that you could try -
Setting the model properties 'StartFcn' to something like :
evalin('base','resultTimTag = datestr(clock, ''yyyymmdd_HHhMM'');')
and setting the model properties 'StopFcn' to :
targetDir = evalin('base','resultTimTag');
mkdir(targetDir);
outputs = dir('*.mat');
for i=1:length(outputs)
movefile(outputs(i).name,targetDir);
end
evalin('base','clear resultTimeTag');
It's possibly a little more brute-force than your approach, but seems to work quite nicely...

Is is possible to programmatically play a Simulink model and measure its states?

I am looking to set up a test set for an existing Simulink model. Ideally I could take full control of the model, explicitly stepping it and measuring the state of any signal on any bus in the model.
As might have been gleaned, this is the precursor of a unit testing system for the model. Being so, I can't really justify changing the model to suit the test, the test must accommodate the model as-is.
The furthest I've got so far is using load_model() to return a handle to the model. From there there seems to be a quite obscure set of functions for accessing the model. I can't see any that relate to accessing states and can't see any further commands that relate to accessing a loaded model.
The easiest way is to use the Data Import/Export function within the Simulink Preferences.
Set the checkbox States and it will store every state of your system for every time step in your workspace, also when you pause the simulation or execute it step by step.
Be aware not to set Save simulation output as single object, in this case the access would be more complicated and you need to follow the instructions here.
To add to the other answer, you probably want to check this page in the documentation: Control Simulation Using the set_param Command. Of interest are the following commands:
set_param(<model_name>, 'SimulationCommand', 'start')
set_param(<model_name>, 'SimulationCommand', 'pause')
set_param(<model_name>, 'SimulationCommand', 'WriteDataLogs')
set_param(<model_name>, 'SimulationCommand', 'continue')
Replace <model_name> by the path to your model file.