Advertisement
Gazer29

SpeedControlv1

Feb 15th, 2021 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.48 KB | None | 0 0
  1. --SpeedControlv1 by Gazer29
  2. --Uses Automation mod version: Alpha 7
  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.  
  10. local RUNNING = true
  11. local MODE = 0 -- 0 SET, 1 RAMP,
  12. local sleep = 1 -- seconds
  13. local display = false
  14.  
  15.  
  16. function helpdisplay()
  17.     print("Speed Control v1")
  18.     print("Commands:")
  19.     print("'Any number'    - Changes set speed")
  20.     print("'+'      - Displays additional information")
  21.     print("'-'      - Hides additional information")
  22.     print("'ramp'       - Changes to ramp mode, requires further input")
  23.     print("'idle'       - Pauses train control")
  24.     print("'stop'       - Ends program")
  25. end
  26.    
  27. function GetTrainPos()
  28.     positions = entity.getLocation()
  29.     xpos = positions.getX()
  30.     ypos = positions.getY()
  31.     zpos = positions.getZ()
  32.     print("X: ", xpos)
  33.     print("Z: ", zpos)
  34. end
  35.  
  36. function GetTrainSpeed()
  37.     speed = entity.getCurrentSpeed()
  38.     return speed
  39. end
  40.  
  41. function setThrottle(x)
  42.     if 0 >= x then x = 0 end
  43.     if 1 <= x then x = 1 end
  44.     entity.setThrottleLevel(x)
  45.     if display then
  46.         print("Throttle: ", x)
  47.         end
  48. end
  49.  
  50. function setBrake(x)
  51.     if 0 >= x then x = 0 end
  52.     if 1 <= x then x = 1 end
  53.     entity.setAirBrakeLevel(x)
  54.     if display then
  55.         print("Brake:   ", x)
  56.         end
  57. end
  58.  
  59. local function reads()
  60.   while RUNNING do
  61.     user = nil
  62.     user = io.read()
  63.     if user == "stop" then 
  64.         print("STOPPING...")
  65.         os.sleep(1)
  66.         stop()
  67.         end
  68.     if user == "." then
  69.         term.clear()
  70.         end
  71.     if user == "idle" then
  72.         MODE = 2
  73.         end
  74.     if user == "ramp" then
  75.         print("Start Speed (type 'x' for current setpoint):")
  76.         c = io.read()
  77.         if c == "x" then
  78.             c = setpoint
  79.             end
  80.         print("End Speed:")
  81.         rampStart = c
  82.         c = io.read()
  83.         print("Ramp Time (seconds):")
  84.         rampEnd = c
  85.         c = io.read()
  86.         rampTime = c
  87.         rampDiff = rampEnd - rampStart
  88.         rampCount = math.floor(rampTime/sleep)
  89.         rampInc = rampDiff / rampCount
  90.         PID:SetPoint(rampStart)
  91.         setpoint = rampStart
  92.         MODE = 1
  93.         end
  94.     if user == "+" then
  95.         display = true
  96.         term.clear()
  97.         end
  98.     if user == "-" then
  99.         display = false
  100.         term.clear()
  101.         os.sleep(1)
  102.         helpdisplay()
  103.         end
  104.     if nil ~= tonumber(user) then
  105.         MODE = 0
  106.         PID:SetPoint(user)
  107.         setpoint = user
  108.         print("Setting Speed: ", user)
  109.         end
  110.     os.sleep(1)
  111.   end
  112. end
  113.  
  114. local function main()
  115.     while RUNNING do
  116.         input = GetTrainSpeed()
  117.         if MODE ~= 2 then
  118.             if MODE == 1 then
  119.                 if rampCount > 0 then
  120.                   rampCount = rampCount - 1
  121.                   setpoint = setpoint + rampInc
  122.                   PID:SetPoint(setpoint)
  123.                   if rampCount == 0 then
  124.                     setpoint = rampEnd
  125.                     PID:SetPoint(rampEnd)
  126.                     end
  127.                   else
  128.                   Mode = 0
  129.                   end
  130.                 end
  131.             PID:SetInput(input)
  132.             PID:Compute()
  133.             output = PID:GetOutput()
  134.             BrakeOut = output * -1
  135.             setThrottle(output)
  136.             setBrake(BrakeOut)
  137.             end
  138.         if display then
  139.             print("CurrentSpeed: ", input)
  140.             if MODE ~= 2 then
  141.                 print("SetPoint: ", setpoint)
  142.                 end
  143.             GetTrainPos()
  144.             print("----")
  145.             os.sleep(sleep)
  146.             term.clear()
  147.             else
  148.             os.sleep(sleep)
  149.             end
  150.  
  151.         end
  152.   end
  153.  
  154. function stop()
  155.   RUNNING = false
  156. end
  157.  
  158. ----------- Main loop ------------ 
  159.    
  160. term.clear()
  161. setpoint = 0
  162. PID:new(0.01, 0.5, 0.0001, 0)
  163. PID:SetInput((GetTrainSpeed()))
  164. PID:SetOutput(0)
  165. PID:SetPoint(setpoint)
  166. PID:SetOutputLimits(-1, 1)
  167. PID:SetMode(1)
  168.  
  169. helpdisplay()
  170.  
  171. local tb = thread.create(main)
  172. local ta = thread.create(reads)
  173. tb:join()
  174. ta:join()
  175. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement