ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
PID.cpp
Go to the documentation of this file.
1#include "PID.h"
2
4 integral(0.0f),
5 error(0.0f),
6 previousError(0.0f),
7 output(0.0f),
8 kp(1.0f),
9 ki(0.0f),
10 kd(0.0f),
11 previousSeconds(0.0f) {}
12
13PID::PID(float kp, float ki, float kd) :
14 integral(0.0f),
15 error(0.0f),
16 previousError(0.0f),
17 output(0.0f),
18 kp(kp),
19 ki(ki),
20 kd(kd),
21 previousSeconds(0.0f) {}
22
24
25float PID::Calculate(float setpoint, float processVariable, unsigned long currentMillis) {
26 float POut, IOut, DOut;
27
28 float currentSeconds = currentMillis / 1000.0f;
29 float dT = currentSeconds - previousSeconds;
30
31 error = setpoint - processVariable;
32 integral += error * dT;
33
34 POut = kp * error;
35 IOut = ki * integral;
36 DOut = kd * ((error - previousError) / dT);
37
38 output = POut + IOut + DOut;
40
41 previousSeconds = currentSeconds;
42
43 return output;
44}
45
46float PID::Calculate(float setpoint, float processVariable, float dT) {
47 float POut, IOut, DOut;
48
49 error = setpoint - processVariable;
50 integral += error * dT;
51
52 POut = kp * error;
53 IOut = ki * integral;
54 DOut = kd * ((error - previousError) / dT);
55
56 output = POut + IOut + DOut;
58
59 return output;
60}
Defines the PID class for implementing a proportional-integral-derivative controller.
float ki
Integral gain.
Definition PID.h:29
float output
Calculated output value.
Definition PID.h:27
float integral
Integral term accumulator.
Definition PID.h:24
float Calculate(float setpoint, float processVariable, unsigned long currentMillis)
Calculates the control output based on the setpoint and process variable.
Definition PID.cpp:25
float kp
Proportional gain.
Definition PID.h:28
float previousSeconds
Time of the previous calculation in seconds.
Definition PID.h:31
float error
Current error value.
Definition PID.h:25
float previousError
Previous error value for derivative calculation.
Definition PID.h:26
PID()
Default constructor for PID, initializes gains to zero.
Definition PID.cpp:3
~PID()
Destructor for PID.
Definition PID.cpp:23
float kd
Derivative gain.
Definition PID.h:30