Robobo
PID.h
1 #ifndef PID_H_INCLUDED
2 #define PID_H_INCLUDED
3 
4 #include "ControlStrategy.h"
5 #include "Arduino.h"
6 
7 class PID: public ControlStrategy
8 {
9  private:
10  float kP;
11  float kI;
12  float kD;
13  uint32_t prevMicros;
14  int32_t integral;
15 
16  public:
17  PID(float kP, float kI, float kD):
18  kP(kP),
19  kI(kI),
20  kD(kD),
21  prevMicros(0),
22  integral(0)
23  {
24  }
25 
26  int32_t calculateControl(int32_t diff)
27  {
28  uint32_t now = micros();
29  uint32_t timeDelta = now - prevMicros;
30  prevMicros = now;
31 
32  //TODO: need to address overflow scenario
33 
34  integral = integral + diff * timeDelta;
35 
36  return diff *kP + integral * kI;
37  }
38 };
39 
40 #endif
Definition: PID.h:7
Definition: ControlStrategy.h:4