Robotics Library  0.7.0
Pid.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Markus Rickert
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 // POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 #ifndef RL_MATH_PID_H
28 #define RL_MATH_PID_H
29 
30 #include "Real.h"
31 
32 namespace rl
33 {
34  namespace math
35  {
39  template<typename T>
40  class Pid
41  {
42  public:
43  Pid() :
44  e(0),
45  kd(0),
46  ki(0),
47  kp(0),
48  i(0)
49  {
50 
51  }
52 
53  virtual ~Pid()
54  {
55 
56  }
57 
65  T operator()(const T& x, const Real& dt)
66  {
67  T e = this->x - x;
68  T p = this->kp * e;
69  this->i += this->ki * e * dt;
70  T d = this->kd * (e - this->e) / dt;
71  this->e = e;
72  return x + p + this->i + d;
73  }
74 
75  void reset()
76  {
77  this->e = 0;
78  this->i = 0;
79  }
80 
85  T kd;
86 
91  T ki;
92 
97  T kp;
98 
100  T x;
101 
102  protected:
103 
104  private:
106  T e;
107 
112  T i;
113  };
114  }
115 }
116 
117 #endif // RL_MATH_PID_H
rl::math::Pid::i
T i
Integral output.
Definition: Pid.h:112
rl::math::Pid
Proportional-Integral-Derivative controller.
Definition: Pid.h:41
rl::math::Pid::e
T e
Previous error.
Definition: Pid.h:106
rl::math::Pid::kd
T kd
Derivative gain.
Definition: Pid.h:85
rl::math::Pid::Pid
Pid()
Definition: Pid.h:43
rl::math::Pid::reset
void reset()
Definition: Pid.h:75
rl::math::Pid::~Pid
virtual ~Pid()
Definition: Pid.h:53
rl::math::Pid::ki
T ki
Integral gain.
Definition: Pid.h:91
rl::math::Pid::x
T x
Setpoint.
Definition: Pid.h:100
rl::math::Pid::operator()
T operator()(const T &x, const Real &dt)
Calculate next step.
Definition: Pid.h:65
Real.h
rl::math::Pid::kp
T kp
Proportional gain.
Definition: Pid.h:97
rl::math::Real
double Real
Definition: Real.h:42
rl
Robotics Library.
Definition: AnalogInput.cpp:30