Matlab Function Block in a Simulink Model

4 次查看(过去 30 天)
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

采纳的回答

Fangjun Jiang
Fangjun Jiang 2023-11-29
编辑:Fangjun Jiang 2023-11-29
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 个评论
Fangjun Jiang
Fangjun Jiang 2023-11-30
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 General Applications 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by