Advertisement
Niverton

Lift-Motor

Sep 12th, 2014
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | None | 0 0
  1. --Lift motor
  2. --###########################
  3. --controller.move(<dir>, <move?>, <Don'tMoveController?>) -> Bools: Are we just testing if we can move? | Should the controller stay where it is? (only the controller, not the computer)
  4.  
  5. --CONST
  6.     M_SIDE = "left" --Modem side
  7.     C_SIDE = "back" --Controller side
  8.     CHANNEL = 1 --Channel to receive orders from
  9.     DIR = { -- Directions codes used by RIM
  10.             ["down"] = 0,
  11.             ["up"] = 1,
  12.             ["north"] = 2,
  13.             ["south"] = 3,
  14.             ["west"] = 4,
  15.             ["east"] = 5
  16.         }
  17. --
  18.  
  19. --FUNC
  20.  
  21. function move(dir, ctrl) --Moves in the given direction using given controller handle (prevents bugs and lift being stuck)
  22.     while true do
  23.         if ctrl.move(dir, true, false) then --We can move
  24.             ctrl.move(dir, false, false) --Moving
  25.             break --Stopping the loop
  26.         else
  27.             print("I'm stuck ! D:")
  28.             os.sleep(5) -- Wait 5 seconds
  29.         end
  30.     end
  31.     os.reboot() --Just in case
  32. end
  33.  
  34. --
  35.  
  36. --MAIN
  37. --Wrapping controller and modem
  38. local controller = peripheral.wrap(C_SIDE)
  39. local modem = peripheral.wrap(M_SIDE)
  40. --Open channel
  41. modem.open(CHANNEL)
  42.  
  43. local pos, dest
  44.  
  45. --If the config file does not exists, we assume that's the first start and that we are at level 0
  46. if not fs.exists("data/motor") then
  47.     fs.makeDir("data")
  48.     print("File not found, starting at level 0")
  49.     pos = 0
  50.     dest = 0
  51.     --Creating the file
  52.     local f = fs.open("data/motor", 'w')
  53.     f.writeLine(pos)
  54.     f.writeLine(dest)
  55.     f.close()
  56. else --If it exists, we read position and destination
  57.     local file = fs.open("data/motor", 'r')
  58.     pos = tonumber(file.readLine())
  59.     dest = tonumber(file.readLine())
  60.     file.close()
  61.     print("Data found: current = "..pos.."; destination = "..dest)
  62. end
  63.  
  64. --Moving
  65. if pos ~= dest then --Position is different from destination, we should move
  66.     print("Position not matching destination")
  67.     if pos > dest then  --If we're above, we go down
  68.         print("Moving down")
  69.         pos = pos-1
  70.         --Writing position before moving, because it will shut down the computer
  71.         local f = fs.open("data/motor", 'w')
  72.         f.writeLine(pos)
  73.         f.writeLine(dest)
  74.         f.close()
  75.        
  76.         move(DIR.down, controller)
  77.     else    --Otherwise we go up
  78.         print("Moving up")
  79.         pos = pos+1
  80.         --
  81.         local f = fs.open("data/motor", 'w')
  82.         f.writeLine(pos)
  83.         f.writeLine(dest)
  84.         f.close()
  85.        
  86.         move(DIR.up, controller)
  87.     end
  88.    
  89.     os.reboot()--
  90. end
  91.  
  92. -- Waiting loop
  93. while true do
  94.     --Getting messages
  95.     local event = {os.pullEvent("modem_message")}
  96.    
  97.     if tonumber(event[5]) ~= nil then --The message is a number
  98.         --Say you got the message (used in the call buttons)
  99.         modem.transmit(CHANNEL, CHANNEL, "pong")
  100.         --Writing down the destination before rebooting after the loop
  101.         dest = tonumber(event[5])
  102.         local f = fs.open("data/motor", 'w')
  103.         f.writeLine(pos)
  104.         f.writeLine(dest)
  105.         f.close()
  106.         break --Stopping the loop
  107.     end
  108. end
  109.  
  110. os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement