Smart Car Controller  1.0
Library to manage a two wheeled robotic vehicle (DC Motors)
SC_PID Class Reference

#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)
 

Detailed Description

Core object for the SC_PID class Implements PID control algorithm DC motor control, as a hybrid fixed-point and floating-point PID controller

Member Enumeration Documentation

◆ control_t

Different ways the controller can operate

Enumerator
DIRECT 

An increase in the control output increases the controlled variable.

REVERSE 

An increase in the control output decreases the controlled variable.

◆ mode_t

Different possible modes for the controller execution

Enumerator
AUTO 

The controller will keep track of time and execute one PID iteration when time expires.

USER 

The user is responsible for calling the PID computation when the calculation time interval expires.

OFF 

The controlled variable is controlled manually (ie, no PID control).

Constructor & Destructor Documentation

◆ SC_PID()

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.

See also
setTuning(), setMode()
Parameters
cvPointer to the current value for the controlled variable.
coPointer to the control output for the controlled variable.
spPointer to the setpoint for the controlled variable.
KpProportional coefficient for the PID algorithm.
KiIntegral coefficient for the PID algorithm.
KdDerivative coefficient for the PID algorithm.
pOnFactor for combining errors (proportional on Error or Measurement).
controllerThe type of controller (one of the controller_t values).

◆ ~SC_PID()

SC_PID::~SC_PID ( void  )

Class Destructor.

Release any allocated memory and clean up anything else.

Member Function Documentation

◆ compute()

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.

See also
setMode(), setPIDPeriod(), setOutputLimits()
Returns
true if the calculation was completed, false otherwise.

◆ getControlType()

control_t SC_PID::getControlType ( void  )

Return the current controller type.

Returns
The controller type (one of the controller_t values).

◆ getError()

int16_t SC_PID::getError ( void  )

Return the current PID error value.

Returns
The error value resulting from the last PID calculation step.

◆ getKd()

float SC_PID::getKd ( void  )

Return the current Kd value.

Returns
The Kd coefficient for PID calculation.

◆ getKi()

float SC_PID::getKi ( void  )

Return the current Ki value.

Returns
The Ki coefficient for PID calculation.

◆ getKp()

float SC_PID::getKp ( void  )

Return the current Kp value.

Returns
The Kp coefficient for PID calculation.

◆ getMode()

mode_t SC_PID::getMode ( void  )

Return the current controller mode.

Returns
The controller mode (one of the mode_t values).

◆ getPIDPeriod()

uint32_t SC_PID::getPIDPeriod ( void  )

Return the current PID calculation period.

Returns
The calculation period in milliseconds.

◆ reset()

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.

◆ setControlType()

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.

Parameters
cTypeThe new control type (one of control_t).

◆ setMode()

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.

See also
compute()
Parameters
newModeThe new mode for the controller (default OFF).

◆ setOutputLimits()

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.

Parameters
minThe low value of the permissible range (default 0).
maxThe high value for the permissible range (=default 255).

◆ setPIDPeriod()

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.

See also
setMode()
Parameters
newPeriodthe new calculation interval in milliseconds.

◆ setTuning() [1/2]

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.

Parameters
KpThe new Proportional coefficient.
KiThe new Integral coefficient.
KdThe new Derivative coefficient.

◆ setTuning() [2/2]

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.

Parameters
KpThe new Proportional coefficient.
KiThe new Integral coefficient.
KdThe new Derivative coefficient.
pOnThe new proportional on Error coefficient (default 1.0).

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