Skip to content

Instantly share code, notes, and snippets.

@quonic
Last active February 16, 2022 22:59
Show Gist options
  • Save quonic/6d8b93600b3c4d593f981e4573a69e2a to your computer and use it in GitHub Desktop.
Save quonic/6d8b93600b3c4d593f981e4573a69e2a to your computer and use it in GitHub Desktop.
A simple 2 line(when compiled) PID for the game StarBase, with "simplified" tuning instructions.
// Approach Example.nolol
// PID settings
start>
KP=0.3 //Some value you need to come up (see tuning section below)
KI=0.01 //Some value you need to come up (see tuning section below), not always needed leave at 1 if not used
KD=4 //Some value you need to come up (see tuning section below), not always needed leave at 1 if not used
bias=0
desired_value=10 // Set to what you want your desired value to be, AKA Set Point
IterationTime=0.2*2 // 0.2*number of line in compiled yolol code
ErrorPrior=0
IntergralPrior=0
macro InvokePid(Input) block
err=desired_value-Input
integral=IntergralPrior+err*IterationTime
derivative=(err-ErrorPrior)/IterationTime
Output=KP*err+KI*integral+KD*derivative+bias
:FcuF=Output
:FcuB=-Output // Changes negative to positive
IterationTime=IterationTime
ErrorPrior=err
IntergralPrior=integral
end
if :Ap==0 then
goto start
end
InvokePid(:RFD)
// StarBasePID.nolol
// Based on: http://robotsforroboticists.com/pid-control/
////// Tuning //////
////// KP //////
// KP = The proportional term is your primary term for controlling the error
// KP to big = weird oscillations that severely overshoot the desired value
// KP to small = you might never minimize the error and not be able to respond
// to changes affecting your system, unless you are using KD and KI terms
////// KI //////
// KI = The integral term lets the controller handle errors that are accumulating over time.
// This is good when you need to handle errors steady state errors.
// KI to big = you are trying to correct error over time so it can interfere with
// your response for dealing with current changes
// KI Note : This term is often the cause of instability in your PID controller.
//
////// KD //////
// KD = The derivative term is looking at how your system is behaving between time intervals.
// This helps dampen your system to improve stability.
// Many motor controllers will only let you configure a PI controller.
// In some cases this can be negative.
// PID settings
KP=1 //Some value you need to come up (see tuning section below)
KI=1 //Some value you need to come up (see tuning section below), not always needed leave at 1 if not used
KD=1 //Some value you need to come up (see tuning section below), not always needed leave at 1 if not used
bias=0
desired_value=10 // Set to what you want your desired value to be, AKA Set Point
IterationTime=0.2*2 // 0.2*number of line in compiled yolol code
ErrorPrior=0
IntergralPrior=0
macro InvokePid(Input, Output) block
err=desired_value-Input
integral=IntergralPrior+err*IterationTime
derivative=(err-ErrorPrior)/IterationTime
Output=KP*err+KI*integral+KD*derivative+bias
IterationTime=IterationTime
ErrorPrior=err
IntergralPrior=integral
end
InvokePid(:MyInput,:MyOutput)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment