Matlab Function Block in a Simulink Model

5 visualizaciones (últimos 30 días)
HASSAN ABDELGABIR
HASSAN ABDELGABIR el 29 de Nov. de 2023
Comentada: HASSAN ABDELGABIR el 30 de Nov. de 2023
I have a Matlab Function Block within a Simulink model that I am using to control some of the switches I have in the model. The function takes real-time voltage measurments from the model, and outputs switching signals.
When I run the model, it seems that it enters an infinite loop(it says "Running" without showing any progress in the execution). And by "Ctrl + C", I get it to forward one step and it is stuck again unless I keep clicking "Ctrl + C". I am not sure what it is behaving this way.
Here is the part of the code that gets stuck:
function [GR, GS, GT] = fcn(DcVolt, VA, VB, VC, VR, VS, VT, limit)
%INITIALIZE OUTPUTS
GR = 0;
GS = 0;
GT = 0;
% INITIALIZE THE PERSISTENT VARIABLE TO STORE THE PREVIOUS VALUE OF VDC
persistent prev_VDC;
if isempty(prev_VDC)
prev_VDC = 0;
end
prev_VDC = DcVolt;
function [ARM,DISARM] = ARM_SWITCH (VAN,VDC,V_PREV, V_NEXT)
if (VDC >= VAN) || (V_PREV >= V_NEXT)
DISARM = 1; ARM = 0;
else
DISARM = 0; ARM = 1;
end
end
function [DISABLE] = DISABLE_SWITCH (VDC, VDCPREV)
if (VDC > (VDCPREV+15))
DISABLE = 1;
else
DISABLE = 0;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while DcVolt < limit
[ARM_R, DISARM_R] = ARM_SWITCH (VA, DcVolt, VC, VB);
[ARM_S, DISARM_S] = ARM_SWITCH (VB, DcVolt, VA, VC);
[ARM_T, DISARM_T] = ARM_SWITCH (VC, DcVolt, VB, VA);
if (ARM_R==1)
GR = 1;
else
GR = 0;
end
if (ARM_S==1)
GS = 1;
else
GS = 0;
end
if (ARM_T==1)
GT = 1;
else
GT = 0;
end
[DISABLE_R] = DISABLE_SWITCH (DcVolt, prev_VDC);
if (DISABLE_R == 1)
GR = 0;
end
[DISABLE_S] = DISABLE_SWITCH (DcVolt, prev_VDC);
if (DISABLE_S == 1)
GS = 0;
end
[DISABLE_T] = DISABLE_SWITCH (DcVolt, prev_VDC);
if (DISABLE_T == 1)
GT = 0;
end
end
end

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 29 de Nov. de 2023
Editada: Fangjun Jiang el 29 de Nov. de 2023
You can't expect to achieve your goal by doing this.
The sequence of the MATLAB Function block can be simplified as this, read the inputs, execute the code, generate the outputs.
The value of "DcVolt" and "limit" won't change until next simulation step, so you have an infinite loop.
The value of "DcVolt" might change during this time, but it is not read in.
Change from "while" to "if" might help you achieve your goal if everything else (like simulation step) have the right set up.
But overall, you can't expect the Simulink model or simulaiton to deal with "real-time voltage measurments". You didn't mention any other element like Simulink Desktop Real-Time
  3 comentarios
Fangjun Jiang
Fangjun Jiang el 30 de Nov. de 2023
Yeah. I launched a campaign/survey to request allowing functions inside a m script. Now, local functions can be defined in a script or function.
HASSAN ABDELGABIR
HASSAN ABDELGABIR el 30 de Nov. de 2023
Yes I missed that. Thank you :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre General Applications en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by