RavenSH4

LiftServer

May 15th, 2022 (edited)
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.44 KB | None | 0 0
  1. --VAR
  2. Clutch = "right"
  3. Gearshift = "back"
  4. IsInitializing = false -- lift is in process of initialization
  5. IsReady = false -- lift is ready, don't moving and await for commands
  6. IsMoving = false -- lift is currently moving
  7. CurrentLevel = 0
  8. TargetLevel = 0
  9. --initialize
  10. rs.setOutput(Clutch , true) --set true to off
  11. rs.setOutput(Gearshift , true) --true to move up
  12. modem = peripheral.wrap("top")
  13. modem.open(21)
  14. ------------------
  15.  
  16. ---lift server start up
  17. function Initialize()
  18.   print("INITIALIZE")
  19.   IsInitializing = true
  20.   TargetLevel = 0
  21.   --send command to reset all clients
  22.   modem.transmit(21, 22, "init")
  23.   --set lift to move down
  24.   rs.setOutput(Gearshift, false)
  25.   sleep(0.5)
  26.   --start lift move down
  27.   rs.setOutput(Clutch, false)
  28. end
  29.  
  30. --
  31. function ProcessCommand(command)
  32.   write("received=")
  33.   print(command)
  34.   --
  35.   if (command == "request") then
  36.     if (IsReady) then
  37.       modem.transmit(21, 22, "ready")
  38.       return
  39.     end
  40.     if (IsMoving) then
  41.       modem.transmit(21, 22, "moving "..TargetLevel )
  42.       return
  43.     end
  44.     return
  45.   end
  46.   --
  47.   if (IsInitializing and command == "contact 0") then
  48.     CurrentLevel = 0
  49.     rs.setOutput(Clutch , true) --set true to off
  50.     rs.setOutput(Gearshift , false) --true to move up
  51.     IsInitializing = false
  52.     IsReady = true
  53.     modem.transmit(21, 22, "ready")
  54.     return
  55.   end
  56.   --
  57.   if (IsMoving) then
  58.     if (command == "contact 0") then
  59.         ProcessContactCommand(0)
  60.     end
  61.     if (command == "contact 1") then
  62.         ProcessContactCommand(1)
  63.     end
  64.     return
  65.   end
  66.   --
  67.   if (IsReady) then
  68.     if (command == "call 0") then
  69.         CallLiftToLevel(0)
  70.     end
  71.     if (command == "call 1") then
  72.         CallLiftToLevel(1)
  73.     end
  74.     return
  75.   end
  76. end
  77.  
  78. --Call lift moving to specified level
  79. function CallLiftToLevel(level)
  80.   if (IsReady == false) then
  81.     return
  82.   end
  83.  --
  84.  if (level == CurrentLevel) then
  85.    return
  86.  end
  87.  if (level > CurrentLevel) then
  88.    --move up
  89.    TargetLevel = level
  90.    IsMoving = true
  91.    modem.transmit(21, 22, "lock 1 UP")
  92.    modem.transmit(21, 22, "moving 1")
  93.    rs.setOutput(Gearshift , true) --true to move up
  94.    -- WAIT FOR RESPONSE
  95.    sleep(0.5)
  96.    rs.setOutput(Clutch , false) --set false to on
  97.  end
  98.  if (level < CurrentLevel) then
  99.    --move down
  100.    TargetLevel = level
  101.    IsMoving = true
  102.    modem.transmit(21, 22, "lock 0 DOWN")
  103.    modem.transmit(21, 22, "moving 0")
  104.    rs.setOutput(Gearshift , false) --false to move down
  105.    -- WAIT FOR RESPONSE
  106.    sleep(0.5)
  107.    rs.setOutput(Clutch , false) --set false to on
  108.  end
  109. end
  110.  
  111. --Process contact command while lift is moving
  112. function ProcessContactCommand(level)
  113.   if (level == TargetLevel) then
  114.     rs.setOutput(Clutch , true) --set true to off
  115.     sleep(0.5)
  116.     CurrentLevel = TargetLevel
  117.     modem.transmit(21, 22, "ready")
  118.     IsMoving = false
  119.   end
  120. end
  121.  
  122. --
  123. function ProcessRedstone()
  124.   detector = rs.getInput("left")
  125.   if (detector == true) then
  126.     modem.transmit(21, 22, "contact 1")
  127.     ProcessCommand("contact 1")
  128.   end
  129. end
  130.  
  131. --initialize
  132. Initialize()
  133. --Main loop
  134. repeat
  135.     --local myTimer = os.startTimer(1)
  136.  
  137.     event,p1,p2,p3,p4,p5 = os.pullEvent()
  138.     if (event=="redstone") then
  139.       ProcessRedstone()
  140.     end
  141.     if (event=="modem_message") then
  142.         --event, side, channel, replyChannel, message, distance
  143.         ProcessCommand(p4)
  144.     end
  145.    
  146.     if event=="timer" then
  147.       print("test")
  148.     end
  149. until event=="char" and p1==("q")
Add Comment
Please, Sign In to add comment