Return different values from a function after certain interval

This is a function in a simulink model that I am working on. It's output goes in a switch case to which different circuits are connected.
Context: v1,v2,v3,v4 are voltages of 4 battery cells. In step1 the function finds the index of cell having max voltage. I want the circuit associated with it to execute completely. Similarly, after this step 2 should run followed by step 3. I need the simulation to run for some time after every step- so I've used a random for loop with random no of iterations(here 50) just to elapse some time.
Problem: I checked the signal after running, it is directly reaching the end of function and returning min voltage battery index. (in short it is not stopping for some time after every step)
P.S.: 1. I'm using discrete timestep 2. the behavior happens in a loop i.e.The circuit is such that the voltages change as the 3 steps are executed hence it is important to calculate max/,min in every iteration.
function y = fcn(v1, v2, v3, v4)
ar=[v1,v2,v3,v4];
%step1
[~,I]=max(ar);
y=I;
n=0;
for i=1:50
n=n+1;
end
%step2
y = 1;
n=0;
for i=1:50
n=n+1;
end
%step3
[~,I1]=min(ar);
y=I1;
n=0;
for i=1:50
n=n+1;
end

6 个评论

Are you using a discrete timestep model, or are you using continuous?
Is this behaviour to occur in a loop, that after step3 you go back to the beginning, finding index of the max, taking the three steps, go back, etc. ? Or is the behaviour of these three steps to be triggered? If it is to be triggered then how do you want to handle the possibility of re-trigger while the steps are proceeding?
Hey Walter
  1. I'm using discrete timestep
  2. yes, the behavior happens in a loop. The circuit is such that the voltages change as the 3 steps are executed, hence it is important to calculate new max/min voltage in every iteration.
I have no idea about re-triggering.
When you say important to calculate those "every iteration", do you mean every timestep, or every time the series of 3 steps has completed ?
every time the series of 3 steps has completed. I have attached the file with the original question, have a look at it. It's for active cell balancing.
You need to reconfigure the situation entirely. Do not use a MATLAB function block for any of it (except perhaps finding the index of the maximum.) Everything else should be a matter of using latching and switch blocks and clock inputs and arithmetic blocks.
I have made changes to signal and the function execution that you recommended. The switching problem is solved. With the switch I used a 'repeating sequence stair' and changed it's parameters accordingly (check out logic analyzer, I've logged the required signals)
Now, the only problem remains is the gate signal given to mosfet to switch it on/off. I'm using 'controlled voltage source' in the model, but I just found it is inappropriate as it maintains the voltage across it's terminals. So if a mosfet is on, then it will remain in on position as voltage source will simply maintain it like that. Other options that I tried were incompatible with the simscape mosfet.
Can you suggest any alternative for this?

请先登录,再进行评论。

回答(1 个)

Hello Pranav,
if you set the controlled voltage source to 0 V, that will turn off the mosefet. You can also use the gate driver block as part of simscape electrical to drive your devices. If the issue is that your pulse is lasting too long, that is an issue with the controller, not the physical model.
Regards,
Joel

类别

帮助中心File Exchange 中查找有关 Simscape Electrical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by