Easiest way to compile a Simulink model into executable? - matlab

I need to turn a complex simulink model that runs in realtime, receive & send data using sharedmemory and socket every 100ms, contains exernal C++ callback functions(for sharedmemory and socket), into an executable. There is no need for UI like graphs or buttons(maybe print some log but nothing more).
I'm quite new to simulink, what is the easiest way to compile such a simulink model into an executable? Would be great if there is also a super detailed guide.
Also a side question - is using C function block(and converting structs to buses) the preferred way to use sharedmemory and socket in simulink? Any better substitution?
Any help would be appreciated. Thanks in advance!

Related

How to implement a modified transfer function?

my field is not that much related to, but I need to build a model-based simulation in simulink. The model has a transfer modified function as follows:
r(s)/q(s)=t/(t*s+1)
I know the basics, however, to implement this, I got into question, whether I have to use a Gain block with value of t before and after a transfer function like this 1/(s+1), or it should be implemented in another fashion? As t is not a constant. Thanks.
Firstly note that t/(t*s + 1) is equivalent to 1/(s+(1/t)). In both cases t > 0 or the system is unstable or ill-defined.
If t was constant then you could use a Transfer Function block, which is the equivalent of the first of the following implementations. Since t is not constant, you cannot use a Transfer Function block, but you can use the second implementation shown below.

open Logic analyzer for simulink model from matlab commandline

Example: I need to select particular signals from simulink model, log them, and open them in logic analyzer.
All the three should be executed from matlab commandline. Can someone help me with that?
Simulink has an extensive API. For marking signals for logging, I think you need to mark them for streaming, as is explained on this page, since the Logic Analyzer uses the same signal selection mechanism as the SDI. The Logic Analyzer has an API as well.

Read simulink signal data into matlab during simulation

I want to continuously read simulink signal data into the command line while the simulation is running. get_param() seems to be blocking so that doesn't quite work when put in an infinite while loop.
I'm now trying to use a UDP send block but I can't seem to receive data. My UDP block sends data to localhost over remote port 25000 and local port 25001.
In matlab I use the following code but it simply times out with no data
u=udp('127.0.0.1', 25001,'LocalPort',25000);
fopen(u)
fread(u)
fclose(u)
delete(u)
What are my options to continuously read out simulink signal data into Matlab CLI?
Control Simulation Using the "set_param()" command like follows:
set_param('sys','SimulationCommand','WriteDataLogs')
For a working example, type "sldemo_varsize_basic" in the matlab command window. Then above command becomes
set_param('sldemo_varsize_basic','SimulationCommand','WriteDataLogs')
If you set the simulation time to sufficiently large and start the simulation, the "simout, simout1", "tout" and "xout" variables are created/updated in the workspace every time you issue the command above.
Unfortunately, I was not able to find a good quality documentation of this feature.
Are you trying to store the value of your model outports DURING simulation? This is not possible because the variables 'simout, simout1", "tout","xout" etc are created only once simulation is OVER.
In order to read/store the value of outports during simulation, you will have to attach an 'Runtime Object' to the outports.
Refer 'Access Block Data During Simulation' in the Simulink documentation or see this link: http://in.mathworks.com/help/simulink/ug/accessing-block-data-during-simulation.html?s_tid=gn_loc_drop
Hope it helps :)
This question has already been answered here using RunTime Objects as I have described above:
https://stackoverflow.com/a/17006419/6580313

Is it possible to include a Simulink Coder executable into a feedback loop outside of Matlab?

I'm using Simulink to build a subsystem, which will then be built up using Simulink Coder to get an executable. I want to include this executable into my main function that is not necessarily written using Matlab. The main function is to implement a non-real time, desktop deployed feedback loop, i.e., (1) read out the output of the subsystem, (2) calculate a new input based on the reading, (3) send the new input to the subsystem.
I've managed to build-up a desktop deployed executable of the subsystem using RSim target. But in the main function (for test purpose, I am using Matlab to write the main function), the executable is one-off executed, where I can't read its output or assign new input during its running.
Thanks & Regards.
Yes
If you want to do a closed loop simulation, you most certainly need access to the simulink solver internal step() function. This can be achieved when you build a dll file and then include it via the header files etc. in your other simulation engine. But to be able to do this you need an embedded coder license. use the ert_shrlib.tlc target.

How to export simulink data to workspace during simulation?

I want to retrieve the data from simulink during simulation, and use serial network function to send these data to another program. Because I need to use another program to do some tricks and send command back to simulink, so I have to get data from simulink during runtime so that another program can make the right command.
I've tried using To Workspace block to export the data.
However, I can only got value in the very beginning of the simulation.
And I've also tried using scope and change some properties: check Save Data To Workspace and Uncheck Limite data to Last.
First, I started simulation, and I found the ScopeData didn't appear in the Workspace. Only when I stop simulation, ScopeData would appear in workspace.
And after that, I can use ScopeData.signals.values to get values.
But what I want is: when I start simulation, ScopeData would appear in workspace so that I can send these data to other program.
Does anyone know how to achieve this?
I found this page might be helpful, but I still don't know how to continuously export data during simulation.
Use get_param to read data from just at the current time. Also to send the data back to Simulink with set_param of a gain or another block.
An example of get_param
First load and start the simulation:
load_system('myModel')
set_param('myModel','SimulationCommand','Start');
To read data on any line of your simulink model:
Get a simulink block object (let's try a Clock with the name Clock):
block = 'myModel/Clock';
rto = get_param(block, 'RuntimeObject');
Then get the data on its first (or any) output port (or input) of that block.
time = rto.OutputPort(1).Data;
You could do the reading, in a timer callback.
Also this might be helpful: Command Line Functionality for Simulink
During simulation Simulink stores logged data in an internal buffer and only writes the data to the Workspace when the simulation is paused or stopped.
It sounds as if you really need to write an S-function (which will get signal values on a timestep-by-timestep basis) and communicate with Proteus that way.
Of course Simulink is a non-realtime simulator, so if you are talking about doing anything resembling real-time control then you are most likely taking the wrong approach altogether.
At any time during simulation you can force Simulink to write the simulation output data to the workspace:
set_param(bdroot,'SimulationCommand','WriteDataLogs');
I've found that this command is quite unstable in my Matlab 2010a for Win64. In particular I have to avoid it when simulation is stopped (i.e. first check
get_param(bdroot,'SimulationStatus') ), otherwise Matlab shows an error and asks to be restarted.