-------------
START OF SECURITY DEVICES LOGIC----------------
-- Works only if EPK is switch-on and if Emergency Brake not applied
if ( Call( "*:GetControlValue", "swt_epk", 0 ) == 1 and Call( "*:GetControlValue", "EPKAutoStop", 0 ) == 0 ) then
warnStartTime = Call( "*:GetControlValue", "warnStartTime", 0 );
epkStartTime = Call( "*:GetControlValue", "epkStartTime", 0 );
lastTestTime = Call( "*:GetControlValue", "lastTestTime", 0 );
warnStartTimeSL = Call( "*:GetControlValue", "warnStartTimeSL", 0 );
epkStartTimeSL = Call( "*:GetControlValue", "epkStartTimeSL", 0 );
lastTestTimeSL = Call( "*:GetControlValue", "lastTestTimeSL", 0 );
ALSNState = Call( "*:GetControlValue", "ALSN", 0 );
-- Frequency of tests of vigilance depends on the ALSN state
if ( ALSNState == 0 ) then -- ALSN light is White
testPeriod = 90;
elseif ( ALSNState == 1 or ALSNState == 2 ) then -- ALSN light is Red-Yellow or Yellow
testPeriod = 30;
elseif ( ALSNState == -1 ) then -- ALSN light is Red
testPeriod = 20;
elseif ( ALSNState == 3 ) then -- ALSN light is Green
testPeriod = 99999; -- if Green then no tests
else
testPeriod = 30;
end
-- If ALSN state changes down (to most warning state) then doing test now
if ( ALSNState < prevALSNState ) then
testPeriod = 0;
end
-- If relay RU14 is OFF or ALSN is OFF, then tests period calculated via time-delay relay (60-90 seconds)
if ( Call( "*:GetControlValue", "swt_ru14", 0 ) == 0 or Call( "*:GetControlValue", "swt_alsn", 0 ) == 1 ) then
testPeriod = 75;
end
-- If device L116U (Neurocom) is ON, and locomotive not moving, and brakes applied then no tests
if ( (Call( "*:GetControlValue", "swt_neurocom", 0 ) == 0) and SpeedometerKPH < 0.1 and (Call( "*:GetControlValue", "BrakeCylinderPressure1", 0 ) + Call( "*:GetControlValue", "BrakeCylinderPressure2", 0 ) > 0) ) then
testPeriod = 99999;
end
if ( IsExpert ) then -- Acts only in Expert mode
------------------------ Control of vigilance
if ( (Call( "*:GetControlValue", "lvrReverser", 0 ) ~= 0) and (simulationTime > lastTestTime + testPeriod) ) then
if ( warnStartTime == 0 ) then
warnStartTime = simulationTime;
end
Call( "*:SetControlValue", "PSS", 0, 1 );
end
-- EPK Whistle after 8 seconds of PSS (Provisional light signaling)
if ( (simulationTime > warnStartTime +

and (warnStartTime ~= 0) and (epkStartTime == 0) ) then
epkStartTime = simulationTime;
Call( "*:SetControlValue", "EPKWhistle", 0, 1 );
end
-- EPK auto Emergency Brake after 7 seconds of EPK Whistle
if ( (simulationTime > epkStartTime + 7) and (epkStartTime ~= 0) ) then
Call( "*:SetControlValue", "TrainBrakeControl", 0, 1 );
Call( "*:SetControlValue", "EPKAutoStop", 0, 1 );
-- If relay RU14 is ON, then cut off load from the generator when Emergency Brakes applied
if ( Call( "*:GetControlValue", "swt_ru14", 0 ) == 1 ) then
Call( "*:SetControlValue", "Reverser", 0, 0 );
end
end
------------------------ Control of self leaving
if ( (SpeedometerKPH > 5) and (SpeedometerKPH > prevSpeedometerKPH) and (stateRegulator == 0) and (simulationTime > lastTestTimeSL + 60) ) then
if ( warnStartTimeSL == 0 ) then
warnStartTimeSL = simulationTime;
end
Call( "*:SetControlValue", "SelfLeav", 0, 1 );
end
-- EPK Whistle after 8 seconds of Self Leaving light
if ( (simulationTime > warnStartTimeSL +

and (warnStartTimeSL ~= 0) and (epkStartTimeSL == 0) ) then
epkStartTimeSL = simulationTime;
Call( "*:SetControlValue", "EPKWhistle", 0, 1 );
end
-- EPK auto Emergency Brake after 7 seconds of EPK Whistle
if ( (simulationTime > epkStartTimeSL + 7) and (epkStartTimeSL ~= 0) ) then
Call( "*:SetControlValue", "TrainBrakeControl", 0, 1 );
Call( "*:SetControlValue", "EPKAutoStop", 0, 1 );
-- If relay RU14 is ON, then cut off load from the generator when Emergency Brakes applied
if ( Call( "*:GetControlValue", "swt_ru14", 0 ) == 1 ) then
Call( "*:SetControlValue", "Reverser", 0, 0 );
end
end
end -- if ( IsExpert ) then -- Acts only in Expert mode
------------------------ Updating state of global variables
Call( "*:SetControlValue", "warnStartTime", 0, warnStartTime );
Call( "*:SetControlValue", "epkStartTime", 0, epkStartTime );
Call( "*:SetControlValue", "warnStartTimeSL", 0, warnStartTimeSL );
Call( "*:SetControlValue", "epkStartTimeSL", 0, epkStartTimeSL );
prevALSNState = ALSNState;
end -- if ( Call( "*:GetControlValue", "swt_epk", 0 ) == 1 and Call( "*:GetControlValue", "EPKAutoStop", 0 ) == 0 )
-------------
END OF SECURITY DEVICES LOGIC------------------