Advertisement
HandieAndy

train_speed_checkpoint

Jun 16th, 2018
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.39 KB | None | 0 0
  1. --[[
  2. Author: Andrew Lalis
  3. File: train_speed_checkpoint.lua
  4. Version: 1.0
  5. Last Modified: 16-06-2018
  6.    
  7. Script used for helping to control the speed of a train. Use many
  8. computers running this script to calibrate trains to run at a certain speed.
  9. --]]
  10.  
  11. --Require statements and component definitions.
  12. local component = require("component")
  13. local event = require("event")
  14. local computer = require("computer")
  15. local term = require("term")
  16. local detector = component.ir_augment_detector
  17. local controller = component.ir_augment_control
  18.  
  19. -- Optimal Speed.
  20. local DESIRED_SPEED = 20
  21.  
  22. --[[
  23. Shortcut for pulling only train events.
  24. func - function(net_address, augment_type, stock_uuid): function to handle the
  25. train event, with augment type being either DETECTOR or LOCO_CONTROL.
  26. --]]
  27. local function pullTrainEvent(func)
  28.     local event_name, net_address, augment_type, stock_uuid = event.pull("ir_train_overhead")
  29.     func(net_address, augment_type, stock_uuid)
  30. end
  31.  
  32. --[[
  33. Determines if a string starts with a given substring.
  34. return - boolean: true if str starts with start, false otherwise.
  35. --]]
  36. local function strStarts(str, start)
  37.     return string.sub(str, 1, string.len(start)) == start
  38. end
  39.  
  40. --[[
  41. Determines if a stock car over the detector is a locomotive by reading info.id
  42. from the detector's info() method. If it begins with rolling_stock/loc, then
  43. it is a locomotive.
  44. --]]
  45. local function isStockLocomotive(id)
  46.     return strStarts(id, "rolling_stock/loc")
  47. end
  48.  
  49. local function balanceSpeed(info, target_speed)
  50.     local diff = target_speed - info.speed
  51.     local percent_diff = info.speed / target_speed
  52.     if diff > 0 then -- We are too slow.
  53.         controller.setBrake(0)
  54.         controller.setThrottle(info.throttle + percent_diff)
  55.     else -- We are too fast.
  56.         controller.setThrottle(0)
  57.         controller.setBrake(percent_diff)
  58.     end
  59. end
  60.  
  61. local function handleTrainEvent(net_address, augment_type, stock_uuid)
  62.     local info = detector.info()
  63.     local data = detector.consist()
  64.     if not (data and info and isStockLocomotive(info.id)) then
  65.         return
  66.     end
  67.     if (augment_type == "DETECTOR") then
  68.         term.clear()
  69.         term.setCursor(1, 1)
  70.         term.write("  Speed: "..data.speed_km.." Km/h")
  71.         term.write("  Dir: "..data.direction)
  72.         term.write("  Weight: "..data.weight_kg.." Kg")
  73.     end
  74.     if (augment_type == "LOCO_CONTROL") then
  75.         balanceSpeed(info, DESIRED_SPEED)
  76.     end
  77. end
  78.  
  79. while true do
  80.     pullTrainEvent(handleTrainEvent)
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement