Advertisement
Guest User

printer.lua

a guest
Nov 29th, 2021
42
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local machine = {
  2.     width = 8,
  3.     height = 8,
  4.     depth = 8,
  5.    
  6.     head_pos = {
  7.      x = 0,
  8.      y = 0,
  9.      z = 0
  10.     },
  11.  
  12.     motorz = peripheral.wrap("top"),
  13.     motorx = peripheral.wrap("left"),
  14.     motory = peripheral.wrap("back")
  15. }
  16. rednet.open("right")
  17. function machine.stopMotion()
  18.   machine.motorz.stop()
  19.   machine.motorx.stop()
  20.   machine.motory.stop()
  21. end
  22.  
  23. function machine.zero()
  24.   sleep(machine.motorx.translate(machine.width, 256))
  25.   machine.stopMotion()
  26.   sleep(machine.motory.translate(machine.depth, 256))
  27.   machine.stopMotion()
  28.   sleep(machine.motorz.translate(machine.height, 256))
  29.   machine.stopMotion()
  30.   machine.head_pos.x = 0
  31.   machine.head_pos.y = 0
  32.   machine.head_pos.z = 0
  33. end
  34.  
  35. function machine.setX(x)
  36.   local x_difference = machine.head_pos.x - x
  37.   print("Moving x by " .. x_difference .. " from " .. machine.head_pos.x.." to " ..x)
  38.   sleep(machine.motorx.translate(x_difference, 256))
  39.   machine.stopMotion()
  40.   machine.head_pos.x = x  
  41. end
  42.  
  43. function machine.setZ(z)
  44.   local z_difference = machine.head_pos.z - z
  45.   print("Moving z by ".. z_difference .. " from "..machine.head_pos.z.." to "..z)
  46.   sleep(machine.motorz.translate(z_difference, 128))
  47.   machine.stopMotion()
  48.   machine.head_pos.z = z
  49. end
  50.  
  51. function machine.setY(y)
  52.   local y_difference = machine.head_pos.y - y
  53.   print("Moving y by ".. y_difference .. " from " .. machine.head_pos.y.." to " .. y)
  54.   sleep(machine.motory.translate(y_difference, 256))
  55.   machine.stopMotion()
  56.   machine.head_pos.y = y
  57. end
  58.  
  59. function machine.setPos(x, y, z)
  60.   machine.setZ(z)
  61.   sleep(0.25)
  62.   machine.setX(x)
  63.   sleep(0.25)
  64.   machine.setY(y)  
  65. end
  66.  
  67. function machine.placeBlock()
  68.   sleep(0.5)
  69.   rednet.broadcast("place", "3dprinter")
  70.   while true do
  71.     event, sender, message, protocol = os.pullEvent("rednet_message")
  72.     if protocol == "3dprinter" and message == "placed" then
  73.       break
  74.     end
  75.   end
  76. end
  77.  
  78. machine.stopMotion()
  79. machine.zero()
  80.  
  81. return machine
Advertisement
RAW Paste Data Copied
Advertisement