Update station 2/4 program

This commit is contained in:
Kirigaya Kazuto 2017-10-25 23:54:35 +08:00
parent 4a13e93cb3
commit de7f462a93

View File

@ -78,6 +78,7 @@ local function readdevice(Name)
if(d~=nil and s~=nil) then if(d~=nil and s~=nil) then
return d.getInput(s) return d.getInput(s)
else else
-- Critical error
error("failed to read device input") error("failed to read device input")
end end
end end
@ -91,7 +92,6 @@ local function train_delegator(Name,callback_func)
callback_func(from,to) callback_func(from,to)
end end
end) end)
print("Delegator: ",ret)
evl:push(ret) evl:push(ret)
end end
@ -99,10 +99,15 @@ local ab_available_1=true
local ab_available_2=true local ab_available_2=true
local ab_timerid_1,ab_time_1 local ab_timerid_1,ab_time_1
local ab_timerid_2,ab_time_2 local ab_timerid_2,ab_time_2
local ab_timerid_out,ab_time_out
local ebus=Queue.new() local ebus=Queue.new()
local function doInit() local function doInit()
evl:push(AddEventListener("interrupted",
function()
ebus:push("stop")
end))
train_delegator("ab_st", train_delegator("ab_st",
function(from,to) function(from,to)
if(from<to) then if(from<to) then
@ -142,15 +147,23 @@ local function TCSMain()
doInit() doInit()
print("TCS Started. Press Ctrl+C to stop.") print("TCS Started. Press Ctrl+C to stop.")
-- Main Processing Loop -- Main Processing Loop
while(true) do local running=true
while(running) do
local ev="no_event" local ev="no_event"
if(ebus:top()~=nil) then if(ebus:top()~=nil) then
ev=ebus:pop() -- Notice: Event is already poped. ev=ebus:pop() -- Notice: Event is already poped.
end end
-- For Debug: Print Event Name
if(ev~="no_event") then
print(ev)
end
if(ev=="no_event") then if(ev=="no_event") then
os.sleep(0.5) -- No event, delay for more info os.sleep(0.5) -- No event, delay for more info
elseif(ev=="ab_new_train") then elseif(ev=="stop") then
running=false
elseif(ev=="ab_new_train") then -- AB New Train
local act=false local act=false
if(readdevice("ab_sr")>0) then -- This train will coming into station if(readdevice("ab_sr")>0) then -- This train will coming into station
if(ab_available_1) then if(ab_available_1) then
@ -162,6 +175,8 @@ local function TCSMain()
ab_time_1=ab_time_1+1 ab_time_1=ab_time_1+1
end, end,
-1) -1)
disabledevice("ab_m")
disabledevice("ab_k1")
end end
else -- This train will pass by station else -- This train will pass by station
if(ab_available_2) then if(ab_available_2) then
@ -173,6 +188,8 @@ local function TCSMain()
ab_time_2=ab_time_2+1 ab_time_2=ab_time_2+1
end, end,
-1) -1)
enabledevice("ab_m")
disabledevice("ab_k2")
end end
end end
@ -183,6 +200,47 @@ local function TCSMain()
else -- Push Event back else -- Push Event back
ebus:push(ev) ebus:push(ev)
end 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 else -- Ignore unknown event
-- Do nothing -- Do nothing
end end