ros2_control - iron
Loading...
Searching...
No Matches
pid.hpp
1// Copyright (c) 2008, Willow Garage, Inc.
2// All rights reserved.
3//
4// Software License Agreement (BSD License 2.0)
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions
8// are met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following
14// disclaimer in the documentation and/or other materials provided
15// with the distribution.
16// * Neither the name of the Willow Garage nor the names of its
17// contributors may be used to endorse or promote products derived
18// from this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31// POSSIBILITY OF SUCH DAMAGE.
32
33#ifndef CONTROL_TOOLBOX__PID_HPP_
34#define CONTROL_TOOLBOX__PID_HPP_
35
36#include <chrono>
37
38#include "rclcpp/duration.hpp"
39#include "realtime_tools/realtime_buffer.hpp"
40
41namespace control_toolbox
42{
43/***************************************************/
107/***************************************************/
108
109class Pid
110{
111public:
115 struct Gains
116 {
127 Gains(double p, double i, double d, double i_max, double i_min)
128 : p_gain_(p), i_gain_(i), d_gain_(d), i_max_(i_max), i_min_(i_min), antiwindup_(true)
129 {
130 }
131
146 Gains(double p, double i, double d, double i_max, double i_min, bool antiwindup)
147 : p_gain_(p), i_gain_(i), d_gain_(d), i_max_(i_max), i_min_(i_min), antiwindup_(antiwindup)
148 {
149 }
150
151 // Default constructor
152 Gains() : p_gain_(0.0), i_gain_(0.0), d_gain_(0.0), i_max_(0.0), i_min_(0.0), antiwindup_(false)
153 {
154 }
155
156 double p_gain_;
157 double i_gain_;
158 double d_gain_;
159 double i_max_;
160 double i_min_;
162 };
163
181 Pid(
182 double p = 0.0, double i = 0.0, double d = 0.0, double i_max = 0.0, double i_min = -0.0,
183 bool antiwindup = false);
184
189 Pid(const Pid & source);
190
194 ~Pid();
195
212 void initialize(
213 double p, double i, double d, double i_max, double i_min, bool antiwindup = false);
214
219 void reset();
220
226 void reset(bool save_i_term);
227
231 void clear_saved_iterm();
232
241 void get_gains(double & p, double & i, double & d, double & i_max, double & i_min);
242
255 void get_gains(
256 double & p, double & i, double & d, double & i_max, double & i_min, bool & antiwindup);
257
263
278 void set_gains(double p, double i, double d, double i_max, double i_min, bool antiwindup = false);
279
286 void set_gains(const Gains & gains);
287
298 [[nodiscard]] double compute_command(double error, const double & dt_s);
299
310 [[nodiscard]] double compute_command(double error, const rcl_duration_value_t & dt_ns);
311
322 [[nodiscard]] double compute_command(double error, const rclcpp::Duration & dt);
323
334 [[nodiscard]] double compute_command(double error, const std::chrono::nanoseconds & dt_ns);
335
347 [[nodiscard]] double compute_command(double error, double error_dot, const double & dt_s);
348
360 [[nodiscard]] double compute_command(
361 double error, double error_dot, const rcl_duration_value_t & dt_ns);
362
374 [[nodiscard]] double compute_command(double error, double error_dot, const rclcpp::Duration & dt);
375
387 [[nodiscard]] double compute_command(
388 double error, double error_dot, const std::chrono::nanoseconds & dt_ns);
389
393 void set_current_cmd(double cmd);
394
398 double get_current_cmd();
399
406 void get_current_pid_errors(double & pe, double & ie, double & de);
407
412 Pid & operator=(const Pid & source)
413 {
414 if (this == &source) {
415 return *this;
416 }
417
418 // Copy the realtime buffer to then new PID class
419 gains_buffer_ = source.gains_buffer_;
420
421 // Reset the state of this PID controller
422 reset();
423
424 return *this;
425 }
426
427protected:
428 // Store the PID gains in a realtime buffer to allow dynamic reconfigure to update it without
429 // blocking the realtime update loop
431
432 double p_error_last_;
433 double p_error_;
434 double d_error_;
435 double i_term_;
436 double cmd_;
437};
438
439} // namespace control_toolbox
440
441#endif // CONTROL_TOOLBOX__PID_HPP_
A basic pid class.
Definition pid.hpp:110
void clear_saved_iterm()
Clear the saved integrator output of this controller.
Definition pid.cpp:100
Pid & operator=(const Pid &source)
Custom assignment operator Does not initialize dynamic reconfigure for PID gains.
Definition pid.hpp:412
Gains get_gains()
Get PID gains for the controller.
Definition pid.cpp:124
~Pid()
Destructor of Pid class.
Definition pid.cpp:75
void set_current_cmd(double cmd)
Set current command for this PID controller.
Definition pid.cpp:216
double p_error_
Definition pid.hpp:433
void reset()
Reset the state of this PID controller.
Definition pid.cpp:84
void get_current_pid_errors(double &pe, double &ie, double &de)
Return PID error terms for the controller.
Definition pid.cpp:220
double d_error_
Definition pid.hpp:434
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...
Definition pid.cpp:77
void set_gains(double p, double i, double d, double i_max, double i_min, bool antiwindup=false)
Set PID gains for the controller.
Definition pid.cpp:126
double get_current_cmd()
Return current command for this PID controller.
Definition pid.cpp:218
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...
Definition pid.cpp:142
double cmd_
Definition pid.hpp:436
double i_term_
Definition pid.hpp:435
Definition realtime_buffer.hpp:44
Definition dither.hpp:46
Store gains in a struct to allow easier realtime buffer usage.
Definition pid.hpp:116
double d_gain_
Definition pid.hpp:158
double i_min_
Definition pid.hpp:160
double i_gain_
Definition pid.hpp:157
Gains(double p, double i, double d, double i_max, double i_min, bool antiwindup)
Optional constructor for passing in values.
Definition pid.hpp:146
Gains(double p, double i, double d, double i_max, double i_min)
Optional constructor for passing in values without antiwindup.
Definition pid.hpp:127
bool antiwindup_
Definition pid.hpp:161
double i_max_
Definition pid.hpp:159
double p_gain_
Definition pid.hpp:156