ros2_control - foxy
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 <memory>
37#include <string>
38
39#include "rclcpp/clock.hpp"
40#include "rclcpp/duration.hpp"
41#include "rclcpp/node.hpp"
42
43#include "realtime_tools/realtime_buffer.h"
44#include "realtime_tools/realtime_publisher.h"
45
46#include "control_toolbox/visibility_control.hpp"
47
48namespace control_toolbox
49{
50/***************************************************/
106/***************************************************/
107
108class CONTROL_TOOLBOX_PUBLIC Pid
109{
110public:
114 struct Gains
115 {
116 // Optional constructor for passing in values without antiwindup
117 Gains(double p, double i, double d, double i_max, double i_min)
118 : p_gain_(p), i_gain_(i), d_gain_(d), i_max_(i_max), i_min_(i_min), antiwindup_(false)
119 {
120 }
121 // Optional constructor for passing in values
122 Gains(double p, double i, double d, double i_max, double i_min, bool antiwindup)
123 : p_gain_(p), i_gain_(i), d_gain_(d), i_max_(i_max), i_min_(i_min), antiwindup_(antiwindup)
124 {
125 }
126 // Default constructor
127 Gains() : p_gain_(0.0), i_gain_(0.0), d_gain_(0.0), i_max_(0.0), i_min_(0.0), antiwindup_(false)
128 {
129 }
130 double p_gain_;
131 double i_gain_;
132 double d_gain_;
133 double i_max_;
134 double i_min_;
136 };
137
149 Pid(
150 double p = 0.0, double i = 0.0, double d = 0.0, double i_max = 0.0, double i_min = -0.0,
151 bool antiwindup = false);
152
157 Pid(const Pid & source);
158
162 ~Pid();
163
174 void initPid(double p, double i, double d, double i_max, double i_min, bool antiwindup = false);
175
179 void reset();
180
189 void getGains(double & p, double & i, double & d, double & i_max, double & i_min);
190 void getGains(
191 double & p, double & i, double & d, double & i_max, double & i_min, bool & antiwindup);
192
197 Gains getGains();
198
207 void setGains(double p, double i, double d, double i_max, double i_min, bool antiwindup = false);
208
213 void setGains(const Gains & gains);
214
225 double computeCommand(double error, uint64_t dt);
226
238 double computeCommand(double error, double error_dot, uint64_t dt);
239
243 void setCurrentCmd(double cmd);
244
248 double getCurrentCmd();
249
253 double getDerivativeError();
254
261 void getCurrentPIDErrors(double & pe, double & ie, double & de);
262
267 Pid & operator=(const Pid & source)
268 {
269 if (this == &source) {
270 return *this;
271 }
272
273 // Copy the realtime buffer to then new PID class
274 gains_buffer_ = source.gains_buffer_;
275
276 // Reset the state of this PID controller
277 reset();
278
279 return *this;
280 }
281
282protected:
283 // Store the PID gains in a realtime buffer to allow dynamic reconfigure to update it without
284 // blocking the realtime update loop
286
288 double p_error_;
289 double i_error_;
290 double d_error_;
291 double cmd_;
292 double error_dot_;
293};
294
295} // namespace control_toolbox
296
297#endif // CONTROL_TOOLBOX__PID_HPP_
A basic pid class.
Definition pid.hpp:109
double error_dot_
Definition pid.hpp:292
Pid & operator=(const Pid &source)
Custom assignment operator Does not initialize dynamic reconfigure for PID gains.
Definition pid.hpp:267
double p_error_
Definition pid.hpp:288
double p_error_last_
Definition pid.hpp:287
double d_error_
Definition pid.hpp:290
double cmd_
Definition pid.hpp:291
double i_error_
Definition pid.hpp:289
Definition realtime_buffer.h:51
Definition dither.hpp:46
Store gains in a struct to allow easier realtime buffer usage.
Definition pid.hpp:115
double d_gain_
Definition pid.hpp:132
double i_min_
Definition pid.hpp:134
double i_gain_
Definition pid.hpp:131
bool antiwindup_
Definition pid.hpp:135
double i_max_
Definition pid.hpp:133
double p_gain_
Definition pid.hpp:130