S-function not found by Simulink unless rebuild - matlab

I am currently using Matlab Simulink with a Raspberry Pi. I have been using the S-Function Builder block to generate an S-Function that I am using with the S-Function Block. However, I always need to keep a copy of the S-Function builder that I need to build at the first start, as Simulink otherwise tells me :
"Error in S-function 'read_accelerometer/S-Function': S-Function 'sfun_read_adxl345' does not exist"
I am keeping the .c and the wrapper.c file, as well as the mexw64 and the tlc files. But that does not matter - I still have to rebuild the S-Function using the Builder block every time I reload my model.
How to circumvent this issue?

Ok, this is kinda sad that I am answering my own question ...
I had a startup script that messed with the mexw64. So, always keep the mexw64 file, even if it is not needed. (recompilation on Raspberry Pi)
Also, specify the generated wrapper function as a module in the S-Fun block.

Related

How to control custom hardware using SIMULINK

I have a piece of hardware and a DLL that can be used to control it. This is essentially a motor controller that I have used from C/C++ and MATLAB. I havetried to create a SIMULINK model using the DLL and I can't seem to get it to spin the motor.
The Simulation Target is set to include the header file and DLL. The Model Properties-> callbacks include MATLAB functions to initialize the DLL and hardware. The model includes a C Caller block that should call the DLL function to set the motor speed.
When I run the simulation, I can see the initialization code being executed, the motor never starts spinning. At the end of the simulation, the cleanup function is executed.
I have two thoughts about this. First, the C Caller block doesn't support sending commands over the serial link. Second, the initialization and cleanup code are MATLAB scripts. Is it possible that the MATLAB initialization is not recognized by SDIMULINK? That is, do I have to call the DLLs init function from the model directly (I'm not sure how to do this, but a WAG would be to embed other C Callers, with the required functions).

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 ?

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.

Integrating the code generated from simulink in the project

I have a simulink model and I generate the code using simulink coder for Generic Real-Time target.
I then copy the generated files (*.c and *.h) into my project in MPLAB.
Here I have the main.c file and here I use MdlStart() for initializing the model one time and then in infinite loop I use MdlOutputs() and MdlUpdate() for running the model. Everything works fine.
Now actually I want to speed the execution of the model and therefore I want to split my model into sub-systems and execute the sub-system only when required (e.g. I want to execute some sub-systems everytime and some only every few ms instead of everytime). I could split the model into subsystems and also made every sub-system atomic so that I get the different function for every sub-system. Now these different sub-system's functions are called from MdlOutputs() and MdlUpdate(). So they execute everytime the model is executed.
Actually I want to use some timer in my MPLAB project and then execute few sub-systems lets say at every 100ms. I also do not want to modify the auto-generated *.c and *.h files.
So instead of executing these sub-system's functions everytime through MdlOutputs() and MdlUpdate(), Is it possible to call the sub-system's functions from my main.c file without touching the auto-generated *.c and *.h files?
I hope that I have explained it well.
waiting for your help.
You should try to model everything you are trying to do with subsystems inside Simulink. For example, you can use "Enabled subsystems" or "Triggered subsystems" to create subsystems which run only on some time steps. I am not sure what kind of timer you are referring to. If it is Simulink time then you would need to run different subsystems at different sample rates which would give you a multirate model. If your timer is just another external source, then try triggered or enabled subsystems. That is the best way to avoid modifying generated files.
Doc for these subsystems are at http://www.mathworks.com/help/simulink/conditional-subsystems-1.html.

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.