LightLib
PROS library for VEX V5: EKF/MCL localization, RAMSETE path following, high-level chassis API
Loading...
Searching...
No Matches
light::PID Class Reference

Discrete PID controller with rich exit-condition support. More...

#include <pid.hpp>

Classes

struct  Constants
 PID gain constants. More...
 
struct  exit_condition_
 Exit condition timing and thresholds. More...
 

Public Member Functions

 PID ()
 Default constructor.
 
 PID (double p, double i=0, double d=0, double start_i=0, std::string name="")
 Constructor with constants.
 
void constants_set (double p, double i=0, double d=0, double p_start_i=0)
 Set constants for PID.
 
void exit_condition_set (int p_small_exit_time, double p_small_error, int p_big_exit_time=0, double p_big_error=0, int p_velocity_exit_time=0, int p_mA_timeout=0)
 Set's constants for exit conditions.
 
void target_set (double input)
 Sets PID target.
 
double compute (double current)
 Computes PID.
 
double compute_error (double err, double current)
 Computes PID, but you compute the error yourself.
 
double target_get ()
 Returns target value.
 
Constants constants_get ()
 Returns constants.
 
bool constants_set_check ()
 Returns true if PID constants are set, returns false if they're all 0.
 
void variables_reset ()
 Resets all variables to 0.
 
void velocity_sensor_secondary_set (double secondary_sensor)
 Updates a secondary sensor for velocity exiting.
 
double velocity_sensor_secondary_get ()
 Returns the updated secondary sensor for velocity exiting.
 
void velocity_sensor_secondary_toggle_set (bool toggle)
 Boolean for if the secondary sensor will be updated or not.
 
bool velocity_sensor_secondary_toggle_get ()
 Returns the boolean for if the secondary sensor will be updated or not.
 
void velocity_sensor_main_exit_set (double zero)
 Sets the threshold that the main sensor will return 0 velocity within.
 
double velocity_sensor_main_exit_get ()
 Returns the threshold that the main sensor will return 0 velocity within.
 
void velocity_sensor_secondary_exit_set (double zero)
 Sets the threshold that the secondary sensor will return 0 velocity within.
 
double velocity_sensor_secondary_exit_get ()
 Returns the threshold that the secondary sensor will return 0 velocity within.
 
light::exit_output exit_condition (bool print=false)
 Iterative exit condition for PID.
 
light::exit_output exit_condition (pros::Motor sensor, bool print=false)
 Iterative exit condition for PID.
 
light::exit_output exit_condition (std::vector< pros::Motor > sensor, bool print=false)
 Iterative exit condition for PID.
 
light::exit_output exit_condition (pros::MotorGroup sensor, bool print=false)
 Iterative exit condition for PID.
 
void name_set (std::string name)
 Sets the name of the PID that prints during exit conditions.
 
std::string name_get ()
 Returns the name of the PID that prints during exit conditions.
 
void i_reset_toggle (bool toggle)
 Enables / disables i resetting when sgn of error changes.
 
bool i_reset_get ()
 Returns if i will reset when sgn of error changes.
 
void timers_reset ()
 Resets all timers for exit conditions.
 

Public Attributes

Constants constants
 Active PID gain constants (read/write).
 
exit_condition_ exit
 Active exit-condition thresholds (read/write).
 
Live PID state

Updated each call to compute() / compute_error().

double output = 0.0
 Last computed control output.
 
double cur = 0.0
 Last sensor value seen.
 
double error = 0.0
 Current error (target − cur).
 
double target = 0.0
 Current target.
 
double prev_error = 0.0
 Previous-cycle error.
 
double prev_current = 0.0
 Previous-cycle sensor value.
 
double integral = 0.0
 Accumulated integral term.
 
double derivative = 0.0
 Last derivative term.
 
long time = 0
 Timestamp of last compute (ms).
 
long prev_time = 0
 Timestamp of previous compute (ms).
 

Detailed Description

Discrete PID controller with rich exit-condition support.

Configure once via the constructor or constants_set(), then call compute() each cycle with the latest sensor reading. Use exit_condition() to detect when the controller has settled.

Definition at line 30 of file pid.hpp.

Constructor & Destructor Documentation

◆ PID() [1/2]

light::PID::PID ( )

Default constructor.

◆ PID() [2/2]

light::PID::PID ( double  p,
double  i = 0,
double  d = 0,
double  start_i = 0,
std::string  name = "" 
)

Constructor with constants.

Parameters
pkP
iki
dkD
p_start_ierror value that i starts within
namestd::string of name that prints

Member Function Documentation

◆ compute()

double light::PID::compute ( double  current)

Computes PID.

Parameters
currentcurrent sensor value

◆ compute_error()

double light::PID::compute_error ( double  err,
double  current 
)

Computes PID, but you compute the error yourself.

Current is only used here for calculative derivative to solve derivative kick.

Parameters
errerror for the PID, you need to calculate this yourself
currentcurrent sensor value

◆ constants_get()

Constants light::PID::constants_get ( )

Returns constants.

◆ constants_set()

void light::PID::constants_set ( double  p,
double  i = 0,
double  d = 0,
double  p_start_i = 0 
)

Set constants for PID.

Parameters
pkP
iki
dkD
p_start_ierror value that i starts within

◆ constants_set_check()

bool light::PID::constants_set_check ( )

Returns true if PID constants are set, returns false if they're all 0.

◆ exit_condition() [1/4]

light::exit_output light::PID::exit_condition ( bool  print = false)

Iterative exit condition for PID.

Parameters
print= false if true, prints when complete

◆ exit_condition() [2/4]

light::exit_output light::PID::exit_condition ( pros::Motor  sensor,
bool  print = false 
)

Iterative exit condition for PID.

Parameters
sensora pros motor on your mechanism
print= false if true, prints when complete

◆ exit_condition() [3/4]

light::exit_output light::PID::exit_condition ( pros::MotorGroup  sensor,
bool  print = false 
)

Iterative exit condition for PID.

Parameters
sensorpros motor group on your mechanism
print= false if true, prints when complete

◆ exit_condition() [4/4]

light::exit_output light::PID::exit_condition ( std::vector< pros::Motor >  sensor,
bool  print = false 
)

Iterative exit condition for PID.

Parameters
sensorpros motors on your mechanism
print= false if true, prints when complete

◆ exit_condition_set()

void light::PID::exit_condition_set ( int  p_small_exit_time,
double  p_small_error,
int  p_big_exit_time = 0,
double  p_big_error = 0,
int  p_velocity_exit_time = 0,
int  p_mA_timeout = 0 
)

Set's constants for exit conditions.

Parameters
p_small_exit_timesets small_exit_time, timer for to exit within smalL_error
p_small_errorsets smalL_error, timer will start when error is within this
p_big_exit_timesets big_exit_time, timer for to exit within big_error
p_big_errorsets big_error, timer will start when error is within this
p_velocity_exit_timesets velocity_exit_time, timer will start when velocity is 0

◆ i_reset_get()

bool light::PID::i_reset_get ( )

Returns if i will reset when sgn of error changes.

True resets, false doesn't.

◆ i_reset_toggle()

void light::PID::i_reset_toggle ( bool  toggle)

Enables / disables i resetting when sgn of error changes.

True resets, false doesn't.

Parameters
toggletrue resets, false doesn't

◆ name_get()

std::string light::PID::name_get ( )

Returns the name of the PID that prints during exit conditions.

◆ name_set()

void light::PID::name_set ( std::string  name)

Sets the name of the PID that prints during exit conditions.

Parameters
namethe name of the mechanism for printing

◆ target_get()

double light::PID::target_get ( )

Returns target value.

◆ target_set()

void light::PID::target_set ( double  input)

Sets PID target.

Parameters
targetnew target for PID

◆ timers_reset()

void light::PID::timers_reset ( )

Resets all timers for exit conditions.

◆ variables_reset()

void light::PID::variables_reset ( )

Resets all variables to 0.

This does not reset constants.

◆ velocity_sensor_main_exit_get()

double light::PID::velocity_sensor_main_exit_get ( )

Returns the threshold that the main sensor will return 0 velocity within.

◆ velocity_sensor_main_exit_set()

void light::PID::velocity_sensor_main_exit_set ( double  zero)

Sets the threshold that the main sensor will return 0 velocity within.

Parameters
zeroa small double

◆ velocity_sensor_secondary_exit_get()

double light::PID::velocity_sensor_secondary_exit_get ( )

Returns the threshold that the secondary sensor will return 0 velocity within.

◆ velocity_sensor_secondary_exit_set()

void light::PID::velocity_sensor_secondary_exit_set ( double  zero)

Sets the threshold that the secondary sensor will return 0 velocity within.

Parameters
zeroa small double

◆ velocity_sensor_secondary_get()

double light::PID::velocity_sensor_secondary_get ( )

Returns the updated secondary sensor for velocity exiting.

◆ velocity_sensor_secondary_set()

void light::PID::velocity_sensor_secondary_set ( double  secondary_sensor)

Updates a secondary sensor for velocity exiting.

Ideal use is IMU during normal drive motions.

Parameters
secondary_sensorsecondary sensor value

◆ velocity_sensor_secondary_toggle_get()

bool light::PID::velocity_sensor_secondary_toggle_get ( )

Returns the boolean for if the secondary sensor will be updated or not.

True uses this sensor, false does not.

◆ velocity_sensor_secondary_toggle_set()

void light::PID::velocity_sensor_secondary_toggle_set ( bool  toggle)

Boolean for if the secondary sensor will be updated or not.

True uses this sensor, false does not.

Parameters
toggletrue uses this sensor, false does not

Member Data Documentation

◆ constants

Constants light::PID::constants

Active PID gain constants (read/write).

Definition at line 154 of file pid.hpp.

◆ cur

double light::PID::cur = 0.0

Last sensor value seen.

Definition at line 290 of file pid.hpp.

◆ derivative

double light::PID::derivative = 0.0

Last derivative term.

Definition at line 296 of file pid.hpp.

◆ error

double light::PID::error = 0.0

Current error (target − cur).

Definition at line 291 of file pid.hpp.

◆ exit

exit_condition_ light::PID::exit

Active exit-condition thresholds (read/write).

Definition at line 157 of file pid.hpp.

◆ integral

double light::PID::integral = 0.0

Accumulated integral term.

Definition at line 295 of file pid.hpp.

◆ output

double light::PID::output = 0.0

Last computed control output.

Definition at line 289 of file pid.hpp.

◆ prev_current

double light::PID::prev_current = 0.0

Previous-cycle sensor value.

Definition at line 294 of file pid.hpp.

◆ prev_error

double light::PID::prev_error = 0.0

Previous-cycle error.

Definition at line 293 of file pid.hpp.

◆ prev_time

long light::PID::prev_time = 0

Timestamp of previous compute (ms).

Definition at line 298 of file pid.hpp.

◆ target

double light::PID::target = 0.0

Current target.

Definition at line 292 of file pid.hpp.

◆ time

long light::PID::time = 0

Timestamp of last compute (ms).

Definition at line 297 of file pid.hpp.


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