Advertisement
Shurhaian

SkunkWorks Logistics Seed Breeder - Remote

May 19th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.45 KB | None | 0 0
  1. --[[ SeedRemote.lua
  2.    Analyzer monitoring remote program
  3.    For use in SkunkWorks AgriCraft Seed Breeder
  4.    Version 1.3 --]]
  5.  
  6. --[[ This should be installed on a computer with a wireless network card,
  7.    attached to three computer-controlled seed analyzers.
  8.    For full setup details, see SeedBreeder.lua
  9.    Once setup is complete, run this file FIRST, then run
  10.    SeedBreeder.lua on the robot.
  11.    A handshake process is included to allow for flexibility in load order,
  12.    but especially in places with high network activity, it may fail if the
  13.    robot's program is started first.]]
  14.  
  15.  
  16. -- CONFIGURATION --  
  17. --[[ Ensure the wireless settings variables below match those on the robot.
  18.    Also be sure to set "AnaSide" below them to whatever matches your own
  19.    orientation, and set "AddrAnaLeft", "AddrAnaMid", and "AddrAnaRight" as
  20.    the addresses of your respective seed analyzers.]]
  21.    
  22. --Wireless settings - these should be identical to the robot's values
  23. CommandPort=9090 -- Network port used to receive commands
  24. local SignalStr=100 -- Wireless signal strength
  25.  
  26.    
  27. --SET ME - This indicates which side of the analyzers the soil is on
  28. --It should be one of "NORTH", "EAST", "SOUTH", or "WEST"
  29. --Note this is looking from the analyzer to the crops, NOT vice versa
  30. AnaSide=""
  31.  
  32. --These should be filled in based on the addresses of your seed analyzers.
  33. --Left and right are based on robot's POV, standing over the crops,
  34. --looking at the analyzers, unlike AnaSide above.
  35. AddrAnaMid=""
  36. AddrAnaLeft=""
  37. AddrAnaRight=""
  38.  
  39. --[[This is how long the computer will wait between attempts to look at the
  40. status of the middle crop plot. This is the only one that is allowed to sit
  41. empty for any length of time, and thus the only one that should be
  42. vulnerable to weeds. If soil + growth boosts mean that weeds aren't being
  43. caught in a timely fashion, decrease this value. If weeds are off, it can be
  44. safely increased to reduce load. --]]
  45. MidTimer=10
  46.  
  47. --[[This is the time between checks for the side plots, which should not
  48. ever be vulnerable to weeds. If you have strong growth acceleration, you may
  49. want to turn this value down. --]]
  50. GrowTimer=20
  51.  
  52. -- END CONFIGURATION --
  53.  
  54. -- FUNCTION DEFINITIONS --
  55. function netSend (msg)
  56.    wNet.broadcast(CommandPort, msg)
  57. end
  58.                        -- This station never expects an immediate response
  59. function netListen()   -- Therefore, unlike the robot, it never needs a timeout
  60.    local _, _, _, _, _, msg = event.pull("modem")
  61.    return msg
  62. end
  63.  
  64. function sumMid()
  65.    if (not (AnaMid.isAnalyzed())) then
  66.       print "Analyzing specimen..."
  67.       AnaMid.analyze() -- Start the analysis
  68.       repeat
  69.          os.sleep(1) -- Give it a second...
  70.       until (AnaMid.isAnalyzed()) -- then try again
  71.       print "Analysis complete!"
  72.    else print "Specimen already analyzed!" end
  73.    local gro, gain, str = AnaMid.getSpecimenStats()
  74.    return gro+gain+str
  75. end
  76.  
  77. function watchMid()
  78.    local midStat = "Empty"
  79.    print "Monitoring middle plot..."
  80.    while (midStat == "Empty") do
  81.       os.sleep(midTimer)
  82.       if (AnaMid.hasWeeds(AnaSide)) then
  83.          midStat="HasWeeds"
  84.          print "WARNING! Weeds detected!"
  85.       elseif (AnaMid.hasPlant(AnaSide)) then
  86.          midStat="HasPlant"
  87.          print "Sprout detected!"
  88.       end
  89.    end
  90.    return midStat
  91. end
  92.  
  93. function checkMidWeeds()
  94.    if (AnaMid.hasWeeds(AnaSide)) then
  95.       print("Weeds still present!")
  96.       netSend("HasWeeds")
  97.    else
  98.       print("Weeds cleared.")
  99.       netSend("HasNoWeeds")
  100.    end
  101. end
  102.  
  103. function growLeft()
  104.    print "Waiting for LEFT plot to grow..."
  105.    while (not (AnaLeft.isMature(AnaSide))) do os.sleep(GrowTimer) end
  106.    print "Done growing!"
  107.    netSend("LeftGrown")
  108. end
  109.  
  110. function growRight()
  111.    print "Waiting for RIGHT plot to grow..."
  112.    while (not (AnaRight.isMature(AnaSide))) do os.sleep(GrowTimer) end
  113.    print "Done growing!"
  114.    netSend("RightGrown")
  115. end
  116.  
  117.  
  118.  
  119. -- END FUNCTION DEFINITIONS --
  120.  
  121. -- Possible instructions --
  122. --[[
  123. "CheckMidWeeds" -> "HasWeeds" | "HasNoWeeds"
  124. "Handshake" -> "Confirm"
  125. "Analyze" -> sum
  126. "GrowLeft" -> "LeftGrown"
  127. "GrowRight" -> "RightGrown"
  128. "WatchMid" -> "HasWeeds" | "HasPlant"
  129.  
  130.  End possible instructions --]]
  131.  
  132.  
  133. local version="1.3"
  134. event = require("event")
  135. component = require("component")
  136.  
  137. wNet = component.proxy(component.modem.address)
  138.  
  139. AnaMid=component.proxy(AddrAnaMid)
  140. AnaLeft=component.proxy(AddrAnaLeft)
  141. AnaRight=component.proxy(AddrAnaRight)
  142.  
  143. print("---------------------------------------------")
  144. print("SkunkWorks Logistics - AgriCraft Seed Breeder")
  145. print("Robot/main control program")
  146. print("Version "..version)
  147. print("---------------------------------------------")
  148.  
  149. wNet.setStrength(SignalStr)
  150. wNet.close()
  151. wNet.open(CommandPort)
  152. print("Communicating on port "..CommandPort.." with power "..SignalStr)
  153. print "Listening for commands..."
  154. while true do -- Main loop
  155.    local cmd = netListen()
  156.    if (cmd=="Handshake") then
  157.       print "Received handshake request! Sending confirmation."
  158.       netSend("Confirm")
  159.    elseif (cmd=="Analyze") then
  160.       local sum=sumMid()
  161.       print("Sending sum of analyzed seed data: "..sum)
  162.       netSend(sum)
  163.    elseif (cmd=="WatchMid") then
  164.       netSend(watchMid())
  165.    elseif (cmd=="CheckMidWeeds") then
  166.       checkMidWeeds()
  167.    elseif (cmd=="GrowLeft") then
  168.       growLeft()
  169.    elseif (cmd="GrowRight") then
  170.       growRight()
  171.    else
  172.       print("Ignoring unrecognized command: "..cmd)
  173.    end -- if cmd
  174. end -- main loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement