WPID Library 5
Loading...
Searching...
No Matches
Public Member Functions | List of all members
wpid::PID Class Reference

A PID class that stores constants and calculates the speed of a motor or motor group with respect to its current error and other internal factors. More...

#include <PID.h>

Public Member Functions

 PID (float kp, float ki, float kd)
 Construct a new PID object. When constructing a PID object, only the kp constant is required, ki and kd can be set to 0 if not used.
 
float calculateSpeed (float error, float max_speed, std::string mech_id)
 Used to calculate the velocity of a motor or motor group. The speed is calculated using PID, with integral clamping, and speed clamping.
 
void setErrorRange (float degrees)
 Set the error range in rotationUnits::deg.
 
int getDelayTime ()
 Gets the delay time of this PID object in milliseconds.
 
void setLowSpeedThreshold (int threshold)
 Set the low speed threshold to determine when the system is slow. If the speed of the system is below this threshold, the loop will end if it is also within the error range. Otherwise the system will continue to correct for any error.
 
void setTimeout (int timeout)
 Set the timeout to use for PID movement. If the timeout is exceeded, the system will stop regardless of the current error or speed. A value of -1 will disable timeouts.
 
void setMaxIntegral (int max_integral)
 Set the maximum value the integral term will contribute to the speed calculations. If the integral term exceeds this value, it will be reduced to this value.
 
bool unfinished (float error, int speed)
 Checks if the movement is unfinished (error still outside the final bounds).
 
void reset (void)
 Resets the previous error and previous integral values.
 
PID copy (void)
 Duplicates the PID object by copying constants and returning a new PID object. Used as a helper for using the same constants on multiple mechanisms.
 
void fileLogging (float error, float speed, float proportional, float integral, float derivative, std::string mech_id)
 Logs the error, speed, integral and derivative values to a text file on a micro SD card on the robot.
 

Detailed Description

A PID class that stores constants and calculates the speed of a motor or motor group with respect to its current error and other internal factors.

Constructor & Destructor Documentation

◆ PID()

wpid::PID::PID ( float  kp,
float  ki,
float  kd 
)
inline

Construct a new PID object. When constructing a PID object, only the kp constant is required, ki and kd can be set to 0 if not used.

Parameters
kpproportional constant
kiintegral constant
kdderivative constant

Member Function Documentation

◆ calculateSpeed()

float PID::calculateSpeed ( float  error,
float  max_speed,
std::string  mech_id 
)

Used to calculate the velocity of a motor or motor group. The speed is calculated using PID, with integral clamping, and speed clamping.

Parameters
errorthe remaining distance to the target
max_speedmaximum velocity allowed in velocityUnits::pct
mech_idstring identifier for motor group to log
Returns
a calculated speed based on all PID parameters

◆ copy()

PID PID::copy ( void  )

Duplicates the PID object by copying constants and returning a new PID object. Used as a helper for using the same constants on multiple mechanisms.

Returns
PID

◆ fileLogging()

void PID::fileLogging ( float  error,
float  speed,
float  proportional,
float  integral,
float  derivative,
std::string  mech_id 
)

Logs the error, speed, integral and derivative values to a text file on a micro SD card on the robot.

Parameters
errorthe robot error value
speedthe calculated speed
proportionalresult of the proportional calculation
integralresult of the integral calculation
derivativeresult of the derivative calculation
mech_idthe mechanism name

◆ getDelayTime()

int PID::getDelayTime ( )

Gets the delay time of this PID object in milliseconds.

Returns
int milliseconds

◆ setErrorRange()

void PID::setErrorRange ( float  degrees)

Set the error range in rotationUnits::deg.

Parameters
degreesthe absolute value of the bounds of the error range

◆ setLowSpeedThreshold()

void PID::setLowSpeedThreshold ( int  threshold)

Set the low speed threshold to determine when the system is slow. If the speed of the system is below this threshold, the loop will end if it is also within the error range. Otherwise the system will continue to correct for any error.

Parameters
thresholdin velocityUnits::pct

◆ setMaxIntegral()

void PID::setMaxIntegral ( int  max_integral)

Set the maximum value the integral term will contribute to the speed calculations. If the integral term exceeds this value, it will be reduced to this value.

Parameters
max_integralin velocityUnits::pct

◆ setTimeout()

void PID::setTimeout ( int  timeout)

Set the timeout to use for PID movement. If the timeout is exceeded, the system will stop regardless of the current error or speed. A value of -1 will disable timeouts.

Parameters
timeoutin milliseconds

◆ unfinished()

bool PID::unfinished ( float  error,
int  speed 
)

Checks if the movement is unfinished (error still outside the final bounds).

Parameters
errorthe current error of the system
Returns
returns true if the error is outside the bounds, false if it is within the bounds

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