How to use Simulink to output to real time microcontroller - matlab

I want to design a system that has to read some values from sensors through Arduino microcontroller using Matlab.
I want to use the simulink inputs as the sensors and the output to the motors,
I wrote an app to do that on Arduino, and also I wrote some functions in Matlab to read these sensors and to control the output, but I want to use simulink to implement a Fuzzy logic Controller the input is the values of sensors and the output is the speed.

If you wrote the functions for MATLAB in C, you can put your code in an S-Function, whether it is for sensor input or actuator output.
Otherwise, either use Embedded M file or S-function in your Simulink model.

Related

How to see voltage wave in simulink/simscape?

I'm analyzing the bahaviour of a RLC circuit and I need to observe the voltage and current wave of the circuit but I cannot find a way to do this. I need something like an oscilloscope but the scope does not work with the elements i am working with. What can I do?
The link below is my circuit, I'm using Simscape/Foundation Library elements
This is my circuit
Simscape and Simulink has different domains. To be able to use each one in the other domain, you need to use Simulink-PS Converter or PS-Simulink converter.
In that example, you have voltage sensor in Simscape and Scope or display in Simulink. To be able to connect them use PS-Simulink Converter.

send data with serial send block in Simulink to Arduiino

I'm trying to send and receive data through a serial port using simulink matlab and Arduino. when to receive data from Arduino to simulink matlab no problem!!
but for sent data to Arduino, I'm faced with this error.
and simulation simulink picture is:
The notation double (c) on the output y from your MATLAB Function block indicates that the signal is numerically complex, see Display Signal Attributes in the documentation for more details. This is the source of your problem, as mentioned in the error message (which is pretty self-explanatory by the way).
To fix it, you need to specify the data type of your outputs to be real in the Ports and Data Manager.
Alternatively, you can add a Complex to Real-Imag to the output(s) of your MATLAB Function block and take only the real or imaginary part of the signal, depending on what you want your algorithm to do.

Run a continuous Simulink model with real-time input?

I'm new to Matlab/Simulink but have a requirement to interface with a Simulink model from a piece of software.
At the moment I have created a simple Simulink function 'Inc' which has 1 input to an addition block with a constant 1 and an output. I have used To/From Workspace blocks and can run the simulation over a fixed time using a time/value vector input, such as simin = [0,0;5,0;5,1;10,1].
What I would like to do is run my model continuously? Reading Workspace values in real-time rather than a pre-defined time based vector?
I can't see how to set this up? Can Matlab/Simulink do this?
To summarise, I would like simin and simout to be single values i.e. simin = 1, then with the model running continuously(infinitely) at the next fixed step simout would update to simout = 2. simin changes would be made at the Workspace at varying intervals.
The short answer is you can't (easily) do that, that's not how Simulink works. The MATLAB workspace is read/accessed at the beginning of the simulation and passed to the Simulink engine, and even if the values in the workspace change before the simulation is finished, this is not taken into account by the Simulink model running.
There are ways to work around this, but if you are a novice to MATLAB & Simulink, be aware that these are fairly advanced techniques, and I would advise to familiarise yourself with Simulink first.
Have a look at these similar questions for suggestions of how to do what you want:
Stream data form MATLAB to Simulink
Problem of variable updating in workspace
How Do I Change a Block Parameter Based on the Output of Another Block?
Tuning block parameters at every time step in a simulation
Simulink Signal Viewing using Event Listeners and a MATLAB UI
Obviously, you need to change the simulation end time to Inf or some large number.

connecting RC circuit to s-function block in Simulink

Can I connect an RC circuit from the Simscape library (as a feedback) to an s-function block and apply a time series as input? I am trying to use the Linearize tool from Control Design to obtain the frequency response of the whole system.
Yes, you can. You need to use the PS-Simulink Converter block to convert the physical signals from your electrical sensor blocks to Simulink signals, and the Simulink-PS Converter block to convert the Simulink signals from your S-function into physical signals to feed into electrical source blocks. Beware of creating algebraic loops though.

ALSA Audio Capture block on Simulink

I need to read and work into a matlab code block with the data coming from the "Raspberry Pi ALSA Audio Capture block" inside Simulink frame. I'm using Matlab r2013a and matlab r2013b, with raspberry Pi Simulink libraries.
That's because i need to develope a special audio analysis.
So my question is: How can I read the stream of data coming from the "Raspberry Pi Audio Capture" block, and modify them before to "plot" or "record" the modified data into a file?
And how to plot such a kind of data in a streaming plot?
And how to record such a kind of data into a file?
Thank you!
Do you have DSP System toolbox? Start by looking at the audio Example in that toolbox. There are many of them. For plotting you need scope blocks. You can find them under "Sinks" in Simulink library. For writing you need "To Multimedia File" block which is also in DSP System toolbox.