ClosedLoop module
Implements a PID algorithm, with integral saturation limits
Allows for a different time period to be used each update, so the algorithm does not have to run with constant timing.
- class ClosedLoop.ClosedLoop(Kp, Ki, Kd, lowerSatLimit, upperSatLimit)
Bases:
object
PID Controller with changeable setpoint and gains.
Does not need to run at a set frequency, adjust gains based on user-input time deltas.
- Parameters:
Kp (float) – value of proportional gain
Ki (float) – value of integral gain
Kd (float) – value of derivative gain
lowerSatLimit (int or long) – Lower saturation limit for I path.
upperSatLimit (int ot long) – Upper saturation limit for I path.
- calculateEfforts(currentValue, msTimePassed)
Calculates P, I, D efforts
Saturated according to set saturation bounds. Use this in an interrupt routiene that is called at the same period according to samplePeriod.
- Parameters:
currentValue (float) – Current “actual” value used to calculate error.
msTimePassed (float) – Time passed in ms since function last called. Used to calculate I and D efforts.
- Returns:
[P effort, I effort, D effort]. The effort itself is not saturated.
- Return type:
list of float
- clearIntegral()
Sets integrated error to 0 but does not turn I path off
To disable the I path, use SetKi to change the I gain to 0
- getError()
Returns list of errors used to caluclate P, I, and D efforts.
- Returns:
[currentError, integratedError, rateOfError] list of errors according to each path.
- Return type:
list of floats
- getTarget()
Returns target value in-use by PID
- Returns:
Target value
- Return type:
float
- setKd(Kd)
Sets D gain.
- Parameters:
Kd (float) – D gain.
- setKi(Ki)
Sets I gain.
Does not affect integrated error.
- Parameters:
Ki (float) – I gain.
- setKp(Kp)
Sets P gain
- Parameters:
Kp (float) – P gain
- setSatLower(lowerSatLimit)
Sets the lower saturation limit
- Parameters:
lowerSatLimit (float) – lower saturation limit for integral error. Not a limit on the magnitude of the I effort.
- setSatUpper(upperSatLimit)
Sets the upper saturation limit
- Parameters:
upperSatLimit (float) – upper saturation limit for integral error. Not a limit on the magnitude of the I effort.
- setTarget(target)
Changes target value.
Does not change I path.