Advertisement
davedumas0

opencomputers robotControll_II --robot side

Feb 11th, 2023 (edited)
1,734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.54 KB | Gaming | 0 0
  1.  
  2.  
  3.  
  4. local term = require("term")
  5. local event = require("event")
  6. local component = require("component")
  7. local os = require("os")
  8.  
  9. local robot = require("robot")
  10. local serialization = require("serialization")
  11.  
  12. local modem = component.modem
  13. local geo = component.geolyzer
  14.  
  15. local computerAddress = "07bdaacd-57ba-467b-8522-3f40cb93e491"
  16.  
  17. inv = component.inventory_controller
  18.  pos = {}
  19.   pos.x = 0
  20.    pos.y = 0
  21.     pos.z = 0
  22.  
  23. local vector = "north"
  24. local message = " "
  25. function setCursorPos (posX, posY)
  26.   term.setCursor(posX, posY)
  27. end
  28.  
  29. function renderText(text)
  30.   term.write(text)
  31. end
  32.  
  33. function getInventoryInfo()
  34.     info =  getInventory()
  35.     sendMsgToComputer(info)
  36.    
  37.  
  38.      executed = true
  39.    
  40. end
  41.  
  42. function executeTurnLeft()
  43.   print("message from computer received=LFT")
  44.   print("executing command robot.turnLeft()")
  45.   if vector == "north" then
  46.     vector = "west"
  47.   elseif vector == "west" then
  48.     vector = "south"
  49.   elseif vector == "south" then
  50.     vector = "east"
  51.   elseif vector == "east" then
  52.     vector = "north"
  53.   end
  54.   robot.turnLeft()
  55.   print(vector)
  56.   sendMsgToComputer("robot.turnLeft()")
  57.   executed = true
  58. end
  59.  
  60. function executeTurnRight()
  61.   print("message from computer received=RHT")
  62.   print("executing command robot.turnRight()")
  63.   if vector == "north" then
  64.     vector = "east"
  65.   elseif vector == "east" then
  66.     vector = "south"
  67.   elseif vector == "south" then
  68.     vector = "west"
  69.   elseif vector == "west" then
  70.     vector = "north"
  71.   end
  72.   robot.turnRight()
  73.   print(vector)
  74.   sendMsgToComputer("robot.turnRight()")
  75.   executed = true
  76. end
  77.  
  78. function executeForward()
  79.   print("message from computer received=FWD")
  80.   print("executing command robot.forward()")
  81.   if vector == "north" then
  82.     pos.y = pos.y + 1
  83.   elseif vector == "east" then
  84.     pos.x = pos.x + 1
  85.   elseif vector == "south" then
  86.     pos.y = pos.y - 1
  87.   elseif vector == "west" then
  88.     pos.x = pos.x - 1
  89.   end
  90.   robot.forward()
  91.   print(vector)
  92.   sendMsgToComputer("robot.forward()")
  93.   executed = true
  94. end
  95.  
  96. function executeBack()
  97.   print("message from computer received=BWD")
  98.   print("executing command robot.back()")
  99.   if vector == "north" then
  100.     pos.y = pos.y - 1
  101.   elseif vector == "east" then
  102.     pos.x = pos.x - 1
  103.   elseif vector == "south" then
  104.     pos.y = pos.y + 1
  105.   elseif vector == "west" then
  106.     pos.x = pos.x + 1
  107.   end
  108.   robot.back()
  109.   print(vector)
  110.   sendMsgToComputer("robot.back()")
  111.   executed = true
  112. end
  113.  
  114. function executeUp()
  115.   print("message from computer received=UP")
  116.   print("executing command robot.up()")
  117.   pos.z = pos.z + 1
  118.   robot.up()
  119.   print(vector)
  120.   sendMsgToComputer("robot.up()")
  121.   executed = true
  122. end
  123.  
  124. function executeDown()
  125.   print("message from computer received=DWN")
  126.   print("executing command robot.down()")
  127.   pos.z = pos.z - 1
  128.   robot.down()
  129.   print(vector)
  130.   sendMsgToComputer("robot.down()")
  131.   executed = true
  132. end
  133.  
  134. function executeDetctUpward()
  135.   print("message from computer recieved="..message)
  136.    print("executing command geo.analyze(1)")
  137.     detctionResult = geo.analyze(1)
  138.     sendMsgToComputer("geo.analyze(1)")
  139.     os.sleep(0.2)
  140.     sendMsgToComputer(detctionResult.name)
  141.   executed = true
  142. end
  143.  
  144. function executeDetctDownward()
  145.   print("message from computer recieved="..message)
  146.    print("executing command robot.detectDown()")
  147.    detctionResult = geo.analyze(0)
  148.    sendMsgToComputer("geo.analyze(0)")
  149.    os.sleep(0.2)
  150.    sendMsgToComputer(detctionResult.name)
  151.   executed = true
  152. end
  153.  
  154. function executeDetctForward()
  155.   print("message from computer recieved="..message)
  156.    print("executing command geo.analyze(3)")
  157.    detctionResult = geo.analyze(3)
  158.    print("detctionResult = "..detctionResult.name)
  159.    sendMsgToComputer("geo.analyze(3)")
  160.    os.sleep(0.2)
  161.    sendMsgToComputer(detctionResult.name)
  162.      
  163.    
  164.   executed = true
  165. end
  166.  
  167. function executeUseForward()
  168.     print("message from computer recieved="..message)
  169.      print("executing command robot.use(3)")
  170.      robot.use(3)
  171.      sendMsgToComputer("robot.use(3)")
  172.        
  173.      
  174.     executed = true
  175. end
  176.  
  177. function getInventory()
  178.     local inv = component.inventory_controller
  179.     local inventory = {}
  180.     for i = 1, 16 do
  181.         a = inv.getStackInInternalSlot(i)
  182.         if  a ~= nil then
  183.           table.insert(inventory, i, a)
  184.         else
  185.             table.insert(inventory, i, "empty")
  186.         end
  187.     end
  188.     return inventory
  189. end
  190.  
  191.  
  192. local messageHandlers = {
  193.     LFT = executeTurnLeft,
  194.     RHT = executeTurnRight,
  195.     FWD = executeForward,
  196.     BWD = executeBack,
  197.     UP = executeUp,
  198.     DWN = executeDown,
  199.     DCT_UPW = executeDetctUpward,
  200.     DCT_DWD = executeDetctDownward,
  201.     DCT_FWD = executeDetctForward,
  202.     USE_FWD = executeUseForward,
  203.     GET_INV = getInventoryInfo,
  204. }
  205.  
  206. -- Open modem on port 5
  207. modem.open(5)
  208.  
  209. -- Function to listen for incoming modem messages
  210. function modemListen()
  211.     -- Wait for a modem message event
  212.     local _, _, from, port, _, msg = event.pull("modem_message")
  213.  
  214.     -- If a message was received
  215.     if msg then
  216.         -- Call the function associated with the message
  217.         local handler = messageHandlers[msg]
  218.         if handler then
  219.             handler()
  220.           os.sleep(0.1)
  221.         end
  222.     end
  223. end
  224.  
  225.  
  226. function sendMsgToComputer (msg)
  227.     msg2 = serialization.serialize(msg)
  228.     modem.open(5)
  229.     modem.setStrength(255)
  230.     sendMessageSuccess = modem.send(computerAddress, 5, msg2)
  231.     print("message "..tostring(msg2).." sent="..tostring(sendMessageSuccess))
  232. end
  233.  
  234. while true do
  235.  modemListen()
  236. end
  237.  
  238.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement