Matlab Function Block in a Simulink Model

3 ビュー (過去 30 日間)
HASSAN ABDELGABIR
HASSAN ABDELGABIR 2023 年 11 月 29 日
コメント済み: HASSAN ABDELGABIR 2023 年 11 月 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.
HASSAN ABDELGABIR
HASSAN ABDELGABIR 2023 年 11 月 30 日
Yes I missed that. Thank you :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGeneral Applications についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by