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일

0 개 추천

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

Paul
Paul 2023년 11월 29일
I'm actually shocked that the two helper functions can be defined inside the body of the main function. Did not know that nested functions are supported.
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.
Yes I missed that. Thank you :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Naming Conventions에 대해 자세히 알아보기

제품

릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by