Simscape/Simulink persistent variable in algebraic loop

23 ビュー (過去 30 日間)
Matthew Mishrikey
Matthew Mishrikey 2021 年 12 月 11 日
回答済み: Alan Lian 2023 年 4 月 10 日
I have a simple Simscape electrical model containg a circuit that trips if a current is exceeded. I want the trip to happen a certain time after the current excess, so I use Matlab function block and the Simulink Clock Block to track time.
The red highlighted portion is flagged as modifying a persistent variable in an algebraic loop.
This page says to avoid such loops:
But I am not sure how to acheive my goal without the MATLAB function block and the Simulink Clock.
The code in my function block is here:
% Monitors current. If over current, trip with a timed delay.
function switch_control = current_monitor(current, clockin)
persistent starttime;
persistent tripped;
if isempty(starttime)
starttime = 0;
end
if isempty(tripped)
tripped = false;
end
if (current > 10)
starttime = clockin;
if ((clockin - starttime) > 0.2) % fixed delay of 200ms
tripped = true;
end
end
if(tripped)
switch_control = 0;
else
switch_control = 1;
end
end
Any insights on what I am doing wrong?

採用された回答

Matthew Mishrikey
Matthew Mishrikey 2021 年 12 月 13 日
I think I managed to figure out a solution. As suggested elsewhere a delay block can "break" the algebraic loop.
I needed three persistent variables to make it work, not sure if there is a simpler way.
The timing was off so I changed the solver and also changed it to fixed step with time step siginificantly smaller than the delay I was looking for. Then it worked very well.

その他の回答 (1 件)

Alan Lian
Alan Lian 2023 年 4 月 10 日
I meet the same problem today, and I try a new way different from you. Put a switch after switch control, and use a step to turn on this signal channel in 1e-5 second. This solution works well in my condiction.

カテゴリ

Help Center および File ExchangeElectrical Sensors についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by