# Closed Transfer Switch Function

4 views (last 30 days)
Alasdair Robertson on 17 Oct 2017
Commented: Prashant Arora on 20 Oct 2017
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

Prashant Arora on 19 Oct 2017
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
Prashant Arora on 20 Oct 2017
~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.

### Community Treasure Hunt

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

Start Hunting!