Khangai Robot Play - Naive  01
THe naive play of the Khangai Robot
pid_algorithms.h
Go to the documentation of this file.
1 /*
2  * pid_algrithms.h
3  *
4  * Created : 11/10/2018
5  * Author : n-is
6  * email : 073bex422.nischal@pcampus.edu.np
7  */
8 
9 #ifndef _PID_ALGORITHMS_H_
10 #define _PID_ALGORITHMS_H_
11 
12 #include <stdint.h>
13 
15 {
16 public:
17  PID_Algorithm() { set_PID(0,0,0); }
18  PID_Algorithm(float p, float i, float d) { set_PID(p,i,d); }
19  virtual float compute(float error, uint32_t dt_millis) = 0;
20  virtual void clear() = 0;
21 
22  void set_P(float p) { p_ = p; }
23  void set_I(float i) { i_ = i; }
24  void set_D(float d) { d_ = d; }
25  void set_PID(float p, float i, float d) {
26  set_P(p);
27  set_I(i);
28  set_D(d);
29  }
30  float get_P() { return p_; }
31  float get_I() { return i_; }
32  float get_D() { return d_; }
33 
34  void set_Limits(float max_out, float min_out) {
35  max_ = max_out;
36  min_ = min_out;
37  }
38 
39  float get_Upper() { return max_; }
40  float get_Lower() { return min_; }
41 
42  virtual ~PID_Algorithm() { }
43 
44 protected:
45  float p_, i_, d_;
46  float max_, min_;
47 };
48 
50 {
51 public:
52  Discrete_PID(float p, float i, float d) :
53  PID_Algorithm(p, i, d) {
54  clear();
55  }
57  clear();
58  }
59  Discrete_PID(Discrete_PID &&) = default;
60  Discrete_PID(const Discrete_PID &) = default;
61  Discrete_PID &operator=(Discrete_PID &&) = default;
62  Discrete_PID &operator=(const Discrete_PID &) = default;
64 
65  /* *** PID Algorithm Description ***
66  * 1) Discrete PID control Algorithm
67  * 2) Integrator Method : Forward Euler
68  * 3) //! Filtered Derivative not used
69  * 4) Output Limited
70  * 5) Form : Parallel
71  * 6) Compensator Formula : Dz = P + I*Ts/(z-1) + D*(z-1)/(Ts*z)
72  * 7) In Time Domain :
73  * y(t) - y(t-1) = a*x(t) + b*x(t-1) + c*x(t-2)
74  * where,
75  * a = P + D/Ts
76  * b = -P + I*Ts - 2*D/Ts
77  * c = D/Ts
78  */
79  float compute(float error, uint32_t dt_millis) {
80  float Ts = (float)dt_millis / 1000.0;
81 
82  float P = get_P();
83  float I = get_I();
84  // We assume that Ts is never zero
85  float D_by_Ts = get_D() / Ts;
86 
87  float a = P + D_by_Ts;
88  float b = -P + I*Ts - 2*D_by_Ts;
89  float c = D_by_Ts;
90 
91  l_output_ += a*error + b*l_err_ + c*ll_err_;
92 
93  if (l_output_ > get_Upper()) {
94  l_output_ = get_Upper();
95  }
96  else if (l_output_ < get_Lower()) {
97  l_output_ = get_Lower();
98  }
99 
100  ll_err_ = l_err_;
101  l_err_ = error;
102 
103  return l_output_;
104  }
105 
106  void clear() {
107  l_output_ = 0;
108  l_err_ = 0;
109  ll_err_ = 0;
110  }
111 
112 private:
113  float l_output_;
114  float l_err_;
115  float ll_err_;
116 };
117 
118 #endif // !_PID_ALGORITHMS_H_
float get_Lower()
Definition: pid_algorithms.h:40
float i_
Definition: pid_algorithms.h:45
void set_Limits(float max_out, float min_out)
Definition: pid_algorithms.h:34
PID_Algorithm()
Definition: pid_algorithms.h:17
virtual float compute(float error, uint32_t dt_millis)=0
void clear()
Definition: pid_algorithms.h:106
Discrete_PID()
Definition: pid_algorithms.h:56
void set_D(float d)
Definition: pid_algorithms.h:24
void set_PID(float p, float i, float d)
Definition: pid_algorithms.h:25
Definition: pid_algorithms.h:14
float get_P()
Definition: pid_algorithms.h:30
virtual ~PID_Algorithm()
Definition: pid_algorithms.h:42
float get_Upper()
Definition: pid_algorithms.h:39
float compute(float error, uint32_t dt_millis)
Definition: pid_algorithms.h:79
void error(Error err)
Definition: error.cpp:13
Discrete_PID(float p, float i, float d)
Definition: pid_algorithms.h:52
float ll_err_
Definition: pid_algorithms.h:115
float d_
Definition: pid_algorithms.h:45
float l_output_
Definition: pid_algorithms.h:113
float p_
Definition: pid_algorithms.h:45
float max_
Definition: pid_algorithms.h:46
float l_err_
Definition: pid_algorithms.h:114
Discrete_PID & operator=(Discrete_PID &&)=default
PID_Algorithm(float p, float i, float d)
Definition: pid_algorithms.h:18
Definition: pid_algorithms.h:49
void set_P(float p)
Definition: pid_algorithms.h:22
~Discrete_PID()
Definition: pid_algorithms.h:63
float min_
Definition: pid_algorithms.h:46
float get_I()
Definition: pid_algorithms.h:31
float get_D()
Definition: pid_algorithms.h:32
virtual void clear()=0
void set_I(float i)
Definition: pid_algorithms.h:23