Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --SpeedControlv1 by Gazer29
- --Uses Automation mod version: Alpha 7
- local component = require("component")
- local term = require ("term")
- local entity = component.entity_link.getAPI("immersiverailroading:locomotivediesel")
- local thread = require("thread")
- require("PIDController")
- local RUNNING = true
- local MODE = 0 -- 0 SET, 1 RAMP,
- local sleep = 1 -- seconds
- local display = false
- function helpdisplay()
- print("Speed Control v1")
- print("Commands:")
- print("'Any number' - Changes set speed")
- print("'+' - Displays additional information")
- print("'-' - Hides additional information")
- print("'ramp' - Changes to ramp mode, requires further input")
- print("'idle' - Pauses train control")
- print("'stop' - Ends program")
- end
- function GetTrainPos()
- positions = entity.getLocation()
- xpos = positions.getX()
- ypos = positions.getY()
- zpos = positions.getZ()
- print("X: ", xpos)
- print("Z: ", zpos)
- end
- function GetTrainSpeed()
- speed = entity.getCurrentSpeed()
- return speed
- end
- function setThrottle(x)
- if 0 >= x then x = 0 end
- if 1 <= x then x = 1 end
- entity.setThrottleLevel(x)
- if display then
- print("Throttle: ", x)
- end
- end
- function setBrake(x)
- if 0 >= x then x = 0 end
- if 1 <= x then x = 1 end
- entity.setAirBrakeLevel(x)
- if display then
- print("Brake: ", x)
- end
- end
- local function reads()
- while RUNNING do
- user = nil
- user = io.read()
- if user == "stop" then
- print("STOPPING...")
- os.sleep(1)
- stop()
- end
- if user == "." then
- term.clear()
- end
- if user == "idle" then
- MODE = 2
- end
- if user == "ramp" then
- print("Start Speed (type 'x' for current setpoint):")
- c = io.read()
- if c == "x" then
- c = setpoint
- end
- print("End Speed:")
- rampStart = c
- c = io.read()
- print("Ramp Time (seconds):")
- rampEnd = c
- c = io.read()
- rampTime = c
- rampDiff = rampEnd - rampStart
- rampCount = math.floor(rampTime/sleep)
- rampInc = rampDiff / rampCount
- PID:SetPoint(rampStart)
- setpoint = rampStart
- MODE = 1
- end
- if user == "+" then
- display = true
- term.clear()
- end
- if user == "-" then
- display = false
- term.clear()
- os.sleep(1)
- helpdisplay()
- end
- if nil ~= tonumber(user) then
- MODE = 0
- PID:SetPoint(user)
- setpoint = user
- print("Setting Speed: ", user)
- end
- os.sleep(1)
- end
- end
- local function main()
- while RUNNING do
- input = GetTrainSpeed()
- if MODE ~= 2 then
- if MODE == 1 then
- if rampCount > 0 then
- rampCount = rampCount - 1
- setpoint = setpoint + rampInc
- PID:SetPoint(setpoint)
- if rampCount == 0 then
- setpoint = rampEnd
- PID:SetPoint(rampEnd)
- end
- else
- Mode = 0
- end
- end
- PID:SetInput(input)
- PID:Compute()
- output = PID:GetOutput()
- BrakeOut = output * -1
- setThrottle(output)
- setBrake(BrakeOut)
- end
- if display then
- print("CurrentSpeed: ", input)
- if MODE ~= 2 then
- print("SetPoint: ", setpoint)
- end
- GetTrainPos()
- print("----")
- os.sleep(sleep)
- term.clear()
- else
- os.sleep(sleep)
- end
- end
- end
- function stop()
- RUNNING = false
- end
- ----------- Main loop ------------
- term.clear()
- setpoint = 0
- PID:new(0.01, 0.5, 0.0001, 0)
- PID:SetInput((GetTrainSpeed()))
- PID:SetOutput(0)
- PID:SetPoint(setpoint)
- PID:SetOutputLimits(-1, 1)
- PID:SetMode(1)
- helpdisplay()
- local tb = thread.create(main)
- local ta = thread.create(reads)
- tb:join()
- ta:join()
- term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement