Template
Chargement...
Recherche...
Aucune correspondance
pid.h
1#ifndef PID_H
2#define PID_H
3
4#include <Arduino.h>
5#include <float.h>
6
7namespace PIDI {
8 struct valeursPID {
9 valeursPID() : Kp(0.0), Ki(0.0), Kd(0.0), initialTime(0), Sp(0.0), Pv(0.0), integral(0.0), previous_error(0.0), previous_derivative(0.0), Out(0.0), Min(-INFINITY), Max(INFINITY) {}
10 float Kp; // Constante proportionnelle
11 float Ki; // Constante intégrale
12 float Kd; // Constante dérivée
13 long initialTime; // Temps initial
14 float Sp; // Set Point (Valeur voulue)
15 float Pv; // Process Value (Valeur réelle)
16 float integral; // Valeur intégrale
17 float previous_error; // Previous error value
19 float Out; // Valeur de sortie
20 float Min;
21 float Max;
22
23 static valeursPID create(float Kp, float Ki, float Kd, float integralCutOff = INFINITY) {
24 valeursPID pid = {};
25 pid.set(Kp, Ki, Kd, integralCutOff);
26 return pid;
27 }
28
29 void set(float Kp, float Ki, float Kd, float Min = -INFINITY, float Max = INFINITY) {
30 this->Kp = Kp;
31 this->Ki = Ki;
32 this->Kd = Kd;
33 this->Min = Min;
34 this->Max = Max;
35 }
36
37 float update() {
38 unsigned long currentTime = micros(); // Get current time in milliseconds
39
40 // Calculate the time difference (dt) since the last update
41 float dt = (currentTime - initialTime) / 1000000.0; // Convert to seconds
42
43 // Calculate the error
44 float error = Sp - Pv;
45
46 // Make not NaN or Infinite values are introduced
47 if (!isfinite(error) || !isfinite(dt)) {
48 return Out;
49 }
50
51 // Make sure dt isn't too small or too large
52 const float epsilon_min = 0.000005;
53 const float epsilon_max = 5;
54 if (dt < epsilon_min || dt > epsilon_max || dt == 0) {
55 return Out;
56 }
57
58 // Update the integral term
59 if (Out > Min && Out < Max) {
60 integral += error * dt;
61 } else {
62 integral = 0;
63 }
64
65 // Make sure it doesn't overflow
66 integral = constrain(integral, -FLT_MAX, FLT_MAX);
67
68 // Calculate the derivative term
69 float derivative = (error - previous_error) / dt;
70
71 // Calculate the control output
72 Out = constrain((Kp * error) + (Ki * integral) + (Kd * derivative), Min, Max);
73
74 // Store the current time for the next update
75 initialTime = currentTime;
76
77 // Store the current error for the next iteration
78 previous_error = error;
79 previous_derivative = derivative;
80
81 return Out;
82 }
83 };
84};
85
86#endif // PID_H
Definition: pid.h:7
Definition: pid.h:8
float Kp
Definition: pid.h:10
long initialTime
Definition: pid.h:13
valeursPID()
Definition: pid.h:9
float Out
Definition: pid.h:19
float Pv
Definition: pid.h:15
float Kd
Definition: pid.h:12
float Sp
Definition: pid.h:14
float Min
Definition: pid.h:20
float integral
Definition: pid.h:16
void set(float Kp, float Ki, float Kd, float Min=-INFINITY, float Max=INFINITY)
Definition: pid.h:29
float Max
Definition: pid.h:21
static valeursPID create(float Kp, float Ki, float Kd, float integralCutOff=INFINITY)
Definition: pid.h:23
float previous_error
Definition: pid.h:17
float update()
Definition: pid.h:37
float Ki
Definition: pid.h:11
float previous_derivative
Definition: pid.h:18