Advertisement
Shurhaian

Elevator Control API

Mar 4th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.12 KB | None | 0 0
  1. -- SkunkWorks Logistics
  2. -- Elevator control API
  3. -- Defining all common functions and variables
  4.  
  5. elevCtrl = "right" -- Bundle leading to elevator controls
  6. doorCtrl = "back" -- Bundle leading to door controls
  7. liteCtrl = "bottom" -- Bundle for controlling call lights
  8. incSig = "left" -- Bundle leading to call signals
  9. monitor = "top" -- Local monitor location
  10.  
  11. elevHome = 1 -- From the bottom, which floor is home? Will be significant in later development
  12. elevFloor = 1 -- Elevator's current position, by floor. Used only for signaling/door control purposes, not motion.
  13. elevPos = 1 -- Elevator's current position, in BLOCKS, not floors
  14.  
  15. floorLev = {1, 7, 13} -- Table of floor elevations.
  16.  
  17. elevUp = colors.white -- Upwards frame motor
  18. elevDown = colors.orange -- Downwards frame moter
  19. elevDep = colors.magenta -- Frame deployer
  20. elevBrk = colors.lightBlue -- Frame breaker
  21.  
  22. floorCol = {colors.orange, colors.magenta, colors.lightBlue} -- Set up the table of per-floor color codes
  23.  
  24. maint = colors.white -- Motor room door and calibration remote
  25. floorCol[0] = maint -- Put it into the main array as floor 0
  26.  
  27. doorsOpen = {true, false, false} -- Default configuration starts on floor 1 with that door (only) open
  28. doorsOpen[0] = false -- Maintenance room door
  29. -- Note that the first attempt to open/close a door will put it in sync, as it explicitly turns channels on/off
  30.  
  31.  
  32. function elevLower( times )
  33.     for i = 1, times do
  34.         rs.setBundledOutput(elevCtrl, elevDown)
  35.         sleep(0.1)
  36.         rs.setBundledOutput(elevCtrl, 0)
  37.         elevPos = elevPos - 1
  38.         sleep(0.7)
  39.         rs.setBundledOutput(elevCtrl, elevBrk)
  40.         sleep(0.1)
  41.         rs.setBundledOutput(elevCtrl, 0)
  42.         sleep(0.1)
  43.     end
  44. end
  45.  
  46. function elevRaise( times )
  47.     for i = 1, times do
  48.         rs.setBundledOutput(elevCtrl, elevDep)
  49.         sleep(0.1)
  50.         rs.setBundledOutput(elevCtrl, elevUp)
  51.         elevPos = elevPos + 1
  52.         sleep(0.1)
  53.         rs.setBundledOutput(elevCtrl, 0)
  54.         sleep(0.8)
  55.     end
  56. end
  57.  
  58. function monPrint( msg )
  59.     term.redirect(mon)
  60.     print(msg)
  61.     term.restore()
  62. end
  63.  
  64. function toggleMaint() -- Maintenance room can be accessed by the appropriate remote
  65.     local doors = rs.getBundledOutput(doorCtrl)
  66.     if doorsOpen[0] then -- Maintenance door is already open
  67.         doors = colors.subtract(doors, maint)
  68.         rs.setBundledOutput(doorCtrl, doors)
  69.         lightOff(0) -- Maintenance lights indicate door status alone
  70.         doorsOpen[0] = false
  71.     else
  72.         doors = colors.combine(doors, maint)
  73.         rs.setBundledOutput(doorCtrl, doors)
  74.         lightOn(0) -- Shows the door's open and/or gives light to see by
  75.         doorsOpen[0] = true
  76.     end
  77. end
  78.  
  79. function doorClose(floor) -- Technically works for maintenance room, but see toggleMaint()
  80.     local doors=rs.getBundledOutput(doorCtrl)
  81.     doors = colors.subtract(doors,floorCol[floor])
  82.     rs.setBundledOutput(doorCtrl, doors)
  83.     doorsOpen[floor]=false
  84. end
  85.  
  86. function doorOpen(floor) -- See notes on doorClose()
  87.     local doors=rs.getBundledOutput(doorCtrl)
  88.     doors = colors.combine(doors,floorCol[floor])
  89.     rs.setBundledOutput(doorCtrl, doors)
  90.     doorsOpen[floor]=true
  91. end
  92.  
  93. function lightOn(floor)
  94.     local lights=rs.getBundledOutput(liteCtrl)
  95.     lights=colors.combine(lights,floorCol[floor])
  96.     rs.setBundledOutput(liteCtrl, lights)
  97. end
  98.  
  99. function lightOff(floor)
  100.     local lights=rs.getBundledOutput(liteCtrl)
  101.     lights=colors.subtract(lights,floorCol[floor])
  102.     rs.setBundledOutput(liteCtrl, lights)
  103. end
  104.  
  105. function lightWarn(floor) -- Used to warn the floor the elevator's at that it's closing and preparing to move
  106.     for i=1, 4 do
  107.         lightOn(floor)
  108.         sleep(0.25)
  109.         lightOff(floor)
  110.         sleep(0.25)
  111.     end
  112. end
  113.  
  114. function moveToFloor(target)    -- Move elevator to the given floor in the array
  115.                                 -- Returns success/failure as a Boolean, then how far it moved as an int
  116.     target = math.floor(target) -- Ensure we're working with an integer value
  117.     if target == 0 then -- Floors are numbered from 1, so 0 is out of bounds
  118.         return false, 0
  119.     elseif target > #floorLev then -- Listing a floor that's beyond the reach of the array
  120.         return false, 0
  121.     else
  122.         local newPos = floorLev[target]
  123.         if newPos == elevPos then -- Already there
  124.             return true, 0
  125.         elseif newPos > elevPos then -- We need to go up
  126.             elevRaise(newPos - elevPos) -- Move the difference
  127.             return true, newPos - elevPos -- Upwards is a positive change
  128.         else -- We must need to go down
  129.             elevLower(elevPos - newPos) -- Move the difference
  130.             return true, newPos - elevPos -- Downwards is a negative change
  131.         end
  132.     end
  133. end
  134.  
  135. function elevCall(target) -- This floor has called, or the up/down buttons been pressed on floor below/above
  136.     if target == 0 then -- Out of bounds. Maintenance room can't call the elevator.
  137.         return false, 0
  138.     elseif target > #floorLev then -- Too high. Shouldn't happen.
  139.         return false, 0
  140.     else
  141.         local newPos = floorLev[target]
  142.         if elevPos == newPos then -- Are we already there?
  143.             elevFloor = target -- Update the elevator's floor location, just in case
  144.             return true, 0 -- We're done
  145.         else
  146.             lightOn(target)
  147.             lightWarn(elevFloor)
  148.             doorClose(elevFloor)
  149.             local succ=moveToFloor(target)
  150.             lightOff(target)
  151.             doorOpen(target)
  152.             elevFloor = target
  153.             return succ -- Pass along the output of moveToFloor()
  154.         end
  155.     end
  156. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement