From ce33adaa9f75ec81ef4c5bf20ea51d22a040b812 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Thu, 22 Mar 2018 23:37:28 +0800 Subject: [PATCH] Add three way crossing program --- crossing3.lua | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 crossing3.lua diff --git a/crossing3.lua b/crossing3.lua new file mode 100644 index 0000000..40f8f1d --- /dev/null +++ b/crossing3.lua @@ -0,0 +1,152 @@ +require("libevent") +require("util") +local component=require("component") +local computer=require("computer") + +-- Hardware +local drecv=proxy("digital_receiver_box") +local dsend=proxy("digital_controller_box") + +local SA=proxy("digital_detector","6") +local SB=proxy("digital_detector","9") +local SC=proxy("digital_detector","2") + +local nameA="StationA" +local nameB="StationB" +local nameC="StationC" + +--[[ + getAspect/setAspect: 1 Green 3 Yellow 5 Red + SigIn : LAIN LAOUT LBIN LBOUT LCIN LCOUT + SigOut: KA MA KB MB KC MC + Device: SA SB SC +]] + +local green=1 +local yellow=3 +local red=5 + +local function getSignal(name) + return drecv.getAspect(name) +end + +local function setSignal(name,val) + dsend.setAspect(name,val) +end + +local function resetSystem() + setSignal("KA",red) + setSignal("KB",red) + setSignal("KC",red) + setSignal("MA",red) + setSignal("MB",red) + setSignal("MC",red) +end + +local function getOperation(trainID,from) + return math.random(0,1) +end + +local function main() + resetSystem() + local bus=CreateEventBus() + bus:listen("minecart",function(e) + return (string.find(e.minecartType,"locomotive")~=nil) + end) + bus:listen("interrupted") + + while true do + print("Waiting event...") + local e=bus:next() + if(e.event=="interrupted") then break end + + -- Try to solve + local ans=-1 + if(e.detectorAddress==SA.address) then + print("A-->?") + ans=getOperation(e.destination,nameA) + elseif(e.detectorAddress==SB.address) then + print("B-->?") + ans=getOperation(e.destination,nameB) + elseif(e.detectorAddress==SC.address) then + print("C-->?") + ans=getOperation(e.destination,nameC) + end + + local solved=false + + if(ans==-1) then + print("Unknown Train") + elseif(ans==0) then + if(e.detectorAddress==SA.address) then + if(getSignal("LBIN")==green) then + print("A-->B") + setSignal("MA",red) + setSignal("KA",green) + WaitEventEx("aspect_changed",nil,"LBIN") + setSignal("KA",red) + solved=true + end + elseif(e.detectorAddress==SB.address) then + if(getSignal("LAIN")==green) then + print("B-->A") + setSignal("MB",red) + setSignal("KB",green) + WaitEventEx("aspect_changed",nil,"LAIN") + setSignal("KB",red) + solved=true + end + elseif(e.detectorAddress==SC.address) then + if(getSignal("LAIN")==green) then + print("C-->A") + setSignal("MB",red) + setSignal("MC",red) + setSignal("KC",green) + WaitEventEx("aspect_changed",nil,"LAIN") + setSignal("KC",red) + solved=true + end + end + elseif(ans==1) then + if(e.detectorAddress==SA.address) then + if(getSignal("LCIN")==green) then + print("A-->C") + setSignal("MA",green) + setSignal("KA",green) + WaitEventEx("aspect_changed",nil,"LCIN") + setSignal("KA",red) + solved=true + end + elseif(e.detectorAddress==SB.address) then + if(getSignal("LCIN")==green) then + print("B-->C") + setSignal("MB",green) + setSignal("KB",green) + WaitEventEx("aspect_changed",nil,"LCIN") + setSignal("KB",red) + solved=true + end + elseif(e.detectorAddress==SC.address) then + if(getSignal("LBIN")==green) then + print("C-->B") + setSignal("MC",green) + setSignal("KC",green) + WaitEventEx("aspect_changed",nil,"LBIN") + setSignal("KC",red) + solved=true + end + end + end + + if(not solved) then + table.insert(bus.events,e) + os.sleep(0.5) + end + end + + resetSystem() +end + +print("Program Started") +main() +print("Program Stopped") \ No newline at end of file