Advertisement
Gazer29

AutoStation1

Apr 11th, 2018
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.11 KB | None | 0 0
  1. local event = require("event")
  2. local component = require("component")
  3. local DetectorAugments = component.list("ir_augment_detector")
  4. local ControllerAugments = component.list("ir_augment_control")
  5. local sides = require("sides")
  6.  
  7. -- Settings that need changing to work --
  8.  
  9. local Control = M               -- 1 = wait for redstone signal, 2 = wait for seconds only, 3 = redstone signal or Wait for seconds
  10. local StationX = X              -- X Coordinate - Station detector block X coordinate
  11. local StationZ = Z              -- Z Coordinate - Station detector block Z coordinate
  12. local Direction = "south"       -- direction ["south", "north"] - Direction of main travel
  13.  
  14. -- Settings --
  15.  
  16. local Side = sides.top          -- Side [sides.top, sides.north, etc] - Side of redstone control block
  17. local InitialSetSpeed = 10      -- speed [0 to +100] - Initial Speed set
  18. local Deadzone = 2              -- speed [0 to +100] - does not control loco is within this Deadzone speed of the set speed
  19. local Threshold = 20            -- speed [0 to +100] - Above this speed difference, SetChange = 1.0
  20. local OtherDirectionThrottle = 0-- Throttle [-1 to 1] - Currently does nothing to loco
  21. local MinSpeed = 2              -- speed [0 to +100] - Minimum Station speed
  22. local MinThrottle = 0.05        -- Throttle [-1 to 1] default 0.005 - Min throttle speed when accelerating
  23. local Wait = 20                 -- seconds [0+] - seconds held at station
  24. local ExitThrottle = 0.4        -- Throttle [-1 to 1] - throttle set to exit the station
  25. local ExtraInfo = 1             -- [1 = Yes, 0 = No] - Yes or no to extra information being displayed
  26. local TweakThrottle = 0.9       -- [0 to 1] - Tweaks the throttle applied on acceleration, 1 = no change
  27.  
  28. -- Do not alter --
  29.  
  30. local MaxX = 0
  31. local MaxZ = 0
  32.  
  33. -- Code ----------------------------------
  34.  
  35. print("\nAutomated station script, Slows loco down from 'WantedSpeed' down to the 'MinSpeed' and stop at a specified augment detector. \nEdit the script settings to permanently setup the script \n")
  36.  
  37. --  If Station block not set above, prompt user for X and Z coordinates and Control mode
  38. if StationX == X then
  39.   print("Type X coordinate of the Detector Augment used for the Station:")
  40.   StationX = io.read()
  41.   print("StationX position set to:", StationX, "\n")
  42.   end
  43. if StationZ == Z then
  44.   print("Type Z coordinate of the Detector Augment used for the Station:")
  45.   StationZ = io.read()
  46.   print("StationZ position set to:", StationZ, "\n")
  47.   end
  48. if Control == M then
  49.   print("Select control mode: 1 = wait for redstone signal, 2 = (default) wait for seconds only, 3 = redstone signal or Wait for seconds")
  50.   Control = io.read()
  51.   print("Mode selected:", Control)
  52.   end
  53.  
  54. print("\nStation is operational \n")
  55.  
  56. -- Listen for the Train Overhead event
  57. event.listen("ir_train_overhead", function(name, address, augment_type, uuid)
  58.  
  59.   if name == "ir_train_overhead" then
  60.     if augment_type == "DETECTOR" then
  61.  
  62.       -- Get the detector
  63.       local Detector = component.proxy(address)
  64.       local DetectorInfo = Detector.info()
  65.  
  66.       -- Check that it's a loco
  67.       if not string.find(DetectorInfo.id, "loco") then
  68.         return
  69.       end
  70.         local x, y, z = Detector.getPos()
  71.  
  72.     -- Calculate difference in x to station
  73.     DiffX = StationX - x
  74.             if DiffX < 0 then
  75.                DiffX = DiffX * -1
  76.                end      
  77.       if DiffX > MaxX then
  78.                MaxX = DiffX
  79.                end  
  80.     if MaxX ~= 0 then
  81.       AdjustX = DiffX / MaxX
  82.       else
  83.       AdjustX = 0
  84.       end  
  85.  
  86.     -- Calculate difference in z to station
  87.     DiffZ = StationZ - z
  88.             if DiffZ < 0 then
  89.                DiffZ = DiffZ * -1
  90.                end      
  91.       if DiffZ > MaxZ then
  92.                MaxZ = DiffZ
  93.                end  
  94.     if MaxZ ~= 0 then
  95.       AdjustZ = DiffZ / MaxZ
  96.       else
  97.       AdjustZ = 0
  98.       end
  99.  
  100.     --  Calculate Adjustment to the speed based off Distance from station
  101.     if (AdjustX ~= 0) then    
  102.       Adjustment = AdjustX  
  103.       end  
  104.     if (AdjustZ ~= 0) then    
  105.       Adjustment = AdjustZ
  106.       end
  107.     if (AdjustX ~= 0 and AdjustZ ~= 0) then  
  108.       Adjustment = AdjustX * AdjustZ
  109.       end
  110.     if (AdjustX == 0) and (AdjustZ == 0) then  
  111.       Adjustment = 0
  112.       end
  113.     if Adjustment > 1 then
  114.       Adjustment = 1
  115.       end
  116.        
  117.         -- Adjust the WantedSpeed by the percentage distance travelled by the loco (accounting for MinSpeed)
  118.         NewWantedSpeed = ((InitialSetSpeed - MinSpeed) * Adjustment) + MinSpeed
  119.         if ExtraInfo == 1 then
  120.           print("Coords:", x, y, z, "NewSpeed: ", NewWantedSpeed)
  121.           end
  122.  
  123.         -- Brake/accelerate proportional to the speed difference (smooths change in speed)
  124.         if DetectorInfo then
  125.              Difference = DetectorInfo.speed - NewWantedSpeed
  126.              if Difference < 0 then
  127.                Difference = Difference * -1
  128.                end
  129.              SetChange = Difference / Threshold
  130.              if Difference > Threshold then
  131.                SetChange = 1.0
  132.                end
  133.             if ExtraInfo == 1 then
  134.               print("Diff:", Difference, "  Speed:", DetectorInfo.speed, "  SetChange:", SetChange)
  135.               end
  136.             end
  137.        
  138.         -- If at station block, halt loco for set Wait then depart loco at ExitThrottle
  139.         if Adjustment == 0 then
  140.             if ExtraInfo == 1 then
  141.               print("At station")
  142.               end    
  143.             -- Loop over all the controllers to update them
  144.             for ControllerUUID, ControllerName in pairs(ControllerAugments) do
  145.                 local Controller = component.proxy(ControllerUUID)
  146.                     Controller.setThrottle(0.0)
  147.                     Controller.setBrake(1.0)
  148.                     end
  149.  
  150.             -- Depending upon Control - use redstone or wait
  151.             if tonumber(Control) == 1 then
  152.                     if ExtraInfo == 1 then
  153.                       print("Control 1")
  154.                       end
  155.                     local rs = component.redstone
  156.                     while rs.getInput(Side) == 0 do
  157.                         os.sleep(0.5)      
  158.                     end
  159.                 else if tonumber(Control) == 2 then
  160.                     if ExtraInfo == 1 then
  161.                       print("Control 2")
  162.                       end
  163.                     os.sleep(Wait)
  164.                 else if tonumber(Control) == 3 then
  165.                     if ExtraInfo == 1 then
  166.                        print("Control 3")
  167.                        end
  168.                     local rs = component.redstone
  169.                     local time = 0
  170.                     while time < Wait do
  171.                         time = time + 1
  172.                         if rs.getInput(Side) ~= 0 then
  173.                             time = Wait
  174.                             end
  175.                         os.sleep(1)
  176.                     end
  177.                 end
  178.                 end
  179.             end
  180.             if ExtraInfo == 1 then
  181.               print("Departing station")
  182.               end
  183.             for ControllerUUID, ControllerName in pairs(ControllerAugments) do
  184.                 local Controller = component.proxy(ControllerUUID)
  185.                     Controller.setBrake(0.0)
  186.                     Controller.setThrottle(ExitThrottle)
  187.                     end
  188.             os.sleep(20)
  189.             end
  190.  
  191.         -- Loop over all the controllers to update them
  192.         for ControllerUUID, ControllerName in pairs(ControllerAugments) do
  193.             local Controller = component.proxy(ControllerUUID)
  194.    
  195.             --  If travelling in not the main Direction then do nothing
  196.               if not string.find(DetectorInfo.direction, Direction) then
  197.                 -- Do nothing
  198.                 -- Controller.setThrottle(OtherDirectionThrottle)
  199.                 -- Controller.setBrake(0.0)
  200.                 if ExtraInfo == Y then
  201.                   print("OtherDirection")
  202.                   end
  203.             else
  204.  
  205.              -- If we're within a certain range of NewWantedSpeed then don't do anything
  206.              if DetectorInfo and math.abs(DetectorInfo.speed - NewWantedSpeed) > Deadzone then
  207.                   -- If we're too far above or below WantedSpeed then speed up or slow down
  208.                   if DetectorInfo.speed < NewWantedSpeed then
  209.                       -- Increase loco speed
  210.                       -- MinThrottle + SetChange
  211.                       SetChange = SetChange + MinThrottle
  212.                       if SetChange > 1 then
  213.                         SetChange = 1
  214.                         end
  215.                        SetChange = SetChange * TweakThrottle
  216.                       Controller.setThrottle(SetChange)
  217.                       Controller.setBrake(0.0)
  218.                   else
  219.                       -- Decrease loco speed
  220.                       Controller.setThrottle(0.040)
  221.                       Controller.setBrake(SetChange)
  222.                   end
  223.               else
  224.                   -- Speed within Deadzone
  225.                   Controller.setThrottle(0.066)
  226.                   Controller.setBrake(0.0)
  227.               end
  228.         end
  229.         end
  230.     end
  231. end
  232. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement