필터 지우기
필터 지우기

I want this code to run only once. Battery Charge/Discharge

조회 수: 7 (최근 30일)
Alexandre Correia
Alexandre Correia 2019년 4월 15일
편집: Khalid Massoud 2022년 11월 10일
Hi guys, newbie here on Simulink, this is my first project.
I'm trying to make a battery charge and discharge simulation, I'm using a project i saw in a youtube amateur tutorial, but the example itself is flawed and I'm trying to do my changes. I have this code here:
What it does is when the SOC is > 80% the system is feeding the Load, when it reaches below 40% it turns of the Load and starts charging the batteries.
function [LoadOn, ChargingOn] = fcn(SOC)
LoadOn = 1;
ChargingOn=0;
if (SOC>80)
LoadOn=1;
ChargingOn=0;
end
if (SOC<40)
LoadOn=0;
ChargingOn=1;
end
The problem that I'm having is that on the iteration that it reaches >40% it correctly changes the variables to Load=0 and Charge=1, but on the next iteration, due to the second and third line of code, it changes back to LoadOn=1 and Charge=0, when I wanted it only to happen at SOC>80%.
And If I remove the 2nd and 3rd line it gives me an error. Basically what I want to do is that the 2nd and 3rd line only run once.
Any suggestions?

채택된 답변

Ben Cunningham
Ben Cunningham 2019년 4월 15일
편집: Ben Cunningham 2019년 4월 16일
One way of doing this would be using Persistent variables - but just as with global variables you will need to be careful - to get back to your starting condition you should use 'clear fcn'.
Additionally the persistent variable should not be the output of the function.
e.g.
function [LoadOn, ChargingOn] = fcn(SOC)
persistent LoadOnPersistent;
persistent ChargingOnPersistent;
if isempty(LoadOnPersistent) % Initial condition
LoadOnPersistent = 1;
ChargingOnPersistent = 0;
end
if (SOC>80)
LoadOnPersistent=1;
ChargingOnPersistent=0;
end
if (SOC<40)
LoadOnPersistent=0;
ChargingOnPersistent=1;
end
LoadOn = LoadOnPersistent;
ChargingOn = ChargingOnPersistent;
end
  댓글 수: 6
SALAH alatai
SALAH alatai 2022년 7월 21일
Can you please share the simulation work for understanding purpose how and where to use this functio?
Khalid Massoud
Khalid Massoud 2022년 11월 10일
편집: Khalid Massoud 2022년 11월 10일
It gives me an error in the line no.10

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

추가 답변 (0개)

커뮤니티

더 많은 답변 보기:  Power Electronics Community

카테고리

Help CenterFile Exchange에서 Sources에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by