Closed Transfer Switch Function

I am trying to programme a simple function to mimic the response of a Closed Automatic Transfer Switch. The reason I’m doing this in a function is because I can’t find appropriate Simulink blocks in order to imitate the functionality.
Basically, I have two inputs, u1 & u2 and two outputs, y1 & y2. If the input u1 is at a value higher than 20, I want the input u2 to flow out of y2. As well as this, I want u1 to still flow out of y1 for 100ms simultaneously, before shutting and becoming 0. This mimics the cross over time in a closed transfer switch where both power supplies are connected in parallel for 100ms or less. Again, I want a similar feature to occur when the input u1 becomes less than 20, but the other way round.
The errors are coming from my “while” function, where I am trying to get the output to stay at a value for 100ms before switching to 0 as I can get the simple switching between two outputs depending on the input function fine.
I've attached the model as well as the function code below. Any help or guidance in anyway is much appreciated.
Ali.
function [y1, y2] = OTS(u1, u2)
coder.extrinsic('clock')
if abs(u1) > 20
y2=u2;
t0=clock;
while etime(clock, t0) < 0.1
y1=u1;
end
y1=0;
else
y1=u1;
t0=clock;
while etime(clock, t0) < 0.1
y2=u2;
end
y2=0;
end

Answers (1)

Hi Alasdair,
This error originates because the following command returns a mxArray instead of double. You should pass it's output to a double variable before using it.
etime(clock,t0)
To resolve the error, you should modify the while loop as shown below:
while trigger
tElapsed = 0; % Initialize tElapsed to take double values
tElapsed = etime(clock, t0); %Pass a mxArray to cast it to double first
if tElapsed < 0.1
trigger = true;
y2 = u2;
else
trigger = false;
end
end
That said, I don't believe this MATLAB Function block will behave as you intended it to be. Essentially the while loop will terminate after 100 ms (not simulation time) and then the output y1 or y2 will be assigned to zero depending on value abs(u1). See the screenshot below:
Perhaps, consider passing time from Simulink to the MATLAB Function block and check it's value in a conditional statement, rather than using a while loop.
Hope this helps!
Prashant

7 Comments

Hi Prashant!
Thanks for this. I really do appreciate your help. Following on from your comment, I have now tried to pass in time from my simulink model using the "clock" block and implement that in my model. However, whilst my model is running, it isn't behaving like I am expecting it to.
My code at the moment is the following;
function [y1, y2] = CTS(u1, u2, t)
stime=0;
while abs(u1)==20
stime = t;
end
if abs(u1) > 20
if (t-stime) < 0.1
y1=u1;
y2=u2;
else
y1=0;
y2=u2;
end
else
if (t-stime) < 0.1
y1=u1;
y2=u2;
else
y2=0;
y1=u1;
end
end
where 't' is the input from the clock block in Simulink. What I think is the problem is that I am not able to assign 't' to anything in my code since it is a variable generated from Simulink? I want to update the value of 'stime' to the current 't' value every time the input voltage passes the switching voltage, 20 in this case, however this doesn't seem to be happening.
Again, sorry to ask another marginally different question, but I think I am much closer now and this is probably a more relevant question than the last one.
Thank you so much for your time, Alasdair.
Hi Alasdair,
You should have persistent variables to store information about 1. State of the signal u1 (up or below threshold) and 2. time at which it crossed the threshold. I think the following code should work for setting stime:
persistent aboveThreshold stime
if isempty(aboveThreshold)
aboveThreshold = false;
stime = 0;
end
if abs(u1)>20
if ~aboveThreshold
aboveThreshold = ~aboveThreshold;
stime = t;
end
else
if aboveThreshold
aboveThreshold = ~aboveThreshold;
stime = t;
end
end
Hi again Prashant,
I'm struggling to decipher what "aboveThreshold" is meant to be doing in your example? I'm getting confused with you using the variable "aboveThreshold" and "abs(u1)>20" as 20 is my Threshold in this case. Furthermore, when your setting "aboveThreshold = ~aboveThreshold", i'm not really sure what is happening here.
Again, thank you so much for your help.
Alasdair.
Never mind. I think I understand. All your doing is resetting "aboveThreshold" from true to false based on the value of the input. Then, every time it is altering from true and false it is resetting the value of 'stime' to the current value of 't'? Right?
That's correct. aboveThreshold is just a flag which switches value whenever the signal crosses the boundary. stime is the time at which this event happens.
Sorry again, just one more thing I've noticed. Why are you setting "aboveThreshold" from True to False in both cases? Which leads me on to question when will "aboveThreshold" be true?
Or is it just the way you've written it? So you're setting "aboveThreshold" from False to True by;
aboveThreshold = ~aboveThreshold;
if you understand my question? Does that make sense?
~aboveThreshold returns false when aboveThreshold is true and vice-versa. So it switches from false to True when signal reaches beyond threshold. and switches from True to false when signal is lower than threshold, because of the if statement conditions. It makes no difference if you change aboveThreshold = true and aboveThreshold = false in first and second assignment respectively.

Sign in to comment.

Categories

Find more on Create and Customize IBIS-AMI Models in Help Center and File Exchange

Asked:

on 17 Oct 2017

Commented:

on 20 Oct 2017

Community Treasure Hunt

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

Start Hunting!