ros2_control - rolling
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 <iostream>
37 #include <memory>
38 #include <stdexcept>
39 #include <string>
40 
41 #include "realtime_tools/realtime_buffer.hpp"
42 
43 #include "control_toolbox/visibility_control.hpp"
44 
45 namespace control_toolbox
46 {
47 /***************************************************/
103 /***************************************************/
104 
105 class CONTROL_TOOLBOX_PUBLIC Pid
106 {
107 public:
111  struct Gains
112  {
124  Gains(double p, double i, double d, double i_max, double i_min)
125  : p_gain_(p), i_gain_(i), d_gain_(d), i_max_(i_max), i_min_(i_min), antiwindup_(true)
126  {
127  }
128 
141  Gains(double p, double i, double d, double i_max, double i_min, bool antiwindup)
142  : p_gain_(p), i_gain_(i), d_gain_(d), i_max_(i_max), i_min_(i_min), antiwindup_(antiwindup)
143  {
144  }
145  // Default constructor
146  Gains() : p_gain_(0.0), i_gain_(0.0), d_gain_(0.0), i_max_(0.0), i_min_(0.0), antiwindup_(false)
147  {
148  }
149  double p_gain_;
150  double i_gain_;
151  double d_gain_;
152  double i_max_;
153  double i_min_;
154  bool antiwindup_;
155  };
156 
171  Pid(
172  double p = 0.0, double i = 0.0, double d = 0.0, double i_max = 0.0, double i_min = -0.0,
173  bool antiwindup = false);
174 
179  Pid(const Pid & source);
180 
184  ~Pid();
185 
199  void initPid(double p, double i, double d, double i_max, double i_min, bool antiwindup = false);
200 
204  void reset();
205 
214  void getGains(double & p, double & i, double & d, double & i_max, double & i_min);
224  void getGains(
225  double & p, double & i, double & d, double & i_max, double & i_min, bool & antiwindup);
226 
231  Gains getGains();
232 
244  void setGains(double p, double i, double d, double i_max, double i_min, bool antiwindup = false);
245 
252  void setGains(const Gains & gains);
253 
264  [[nodiscard]] double computeCommand(double error, uint64_t dt);
265 
277  [[nodiscard]] double computeCommand(double error, double error_dot, uint64_t dt);
278 
282  void setCurrentCmd(double cmd);
283 
287  double getCurrentCmd();
288 
292  double getDerivativeError();
293 
300  void getCurrentPIDErrors(double & pe, double & ie, double & de);
301 
306  Pid & operator=(const Pid & source)
307  {
308  if (this == &source) {
309  return *this;
310  }
311 
312  // Copy the realtime buffer to then new PID class
313  gains_buffer_ = source.gains_buffer_;
314 
315  // Reset the state of this PID controller
316  reset();
317 
318  return *this;
319  }
320 
321 protected:
322  // Store the PID gains in a realtime buffer to allow dynamic reconfigure to update it without
323  // blocking the realtime update loop
325 
326  double p_error_last_;
327  double p_error_;
328  double i_error_;
329  double d_error_;
330  double cmd_;
331  double error_dot_;
332 };
333 
334 } // namespace control_toolbox
335 
336 #endif // CONTROL_TOOLBOX__PID_HPP_
A basic pid class.
Definition: pid.hpp:106
double error_dot_
Definition: pid.hpp:331
double p_error_
Definition: pid.hpp:327
double p_error_last_
Definition: pid.hpp:326
double d_error_
Definition: pid.hpp:329
Pid & operator=(const Pid &source)
Custom assignment operator Does not initialize dynamic reconfigure for PID gains.
Definition: pid.hpp:306
double cmd_
Definition: pid.hpp:330
double i_error_
Definition: pid.hpp:328
Definition: realtime_buffer.hpp:44
Definition: dither.hpp:46
Store gains in a struct to allow easier realtime buffer usage.
Definition: pid.hpp:112
double d_gain_
Definition: pid.hpp:151
double i_min_
Definition: pid.hpp:153
double i_gain_
Definition: pid.hpp:150
Gains(double p, double i, double d, double i_max, double i_min, bool antiwindup)
Optional constructor for passing in values.
Definition: pid.hpp:141
Gains(double p, double i, double d, double i_max, double i_min)
Optional constructor for passing in values without antiwindup.
Definition: pid.hpp:124
bool antiwindup_
Definition: pid.hpp:154
double i_max_
Definition: pid.hpp:152
double p_gain_
Definition: pid.hpp:149