![]() |
Smart Car Controller
1.0
Library to manage a two wheeled robotic vehicle (DC Motors)
|
#include <SC_PID.h>
Public Types | |
Enumerated Types and Constants. | |
enum | mode_t { AUTO , USER , OFF } |
enum | control_t { DIRECT , REVERSE } |
Public Member Functions | |
Class constructor and destructor. | |
SC_PID (int16_t *cv, int16_t *co, int16_t *sp, float Kp, float Ki, float Kd, float pOn=1.0, control_t controller=DIRECT) | |
~SC_PID (void) | |
Methods for core object control. | |
bool | compute (void) |
void | reset (void) |
void | setPIDPeriod (uint32_t newPeriod) |
void | setMode (mode_t newMode) |
void | setOutputLimits (int16_t min, int16_t max) |
void | setTuning (float Kp, float Ki, float Kd, float pOn) |
void | setTuning (float Kp, float Ki, float Kd) |
Utility Functions. | |
void | setControlType (control_t cType) |
int16_t | getError (void) |
float | getKp (void) |
float | getKi (void) |
float | getKd (void) |
uint32_t | getPIDPeriod (void) |
mode_t | getMode (void) |
control_t | getControlType (void) |
Core object for the SC_PID class Implements PID control algorithm DC motor control, as a hybrid fixed-point and floating-point PID controller
enum SC_PID::control_t |
enum SC_PID::mode_t |
Different possible modes for the controller execution
SC_PID::SC_PID | ( | int16_t * | cv, |
int16_t * | co, | ||
int16_t * | sp, | ||
float | Kp, | ||
float | Ki, | ||
float | Kd, | ||
float | pOn = 1.0 , |
||
control_t | controller = DIRECT |
||
) |
Class Constructor.
Instantiate a new instance of the class.
The main function for the core object is to reset the internal shared variables and timers to default values.
cv | Pointer to the current value for the controlled variable. |
co | Pointer to the control output for the controlled variable. |
sp | Pointer to the setpoint for the controlled variable. |
Kp | Proportional coefficient for the PID algorithm. |
Ki | Integral coefficient for the PID algorithm. |
Kd | Derivative coefficient for the PID algorithm. |
pOn | Factor for combining errors (proportional on Error or Measurement). |
controller | The type of controller (one of the controller_t values). |
SC_PID::~SC_PID | ( | void | ) |
Class Destructor.
Release any allocated memory and clean up anything else.
bool SC_PID::compute | ( | void | ) |
Perform PID calculation.
Perform the next step in the PID calculation. How this occurs will depend on the current controller mode (AUTO, USER, OFF) and the time period set.
In AUTO mode the method should be called frequently (ideally every time through loop()) and the return value will inform the application when the calculation actually happened.
In USER mode, the method will run when it is invoked under the assumption that the time elapsed between steps is the currently set PID calculation period.
In OFF mode (default) the calculation is never executed.
The current value, setpoint and new control output used are located by the pointers passed to the constructor.
control_t SC_PID::getControlType | ( | void | ) |
Return the current controller type.
int16_t SC_PID::getError | ( | void | ) |
Return the current PID error value.
float SC_PID::getKd | ( | void | ) |
Return the current Kd value.
float SC_PID::getKi | ( | void | ) |
Return the current Ki value.
float SC_PID::getKp | ( | void | ) |
Return the current Kp value.
mode_t SC_PID::getMode | ( | void | ) |
Return the current controller mode.
uint32_t SC_PID::getPIDPeriod | ( | void | ) |
Return the current PID calculation period.
void SC_PID::reset | ( | void | ) |
Reset the PID calculation.
Reset the PID calculation. The library will ensure a bumpless transition between current and reset state.
Not generally required but available.
void SC_PID::setControlType | ( | control_t | cType | ) |
Set the controller type.
Sets the controller type. This is already set in the constructor and is unlikely to change for a given control application.
For DIRECT controllers an increase in the output causes an increase in the input. The opposite is true for REVERSE controllers.
cType | The new control type (one of control_t). |
void SC_PID::setMode | ( | mode_t | newMode | ) |
Set the controller mode.
Sets the controller mode to one of the mode_t values
In AUTO mode the library tracks time and executes the PID calculation when the correct period has expired.
In USER mode, the library does not track time and the PID calculation will be run whenever it is invoked.
In OFF mode (default) the PID calculation is never executed.
newMode | The new mode for the controller (default OFF). |
void SC_PID::setOutputLimits | ( | int16_t | min, |
int16_t | max | ||
) |
Set the PID control limits
Sets the upper and lower values for the control output. The value will always be clamped to this range. The default is [0, 255], which suits an Arduino PWM output.
min | The low value of the permissible range (default 0). |
max | The high value for the permissible range (=default 255). |
void SC_PID::setPIDPeriod | ( | uint32_t | newPeriod | ) |
Reset the PID calculation period.
Reset the PID calculation period. Really only applies in AUTO mode.
Not generally required but available.
newPeriod | the new calculation interval in milliseconds. |
void SC_PID::setTuning | ( | float | Kp, |
float | Ki, | ||
float | Kd | ||
) |
Set the PID coefficients only.
This overload of the method allows the PID coefficients to be changed during run time. These will replace the current coefficients, including those supplied with the constructor.
This method can be used to implement adaptive control or during tuning of the PID control loop.
Kp | The new Proportional coefficient. |
Ki | The new Integral coefficient. |
Kd | The new Derivative coefficient. |
void SC_PID::setTuning | ( | float | Kp, |
float | Ki, | ||
float | Kd, | ||
float | pOn | ||
) |
Set the PID coefficients and Proportional on Error.
Allows the PID coefficients and proportional on error factor to be changed during run time. These will replace the current coefficients, including those with the constructor.
This method can be used to implement adaptive control or during tuning of the PID control loop.
The is the Proportional on Error weighting value is expressed as a number between 0.0 and 1.0. This controls the mix of Proportional on Error (PonE) and Proportional on Measurement (PonM) that's used in the compute algorithm. Note that POn controls the PonE amount, whereas the remainder (1-PonE) is the PonM amount.
Kp | The new Proportional coefficient. |
Ki | The new Integral coefficient. |
Kd | The new Derivative coefficient. |
pOn | The new proportional on Error coefficient (default 1.0). |