mirror of
https://github.com/Kiritow/OpenComputerScripts.git
synced 2024-03-22 13:10:46 +08:00
Update Station 2/6 program
This commit is contained in:
parent
3dd272f08f
commit
ddcf324a01
602
station_2_6.lua
602
station_2_6.lua
|
@ -1,7 +1,6 @@
|
||||||
--[[
|
--[[
|
||||||
Station 2/6 Standard Schedule Program
|
Station 2/6 Standard Schedule Program
|
||||||
]]
|
]]
|
||||||
|
|
||||||
require("libevent")
|
require("libevent")
|
||||||
require("util")
|
require("util")
|
||||||
require("checkarg")
|
require("checkarg")
|
||||||
|
@ -19,52 +18,41 @@ local redout3 = proxy("redstone", "90")
|
||||||
local redout4 = proxy("redstone", "53")
|
local redout4 = proxy("redstone", "53")
|
||||||
|
|
||||||
-- Redirect Table
|
-- Redirect Table
|
||||||
local redirect_tb=
|
local redirect_tb = {
|
||||||
{
|
|
||||||
["ab_st"] = {redin1, sides.north},
|
["ab_st"] = {redin1, sides.north},
|
||||||
["ab_sr"] = {redin1, sides.east},
|
["ab_sr"] = {redin1, sides.east},
|
||||||
["ab_sv"] = {redin1, sides.south},
|
["ab_sv"] = {redin1, sides.south},
|
||||||
["ab_lout"] = {redin1, sides.west},
|
["ab_lout"] = {redin1, sides.west},
|
||||||
|
|
||||||
["ba_st"] = {redin2, sides.north},
|
["ba_st"] = {redin2, sides.north},
|
||||||
["ba_sr"] = {redin2, sides.east},
|
["ba_sr"] = {redin2, sides.east},
|
||||||
["ba_sv"] = {redin2, sides.south},
|
["ba_sv"] = {redin2, sides.south},
|
||||||
["ba_lout"] = {redin2, sides.west},
|
["ba_lout"] = {redin2, sides.west},
|
||||||
|
|
||||||
-- ins: Inside station sensor
|
-- ins: Inside station sensor
|
||||||
["s1"] = {redin3, sides.north},
|
["s1"] = {redin3, sides.north},
|
||||||
["s2"] = {redin3, sides.east},
|
["s2"] = {redin3, sides.east},
|
||||||
["s3"] = {redin3, sides.south},
|
["s3"] = {redin3, sides.south},
|
||||||
["s4"] = {redin3, sides.west},
|
["s4"] = {redin3, sides.west},
|
||||||
|
|
||||||
["s5"] = {redin4, sides.north},
|
["s5"] = {redin4, sides.north},
|
||||||
["s6"] = {redin4, sides.east},
|
["s6"] = {redin4, sides.east},
|
||||||
["st_l"] = {redin4, sides.south},
|
["st_l"] = {redin4, sides.south},
|
||||||
|
|
||||||
|
|
||||||
-- Output
|
-- Output
|
||||||
["m1"] = {redout1, sides.north},
|
["m1"] = {redout1, sides.north},
|
||||||
["m2"] = {redout1, sides.east},
|
["m2"] = {redout1, sides.east},
|
||||||
["m3"] = {redout1, sides.south},
|
["m3"] = {redout1, sides.south},
|
||||||
["m4"] = {redout1, sides.west},
|
["m4"] = {redout1, sides.west},
|
||||||
|
|
||||||
["m5"] = {redout2, sides.north},
|
["m5"] = {redout2, sides.north},
|
||||||
["m6"] = {redout2, sides.east},
|
["m6"] = {redout2, sides.east},
|
||||||
["k1"] = {redout2, sides.south},
|
["k1"] = {redout2, sides.south},
|
||||||
["k2"] = {redout2, sides.west},
|
["k2"] = {redout2, sides.west},
|
||||||
|
|
||||||
["k3"] = {redout3, sides.north},
|
["k3"] = {redout3, sides.north},
|
||||||
["k4"] = {redout3, sides.east},
|
["k4"] = {redout3, sides.east},
|
||||||
["k5"] = {redout3, sides.south},
|
["k5"] = {redout3, sides.south},
|
||||||
["k6"] = {redout3, sides.west},
|
["k6"] = {redout3, sides.west},
|
||||||
|
|
||||||
["ab_ko"] = {redout4, sides.north},
|
["ab_ko"] = {redout4, sides.north},
|
||||||
["ba_ko"] = {redout4, sides.east},
|
["ba_ko"] = {redout4, sides.east},
|
||||||
|
|
||||||
["last_unused"] = {"unused", sides.north}
|
["last_unused"] = {"unused", sides.north}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
local function getNameFromRaw(Device, Side)
|
local function getNameFromRaw(Device, Side)
|
||||||
for k, t in pairs(redirect_tb) do
|
for k, t in pairs(redirect_tb) do
|
||||||
if (t[1].address == Device and t[2] == Side) then
|
if (t[1].address == Device and t[2] == Side) then
|
||||||
|
@ -103,26 +91,37 @@ local function readdevice(Name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function trigger(Name)
|
||||||
|
enabledevice(Name)
|
||||||
|
os.sleep(0.25)
|
||||||
|
disabledevice(Name)
|
||||||
|
end
|
||||||
|
|
||||||
local evl = Queue.new()
|
local evl = Queue.new()
|
||||||
|
|
||||||
local function train_delegator(Name, callback_func)
|
local function train_delegator(Name, callback_func)
|
||||||
evl:push(AddEventListener("redstone_changed",
|
evl:push(
|
||||||
|
AddEventListener(
|
||||||
|
"redstone_changed",
|
||||||
function(ev, dev, sd, from, to)
|
function(ev, dev, sd, from, to)
|
||||||
if (getNameFromRaw(dev, sd) == Name) then
|
if (getNameFromRaw(dev, sd) == Name) then
|
||||||
callback_func(from, to)
|
callback_func(from, to)
|
||||||
end
|
end
|
||||||
end))
|
end
|
||||||
|
)
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Data for 6 ways.
|
-- Data for 6 ways.
|
||||||
local isfree = {true, true, true, true, true, true}
|
local isfree = {true, true, true, true, true, true}
|
||||||
local timerid={0,0,0,0,0,0}
|
local timerid = {-1, -1, -1, -1, -1, -1}
|
||||||
local timecnt = {0, 0, 0, 0, 0, 0}
|
local timecnt = {0, 0, 0, 0, 0, 0}
|
||||||
local revflag = {false, false, false, false, false, false}
|
local revflag = {false, false, false, false, false, false}
|
||||||
local ab_timerid_out, ab_time_out = 0, 0
|
local ab_timerid_out, ab_time_out = 0, 0
|
||||||
local ba_timerid_out, ba_time_out = 0, 0
|
local ba_timerid_out, ba_time_out = 0, 0
|
||||||
local _side_line_free = true
|
local _side_line_free = true
|
||||||
local _side_line_hw = true
|
local _side_line_hw = true
|
||||||
|
local _auto_unlock_side_line_hw = false
|
||||||
|
|
||||||
local function isSideLineFree()
|
local function isSideLineFree()
|
||||||
return _side_line_free and _side_line_hw
|
return _side_line_free and _side_line_hw
|
||||||
|
@ -133,48 +132,127 @@ local function lockSideLine()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function unlockSideLine()
|
local function unlockSideLine()
|
||||||
|
if (not _auto_unlock_side_line_hw) then -- Manual
|
||||||
_side_line_free = true
|
_side_line_free = true
|
||||||
|
end -- else it will be unlock automatically
|
||||||
|
end
|
||||||
|
|
||||||
|
local function setSideLineAutoUnlock(flag)
|
||||||
|
checkbool(flag)
|
||||||
|
_auto_unlock_side_line_hw = flag
|
||||||
|
if (flag) then
|
||||||
|
print("SideLine AutoUnlock is now enabled")
|
||||||
|
else
|
||||||
|
print("SideLine AutoUnlock is not disabled")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local bus = Queue.new()
|
local bus = Queue.new()
|
||||||
|
|
||||||
|
local function addNormalStationTimer(n) -- Add Normal Timer
|
||||||
|
checknumber(n)
|
||||||
|
if (timerid[n] ~= -1) then
|
||||||
|
print("Error: add another timer to station ", n)
|
||||||
|
bus:push("halt")
|
||||||
|
end
|
||||||
|
timecnt[n] = 1
|
||||||
|
timerid[n] =
|
||||||
|
AddTimer(
|
||||||
|
1,
|
||||||
|
function()
|
||||||
|
timecnt[n] = timecnt[n] + 1
|
||||||
|
if (timecnt[n] == 5) then
|
||||||
|
bus:push("s" .. n .. "_timeout")
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
-1
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function addSimpleStationTimer(n) -- Add Simple Timer
|
||||||
|
checknumber(n)
|
||||||
|
if (timerid[n] ~= -1) then
|
||||||
|
print("Error: add another timer to station ", n)
|
||||||
|
bus:push("halt")
|
||||||
|
end
|
||||||
|
timecnt[n] = 1
|
||||||
|
timerid[n] =
|
||||||
|
AddTimer(
|
||||||
|
1,
|
||||||
|
function()
|
||||||
|
timecnt[n] = timecnt[n] + 1
|
||||||
|
end,
|
||||||
|
-1
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function stopTimer(n) -- Stop Timer
|
||||||
|
checknumber(n)
|
||||||
|
if (timerid[n] == -1) then
|
||||||
|
print("Error: stopping a non-exist timer")
|
||||||
|
bus:push("halt")
|
||||||
|
end
|
||||||
|
RemoveTimer(timerid[n])
|
||||||
|
timerid[n] = -1
|
||||||
|
timecnt[n] = 0
|
||||||
|
end
|
||||||
|
|
||||||
local function doInit()
|
local function doInit()
|
||||||
evl:push(AddEventListener("interrupted",
|
setSideLineAutoUnlock(true)
|
||||||
|
|
||||||
|
evl:push(
|
||||||
|
AddEventListener(
|
||||||
|
"interrupted",
|
||||||
function()
|
function()
|
||||||
bus:push("stop")
|
bus:push("stop")
|
||||||
end))
|
end
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
train_delegator("ab_st",
|
train_delegator(
|
||||||
|
"ab_st",
|
||||||
function(from, to)
|
function(from, to)
|
||||||
if (from < to) then
|
if (from < to) then
|
||||||
bus:push("ab_new_train")
|
bus:push("ab_new_train")
|
||||||
end
|
end
|
||||||
end)
|
end
|
||||||
|
)
|
||||||
|
|
||||||
train_delegator("ba_st",
|
train_delegator(
|
||||||
|
"ba_st",
|
||||||
function(from, to)
|
function(from, to)
|
||||||
if (from < to) then
|
if (from < to) then
|
||||||
bus:push("ba_new_train")
|
bus:push("ba_new_train")
|
||||||
end
|
end
|
||||||
end)
|
end
|
||||||
|
)
|
||||||
|
|
||||||
train_delegator("st_l",
|
train_delegator(
|
||||||
|
"st_l",
|
||||||
function(from, to)
|
function(from, to)
|
||||||
if(from<to) then
|
if (from < to) then -- Red to Green.
|
||||||
_side_line_hw = true
|
_side_line_hw = true
|
||||||
else
|
if (_auto_unlock_side_line_hw) then
|
||||||
|
_side_line_free = true
|
||||||
|
end
|
||||||
|
else -- Green to Red.
|
||||||
_side_line_hw = false
|
_side_line_hw = false
|
||||||
end
|
end
|
||||||
end)
|
end
|
||||||
|
)
|
||||||
|
|
||||||
local smt = {"s1", "s2", "s3", "s4", "s5", "s6"}
|
local smt = {"s1", "s2", "s3", "s4", "s5", "s6"}
|
||||||
for k, v in pairs(smt) do
|
for k, v in pairs(smt) do
|
||||||
train_delegator(v,
|
train_delegator(
|
||||||
|
v,
|
||||||
function(from, to)
|
function(from, to)
|
||||||
if (from < to) then
|
if (from < to) then
|
||||||
bus:push(v .. "_ready")
|
bus:push(v .. "_ready")
|
||||||
|
else
|
||||||
|
bus:push(v .. "_empty")
|
||||||
end
|
end
|
||||||
end)
|
end
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -185,8 +263,7 @@ local function doCleanUp()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function doClearOutput()
|
local function doClearOutput()
|
||||||
local smt=
|
local smt = {"m1", "m2", "m3", "m4", "m5", "m6", "k1", "k2", "k3", "k4", "k5", "k6", "ab_ko", "ba_ko"}
|
||||||
{"m1","m2","m3","m4","m5","m6","k1","k2","k3","k4","k5","k6","ab_ko","ba_ko"}
|
|
||||||
|
|
||||||
for k, v in pairs(smt) do
|
for k, v in pairs(smt) do
|
||||||
disabledevice(v)
|
disabledevice(v)
|
||||||
|
@ -203,15 +280,76 @@ local function doCheck()
|
||||||
print("Check Pass.")
|
print("Check Pass.")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function startABTimer()
|
||||||
|
ab_time_out = 1
|
||||||
|
ab_timerid_out =
|
||||||
|
AddTimer(
|
||||||
|
1,
|
||||||
|
function()
|
||||||
|
ab_time_out = ab_time_out + 1
|
||||||
|
if (ab_time_out == 8) then
|
||||||
|
bus:push("ab_time_out_needstop")
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
-1
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
local function startBATimer()
|
local function startBATimer()
|
||||||
ba_time_out = 1
|
ba_time_out = 1
|
||||||
ba_timerid_out=AddTimer(1,
|
ba_timerid_out =
|
||||||
|
AddTimer(
|
||||||
|
1,
|
||||||
function()
|
function()
|
||||||
ba_time_out = ba_time_out + 1
|
ba_time_out = ba_time_out + 1
|
||||||
if (ba_time_out == 8) then
|
if (ba_time_out == 8) then
|
||||||
bus:push("ba_time_out_needstop")
|
bus:push("ba_time_out_needstop")
|
||||||
end
|
end
|
||||||
end,-1)
|
end,
|
||||||
|
-1
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function proc1236(n, ev)
|
||||||
|
checknumber(n)
|
||||||
|
local done = false
|
||||||
|
if (isSideLineFree()) then -- Side line must be free(or we can't move at all)
|
||||||
|
if (timecnt[n] >= timecnt[1] and timecnt[n] >= timecnt[2] and timecnt[n] >= timecnt[3] and timecnt[n] >= timecnt[6]) then
|
||||||
|
if (revflag[1]) then -- Way n need reverse
|
||||||
|
if (isfree[4]) then -- Way n --> Way 4
|
||||||
|
isfree[4] = false
|
||||||
|
revflag[4] = false
|
||||||
|
disabledevice("m3")
|
||||||
|
disabledevice("m4")
|
||||||
|
disabledevice("k4")
|
||||||
|
done = true
|
||||||
|
elseif (isfree[5]) then --- Way n --> Way5
|
||||||
|
isfree[5] = false
|
||||||
|
revflag[5] = false
|
||||||
|
disabledevice("m3")
|
||||||
|
enabledevice("m4")
|
||||||
|
disabledevice("k5")
|
||||||
|
done = true
|
||||||
|
end
|
||||||
|
else -- Way n does not need reverse
|
||||||
|
if (ab_time_out == 0 and readdevice("ab_lout") > 0) then -- Can move to next station (B)
|
||||||
|
startABTimer()
|
||||||
|
enabledevice("m3")
|
||||||
|
done = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if (done) then -- Clear way n
|
||||||
|
stopTimer(n)
|
||||||
|
isfree[n] = true
|
||||||
|
lockSideLine()
|
||||||
|
trigger("k" .. n)
|
||||||
|
unlockSideLine()
|
||||||
|
else
|
||||||
|
bus:push(ev)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function TCSMain()
|
local function TCSMain()
|
||||||
|
@ -240,6 +378,9 @@ local function TCSMain()
|
||||||
-- No event, no action.
|
-- No event, no action.
|
||||||
elseif (ev == "stop") then
|
elseif (ev == "stop") then
|
||||||
running = false
|
running = false
|
||||||
|
elseif (ev == "halt") then
|
||||||
|
print("Exception Occured! Something goes wrong!")
|
||||||
|
running = false
|
||||||
elseif (ev == "ab_new_train") then
|
elseif (ev == "ab_new_train") then
|
||||||
local act = false
|
local act = false
|
||||||
if (readdevice("ab_sr") > 0) then -- Train will stop
|
if (readdevice("ab_sr") > 0) then -- Train will stop
|
||||||
|
@ -307,14 +448,13 @@ local function TCSMain()
|
||||||
end
|
end
|
||||||
|
|
||||||
if (act) then
|
if (act) then
|
||||||
enabledevice("ab_ko")
|
trigger("ab_ko")
|
||||||
os.sleep(0.25)
|
|
||||||
disabledevice("ab_ko")
|
|
||||||
else
|
else
|
||||||
bus:push(ev)
|
bus:push(ev)
|
||||||
end
|
end
|
||||||
elseif (ev == "ba_new_train") then
|
elseif (ev == "ba_new_train") then
|
||||||
local act = false
|
local act = false
|
||||||
|
if (isSideLineFree()) then -- Side line free, can move in.
|
||||||
if (readdevice("ba_sr") > 0) then -- Train will stop
|
if (readdevice("ba_sr") > 0) then -- Train will stop
|
||||||
if (readdevice("ba_sv") > 0) then -- Train will reverse
|
if (readdevice("ba_sv") > 0) then -- Train will reverse
|
||||||
if (isfree[5]) then -- Train --> Way5 (will reverse)
|
if (isfree[5]) then -- Train --> Way5 (will reverse)
|
||||||
|
@ -337,7 +477,7 @@ local function TCSMain()
|
||||||
end
|
end
|
||||||
else -- Train will pass
|
else -- Train will pass
|
||||||
if (readdevice("ba_sv") > 0) then -- Train will reverse
|
if (readdevice("ba_sv") > 0) then -- Train will reverse
|
||||||
if(isfree[5]) then -- Train --> Way5 (will not reverse)
|
if (isfree[5]) then -- Train --> Way5 (will reverse)
|
||||||
isfree[5] = false
|
isfree[5] = false
|
||||||
revflag[5] = true
|
revflag[5] = true
|
||||||
|
|
||||||
|
@ -346,14 +486,14 @@ local function TCSMain()
|
||||||
act = true
|
act = true
|
||||||
end
|
end
|
||||||
else -- Train does not need reverse
|
else -- Train does not need reverse
|
||||||
if(isfree[4]) then
|
if (isfree[4]) then -- Train --> Way4 (not reverse)
|
||||||
isfree[4] = false
|
isfree[4] = false
|
||||||
revflag[4] = false
|
revflag[4] = false
|
||||||
|
|
||||||
disabledevice("m4")
|
disabledevice("m4")
|
||||||
disabledevice("k4")
|
disabledevice("k4")
|
||||||
act = true
|
act = true
|
||||||
elseif(isfree[5]) then
|
elseif (isfree[5]) then -- Train --> Way5 (not reverse)
|
||||||
isfree[5] = false
|
isfree[5] = false
|
||||||
revflag[5] = false
|
revflag[5] = false
|
||||||
|
|
||||||
|
@ -363,298 +503,37 @@ local function TCSMain()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if (act) then
|
if (act) then
|
||||||
enabledevice("ba_ko")
|
lockSideLine()
|
||||||
os.sleep(0.25)
|
trigger("ba_ko")
|
||||||
disabledevice("ba_ko")
|
unlockSideLine()
|
||||||
else
|
else
|
||||||
bus:push(ev)
|
bus:push(ev)
|
||||||
end
|
end
|
||||||
elseif (ev == "s1_ready") then -- Add in-station timer
|
elseif (ev == "s1_ready") then -- Add in-station timer
|
||||||
timecnt[1]=1
|
addNormalStationTimer(1) --> s1_timeout
|
||||||
timerid[1]=AddTimer(1,
|
|
||||||
function()
|
|
||||||
timecnt[1]=timecnt[1]+1
|
|
||||||
if(timecnt[1]==5) then
|
|
||||||
bus:push("s1_timeout")
|
|
||||||
end
|
|
||||||
end,-1)
|
|
||||||
elseif (ev == "s2_ready") then -- Add in-station timer
|
elseif (ev == "s2_ready") then -- Add in-station timer
|
||||||
timecnt[2]=1
|
addNormalStationTimer(2) --> s2_timeout
|
||||||
timerid[2]=AddTimer(1,
|
|
||||||
function()
|
|
||||||
timecnt[2]=timecnt[2]+1
|
|
||||||
if(timecnt[2]==5) then
|
|
||||||
bus:push("s2_timeout")
|
|
||||||
end
|
|
||||||
end,-1)
|
|
||||||
elseif (ev == "s3_ready") then -- Judge or turn on timer
|
elseif (ev == "s3_ready") then -- Judge or turn on timer
|
||||||
local needtimer=false
|
addSimpleStationTimer(3)
|
||||||
if(revflag[3]) then -- Need rev. Go line 4 or line 5
|
bus:push("s3_pending") --> s3_pending
|
||||||
if(isfree[4]) then -- Way 3 --> Way 4
|
|
||||||
if(isSideLineFree()) then
|
|
||||||
isfree[4]=false
|
|
||||||
revflag[4]=false
|
|
||||||
|
|
||||||
lockSideLine()
|
|
||||||
disabledevice("m3")
|
|
||||||
disabledevice("m4")
|
|
||||||
disabledevice("k4")
|
|
||||||
enabledevice("k3")
|
|
||||||
os.sleep(0.25)
|
|
||||||
disabledevice("k3")
|
|
||||||
os.sleep(3)
|
|
||||||
unlockSideLine()
|
|
||||||
else
|
|
||||||
needtimer=true
|
|
||||||
end
|
|
||||||
elseif(isfree[5]) then -- Way 3 --> Way 5
|
|
||||||
if(isSideLineFree()) then
|
|
||||||
isfree[5]=false
|
|
||||||
revflag[5]=false
|
|
||||||
lockSideLine()
|
|
||||||
disabledevice("m3")
|
|
||||||
enabledevice("m4")
|
|
||||||
disabledevice("k5")
|
|
||||||
enabledevice("k3")
|
|
||||||
os.sleep(0.25)
|
|
||||||
disabledevice("k3")
|
|
||||||
os.sleep(3)
|
|
||||||
unlockSideLine()
|
|
||||||
else
|
|
||||||
needtimer=true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else -- way 3 does not need rev
|
|
||||||
if(isSideLineFree()) then
|
|
||||||
lockSideLine()
|
|
||||||
enabledevice("m3")
|
|
||||||
enabledevice("k3")
|
|
||||||
os.sleep(0.25)
|
|
||||||
disabledevice("k3")
|
|
||||||
os.sleep(3)
|
|
||||||
unlockSideLine()
|
|
||||||
else
|
|
||||||
needtimer=true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if(needtimer) then -- We need a timer... (can't move, sad >_<)
|
|
||||||
timecnt[3]=1
|
|
||||||
timerid[3]=AddTimer(1,
|
|
||||||
function()
|
|
||||||
timecnt[3]=timecnt[3]+1
|
|
||||||
end,-1)
|
|
||||||
bus:push("s3_pending") -- Tell scheduler Way 3 is pending
|
|
||||||
else -- Yeah! Moved directly! Now we mark way 3 as free
|
|
||||||
isfree[3]=true
|
|
||||||
end
|
|
||||||
elseif (ev == "s4_ready") then
|
elseif (ev == "s4_ready") then
|
||||||
local act=false
|
addSimpleStationTimer(4)
|
||||||
if(ba_time_out==0 and readdevice("ba_lout")>0) then -- Can let go
|
bus:push("s4_pending") --> s4_pending
|
||||||
if(isfree[5] or revflag[5]==true) then -- Way5 is free or Way5 will reverse
|
|
||||||
act=true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if(act) then
|
|
||||||
-- Add BA Timer
|
|
||||||
startBATimer()
|
|
||||||
enabledevice("k4")
|
|
||||||
os.sleep(0.25)
|
|
||||||
disabledevice("k4")
|
|
||||||
isfree[4]=true
|
|
||||||
else
|
|
||||||
timecnt[4]=0
|
|
||||||
timerid[4]=AddTimer(1,
|
|
||||||
function()
|
|
||||||
timecnt[4]=timecnt[4]+1
|
|
||||||
end,-1)
|
|
||||||
bus:push("s4_pending") -- Tell scheduler Way 4 is pending
|
|
||||||
end
|
|
||||||
elseif (ev == "s5_ready") then -- Train must stay at way5 whether it wants to stop or not.
|
elseif (ev == "s5_ready") then -- Train must stay at way5 whether it wants to stop or not.
|
||||||
timecnt[5]=1
|
addNormalStationTimer(5) --> s5_timeout
|
||||||
timerid[5]=AddTimer(1,
|
|
||||||
function()
|
|
||||||
timecnt[5]=timecnt[5]+1
|
|
||||||
if(timecnt[5]==5) then
|
|
||||||
bus:push("s5_timeout")
|
|
||||||
end
|
|
||||||
end,-1)
|
|
||||||
elseif (ev == "s6_ready") then
|
elseif (ev == "s6_ready") then
|
||||||
timecnt[6]=1
|
addNormalStationTimer(5) --> s6_timeout
|
||||||
timerid[6]=AddTimer(1,
|
|
||||||
function()
|
|
||||||
timecnt[6]=timecnt[6]+1
|
|
||||||
if(timecnt[6]==5) then
|
|
||||||
bus:push("s6_timeout")
|
|
||||||
end
|
|
||||||
end,-1)
|
|
||||||
elseif (ev == "s1_timeout") then
|
elseif (ev == "s1_timeout") then
|
||||||
local done=false
|
proc1236(1, ev)
|
||||||
if(isSideLineFree()) then -- Side line must be free(or we can't move at all)
|
|
||||||
if(revflag[1]) then -- Way1 need reverse
|
|
||||||
if(isfree[4]) then -- Way 1 --> Way 4
|
|
||||||
done=true
|
|
||||||
lockSideLine()
|
|
||||||
isfree[4]=false
|
|
||||||
revflag[4]=false
|
|
||||||
disabledevice("m3")
|
|
||||||
disabledevice("m4")
|
|
||||||
disabledevice("k4")
|
|
||||||
|
|
||||||
enabledevice("k1")
|
|
||||||
os.sleep(3)
|
|
||||||
disabledevice("k1")
|
|
||||||
|
|
||||||
unlockSideLine()
|
|
||||||
elseif(isfree[5]) then --- Way1 --> Way5
|
|
||||||
done=true
|
|
||||||
lockSideLine()
|
|
||||||
isfree[5]=false
|
|
||||||
revflag[5]=false
|
|
||||||
disabledevice("m3")
|
|
||||||
enabledevice("m4")
|
|
||||||
disabledevice("k5")
|
|
||||||
|
|
||||||
enabledevice("k1")
|
|
||||||
os.sleep(3)
|
|
||||||
disabledevice("k1")
|
|
||||||
|
|
||||||
unlockSideLine()
|
|
||||||
end
|
|
||||||
else -- Way 1 does not need reverse
|
|
||||||
if(readdevice("ab_lout")>0) then -- Can move to next station (B)
|
|
||||||
done=true
|
|
||||||
lockSideLine()
|
|
||||||
enabledevice("m3")
|
|
||||||
|
|
||||||
enabledevice("k1")
|
|
||||||
os.sleep(3)
|
|
||||||
disabledevice("k1")
|
|
||||||
|
|
||||||
unlockSideLine()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if(done) then -- Clear way1
|
|
||||||
RemoveTimer(timerid[1])
|
|
||||||
timecnt[1]=0
|
|
||||||
isfree[1]=true
|
|
||||||
else
|
|
||||||
bus:push(ev)
|
|
||||||
end
|
|
||||||
elseif (ev == "s2_timeout") then
|
elseif (ev == "s2_timeout") then
|
||||||
local done=false
|
proc1236(2, ev)
|
||||||
if(isSideLineFree()) then -- Side line must be free(or we can't move at all)
|
elseif (ev == "s3_pending") then
|
||||||
if(revflag[2]) then -- Way2 need reverse
|
proc1236(3, ev)
|
||||||
if(isfree[4]) then -- Way 2 --> Way 4
|
|
||||||
done=true
|
|
||||||
lockSideLine()
|
|
||||||
isfree[4]=false
|
|
||||||
revflag[4]=false
|
|
||||||
disabledevice("m3")
|
|
||||||
disabledevice("m4")
|
|
||||||
disabledevice("k4")
|
|
||||||
|
|
||||||
enabledevice("k2")
|
|
||||||
os.sleep(3)
|
|
||||||
disabledevice("k2")
|
|
||||||
|
|
||||||
unlockSideLine()
|
|
||||||
elseif(isfree[5]) then --- Way2 --> Way5
|
|
||||||
done=true
|
|
||||||
lockSideLine()
|
|
||||||
isfree[5]=false
|
|
||||||
revflag[5]=false
|
|
||||||
disabledevice("m3")
|
|
||||||
enabledevice("m4")
|
|
||||||
disabledevice("k5")
|
|
||||||
|
|
||||||
enabledevice("k2")
|
|
||||||
os.sleep(3)
|
|
||||||
disabledevice("k2")
|
|
||||||
|
|
||||||
unlockSideLine()
|
|
||||||
end
|
|
||||||
else -- Way 2 does not need reverse
|
|
||||||
if(readdevice("ab_lout")>0) then -- Can move to next station (B)
|
|
||||||
done=true
|
|
||||||
lockSideLine()
|
|
||||||
enabledevice("m3")
|
|
||||||
|
|
||||||
enabledevice("k2")
|
|
||||||
os.sleep(3)
|
|
||||||
disabledevice("k2")
|
|
||||||
|
|
||||||
unlockSideLine()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if(done) then -- Clear way2
|
|
||||||
RemoveTimer(timerid[2])
|
|
||||||
timecnt[2]=0
|
|
||||||
isfree[2]=true
|
|
||||||
else
|
|
||||||
bus:push(ev)
|
|
||||||
end
|
|
||||||
elseif (ev == "s6_timeout") then
|
elseif (ev == "s6_timeout") then
|
||||||
local done=false
|
proc1236(6, ev)
|
||||||
if(isSideLineFree()) then -- Side line must be free(or we can't move at all)
|
|
||||||
if(revflag[6]) then -- Way6 need reverse
|
|
||||||
if(isfree[4]) then -- Way 6 --> Way 4
|
|
||||||
done=true
|
|
||||||
lockSideLine()
|
|
||||||
isfree[4]=false
|
|
||||||
revflag[4]=false
|
|
||||||
disabledevice("m3")
|
|
||||||
disabledevice("m4")
|
|
||||||
disabledevice("k4")
|
|
||||||
|
|
||||||
enabledevice("k6")
|
|
||||||
os.sleep(3)
|
|
||||||
disabledevice("k6")
|
|
||||||
|
|
||||||
unlockSideLine()
|
|
||||||
elseif(isfree[5]) then --- Way6 --> Way5
|
|
||||||
done=true
|
|
||||||
lockSideLine()
|
|
||||||
isfree[5]=false
|
|
||||||
revflag[5]=false
|
|
||||||
disabledevice("m3")
|
|
||||||
enabledevice("m4")
|
|
||||||
disabledevice("k5")
|
|
||||||
|
|
||||||
enabledevice("k6")
|
|
||||||
os.sleep(3)
|
|
||||||
disabledevice("k6")
|
|
||||||
|
|
||||||
unlockSideLine()
|
|
||||||
end
|
|
||||||
else -- Way 6 does not need reverse
|
|
||||||
if(readdevice("ab_lout")>0) then -- Can move to next station (B)
|
|
||||||
done=true
|
|
||||||
lockSideLine()
|
|
||||||
enabledevice("m3")
|
|
||||||
|
|
||||||
enabledevice("k6")
|
|
||||||
os.sleep(3)
|
|
||||||
disabledevice("k6")
|
|
||||||
|
|
||||||
unlockSideLine()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if(done) then -- Clear way6
|
|
||||||
RemoveTimer(timerid[6])
|
|
||||||
timecnt[6]=0
|
|
||||||
isfree[6]=true
|
|
||||||
else
|
|
||||||
bus:push(ev)
|
|
||||||
end
|
|
||||||
elseif (ev == "s5_timeout") then
|
elseif (ev == "s5_timeout") then
|
||||||
local done = false
|
local done = false
|
||||||
if (revflag[5]) then -- Need reverse
|
if (revflag[5]) then -- Need reverse
|
||||||
|
@ -663,107 +542,42 @@ local function TCSMain()
|
||||||
revflag[6] = false
|
revflag[6] = false
|
||||||
enabledevice("m5")
|
enabledevice("m5")
|
||||||
disabledevice("k6")
|
disabledevice("k6")
|
||||||
|
|
||||||
enabledevice("k5")
|
|
||||||
os.sleep(0.25)
|
|
||||||
disabledevice("k5")
|
|
||||||
|
|
||||||
done = true
|
done = true
|
||||||
end -- If way6 is not free, we cannot reverse to it. (bang!)
|
end -- If way6 is not free, we cannot reverse to it. (bang!)
|
||||||
else -- Does not need reverse
|
else -- Does not need reverse
|
||||||
if(ba_time_out==0 and readdevice("ba_lout")>0) then -- Can let go
|
if (ba_time_out == 0 and timecnt[5] >= timecnt[4] and readdevice("ba_lout") > 0) then -- Can let go
|
||||||
if(isfree[4] or timecnt[4]==0 or timecnt[5]>=timecnt[4]) then -- If Way4 is free, or way4 is not ready, or way5 wait longer
|
|
||||||
startBATimer()
|
|
||||||
disabledevice("m5")
|
disabledevice("m5")
|
||||||
enabledevice("k5")
|
|
||||||
os.sleep(0.25)
|
|
||||||
disabledevice("k5")
|
|
||||||
|
|
||||||
done = true
|
done = true
|
||||||
end -- We just cannot move. (We must wait!)
|
end -- Cannot move.
|
||||||
end -- Cannot move at all.
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if (done) then
|
if (done) then
|
||||||
RemoveTimer(timerid[5])
|
startBATimer()
|
||||||
timecnt[5]=0
|
stopTimer(5)
|
||||||
|
trigger("k5")
|
||||||
isfree[5] = true -- Mark way5 as free
|
isfree[5] = true -- Mark way5 as free
|
||||||
else
|
else
|
||||||
bus:push(ev)
|
bus:push(ev)
|
||||||
end
|
end
|
||||||
elseif(ev=="s3_pending") then
|
|
||||||
local done=false
|
|
||||||
if(revflag[3]) then -- Need rev. Go line 4 or line 5
|
|
||||||
if(isfree[4]) then -- Way 3 --> Way 4
|
|
||||||
if(isSideLineFree()) then
|
|
||||||
isfree[4]=false
|
|
||||||
revflag[4]=false
|
|
||||||
|
|
||||||
lockSideLine()
|
|
||||||
disabledevice("m3")
|
|
||||||
disabledevice("m4")
|
|
||||||
disabledevice("k4")
|
|
||||||
enabledevice("k3")
|
|
||||||
os.sleep(0.25)
|
|
||||||
disabledevice("k3")
|
|
||||||
os.sleep(3)
|
|
||||||
unlockSideLine()
|
|
||||||
done=true
|
|
||||||
end
|
|
||||||
elseif(isfree[5]) then -- Way 3 --> Way 5
|
|
||||||
if(isSideLineFree()) then
|
|
||||||
isfree[5]=false
|
|
||||||
revflag[5]=false
|
|
||||||
lockSideLine()
|
|
||||||
disabledevice("m3")
|
|
||||||
enabledevice("m4")
|
|
||||||
disabledevice("k5")
|
|
||||||
enabledevice("k3")
|
|
||||||
os.sleep(0.25)
|
|
||||||
disabledevice("k3")
|
|
||||||
os.sleep(3)
|
|
||||||
unlockSideLine()
|
|
||||||
done=true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else -- way 3 does not need rev
|
|
||||||
if(isSideLineFree()) then
|
|
||||||
lockSideLine()
|
|
||||||
enabledevice("m3")
|
|
||||||
enabledevice("k3")
|
|
||||||
os.sleep(0.25)
|
|
||||||
disabledevice("k3")
|
|
||||||
os.sleep(3)
|
|
||||||
unlockSideLine()
|
|
||||||
done=true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if(done) then -- mark way 3 as free
|
|
||||||
isfree[3]=true
|
|
||||||
RemoveTimer(timerid[3])
|
|
||||||
timecnt[3]=0
|
|
||||||
else
|
|
||||||
bus:push(ev)
|
|
||||||
end
|
|
||||||
elseif (ev == "s4_pending") then
|
elseif (ev == "s4_pending") then
|
||||||
local okay = false
|
local okay = false
|
||||||
if (ba_time_out == 0 and readdevice("ba_lout") > 0) then -- Can let go
|
if (ba_time_out == 0 and readdevice("ba_lout") > 0) then -- Can let go
|
||||||
if(isfree[5] or revflag[5]==true or timecnt[4]>=timecnt[5]) then -- Way5 is free or Way5 will reverse
|
if (isfree[5] or revflag[5] or (timecnt[4] >= timecnt[5])) then -- Way5 is free or Way5 will reverse
|
||||||
okay = true
|
okay = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if (okay) then
|
if (okay) then
|
||||||
-- Add BA Timer
|
|
||||||
startBATimer()
|
startBATimer()
|
||||||
enabledevice("k4")
|
stopTimer(4)
|
||||||
os.sleep(0.25)
|
trigger("k4")
|
||||||
disabledevice("k4")
|
isfree[4] = true -- Mark way 4 as free
|
||||||
RemoveTimer(timerid[4])
|
|
||||||
timecnt[4]=0
|
|
||||||
isfree[4]=true
|
|
||||||
else
|
else
|
||||||
bus:push(ev)
|
bus:push(ev)
|
||||||
end
|
end
|
||||||
|
elseif (ev == "ab_time_out_needstop") then
|
||||||
|
RemoveTimer(ab_timerid_out)
|
||||||
|
ab_time_out = 0
|
||||||
elseif (ev == "ba_time_out_needstop") then
|
elseif (ev == "ba_time_out_needstop") then
|
||||||
RemoveTimer(ba_timerid_out)
|
RemoveTimer(ba_timerid_out)
|
||||||
ba_time_out = 0
|
ba_time_out = 0
|
||||||
|
|
Loading…
Reference in New Issue
Block a user