So, I was messing about with CC: VS’s Lua scripts and decided to make my own new PID library!
This library supports scalar, vector, and quaternion PID controllers, both discrete and continuous
It also supports output clamping and integral limiting and has a tidy string version via tostring
Methods
pid.new(target:number|vector|quaterion[,p:number[,i:number[,d:number[,discrete:boolean]]]]): pid
This method creates a new PID instance with the target being the setpoint
P defaults to 1, I and D default to 0 and discrete defaults to true
pid:step(value:number|vector|quaternion[,dt:number]): number|vector|quaternion
This method advances the PID a single step
What value accepts depends on what settarget was
dt is the time since the last step, defaults to 1
pid:clampOutput([min:number, max:number])
This method clamps the control output
If no arguments are provided, the clamp is removed
pid:limitIntegral([min:number, max:number])
This method limits the integral for anti-windup
If no arguments are provided, the limits are removed
Altitude PID Example
local pid = require("pid")
local thruster = peripheral.wrap("bottom")
thruster.setPower(0)
local target = 70
local control = pid.new(target, 2, 0, 0, true)
control:clampOutput(0, 15)
print(control)
while true do
local current = ship.getWorldspacePosition().y
local output = control:step(current)
print(output)
thruster.setPower(output)
os.sleep(0.01)
end