RIT VEXU Core API
Loading...
Searching...
No Matches
PID Class Reference

#include <pid.h>

Inheritance diagram for PID:
Feedback

Public Types

enum  ERROR_TYPE

Public Member Functions

 PID (double kp, double ki, double kd, double deadband, double on_target_time, ERROR_TYPE error_method=PID::LINEAR)
 Create the PID object.
void init (double start_pt, double set_pt) override
double update (double sensor_val) override
double update (double sensor_val, double v_setpt)
double get_sensor_val () const
 gets the sensor value that we were last updated with
double get () override
 Gets the current PID out value, from when update() was last run.
void set_limits (double lower, double upper) override
bool is_on_target () override
void reset ()
 Reset the PID loop by resetting time since 0 and accumulated error.
double get_error ()
 Get the delta between the current sensor data and the target.
double get_output ()
 Get the delta between the current sensor data and the target.
double get_target () const
void set_target (double target)
 Set the target for the PID loop, where the robot is trying to end up.

Public Attributes

double kp
 Proportional coeffecient; p * error().
double ki
 Integral coefficient; i * integral(error).
double kd
 Derivative coefficient; d * derivative(error).
double deadband
 A deadband that determines the threshold for what counts as finished.
double on_target_time
 The time, in seconds, that we have to be in the threshold in order to be considered on target.
ERROR_TYPE error_method
 Determines whether error is performed as a simple subtraction (LINEAR) or wrapping (ANGULAR).

Detailed Description

PID Class

Defines a standard feedback loop using the constants kP, kI, kD, deadband, and on_target_time. The formula is:

out = kP*error + kI*integral(d Error) + kD*(dError/dt)

The PID object will determine it is "on target" when the error is within the deadband, for a duration of on_target_time

Author
Ryan McGee
Date
4/3/2020

Member Enumeration Documentation

◆ ERROR_TYPE

An enum to distinguish between a linear and angular caluclation of PID error.

Constructor & Destructor Documentation

◆ PID()

PID::PID ( double kp,
double ki,
double kd,
double deadband,
double on_target_time,
PID::ERROR_TYPE error_method = PID::LINEAR )

Create the PID object.

Create the PID object

Parameters
kpCoefficient double p. Direct error to voltage.
kiCoefficient double i. Integral of error to voltage. Helps get the motor "unstuck" if P is not enough to drive the motor.
kdCoefficient double d. Derivative of error to voltage. Helps prevent overshooting.
deadbandDouble. Acceptable margin of error.
on_target_timeDouble. How long the reading must be within the deadband to consider robot "aligned."
error_methodERROR_TYPE struct. Can be either LINEAR or ANGULAR. Determines how we want the PID to enact, on position or rotation for example.

Member Function Documentation

◆ get()

double PID::get ( )
overridevirtual

Gets the current PID out value, from when update() was last run.

Gets the current PID out value, from when update() was last run

Returns
the Out value of the controller (voltage, RPM, whatever the PID controller is controlling)

Implements Feedback.

◆ get_error()

double PID::get_error ( )

Get the delta between the current sensor data and the target.

Get the delta between the current sensor data and the target

Returns
the error calculated. how it is calculated depends on error_method

◆ get_output()

double PID::get_output ( )

Get the delta between the current sensor data and the target.

Get the output calculated from the P, I, D and Error values

Returns
the output calculated from the pid controller

◆ get_sensor_val()

double PID::get_sensor_val ( ) const

gets the sensor value that we were last updated with

Returns
sensor_val

◆ get_target()

double PID::get_target ( ) const

Get the PID's target

Returns
the target the PID controller is trying to achieve

◆ init()

void PID::init ( double start_pt,
double set_pt )
overridevirtual

Inherited from Feedback for interoperability. Update the setpoint and reset integral accumulation

start_pt can be safely ignored in this feedback controller

Parameters
start_ptcommpletely ignored for PID. necessary to satisfy Feedback base
set_ptsets the target of the PID controller
start_velcompletely ignored for PID. necessary to satisfy Feedback base
end_velsets the target end velocity of the PID controller

Implements Feedback.

◆ is_on_target()

bool PID::is_on_target ( )
overridevirtual

Checks if the PID controller is on target.

Returns
true if the loop is within [deadband] for [on_target_time] seconds

Returns true if the loop is within [deadband] for [on_target_time] seconds

Implements Feedback.

◆ set_limits()

void PID::set_limits ( double lower,
double upper )
overridevirtual

Set the limits on the PID out. The PID out will "clip" itself to be between the limits.

Parameters
lowerthe lower limit. the PID controller will never command the output go below lower
upperthe upper limit. the PID controller will never command the output go higher than upper

Set the limits on the PID out. The PID out will "clip" itself to be between the limits.

Implements Feedback.

◆ set_target()

void PID::set_target ( double target)

Set the target for the PID loop, where the robot is trying to end up.

Set the target for the PID loop, where the robot is trying to end up

Parameters
targetthe sensor reading we would like to achieve

◆ update() [1/2]

double PID::update ( double sensor_val)
overridevirtual

Update the PID loop by taking the time difference from last update, and running the PID formula with the new sensor data

Parameters
sensor_valthe distance, angle, encoder position or whatever it is we are measuring
Returns
the new output. What would be returned by PID::get()

Implements Feedback.

◆ update() [2/2]

double PID::update ( double sensor_val,
double v_setpt )

Update the PID loop by taking the time difference from last update, and running the PID formula with the new sensor data

Parameters
sensor_valthe distance, angle, encoder position or whatever it is we are measuring
v_setptExpected velocity setpoint, to subtract from the D term (for velocity control)
Returns
the new output. What would be returned by PID::get()

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