PID.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef IGNITION_MATH_PID_HH_
18 #define IGNITION_MATH_PID_HH_
19 
20 #include <chrono>
21 #include <ignition/math/Helpers.hh>
22 #include <ignition/math/config.hh>
23 
24 namespace ignition
25 {
26  namespace math
27  {
28  inline namespace IGNITION_MATH_VERSION_NAMESPACE
29  {
36  class IGNITION_MATH_VISIBLE PID
37  {
55  public: PID(const double _p = 0.0,
56  const double _i = 0.0,
57  const double _d = 0.0,
58  const double _imax = -1.0,
59  const double _imin = 0.0,
60  const double _cmdMax = -1.0,
61  const double _cmdMin = 0.0,
62  const double _cmdOffset = 0.0);
63 
65  public: ~PID() = default;
66 
84  public: void Init(const double _p = 0.0,
85  const double _i = 0.0,
86  const double _d = 0.0,
87  const double _imax = -1.0,
88  const double _imin = 0.0,
89  const double _cmdMax = -1.0,
90  const double _cmdMin = 0.0,
91  const double _cmdOffset = 0.0);
92 
95  public: void SetPGain(const double _p);
96 
99  public: void SetIGain(const double _i);
100 
103  public: void SetDGain(const double _d);
104 
107  public: void SetIMax(const double _i);
108 
111  public: void SetIMin(const double _i);
112 
115  public: void SetCmdMax(const double _c);
116 
119  public: void SetCmdMin(const double _c);
120 
124  public: void SetCmdOffset(const double _c);
125 
128  public: double PGain() const;
129 
132  public: double IGain() const;
133 
136  public: double DGain() const;
137 
140  public: double IMax() const;
141 
144  public: double IMin() const;
145 
148  public: double CmdMax() const;
149 
152  public: double CmdMin() const;
153 
156  public: double CmdOffset() const;
157 
165  public: double Update(const double _error,
166  const std::chrono::duration<double> &_dt);
167 
170  public: void SetCmd(const double _cmd);
171 
174  public: double Cmd() const;
175 
180  public: void Errors(double &_pe, double &_ie, double &_de) const;
181 
185  public: PID &operator=(const PID &_p);
186 
188  public: void Reset();
189 
191  private: double pErrLast = 0.0;
192 
194  private: double pErr = 0.0;
195 
197  private: double iErr = 0.0;
198 
200  private: double dErr = 0.0;
201 
203  private: double pGain;
204 
206  private: double iGain = 0.0;
207 
209  private: double dGain = 0.0;
210 
212  private: double iMax = -1.0;
213 
215  private: double iMin = 0.0;
216 
218  private: double cmd = 0.0;
219 
221  private: double cmdMax = -1.0;
222 
224  private: double cmdMin = 0.0;
225 
227  private: double cmdOffset = 0.0;
228  };
229  }
230  }
231 }
232 #endif
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::Init
void Init(const double _p=0.0, const double _i=0.0, const double _d=0.0, const double _imax=-1.0, const double _imin=0.0, const double _cmdMax=-1.0, const double _cmdMin=0.0, const double _cmdOffset=0.0)
Initialize PID-gains and integral term limits:[iMax:iMin]-[I1:I2].
Helpers.hh
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::SetIMax
void SetIMax(const double _i)
Set the integral upper limit.
ignition
Definition: Angle.hh:40
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::IMax
double IMax() const
Get the integral upper limit.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::CmdMin
double CmdMin() const
Get the maximum value for the command.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::Reset
void Reset()
Reset the errors and command.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::CmdOffset
double CmdOffset() const
Get the offset value for the command.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::SetCmdMin
void SetCmdMin(const double _c)
Set the minimum value for the command.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::Errors
void Errors(double &_pe, double &_ie, double &_de) const
Return PID error terms for the controller.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::PGain
double PGain() const
Get the proportional Gain.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::SetIMin
void SetIMin(const double _i)
Set the integral lower limit.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::IMin
double IMin() const
Get the integral lower limit.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::SetCmdOffset
void SetCmdOffset(const double _c)
Set the offset value for the command, which is added to the result of the PID controller.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::IGain
double IGain() const
Get the integral Gain.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::CmdMax
double CmdMax() const
Get the maximum value for the command.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::DGain
double DGain() const
Get the derivative Gain.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::SetPGain
void SetPGain(const double _p)
Set the proportional Gain.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID
Generic PID controller class. Generic proportional-integral-derivative controller class that keeps tr...
Definition: PID.hh:37
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::SetCmd
void SetCmd(const double _cmd)
Set current target command for this PID controller.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::Cmd
double Cmd() const
Return current command for this PID controller.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::operator=
PID & operator=(const PID &_p)
Assignment operator.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::~PID
~PID()=default
Destructor.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::SetIGain
void SetIGain(const double _i)
Set the integral Gain.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::PID
PID(const double _p=0.0, const double _i=0.0, const double _d=0.0, const double _imax=-1.0, const double _imin=0.0, const double _cmdMax=-1.0, const double _cmdMin=0.0, const double _cmdOffset=0.0)
Constructor, zeros out Pid values when created and initialize Pid-gains and integral term limits:[iMa...
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::SetDGain
void SetDGain(const double _d)
Set the derivtive Gain.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::Update
double Update(const double _error, const std::chrono::duration< double > &_dt)
Update the Pid loop with nonuniform time step size.
ignition::math::IGNITION_MATH_VERSION_NAMESPACE::PID::SetCmdMax
void SetCmdMax(const double _c)
Set the maximum value for the command.