How to call M file to Simulink model? - matlab

I got a script from this link and it runs correctly in MATLAB. However, when I create a Simulink model, it runs normally, but I can get the data from m file.
MATLAB script:
function data = Loadcell()
eml.extrinsic('arduino','addon','read_HX711')
a = arduino('COM5','Mega2560','libraries','ExampleAddon/HX711');
while 1
LoadCell = addon(a, 'ExampleAddon/HX711',{'D2','D3'});
data = read_HX711(LoadCell)
end
end
Simulink function
function data = Loadcell()
coder.extrinsic('Loadcell');
Loadcell = zeros('double');
data = zeros('double');
data = Loadcell
end
I run Simulink in external mode using Arduino and I don't get the data from that code. How can I use that MATLAB script to use it in the Simulink block model and get the data?

It's most likely a couple of issues
naming your Embedded MATLAB function in Simulink the same as the MATLAB function. (How would you expect Simulink to know which function it's supposed to call? The MATLAB function, or re-entering the Embedded MATLAB function?)
defining Loadcell as an extrinsic function, then (re)defining it to be a double 0.0, then trying to execute it as a function.
I suspect you want something like,
function data = Loadcell_Wrapper()
coder.extrinsic('Loadcell');
data = zeros(1,1,'double');
data = Loadcell()
end
(Also, not part of your question, but isn't there an infinite loop in the MATLAB code?)

Related

Run Simulink changing input varaibles from Matlab function

I am running Simulink from a Matlab function, as I need to start and stop the simulation multiple times changing parameters. When I run Simulink from the main script, there are no problems, Simulink can read the workspace. However, as soon as I make the script as a function so that I can run it for different input data.
function [Out] = Funtion (Var1, Var2 ....)
simout = sim('Simulinkblock');
Yx = simout.yout{1}.Values.Data;
Cx = Yx(end);
end
Simulink reads from the base workspace by default, you either need to change this using the following simset options in your function:
options = simset('SrcWorkspace','current');
sim('modelname',[],options)
Or assign the variables to the base workspace from your function using assignin before calling sim

How can I call a m file script into simulink block which uses simout file?

I have a simulink file and I'm working on designing a controller.
The file contains several electronics converters connected in parallel and each converter has a controller. The converter measures voltage and current and pass them to the controller to control.
The simulink file generates the simout file after simulation and that simout file is being used to calculate the impedance of the electrical grid in a m script file as a transfer function. I need to use the transfer function of impedance from the workspace in the place of " Req +sLeq" as in the purple marked position of the picture.
In other words, in my controller design, I need to call that m-file transfer function into a block and use the transfer function (impedance) as active damping during the simulation.
I'm not sure how can I call the transfer function from the m file (generated from simout) back to the simulink block during simulation.
The simulation need to be done in one go as closed loop system.
Please help me for this.
I can think of two ways to solve this. First, you can try to use a Matlab function block:
https://www.mathworks.com/help/simulink/slref/matlabfunction.html
Otherwise, you can always split your routines. Split your simulations, obtain your transfer function -which now exists in the workspace- then run your control system simulation using a separate file.
Dummy main-file
simOut = sim('electricalSimulation'); % Your first simulation
output = simOut.output;
input = simOut.input;
electricalTf = obtainTF(output, input); % Your identification routine
sim('completeSystem');

How to initialize constants for a Simulink model from a MATLAB script?

I am building a Simulink model with Matlab Function blocks. These function blocks have a lot of constants, for example g=9.8. I want to initialize all of these constants in one go in a Matlab script, so that I don't have to do so in each function block.
Here's what I have tried until now:
In the Matlab Function block I have initialized the variables using a Constant block, which is given as a input to the function block. This system works, but there are a lot of constant blocks in the model and it's getting very clustered.
I have also tried declaring these variables as global variables in the Matlab script. This does not work.
Another way that I have tried is so to create a function script for these constants and then load this function script in the Matlab Function block. This does not work.
Is there a way that I can just initialize these values from the Matlab script and the Simulink model reads it from the Matlab script, without me having to use these constant blocks?
%refercode
%matlabscript
Initialization values;
sim('filenmae.slx');
post processing;
%simulink model
constant blocks(initialization values) -> matlab function block -> output;
%end
What's the best way to solve this problem?
You could have a single struct containing your variables, which can be selectively used in your Matlab Function blocks. This means you can just have a single Constant block and additional function input, intialized from your script.
This MathWorks article shows how you can convert the struct into a Simulink Bus for use in your model (you can't use the struct in a constant block directly):
https://blogs.mathworks.com/simulink/2011/12/05/initializing-buses-using-a-matlab-structure/
Giving you something like this:
% initialise constants within struct, keeps the workspace tidy too!
vars = struct();
vars.g = 9.8;
vars.lambda = 2;
% Create bus data for the variables struct
varsInfo = Simulink.Bus.createObject(vars);
% Sim the model
sim( 'myModel.slx' );
With your constant block configured for the bus as described in the linked article:
Then you can access this within your function
function y = ( y, vars )
% MATLAB Function block function within myModel.slx
y = vars.lambda + u * vars.g;

External matlab optimization function (value) needs be called in Simulink

I have a simulink file, where I need to call a certain optimization function:
function u = MPC(r, x0, Thorizon, Q, W, cons)
R = [];
for s=1:Thorizon+1
R = vertcat(R, r);
end
ops = sdpsettings('solver','quadprog','verbose',0);
U = sdpvar(Thorizon+1,1);
cost = (R - (Q*U+W*x0))'*(R-(Q*U+W*x0))+U'*U;
optimize(cons,cost,ops);
u = value(U);
u = u(1);
end
However, I'm not sure how to implement this in Simulink.
I tried it with a matlab function block, but a couple of problems arise.
First of all, it returns some errors like
Function 'eval' is not supported for code generation. Consider adding
coder.extrinsic('eval') at the top of the function to bypass code
generation.
or like
Function 'sdpsettings.m' (#103.12741.12757), line 355, column 12:
"temporaryOptions" Launch diagnostic report.
It has (probably) to do with the fact that the optimizer (sdp) is an external function, one that is part of a seperately installed library.
The other possible issue, is one with the input arguments.
If I were to use a Matlab function block, there would be a problem with the cons argument, one that can take inequality forms, like
U<1
The function, does however, work in the Matlab environment itself.
But the output of this function, needs to be the input of a continuous non-linearm model present in simulink.
So my question is,
are there any suggestions, to either: Make such function in Simulink work, like via a Matlab function block, or use the function in the matlab runtime environment itself, and send the output value repeatedly to the simulink model?

How to call out sim in matlab function block

I am trying to run a PID controller (another model in simulink) using sim command in a matlab function block.
However it takes long iteration, seems to be that the matlab function block need more time.
How to get faster result?
Here is my code in matlab function block:
function [Kpp,Kii,Kdd] = fcn(u)
coder.extrinsic('sim');
sim('ExternePID'); %call out to ExternePID model
Don't do that, that won't work. Instead include your second Simulink model as a referenced model in your top-level Simulink model. This way, both models can run concurrently.