VR-Engage  2.2
Loading...
Searching...
No Matches
makVre::MiniPID Class Reference

Detailed Description

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
 

Constructor & Destructor Documentation

◆ MiniPID() [1/2]

makVre::MiniPID::MiniPID ( double p,
double i,
double d )

Constructor with P, I, and D parameters.

Parameters
pProportional gain coefficient
iIntegral gain coefficient
dDerivative gain coefficient

◆ MiniPID() [2/2]

makVre::MiniPID::MiniPID ( double p,
double i,
double d,
double f )

Constructor with P, I, D, and F parameters.

Parameters
pProportional gain coefficient
iIntegral gain coefficient
dDerivative gain coefficient
fFeed-forward gain coefficient

Member Function Documentation

◆ setP()

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.

Parameters
pNew proportional gain value

◆ setI()

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.

Parameters
iNew integral gain value

◆ setD()

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.

Parameters
dNew derivative gain value

◆ setF()

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.

Parameters
fNew feed-forward gain value

◆ setPID() [1/2]

void makVre::MiniPID::setPID ( double p,
double i,
double d )

Sets all three standard PID parameters at once.

Parameters
pNew proportional gain value
iNew integral gain value
dNew derivative gain value

◆ setPID() [2/2]

void makVre::MiniPID::setPID ( double p,
double i,
double d,
double f )

Sets all four PID parameters including feed-forward at once.

Parameters
pNew proportional gain value
iNew integral gain value
dNew derivative gain value
fNew feed-forward gain value

◆ setMaxIOutput()

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.

Parameters
maximumMaximum I component contribution (in the same units as the output)

References maximum().

◆ setOutputLimits() [1/2]

void makVre::MiniPID::setOutputLimits ( double output)

Sets symmetric output limits (from -output to +output)

Parameters
outputMaximum output magnitude (minimum is set to -output)

◆ setOutputLimits() [2/2]

void makVre::MiniPID::setOutputLimits ( double minimum,
double maximum )

Sets asymmetric output limits.

Parameters
minimumMinimum output value
maximumMaximum output value

References maximum(), and minimum().

◆ setDirection()

void makVre::MiniPID::setDirection ( bool reversed)

Sets the direction of the PID controller.

Parameters
reversedIf true, reverses the PID output

References reversed.

◆ setSetpoint()

void makVre::MiniPID::setSetpoint ( double setpoint)

Sets the target setpoint for the PID calculations.

Parameters
setpointThe desired target value

References setpoint.

◆ reset()

void makVre::MiniPID::reset ( )

Resets the controller state.

Erases the integral term buildup and removes derivative gain on the next loop.

◆ setOutputRampRate()

void makVre::MiniPID::setOutputRampRate ( double rate)

Sets the maximum rate the output can increase per cycle.

Parameters
rateMaximum change in output per iteration

◆ setSetpointRange()

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.

Parameters
rangeMaximum allowed setpoint distance from the current position

◆ setOutputFilter()

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.

Parameters
strengthFilter strength between 0 and 1 (0 = no filter, 1 = infinite filter)

◆ getOutput() [1/3]

double makVre::MiniPID::getOutput ( )

Calculates the PID output using the last provided setpoint and actual values.

Returns
Calculated output value

◆ getOutput() [2/3]

double makVre::MiniPID::getOutput ( double actual)

Calculates the PID output for a new actual value and the stored setpoint.

Parameters
actualCurrent measured value
Returns
Calculated output value

◆ getOutput() [3/3]

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.

Parameters
actualCurrent measured value
setpointDesired target value
Returns
Calculated output value to drive the actual to the setpoint

References setpoint.

◆ clamp()

double makVre::MiniPID::clamp ( double value,
double min,
double max )
private

Clamps a value between minimum and maximum bounds.

Parameters
valueValue to clamp
minMinimum allowed value
maxMaximum allowed value
Returns
Clamped value

◆ bounded()

bool makVre::MiniPID::bounded ( double value,
double min,
double max )
private

Checks if a value is within bounds (exclusive)

Parameters
valueValue to check
minMinimum bound (exclusive)
maxMaximum bound (exclusive)
Returns
true if min < value < max

◆ checkSigns()

void makVre::MiniPID::checkSigns ( )
private

Ensures all PID parameters have the correct sign.

The correct sign depends on the 'reversed' setting

◆ init()

void makVre::MiniPID::init ( )
private

Initializes controller parameters to default values.

Member Data Documentation

◆ P

double makVre::MiniPID::P
private

Proportional gain coefficient.

◆ I

double makVre::MiniPID::I
private

Integral gain coefficient.

◆ D

double makVre::MiniPID::D
private

Derivative gain coefficient.

◆ F

double makVre::MiniPID::F
private

Feed-forward gain coefficient.

◆ maxIOutput

double makVre::MiniPID::maxIOutput
private

Maximum output value contributed by the I component.

◆ maxError

double makVre::MiniPID::maxError
private

Maximum error value used for I term calculation.

◆ errorSum

double makVre::MiniPID::errorSum
private

Sum of errors for the I term.

◆ maxOutput

double makVre::MiniPID::maxOutput
private

Maximum allowed output value.

◆ minOutput

double makVre::MiniPID::minOutput
private

Minimum allowed output value.

◆ setpoint

double makVre::MiniPID::setpoint
private

Target setpoint value.

Referenced by getOutput(), and setSetpoint().

◆ lastActual

double makVre::MiniPID::lastActual
private

Last measured input value (used for D term)

◆ firstRun

bool makVre::MiniPID::firstRun
private

Flag indicating if this is the first execution.

◆ reversed

bool makVre::MiniPID::reversed
private

Flag indicating if the controller direction is reversed.

Referenced by setDirection().

◆ outputRampRate

double makVre::MiniPID::outputRampRate
private

Maximum rate the output can increase per cycle.

◆ lastOutput

double makVre::MiniPID::lastOutput
private

Last calculated output value.

◆ outputFilter

double makVre::MiniPID::outputFilter
private

Output filter strength (0-1)

◆ setpointRange

double makVre::MiniPID::setpointRange
private

Maximum allowed setpoint distance from the current position.


The documentation for this class was generated from the following file: