ros2_control - rolling
Classes | Public Member Functions | Protected Attributes | List of all members
control_toolbox::Pid Class Reference

A basic pid class. More...

#include <pid.hpp>

Collaboration diagram for control_toolbox::Pid:
Collaboration graph
[legend]

Classes

struct  Gains
 Store gains in a struct to allow easier realtime buffer usage. More...
 

Public Member Functions

 Pid (double p=0.0, double i=0.0, double d=0.0, double i_max=0.0, double i_min=-0.0, bool antiwindup=false)
 Constructor, zeros out Pid values when created and initialize Pid-gains and integral term limits. Does not initialize dynamic reconfigure for PID gains. More...
 
 Pid (const Pid &source)
 Copy constructor required for preventing mutexes from being copied. More...
 
 ~Pid ()
 Destructor of Pid class.
 
void initPid (double p, double i, double d, double i_max, double i_min, bool antiwindup=false)
 Zeros out Pid values and initialize Pid-gains and integral term limits Does not initialize the node's parameter interface for PID gains. More...
 
void reset ()
 Reset the state of this PID controller.
 
void getGains (double &p, double &i, double &d, double &i_max, double &i_min)
 Get PID gains for the controller. More...
 
void getGains (double &p, double &i, double &d, double &i_max, double &i_min, bool &antiwindup)
 Get PID gains for the controller. More...
 
Gains getGains ()
 Get PID gains for the controller. More...
 
void setGains (double p, double i, double d, double i_max, double i_min, bool antiwindup=false)
 Set PID gains for the controller. More...
 
void setGains (const Gains &gains)
 Set PID gains for the controller. More...
 
double computeCommand (double error, uint64_t dt)
 Set the PID error and compute the PID command with nonuniform time step size. The derivative error is computed from the change in the error and the timestep dt. More...
 
double computeCommand (double error, double error_dot, uint64_t dt)
 Set the PID error and compute the PID command with nonuniform time step size. This also allows the user to pass in a precomputed derivative error. More...
 
void setCurrentCmd (double cmd)
 Set current command for this PID controller.
 
double getCurrentCmd ()
 Return current command for this PID controller.
 
double getDerivativeError ()
 Return derivative error.
 
void getCurrentPIDErrors (double &pe, double &ie, double &de)
 Return PID error terms for the controller. More...
 
Pidoperator= (const Pid &source)
 Custom assignment operator Does not initialize dynamic reconfigure for PID gains.
 

Protected Attributes

realtime_tools::RealtimeBuffer< Gainsgains_buffer_
 
double p_error_last_
 
double p_error_
 
double i_error_
 
double d_error_
 
double cmd_
 
double error_dot_
 

Detailed Description

A basic pid class.

This class implements a generic structure that can be used to create a wide range of pid controllers. It can function independently or be subclassed to provide more specific controls based on a particular control loop.

In particular, this class implements the standard pid equation:

\(command = p_{term} + i_{term} + d_{term} \)

where:

given:

Parameters
pProportional gain
dDerivative gain
iIntegral gain
i_clampMin/max bounds for the integral windup, the clamp is applied to the \(i_{term}\)

Usage

To use the Pid class, you should first call some version of init() (in non-realtime) and then call updatePid() at every update step. For example:

control_toolbox::Pid pid;
pid.initPid(6.0, 1.0, 2.0, 0.3, -0.3);
double position_desi_ = 0.5;
...
rclcpp::Time last_time = get_clock()->now();
while (true) {
rclcpp::Time time = get_clock()->now();
double effort = pid.computeCommand(position_desi_ - currentPosition(), (time - last_time).nanoseconds());
last_time = time;
}

Constructor & Destructor Documentation

◆ Pid() [1/2]

control_toolbox::Pid::Pid ( double  p = 0.0,
double  i = 0.0,
double  d = 0.0,
double  i_max = 0.0,
double  i_min = -0.0,
bool  antiwindup = false 
)

Constructor, zeros out Pid values when created and initialize Pid-gains and integral term limits. Does not initialize dynamic reconfigure for PID gains.

Parameters
pThe proportional gain.
iThe integral gain.
dThe derivative gain.
i_maxThe max integral windup.
i_minThe min integral windup.
antiwindupIf true, antiwindup is enabled and i_max/i_min are enforced
Exceptions
Anstd::invalid_argument exception is thrown if i_min > i_max

◆ Pid() [2/2]

control_toolbox::Pid::Pid ( const Pid source)

Copy constructor required for preventing mutexes from being copied.

Parameters
source- Pid to copy

Member Function Documentation

◆ computeCommand() [1/2]

double control_toolbox::Pid::computeCommand ( double  error,
double  error_dot,
uint64_t  dt 
)

Set the PID error and compute the PID command with nonuniform time step size. This also allows the user to pass in a precomputed derivative error.

Parameters
errorError since last call (error = target - state)
error_dotd(Error)/dt since last call
dtChange in time since last call in nanoseconds
Returns
PID command

◆ computeCommand() [2/2]

double control_toolbox::Pid::computeCommand ( double  error,
uint64_t  dt 
)

Set the PID error and compute the PID command with nonuniform time step size. The derivative error is computed from the change in the error and the timestep dt.

Parameters
errorError since last call (error = target - state)
dtChange in time since last call in nanoseconds
Returns
PID command

◆ getCurrentPIDErrors()

void control_toolbox::Pid::getCurrentPIDErrors ( double &  pe,
double &  ie,
double &  de 
)

Return PID error terms for the controller.

Parameters
peThe proportional error.
ieThe integral error.
deThe derivative error.

◆ getGains() [1/3]

Pid::Gains control_toolbox::Pid::getGains ( )

Get PID gains for the controller.

Returns
gains A struct of the PID gain values

◆ getGains() [2/3]

void control_toolbox::Pid::getGains ( double &  p,
double &  i,
double &  d,
double &  i_max,
double &  i_min 
)

Get PID gains for the controller.

Parameters
pThe proportional gain.
iThe integral gain.
dThe derivative gain.
i_maxThe max integral windup.
i_minThe min integral windup.

◆ getGains() [3/3]

void control_toolbox::Pid::getGains ( double &  p,
double &  i,
double &  d,
double &  i_max,
double &  i_min,
bool &  antiwindup 
)

Get PID gains for the controller.

Parameters
pThe proportional gain.
iThe integral gain.
dThe derivative gain.
i_maxThe max integral windup.
i_minThe min integral windup.
antiwindupIf true, antiwindup is enabled and i_max/i_min are enforced

◆ initPid()

void control_toolbox::Pid::initPid ( double  p,
double  i,
double  d,
double  i_max,
double  i_min,
bool  antiwindup = false 
)

Zeros out Pid values and initialize Pid-gains and integral term limits Does not initialize the node's parameter interface for PID gains.

Parameters
pThe proportional gain.
iThe integral gain.
dThe derivative gain.
i_maxThe max integral windup.
i_minThe min integral windup.
antiwindupIf true, antiwindup is enabled and i_max/i_min are enforced
Note
New gains are not applied if i_min_ > i_max_

◆ setGains() [1/2]

void control_toolbox::Pid::setGains ( const Gains gains)

Set PID gains for the controller.

Parameters
gainsA struct of the PID gain values
Note
New gains are not applied if gains.i_min_ > gains.i_max_

◆ setGains() [2/2]

void control_toolbox::Pid::setGains ( double  p,
double  i,
double  d,
double  i_max,
double  i_min,
bool  antiwindup = false 
)

Set PID gains for the controller.

Parameters
pThe proportional gain.
iThe integral gain.
dThe derivative gain.
i_maxThe max integral windup.
i_minThe min integral windup.
antiwindupIf true, antiwindup is enabled and i_max/i_min are enforced
Note
New gains are not applied if i_min > i_max

Member Data Documentation

◆ cmd_

double control_toolbox::Pid::cmd_
protected

Command to send.

◆ d_error_

double control_toolbox::Pid::d_error_
protected

Derivative of position error.

◆ error_dot_

double control_toolbox::Pid::error_dot_
protected

Derivative error

◆ i_error_

double control_toolbox::Pid::i_error_
protected

Integral of position error.

◆ p_error_

double control_toolbox::Pid::p_error_
protected

Position error.

◆ p_error_last_

double control_toolbox::Pid::p_error_last_
protected

_Save position state for derivative state calculation.


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