Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- airship_control.lua
- -- Modes:
- -- F = flight
- -- V = vertical anchor (hold Y only)
- -- A = full anchor (hold X/Y/Z)
- -- Q = quit
- --
- -- Works with CC:VS direct force control.
- if not ship then
- error("No ship API found. Put this computer on a VS ship.")
- end
- local running = true
- local mode = "flight" -- flight / vertical / full
- local status = "Ready"
- local anchorY = nil
- local anchorPos = nil
- local CFG = {
- gravityBias = 10.00,
- -- Vertical mode
- Py_v = 3.2,
- Dy_v = 6.2,
- maxAccelY_v = 10.0,
- -- Full anchor mode
- Pxy = 2.0,
- Dxy = 3.8,
- Py_f = 3.2,
- Dy_f = 6.2,
- maxAccelXY = 8.0,
- maxAccelY_f = 10.0,
- fallbackDt = 0.05,
- }
- local last = {
- pos = vector.new(0,0,0),
- vel = vector.new(0,0,0),
- mass = 0,
- err = vector.new(0,0,0),
- accel = vector.new(0,0,0),
- force = vector.new(0,0,0),
- }
- local function clamp(v, mn, mx)
- if v < mn then return mn end
- if v > mx then return mx end
- return v
- end
- local function clear()
- term.setCursorPos(1, 1)
- term.clear()
- end
- local function setFlight()
- mode = "flight"
- anchorY = nil
- anchorPos = nil
- status = "Flight mode"
- end
- local function setVerticalAnchor()
- local pos = ship.getWorldspacePosition()
- anchorY = pos.y
- anchorPos = nil
- mode = "vertical"
- status = ("Vertical anchor set at Y %.2f"):format(anchorY)
- end
- local function setFullAnchor()
- local pos = ship.getWorldspacePosition()
- anchorPos = vector.new(pos.x, pos.y, pos.z)
- anchorY = pos.y
- mode = "full"
- status = ("Full anchor set at %.2f / %.2f / %.2f"):format(anchorPos.x, anchorPos.y, anchorPos.z)
- end
- local function draw()
- clear()
- print("== Airship Control ==")
- print("Mode : " .. mode)
- if anchorY then
- print(("Anchor Y: %.2f"):format(anchorY))
- else
- print("Anchor Y: nil")
- end
- if anchorPos then
- print(("Anchor X: %.2f"):format(anchorPos.x))
- print(("Anchor Z: %.2f"):format(anchorPos.z))
- else
- print("Anchor X: nil")
- print("Anchor Z: nil")
- end
- print(("Pos X : %.2f"):format(last.pos.x))
- print(("Pos Y : %.2f"):format(last.pos.y))
- print(("Pos Z : %.2f"):format(last.pos.z))
- print(("Vel X : %.3f"):format(last.vel.x))
- print(("Vel Y : %.3f"):format(last.vel.y))
- print(("Vel Z : %.3f"):format(last.vel.z))
- print(("Mass : %.1f"):format(last.mass))
- print("")
- print("F = flight")
- print("V = hold current Y")
- print("A = full anchor here")
- print("Q = quit")
- print("")
- print("Status: " .. status)
- end
- local function physicsStep()
- local pos = ship.getWorldspacePosition()
- local vel = ship.getVelocity()
- local mass = ship.getMass()
- last.pos = pos
- last.vel = vel
- last.mass = mass
- last.err = vector.new(0,0,0)
- last.accel = vector.new(0,0,0)
- last.force = vector.new(0,0,0)
- if mode == "flight" then
- return
- end
- local ax, ay, az = 0, 0, 0
- if mode == "vertical" and anchorY then
- local errY = anchorY - pos.y
- ay = (CFG.Py_v * errY) - (CFG.Dy_v * vel.y)
- ay = clamp(ay, -CFG.maxAccelY_v, CFG.maxAccelY_v)
- last.err = vector.new(0, errY, 0)
- last.accel = vector.new(0, ay, 0)
- elseif mode == "full" and anchorPos then
- local err = anchorPos - pos
- ax = (CFG.Pxy * err.x) - (CFG.Dxy * vel.x)
- ay = (CFG.Py_f * err.y) - (CFG.Dy_f * vel.y)
- az = (CFG.Pxy * err.z) - (CFG.Dxy * vel.z)
- ax = clamp(ax, -CFG.maxAccelXY, CFG.maxAccelXY)
- ay = clamp(ay, -CFG.maxAccelY_f, CFG.maxAccelY_f)
- az = clamp(az, -CFG.maxAccelXY, CFG.maxAccelXY)
- last.err = err
- last.accel = vector.new(ax, ay, az)
- end
- local force = vector.new(
- mass * ax,
- mass * (CFG.gravityBias + ay),
- mass * az
- )
- ship.applyWorldForce(force)
- last.force = force
- end
- local function physicsLoop()
- while running do
- local ok = pcall(function()
- ship.pullPhysicsTicks()
- end)
- if not ok then
- sleep(CFG.fallbackDt)
- end
- local ok2, err = pcall(physicsStep)
- if not ok2 then
- status = "Physics error: " .. tostring(err)
- end
- draw()
- end
- end
- local function inputLoop()
- while running do
- local _, key = os.pullEvent("key")
- if key == keys.q then
- running = false
- status = "Stopping..."
- elseif key == keys.f then
- setFlight()
- elseif key == keys.v then
- setVerticalAnchor()
- elseif key == keys.a then
- setFullAnchor()
- end
- end
- end
- draw()
- parallel.waitForAny(physicsLoop, inputLoop)
- clear()
- print("Stopped.")
Advertisement
Add Comment
Please, Sign In to add comment