|
VR-Engage
2.2
|
This class implements a PID controller with additional features like feed-forward control, output ramping, setpoint limiting, and output filtering to prevent oscillations. It provides methods for tuning individual parameters and configuring various aspects of the controller's behavior.
#include <pid.h>
Public Member Functions | |
| MiniPID (double p, double i, double d) | |
| MiniPID (double p, double i, double d, double f) | |
| void | setP (double p) |
| void | setI (double i) |
| void | setD (double d) |
| void | setF (double f) |
| void | setPID (double p, double i, double d) |
| void | setPID (double p, double i, double d, double f) |
| void | setMaxIOutput (double maximum) |
| void | setOutputLimits (double output) |
| void | setOutputLimits (double minimum, double maximum) |
| void | setDirection (bool reversed) |
| void | setSetpoint (double setpoint) |
| void | reset () |
| void | setOutputRampRate (double rate) |
| void | setSetpointRange (double range) |
| void | setOutputFilter (double strength) |
| double | getOutput () |
| double | getOutput (double actual) |
| double | getOutput (double actual, double setpoint) |
Private Member Functions | |
| double | clamp (double value, double min, double max) |
| bool | bounded (double value, double min, double max) |
| void | checkSigns () |
| void | init () |
Private Attributes | |
| double | P |
| double | I |
| double | D |
| double | F |
| double | maxIOutput |
| double | maxError |
| double | errorSum |
| double | maxOutput |
| double | minOutput |
| double | setpoint |
| double | lastActual |
| bool | firstRun |
| bool | reversed |
| double | outputRampRate |
| double | lastOutput |
| double | outputFilter |
| double | setpointRange |
| makVre::MiniPID::MiniPID | ( | double | p, |
| double | i, | ||
| double | d ) |
Constructor with P, I, and D parameters.
| p | Proportional gain coefficient |
| i | Integral gain coefficient |
| d | Derivative gain coefficient |
| makVre::MiniPID::MiniPID | ( | double | p, |
| double | i, | ||
| double | d, | ||
| double | f ) |
Constructor with P, I, D, and F parameters.
| p | Proportional gain coefficient |
| i | Integral gain coefficient |
| d | Derivative gain coefficient |
| f | Feed-forward gain coefficient |
| void makVre::MiniPID::setP | ( | double | p | ) |
Sets the proportional gain coefficient.
This parameter responds quickly to changes in setpoint and provides most of the initial driving force to make corrections. For position-based controllers, this is the first parameter to tune, with I second.
| p | New proportional gain value |
| void makVre::MiniPID::setI | ( | double | i | ) |
Sets the integral gain coefficient.
This parameter is used for overcoming disturbances and ensuring that the controller always reaches the setpoint. Typically tuned second for position-based modes, and third for rate-based modes.
| i | New integral gain value |
| void makVre::MiniPID::setD | ( | double | d | ) |
Sets the derivative gain coefficient.
This parameter responds quickly to large changes in error and helps prevent overshoot. Small values prevent the P and I terms from causing overshoot.
| d | New derivative gain value |
| void makVre::MiniPID::setF | ( | double | f | ) |
Sets the feed-forward gain coefficient.
This parameter is excellent for velocity, rate, and other continuous control modes where you can expect a rough output value based solely on the setpoint. Should not be used in position-based control modes.
| f | New feed-forward gain value |
| void makVre::MiniPID::setPID | ( | double | p, |
| double | i, | ||
| double | d ) |
Sets all three standard PID parameters at once.
| p | New proportional gain value |
| i | New integral gain value |
| d | New derivative gain value |
| void makVre::MiniPID::setPID | ( | double | p, |
| double | i, | ||
| double | d, | ||
| double | f ) |
Sets all four PID parameters including feed-forward at once.
| p | New proportional gain value |
| i | New integral gain value |
| d | New derivative gain value |
| f | New feed-forward gain value |
| void makVre::MiniPID::setMaxIOutput | ( | double | maximum | ) |
Sets the maximum output value contributed by the I component.
This can be used to prevent large windup issues and make tuning simpler.
| maximum | Maximum I component contribution (in the same units as the output) |
References maximum().
| void makVre::MiniPID::setOutputLimits | ( | double | output | ) |
Sets symmetric output limits (from -output to +output)
| output | Maximum output magnitude (minimum is set to -output) |
| void makVre::MiniPID::setOutputLimits | ( | double | minimum, |
| double | maximum ) |
| void makVre::MiniPID::setDirection | ( | bool | reversed | ) |
Sets the direction of the PID controller.
| reversed | If true, reverses the PID output |
References reversed.
| void makVre::MiniPID::setSetpoint | ( | double | setpoint | ) |
Sets the target setpoint for the PID calculations.
| setpoint | The desired target value |
References setpoint.
| void makVre::MiniPID::reset | ( | ) |
Resets the controller state.
Erases the integral term buildup and removes derivative gain on the next loop.
| void makVre::MiniPID::setOutputRampRate | ( | double | rate | ) |
Sets the maximum rate the output can increase per cycle.
| rate | Maximum change in output per iteration |
| void makVre::MiniPID::setSetpointRange | ( | double | range | ) |
Sets a limit on how far the setpoint can be from the current position.
This can simplify tuning by allowing tuning over a small range to apply to a much larger range. It limits the reactivity of the P term and restricts the impact of large D term during large setpoint adjustments. May increase lag and I term if range is too small.
| range | Maximum allowed setpoint distance from the current position |
| void makVre::MiniPID::setOutputFilter | ( | double | strength | ) |
Sets a filter on the output to reduce sharp oscillations.
A value of 0.1 is a good starting point. Larger values smooth P and D oscillations, but may force larger I values.
| strength | Filter strength between 0 and 1 (0 = no filter, 1 = infinite filter) |
| double makVre::MiniPID::getOutput | ( | ) |
Calculates the PID output using the last provided setpoint and actual values.
| double makVre::MiniPID::getOutput | ( | double | actual | ) |
Calculates the PID output for a new actual value and the stored setpoint.
| actual | Current measured value |
| double makVre::MiniPID::getOutput | ( | double | actual, |
| double | setpoint ) |
Calculates the PID output for new actual and setpoint values.
This is the main calculation function. It computes all PID terms and applies all configured constraints.
| actual | Current measured value |
| setpoint | Desired target value |
References setpoint.
|
private |
Clamps a value between minimum and maximum bounds.
| value | Value to clamp |
| min | Minimum allowed value |
| max | Maximum allowed value |
|
private |
Checks if a value is within bounds (exclusive)
| value | Value to check |
| min | Minimum bound (exclusive) |
| max | Maximum bound (exclusive) |
|
private |
Ensures all PID parameters have the correct sign.
The correct sign depends on the 'reversed' setting
|
private |
Initializes controller parameters to default values.
|
private |
Proportional gain coefficient.
|
private |
Integral gain coefficient.
|
private |
Derivative gain coefficient.
|
private |
Feed-forward gain coefficient.
|
private |
Maximum output value contributed by the I component.
|
private |
Maximum error value used for I term calculation.
|
private |
Sum of errors for the I term.
|
private |
Maximum allowed output value.
|
private |
Minimum allowed output value.
|
private |
Target setpoint value.
Referenced by getOutput(), and setSetpoint().
|
private |
Last measured input value (used for D term)
|
private |
Flag indicating if this is the first execution.
|
private |
Flag indicating if the controller direction is reversed.
Referenced by setDirection().
|
private |
Maximum rate the output can increase per cycle.
|
private |
Last calculated output value.
|
private |
Output filter strength (0-1)
|
private |
Maximum allowed setpoint distance from the current position.