ros2_control - rolling
|
A basic pid class. More...
#include <pid.hpp>
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. | |
Pid (const Pid &source) | |
Copy constructor required for preventing mutexes from being copied. | |
~Pid () | |
Destructor of Pid class. | |
void | initialize (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. | |
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. | |
void | reset () |
Reset the state of this PID controller. | |
void | reset (bool save_iterm) |
Reset the state of this PID controller. | |
void | clear_saved_iterm () |
Clear the saved integrator output of this controller. | |
void | get_gains (double &p, double &i, double &d, double &i_max, double &i_min) |
Get PID gains for the controller. | |
void | getGains (double &p, double &i, double &d, double &i_max, double &i_min) |
Get PID gains for the controller. | |
void | get_gains (double &p, double &i, double &d, double &i_max, double &i_min, bool &antiwindup) |
Get PID gains for the controller. | |
void | getGains (double &p, double &i, double &d, double &i_max, double &i_min, bool &antiwindup) |
Get PID gains for the controller. | |
Gains | get_gains () |
Get PID gains for the controller. | |
Gains | getGains () |
Get PID gains for the controller. | |
void | set_gains (double p, double i, double d, double i_max, double i_min, bool antiwindup=false) |
Set PID gains for the controller. | |
void | setGains (double p, double i, double d, double i_max, double i_min, bool antiwindup=false) |
Set PID gains for the controller. | |
void | set_gains (const Gains &gains) |
Set PID gains for the controller. | |
void | setGains (const Gains &gains) |
Set PID gains for the controller. | |
double | compute_command (double error, const double &dt_s) |
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_s . | |
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 . | |
double | compute_command (double error, const rcl_duration_value_t &dt_ns) |
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_ns . | |
double | compute_command (double error, const rclcpp::Duration &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 . | |
double | compute_command (double error, const std::chrono::nanoseconds &dt_ns) |
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_ns . | |
double | compute_command (double error, double error_dot, const double &dt_s) |
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. | |
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. | |
double | compute_command (double error, double error_dot, const rcl_duration_value_t &dt_ns) |
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. | |
double | compute_command (double error, double error_dot, const rclcpp::Duration &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. | |
double | compute_command (double error, double error_dot, const std::chrono::nanoseconds &dt_ns) |
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. | |
void | set_current_cmd (double cmd) |
Set current command for this PID controller. | |
void | setCurrentCmd (double cmd) |
Set current command for this PID controller. | |
double | get_current_cmd () |
Return current command for this PID controller. | |
double | getCurrentCmd () |
Return current command for this PID controller. | |
double | getDerivativeError () |
Return derivative error. | |
void | get_current_pid_errors (double &pe, double &ie, double &de) |
Return PID error terms for the controller. | |
void | getCurrentPIDErrors (double &pe, double &ie, double &de) |
Return PID error terms for the controller. | |
Pid & | operator= (const Pid &source) |
Custom assignment operator Does not initialize dynamic reconfigure for PID gains. | |
Protected Attributes | |
realtime_tools::RealtimeBuffer< Gains > | gains_buffer_ |
double | p_error_last_ |
double | p_error_ |
double | i_error_ |
double | d_error_ |
double | cmd_ |
double | error_dot_ |
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.
This class also allows for retention of integral term on reset. This is useful for control loops that are enabled/disabled with a constant steady-state external disturbance. Once the integrator cancels out the external disturbance, disabling/resetting/ re-enabling closed-loop control does not require the integrator to wind up again.
In particular, this class implements the standard pid equation:
\(command = p_{term} + i_{term} + d_{term} \)
where:
given:
p | Proportional gain |
d | Derivative gain |
i | Integral gain |
i_clamp | Min/max bounds for the integral windup, the clamp is applied to the \(i_{term}\) |
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.initialize(6.0, 1.0, 2.0, 0.3, -0.3); double position_desired = 0.5; ... rclcpp::Time last_time = get_clock()->now(); while (true) { rclcpp::Time time = get_clock()->now(); double effort = pid.compute_command(position_desired - currentPosition(), time - last_time); last_time = time; }
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.
p | The proportional gain. |
i | The integral gain. |
d | The derivative gain. |
i_max | Upper integral clamp. |
i_min | Lower integral clamp. |
antiwindup | Antiwindup functionality. When set to true, limits the integral error to prevent windup; otherwise, constrains the integral contribution to the control output. i_max and i_min are applied in both scenarios. |
An | std::invalid_argument exception is thrown if i_min > i_max |
control_toolbox::Pid::Pid | ( | const Pid & | source | ) |
Copy constructor required for preventing mutexes from being copied.
source | - Pid to copy |
double control_toolbox::Pid::compute_command | ( | double | error, |
const double & | dt_s | ||
) |
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_s
.
error | Error since last call (error = target - state) |
dt_s | Change in time since last call in seconds |
double control_toolbox::Pid::compute_command | ( | double | error, |
const rcl_duration_value_t & | dt_ns | ||
) |
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_ns
.
error | Error since last call (error = target - state) |
dt_ns | Change in time since last call, measured in nanoseconds. |
double control_toolbox::Pid::compute_command | ( | double | error, |
const rclcpp::Duration & | 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
.
error | Error since last call (error = target - state) |
dt | Change in time since last call |
double control_toolbox::Pid::compute_command | ( | double | error, |
const std::chrono::nanoseconds & | dt_ns | ||
) |
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_ns
.
error | Error since last call (error = target - state) |
dt_ns | Change in time since last call |
double control_toolbox::Pid::compute_command | ( | double | error, |
double | error_dot, | ||
const double & | dt_s | ||
) |
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.
error | Error since last call (error = target - state) |
error_dot | d(Error)/dt_s since last call |
dt_s | Change in time since last call in seconds |
double control_toolbox::Pid::compute_command | ( | double | error, |
double | error_dot, | ||
const rcl_duration_value_t & | dt_ns | ||
) |
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.
error | Error since last call (error = target - state) |
error_dot | d(Error)/dt_ns since last call |
dt_ns | Change in time since last call, measured in nanoseconds. |
double control_toolbox::Pid::compute_command | ( | double | error, |
double | error_dot, | ||
const rclcpp::Duration & | 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.
error | Error since last call (error = target - state) |
error_dot | d(Error)/dt since last call |
dt | Change in time since last call |
double control_toolbox::Pid::compute_command | ( | double | error, |
double | error_dot, | ||
const std::chrono::nanoseconds & | dt_ns | ||
) |
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.
error | Error since last call (error = target - state) |
error_dot | d(Error)/(dt_ns/1e9) since last call |
dt_ns | Change in time since last call, measured in nanoseconds. |
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.
error | Error since last call (error = target - state) |
error_dot | d(Error)/(dt/1e9) since last call |
dt | Change in time since last call in nanoseconds |
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
.
error | Error since last call (error = target - state) |
dt | Change in time since last call in nanoseconds |
void control_toolbox::Pid::get_current_pid_errors | ( | double & | pe, |
double & | ie, | ||
double & | de | ||
) |
Return PID error terms for the controller.
pe | The proportional error. |
ie | The integral error. |
de | The derivative error. |
Pid::Gains control_toolbox::Pid::get_gains | ( | ) |
Get PID gains for the controller.
void control_toolbox::Pid::get_gains | ( | double & | p, |
double & | i, | ||
double & | d, | ||
double & | i_max, | ||
double & | i_min | ||
) |
Get PID gains for the controller.
p | The proportional gain. |
i | The integral gain. |
d | The derivative gain. |
i_max | Upper integral clamp. |
i_min | Lower integral clamp. |
void control_toolbox::Pid::get_gains | ( | double & | p, |
double & | i, | ||
double & | d, | ||
double & | i_max, | ||
double & | i_min, | ||
bool & | antiwindup | ||
) |
Get PID gains for the controller.
p | The proportional gain. |
i | The integral gain. |
d | The derivative gain. |
i_max | Upper integral clamp. |
i_min | Lower integral clamp. |
antiwindup | Antiwindup functionality. When set to true, limits the integral error to prevent windup; otherwise, constrains the integral contribution to the control output. i_max and i_min are applied in both scenarios. |
void control_toolbox::Pid::getCurrentPIDErrors | ( | double & | pe, |
double & | ie, | ||
double & | de | ||
) |
Return PID error terms for the controller.
pe | The proportional error. |
ie | The integral error. |
de | The derivative error. |
Pid::Gains control_toolbox::Pid::getGains | ( | ) |
Get PID gains for the controller.
void control_toolbox::Pid::getGains | ( | double & | p, |
double & | i, | ||
double & | d, | ||
double & | i_max, | ||
double & | i_min | ||
) |
Get PID gains for the controller.
p | The proportional gain. |
i | The integral gain. |
d | The derivative gain. |
i_max | Upper integral clamp. |
i_min | Lower integral clamp. |
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.
p | The proportional gain. |
i | The integral gain. |
d | The derivative gain. |
i_max | Upper integral clamp. |
i_min | Lower integral clamp. |
antiwindup | Antiwindup functionality. When set to true, limits the integral error to prevent windup; otherwise, constrains the integral contribution to the control output. i_max and i_min are applied in both scenarios. |
void control_toolbox::Pid::initialize | ( | 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.
p | The proportional gain. |
i | The integral gain. |
d | The derivative gain. |
i_max | Upper integral clamp. |
i_min | Lower integral clamp. |
antiwindup | Antiwindup functionality. When set to true, limits the integral error to prevent windup; otherwise, constrains the integral contribution to the control output. i_max and i_min are applied in both scenarios. |
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.
p | The proportional gain. |
i | The integral gain. |
d | The derivative gain. |
i_max | Upper integral clamp. |
i_min | Lower integral clamp. |
antiwindup | Antiwindup functionality. When set to true, limits the integral error to prevent windup; otherwise, constrains the integral contribution to the control output. i_max and i_min are applied in both scenarios. |
void control_toolbox::Pid::reset | ( | ) |
Reset the state of this PID controller.
void control_toolbox::Pid::reset | ( | bool | save_iterm | ) |
Reset the state of this PID controller.
save_iterm | boolean indicating if integral term is retained on reset() |
void control_toolbox::Pid::set_gains | ( | const Gains & | gains | ) |
Set PID gains for the controller.
gains | A struct of the PID gain values |
void control_toolbox::Pid::set_gains | ( | double | p, |
double | i, | ||
double | d, | ||
double | i_max, | ||
double | i_min, | ||
bool | antiwindup = false |
||
) |
Set PID gains for the controller.
p | The proportional gain. |
i | The integral gain. |
d | The derivative gain. |
i_max | Upper integral clamp. |
i_min | Lower integral clamp. |
antiwindup | Antiwindup functionality. When set to true, limits the integral error to prevent windup; otherwise, constrains the integral contribution to the control output. i_max and i_min are applied in both scenarios. |
void control_toolbox::Pid::setGains | ( | const Gains & | gains | ) |
Set PID gains for the controller.
gains | A struct of the PID gain values |
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.
p | The proportional gain. |
i | The integral gain. |
d | The derivative gain. |
i_max | Upper integral clamp. |
i_min | Lower integral clamp. |
antiwindup | Antiwindup functionality. When set to true, limits the integral error to prevent windup; otherwise, constrains the integral contribution to the control output. i_max and i_min are applied in both scenarios. |
|
protected |
Derivative of error.
|
protected |
Integral of error.
|
protected |
Command to send.
|
protected |
Error.
|
protected |
Save state for derivative state calculation.