From de7f462a9366fab2aec4d54afb60c0f6a0987007 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Wed, 25 Oct 2017 23:54:35 +0800 Subject: [PATCH] Update station 2/4 program --- station_2_4.lua | 64 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/station_2_4.lua b/station_2_4.lua index 7641940..23a23dc 100644 --- a/station_2_4.lua +++ b/station_2_4.lua @@ -78,6 +78,7 @@ local function readdevice(Name) if(d~=nil and s~=nil) then return d.getInput(s) else + -- Critical error error("failed to read device input") end end @@ -91,7 +92,6 @@ local function train_delegator(Name,callback_func) callback_func(from,to) end end) - print("Delegator: ",ret) evl:push(ret) end @@ -99,10 +99,15 @@ local ab_available_1=true local ab_available_2=true local ab_timerid_1,ab_time_1 local ab_timerid_2,ab_time_2 +local ab_timerid_out,ab_time_out local ebus=Queue.new() local function doInit() + evl:push(AddEventListener("interrupted", + function() + ebus:push("stop") + end)) train_delegator("ab_st", function(from,to) if(from0) then -- This train will coming into station if(ab_available_1) then @@ -162,6 +175,8 @@ local function TCSMain() ab_time_1=ab_time_1+1 end, -1) + disabledevice("ab_m") + disabledevice("ab_k1") end else -- This train will pass by station if(ab_available_2) then @@ -173,6 +188,8 @@ local function TCSMain() ab_time_2=ab_time_2+1 end, -1) + enabledevice("ab_m") + disabledevice("ab_k2") end end @@ -183,6 +200,47 @@ local function TCSMain() else -- Push Event back ebus:push(ev) end + elseif(ev=="ab_ins1_ready") then + if(ab_time_out==0 and ab_time_1>6 and ab_time_1>ab_time_2 and readdevice("ab_lout")>0) then + RemoveTimer(ab_timerid_1) + ab_time_1=0 + ab_available_1=true + + ab_time_out=1 + ab_timerid_out=AddTimer(1, + function() + ab_time_out=ab_time_out+1 + if(ab_time_out==5) then + ebus:push("ab_time_out_needstop") + end + end) + + enabledevice("ab_k1") + else + ebus:push(ev) + end + elseif(ev=="ab_time_out_needstop") then + RemoveTimer(ab_timerid_out) + ab_time_out=0 + elseif(ev=="ab_ins2_ready") then + if(ab_time_out==0 and ab_time_2>ab_time_1 and readdevice("ab_lout")>0) then + RemoveTimer(ab_timerid_2) + ab_time_2=0 + ab_available_2=true + + ab_time_out=1 + ab_timerid_out=AddTimer(1, + function() + ab_time_out=ab_time_out+1 + if(ab_time_out==5) then + ebus:push("ab_time_out_needstop") + end + end) + + enabledevice("ab_k2") + else + ebus:push(ev) + end else -- Ignore unknown event -- Do nothing end