ART v1.0-alpha
A Robot Template that raises the floor for VRC teams
Loading...
Searching...
No Matches
art::PID Class Reference

A general use PID class. More...

#include <PID.h>

Public Member Functions

 PID ()
 Constructs a new PID object.
 
void reset ()
 Resets the PID object.
 
void setConstants (double kp, double ki, double kd)
 Sets the PID constants.
 
PIDwithConstants (double kp, double ki, double kd)
 Sets the PID constans and returns a refrence to the PID object.
 
void setConstants (double kp, double ki, double kd, double ff)
 Sets the PID constants.
 
PIDwithConstants (double kp, double ki, double kd, double ff)
 Sets the PID constans and returns a refrence to the PID object.
 
double getkp ()
 Returns the value of m_kp.
 
double getki ()
 Returns the value of m_ki.
 
double getkd ()
 Returns the value of m_kd.
 
double getff ()
 Returns the value of m_ff.
 
void setIntegralZone (double integralZone)
 Sets the range within which the error will be applied to the integral term.
 
PIDwithIntegralZone (double integralZone)
 Set range within which the error will be applied to the integral term and returns a PID refrence.
 
void setTimeout (double timeout)
 Sets the timeout duration.
 
PIDwithTimeout (double timeout)
 Sets the timeout duration and returns a refrence to the PID object.
 
void setSettleZone (double settleZone)
 Set the range within which the PID is considered settled.
 
PIDwithSettleZone (double settleZone)
 Set the range within which the PID is considered settled and returns a refrence to the PID object.
 
void setSettleTimeout (double settleTimeout)
 Set the duration the PID must be settled for before being complete.
 
PIDwithSettleTimeout (double settleTimeout)
 Set the duration the PID must be settled for before being complete.
 
double calculate (double error)
 Runs the PID by providing an input, then calculates and returns an output.
 
double calculate (double target, double feedback)
 An alternate call to calculate(double error) that seperates the target and feedback for ease of use.
 
bool isCompleted ()
 Gets the current state of the PID.
 
double timePassed ()
 Gets the amoount of time passed since the PID started.
 
double settledTimePassed ()
 Gets the amoount of time the PID has been settled for.
 
double getProportional ()
 Get the output produced by the Proportional(P) term.
 
double getIntegral ()
 Get the output produced by the Integral(I) term.
 
double getDerivative ()
 Get the output produced by the Derivative(D) term.
 

Private Attributes

double m_error {0}
 The error last fed into the PID controller.
 
double m_prevError {0}
 The error fed into the PID controller on it's previous iteration.
 
double m_derivative {0}
 The derivate calculated by the PID controller.
 
double m_integral {0}
 The integral caluculated by the PID controller.
 
double m_integralZone {0}
 The range within which the error is applied to calculate the integral.
 
double m_kp {0}
 A constant applied to the Proportional term.
 
double m_ki {0}
 A constant applied to the Integral term.
 
double m_kd {0}
 A constant applied to the Derivative term.
 
double m_ff {0}
 A constant applied directly to the output.
 
TimePoint m_startTime
 A point in time representing when the PID started.
 
double m_timeout {0}
 The duration the PID should run for before it is considered finished.
 
TimePoint m_startSettledTime
 A point in time representing when the PID first entered the acceptable error range.
 
double m_settleTimeout {0}
 The duration the error must be within before the PID is considered complete.
 
double m_settleZone {0}
 The range which the error must be for the PID to be considered settled.
 

Detailed Description

A general use PID class.

A PID controller class. The class bundles all the functionality of a PID controller into a single class so it can simply be fed inputs and output values back.

Most often, the PID class will be used like this:

art::PID pid = art::PID().withConstants(2, 10, -5)
while( pid.isCompleted() == false ){
double out = pid.calculate( input, target );
// do soemthing with output
vex::wait(20, vex::msec);
}
A general use PID class.
Definition PID.h:59
PID & withConstants(double kp, double ki, double kd)
Sets the PID constans and returns a refrence to the PID object.
Definition PID.cpp:43
PID & withIntegralZone(double integralZone)
Set range within which the error will be applied to the integral term and returns a PID refrence.
Definition PID.cpp:67
double calculate(double error)
Runs the PID by providing an input, then calculates and returns an output.
Definition PID.cpp:94
bool isCompleted()
Gets the current state of the PID.
Definition PID.cpp:127
PID & withTimeout(double timeout)
Sets the timeout duration and returns a refrence to the PID object.
Definition PID.cpp:74
PID & withSettleZone(double settleZone)
Set the range within which the PID is considered settled and returns a refrence to the PID object.
Definition PID.cpp:81
PID & withSettleTimeout(double settleTimeout)
Set the duration the PID must be settled for before being complete.
Definition PID.cpp:88

First, the object is constructed and set up with certain parameters. Then, input values can be fed in from sensors or some other feedback device while feeding the output to a motor or some other thing to produce the desired outcome. This can be repeated until the conditions the PID is set to are complete, or simply as long as you want the PID to move toward it's target.

Definition at line 58 of file PID.h.

Constructor & Destructor Documentation

◆ PID()

art::PID::PID ( )

Constructs a new PID object.

Takes no parameters, so a new object can be constructed either by simply defining it or by defining an object and using the assignment operator (the equals sign =) to assign a newly constructed object to it.

art::PID pid; //declare a PID object
art::PID pid2 = art::PID(); //declare a PID object and assign a new PID to it

The second option is recommended because calling the constructor returns a refrence to a PID object. As long as a PID object refrence is on the right side of the equals sign, the PID object on the left will copy the attributes of the refrence. This allows methods that return a refrence to a PID obect to be chained to the constructor. These methods all start with "with" and can also be chained to each other.

Constructor with chained methods:

Note
The constants default to 1(m_kp), 0(m_ki), 0(m_kd) and 0(m_ff)

Definition at line 19 of file PID.cpp.

Member Function Documentation

◆ calculate() [1/2]

double art::PID::calculate ( double error)

Runs the PID by providing an input, then calculates and returns an output.

Parameters
errorthe PID's input, the target minus the current value
Returns
double the PID's output

This method is the heart of the PID class. It takes the error as an input and uses it to calculate each individual term before outputting the sum of the 3 terms. Calling this method updates several of the is the only was to update several of the member variables, so they will remain constant until calculate(double error) is called.

Both of the timeouts will continue to count down regaurdless of whether calculate is called, however the PID will not update whether it is settled or not unless it is fed values using calculate.

Definition at line 94 of file PID.cpp.

◆ calculate() [2/2]

double art::PID::calculate ( double target,
double feedback )

An alternate call to calculate(double error) that seperates the target and feedback for ease of use.

Parameters
targetthe value the PID should move toward
feedbackthe value representing the PID's current state or position
Returns
double the output of the PID

This method simply calls calculate(double error), but takes 2 parameters and calculates error to be the target minus the feedback. This method is to simply make it easier to ensure the calculations are done right by seperating and naming the target and the feedback to prevent flipping them.

Definition at line 122 of file PID.cpp.

◆ getDerivative()

double art::PID::getDerivative ( )

Get the output produced by the Derivative(D) term.

Returns
double (the output of the D term)

The D term of a PID controller is the change in error times a constant. Therefore, the output of the Proportional term here is m_derivative times m_kd, which is what this method returns

Definition at line 154 of file PID.cpp.

◆ getff()

double art::PID::getff ( )

Returns the value of m_ff.

Returns
double (the value of m_ff)

Definition at line 64 of file PID.cpp.

◆ getIntegral()

double art::PID::getIntegral ( )

Get the output produced by the Integral(I) term.

Returns
double (the output of the I term)

The I term of a PID controller is the sumation of the error over a period of time multiplied by a constant. Therefore, the output of the Integral term here is m_integral times m_ki, which is what this method returns.

Definition at line 153 of file PID.cpp.

◆ getkd()

double art::PID::getkd ( )

Returns the value of m_kd.

Returns
double (the value of m_kd)

Definition at line 63 of file PID.cpp.

◆ getki()

double art::PID::getki ( )

Returns the value of m_ki.

Returns
double (the value of m_ki)

Definition at line 62 of file PID.cpp.

◆ getkp()

double art::PID::getkp ( )

Returns the value of m_kp.

Returns
double (the value of m_kp)

Definition at line 61 of file PID.cpp.

◆ getProportional()

double art::PID::getProportional ( )

Get the output produced by the Proportional(P) term.

Returns
double (the output of the P term)

The P term of a PID controller is the error times a constant. Therefore, the output of the Proportional term here is m_error times m_kp, which is what this method returns.

Definition at line 152 of file PID.cpp.

◆ isCompleted()

bool art::PID::isCompleted ( )

Gets the current state of the PID.

Returns
bool - whether or not the PID is finished running

This method decides whether or not the PID is finished running using two checks. The first check is that the PID has been running for longer than the timeout specified by m_timeout. The second checks if the error has been within the range of m_settleZone for more than m_settleZoneTimeout seconds. If either of these conditions is true, the PID is considered completed and the method returns true.

Once the PID is considered complete, it can still be rendered incomplete if calculate is called with a new error outside the m_settleZone or it is reset() to be run again from scratch.

Definition at line 127 of file PID.cpp.

◆ reset()

void art::PID::reset ( )

Resets the PID object.

Once reset, the PID's derivative and integral terms are set back to 0. Both the error and previous-error are also set to 0 and both TimePoints are reset to the present.

By doing this, the PID will be ready to be used from a new starting state. If the PID is not reset, it may not function properly, as it may thing it is finished when it is not, or the integral term will persist and may cause the output to be unpredictable.

Definition at line 25 of file PID.cpp.

◆ setConstants() [1/2]

void art::PID::setConstants ( double kp,
double ki,
double kd )

Sets the PID constants.

Parameters
kpa new value for m_kp
kia new value for m_ki
kda new value for m_kd

Sets the values for m_kp, m_ki and m_kd using the parameters.

Definition at line 37 of file PID.cpp.

◆ setConstants() [2/2]

void art::PID::setConstants ( double kp,
double ki,
double kd,
double ff )

Sets the PID constants.

Parameters
kpa new value for m_kp
kia new value for m_ki
kda new value for m_kd
ffa new value for m_ff

Sets the values for m_kp, m_ki, m_kd, and ff using the parameters.

Definition at line 48 of file PID.cpp.

◆ setIntegralZone()

void art::PID::setIntegralZone ( double integralZone)

Sets the range within which the error will be applied to the integral term.

Parameters
integralZonea positive number representing the highest absolute value of the error applied to the integral term

This method sets m_integralZone which is the range of the error that the integral will be applied using. Setting the integral zone to 0 will always apply the error to the integral.

The inputted number should be positive beacuse it is compared with the absolute value of the error. If the integralZone is set to 5, the error will be applied when it is between -5 and 5.

Definition at line 66 of file PID.cpp.

◆ setSettleTimeout()

void art::PID::setSettleTimeout ( double settleTimeout)

Set the duration the PID must be settled for before being complete.

Parameters
settleTimeoutthe number of seconds the PID must be settled before being complete

This method sets m_settleTimeout. Once the PID is considered settled, a TimePoint is set and counts down until the settletimeout is reached, at which point the PID is complete and isComplete() returns true.

Definition at line 87 of file PID.cpp.

◆ setSettleZone()

void art::PID::setSettleZone ( double settleZone)

Set the range within which the PID is considered settled.

Parameters
settleZonethe value below which the error is considered settled

This method sets m_settleZone. While the absolute value of the error is less than or equal to this value, the settleTimeout will continue to count down. Once that time is up, the PID will be considered complete.

Definition at line 80 of file PID.cpp.

◆ setTimeout()

void art::PID::setTimeout ( double timeout)

Sets the timeout duration.

Parameters
timeoutthe number of seconds the PID should run for before timing out

This method sets m_timeout, which is the number of seconds the PID will run for before isCompleted() returns true.

Definition at line 73 of file PID.cpp.

◆ settledTimePassed()

double art::PID::settledTimePassed ( )

Gets the amoount of time the PID has been settled for.

Returns
double - the number of seconds the error has remained in the settleZone

This method returns the amount of time, in seconds, since the error was last outside the settleZone (m_settleZone). It keeps a TimePoint to this time and returns the time since then. If the PID is not settled, the time will be incredibly short (less than a few milliseconds) and can go back to being near 0 if the error exists the settleZone range.

Definition at line 146 of file PID.cpp.

◆ timePassed()

double art::PID::timePassed ( )

Gets the amoount of time passed since the PID started.

Returns
double - the number of seconds the PID has been running

This method returns the amount of time, in seconds, since the PID was constructed or last reset. This is calculated using the TimePoint m_startTime, which is set upon creation and updated using reset().

Definition at line 140 of file PID.cpp.

◆ withConstants() [1/2]

PID & art::PID::withConstants ( double kp,
double ki,
double kd )

Sets the PID constans and returns a refrence to the PID object.

Parameters
kpa new value for m_kp
kia new value for m_ki
kda new value for m_kd
Returns
PID& (a PID refrence)

Sets the values for m_kp, m_ki and m_kd using the parameters. Then, returns a refrence to the PID object, so that more methods can be chained and it can be assigned to an object using the assignment operator (=).

art::PID pid = art::PID().withConstants(2, 10, -5)

Definition at line 43 of file PID.cpp.

◆ withConstants() [2/2]

PID & art::PID::withConstants ( double kp,
double ki,
double kd,
double ff )

Sets the PID constans and returns a refrence to the PID object.

Parameters
kpa new value for m_kp
kia new value for m_ki
kda new value for m_kd
ffa new value for m_ff
Returns
PID& (a PID refrence)

Sets the values for m_kp, m_ki, m_kd, and ff using the parameters. Then, returns a refrence to the PID object, so that more methods can be chained and it can be assigned to an object using the assignment operator (=).

art::PID pid = art::PID().withConstants(2, 10, -5, 1)

Definition at line 55 of file PID.cpp.

◆ withIntegralZone()

PID & art::PID::withIntegralZone ( double integralZone)

Set range within which the error will be applied to the integral term and returns a PID refrence.

Parameters
integralZonea positive number representing the highest absolute value of the error applied to the integral term
Returns
PID& (a PID refrence)

Calls setIntegralZone(double integralZone), but also returns a refrence to the PID object, so that more methods can be chained and it can be assigned to an object using the assignment operator (=).

Definition at line 67 of file PID.cpp.

◆ withSettleTimeout()

PID & art::PID::withSettleTimeout ( double settleTimeout)

Set the duration the PID must be settled for before being complete.

Parameters
settleTimeoutthe number of seconds the PID must be settled before being complete
Returns
PID& (a PID refrence)

This method calls setSettleTimout(double settleTimout), but also returns a refrence to the PID object, so that more methods can be chained and it can be assigned to an object using the assignment operator (=).

Definition at line 88 of file PID.cpp.

◆ withSettleZone()

PID & art::PID::withSettleZone ( double settleZone)

Set the range within which the PID is considered settled and returns a refrence to the PID object.

Parameters
settleZonethe value below which the error is considered settled
Returns
PID& (a PID refrence)

This method calls setSettleZone(double settleZone), but also returns a refrence to the PID object, so that more methods can be chained and it can be assigned to an object using the assignment operator (=).

Definition at line 81 of file PID.cpp.

◆ withTimeout()

PID & art::PID::withTimeout ( double timeout)

Sets the timeout duration and returns a refrence to the PID object.

Parameters
timeoutthe number of seconds the PID should run for before timing out
Returns
PID& (a PID refrence)

This method calls setTimeout(double timeout), but also returns a refrence to the PID object, so that more methods can be chained and it can be assigned to an object using the assignment operator (=).

art::PID pid = art::PID().withConstants(2, 10, -5)

Definition at line 74 of file PID.cpp.

Member Data Documentation

◆ m_derivative

double art::PID::m_derivative {0}
private

The derivate calculated by the PID controller.

Calculated using the error and previous error, the derivative represents the change in error.

The D term in PID is the Derivative multiplied by the constant.

Definition at line 466 of file PID.h.

◆ m_error

double art::PID::m_error {0}
private

The error last fed into the PID controller.

Stores the current error of the PID controller. This is calculated and stored when calculate(double target, double feedback) is called or simply stored from calculate(double error);

Definition at line 445 of file PID.h.

◆ m_ff

double art::PID::m_ff {0}
private

A constant applied directly to the output.

A constant applied directly to the output, meaning it doesn't change in relation to the error in any way. Useful for heavy position control for arms or anytime a base output is desired. Honestly doesn't have that many good use-cases though.

This constant can be set using either of these Methods:

setConstants(double kp, double ki, double kd, double ff)
withConstants(double kp, double ki, double kd, double ff)

Definition at line 541 of file PID.h.

◆ m_integral

double art::PID::m_integral {0}
private

The integral caluculated by the PID controller.

The integral is calculated as the sum of the error over an extended period of time. The error is only added to the sum if it is less than or equal to the integral zone.

The I term in PID is the Integral multiplied by the constant.

Definition at line 477 of file PID.h.

◆ m_integralZone

double art::PID::m_integralZone {0}
private

The range within which the error is applied to calculate the integral.

The error is only added to the sum if it is less than or equal to the integral zone.

Definition at line 484 of file PID.h.

◆ m_kd

double art::PID::m_kd {0}
private

A constant applied to the Derivative term.

A constant applied to the Derivative term, allowing the term to be tuned individually of the other terms, adjusting the significance of the term by increasing or decreasing its value.

This constant can be set using any of these Methods:

setConstants(double kp, double ki, double kd)
withConstants(double kp, double ki, double kd)
setConstants(double kp, double ki, double kd, double ff)
withConstants(double kp, double ki, double kd, double ff)

Definition at line 528 of file PID.h.

◆ m_ki

double art::PID::m_ki {0}
private

A constant applied to the Integral term.

A constant applied to the Integral term, allowing the term to be tuned individually of the other terms, adjusting the significance of the term by increasing or decreasing its value.

This constant can be set using any of these Methods:

setConstants(double kp, double ki, double kd)
withConstants(double kp, double ki, double kd)
setConstants(double kp, double ki, double kd, double ff)
withConstants(double kp, double ki, double kd, double ff)

Definition at line 514 of file PID.h.

◆ m_kp

double art::PID::m_kp {0}
private

A constant applied to the Proportional term.

A constant applied to the Proportional term, allowing the term to be tuned individually of the other terms, adjusting the significance of the term by increasing or decreasing its value.

This constant can be set using any of these Methods:

setConstants(double kp, double ki, double kd)
withConstants(double kp, double ki, double kd)
setConstants(double kp, double ki, double kd, double ff)
withConstants(double kp, double ki, double kd, double ff)

Definition at line 499 of file PID.h.

◆ m_prevError

double art::PID::m_prevError {0}
private

The error fed into the PID controller on it's previous iteration.

Stores the pervious error of the PID controller. After all calculations are complete, the error is stored as the previous error so that the derivative(the change in error) can be calculated upon another call of calculate().'

The P term in PID stands for proportional. The term is equal to the constant multiplied by the error.

Definition at line 456 of file PID.h.

◆ m_settleTimeout

double art::PID::m_settleTimeout {0}
private

The duration the error must be within before the PID is considered complete.

After this amount of time(in seconds) isCompleted() will return true. This is calculated using m_startSettledTime, when the absolute value of the error first becomes less than or equal to m_settleZone.

This can be set using setSettleTimeout(double timeout) or withSettleTimeout(double timeout).

Definition at line 591 of file PID.h.

◆ m_settleZone

double art::PID::m_settleZone {0}
private

The range which the error must be for the PID to be considered settled.

While the absolute value of the error is less than or equal to this value, the settleTimeout will continue to count down. Once that time is up, the PID will be considered complete.

The unit for values is up to you, but keep it consistant with the inputs and the error.

This can be set using setSettleZone(double settleZone) or withSettleZone(double settleZone).

Definition at line 606 of file PID.h.

◆ m_startSettledTime

TimePoint art::PID::m_startSettledTime
private

A point in time representing when the PID first entered the acceptable error range.

The point in time is taken when the absolute value of the error first becomes less than or equal to m_settleZone. This can then be used to keep track of how long the error has been within an acceptable range. This allows for the PID to be considered complete once the error settles for a specified amount of time.

The time since this point can be accessed using settledTimePassed().

Definition at line 580 of file PID.h.

◆ m_startTime

TimePoint art::PID::m_startTime
private

A point in time representing when the PID started.

The point in time is taken when the PID object is constructed or when it is reset. This can then be used to keep a timer running tracking the duration the PID has been trying to reach its target. This allows for a timeout to be added to isComplete(), preventing the PID from stalling indefinetely.

The time since this point can be accessed using timePassed();

Definition at line 554 of file PID.h.

◆ m_timeout

double art::PID::m_timeout {0}
private

The duration the PID should run for before it is considered finished.

After this amount of time(in seconds) isCompleted() will return true. This is calculated using m_startTime, so it either starts from when the PID was constructed or last reset.

This can be set using setTimeout(double timeout) or withTimeout(double timeout).

Definition at line 566 of file PID.h.


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