Gazer29

EngineControlv2

Apr 9th, 2021 (edited)
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Engine Control v2 by Gazer29
  2. --Uses Automation mod
  3.  
  4. local component = require("component")
  5. local term = require ("term")
  6. local entity = component.entity_link.getAPI("immersiverailroading:locomotivediesel")
  7. local thread = require("thread")
  8. require("PIDController")
  9. local serial = require("serialization")
  10. local fs = require("filesystem")
  11. local event = require("event")
  12. local modem = component.modem
  13.  
  14. local ATC = false
  15. local RUNNING = true
  16. local MODE = 3 -- 0 SET,
  17. local sleep = 1 -- seconds
  18. local display = false
  19. local iteration = 1
  20. local timer = nil
  21.  
  22. local CONFIG_FILE = "/home/CMD.dat"
  23. local CONFIG = nil
  24. local DEFAULT_CONFIG = {}
  25. local data = nil
  26.  
  27. local function saveConfig(file, cfg) -- Adapted from HandieAndy's code
  28.   local f = io.open(file, "w")
  29.   if f == nil then error("Couldn't open " .. file .. " to write config.") end
  30.   f:write(serial.serialize(cfg, 100000))
  31.   f:close()
  32.   print("Saved to file: ", CONFIG_FILE)
  33. end
  34.  
  35. local function loadConfig() -- Adapted from HandieAndy's code
  36.   local f = io.open(CONFIG_FILE, "r")
  37.   if f == nil then
  38.     return DEFAULT_CONFIG
  39.     else
  40.     local config = serial.unserialize(f:read("*a"))
  41.     f:close()
  42.     return config
  43.     end
  44. end
  45.    
  46. function GetTrainPos()
  47.     positions = entity.getLocation()
  48.     xpos = positions.getX()
  49.     ypos = positions.getY()
  50.     zpos = positions.getZ()
  51.     print("X:   ", xpos)
  52.     print("Z:   ", zpos)
  53. end
  54.  
  55. function GetTrainSpeed()
  56.     speed = entity.getCurrentSpeed()
  57.     return speed
  58. end
  59.  
  60. function GetUUID()
  61.     uuid = entity.getUUID()
  62.     return uuid
  63. end
  64.  
  65. function setThrottle(x)
  66.     if 0 >= x then x = 0 end
  67.     if 1 <= x then x = 1 end
  68.     entity.setThrottleLevel(x)
  69.     if display then
  70.         print("Throttle: ", x)
  71.         end
  72. end
  73.  
  74. function setBrake(x)
  75.     if 0 >= x then x = 0 end
  76.     if 1 <= x then x = 1 end
  77.     entity.setAirBrakeLevel(x)
  78.     if display then
  79.         print("Brake:   ", x)
  80.         end
  81. end
  82.  
  83. local function main()
  84.     while RUNNING do
  85.         os.sleep(sleep)
  86.         input = GetTrainSpeed()
  87.         if data ~= nil then
  88.             if data[uuid] ~= nil then
  89.                 setpoint = data[uuid].setspeed
  90.                 print(setpoint)
  91.                 PID:SetPoint(setpoint)
  92.                 end
  93.             end
  94.         PID:SetInput(input)
  95.         PID:Compute()
  96.         output = PID:GetOutput()
  97.         BrakeOut = output * -1
  98.         setThrottle(output)
  99.         setBrake(BrakeOut)
  100.         end
  101.   end
  102.  
  103. function stop()
  104.   RUNNING = false
  105. end
  106.    
  107. local function handleMessage(host, msg)
  108.   local packet = serial.unserialize(msg)
  109.   if packet == nil then
  110.     print("nil packet")
  111.     return
  112.   end
  113.   if packet.t == "STATUS" then
  114.         --print(packet.v)
  115.         data = packet.v
  116.   else
  117.     print("unknown command")
  118.   end
  119. end
  120.  
  121. function start()
  122.     modem.open(54421)
  123.     while RUNNING do
  124.       local eventName, p1, p2, p3, p4, p5 = event.pull()
  125.       if eventName == "modem_message" then
  126.         --print("recv")
  127.         handleMessage(p2, p5)
  128.       end
  129.     end
  130.     modem.close(54421)
  131. end
  132.  
  133. ----------- Main loop ------------ 
  134.    
  135. setpoint = 0
  136. PID:new(0.01, 0.5, 0.0001, 0)
  137. PID:SetInput((GetTrainSpeed()))
  138. PID:SetOutput(0)
  139. PID:SetPoint(setpoint)
  140. PID:SetOutputLimits(-1, 1)
  141. PID:SetMode(1)
  142. uuid = GetUUID()
  143.  
  144. --main()
  145. local td = thread.create(start)
  146. local ta = thread.create(main)
  147. ta:join()
  148. td:join()
  149.  
Add Comment
Please, Sign In to add comment