arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
PIDController.h
1
2
3#include <cmath>
4
5#pragma once
6
7namespace audio_tools {
8
16 public:
17 // dt - loop interval time
18 // max - maximum value of manipulated variable
19 // min - minimum value of manipulated variable
20 // kp - proportional gain
21 // ki - Integral gain
22 // kd - derivative gain
23 bool begin(float dt, float max, float min, float kp, float ki, float kd) {
24 this->dt = dt;
25 this->max = max;
26 this->min = min;
27 this->kp = kp;
28 this->kd = kd;
29 this->ki = ki;
30 return true;
31 }
32
33 // target = desired process value
34 // measured = current process value:
35 // returns new process value
36 float calculate(float target, float measured) {
37 // Calculate errori
38 float error = target - measured;
39
40 // Proportional term
41 float pout = kp * error;
42
43 // Interal term
44 integral += error * dt;
45 float Iout = ki * integral;
46
47 // Derivative term
48 assert(dt != 0.0);
49
50 float derivative = (error - preerror) / dt;
51 float dout = kd * derivative;
52
53 // Calculate total output
54 float output = pout + Iout + dout;
55
56 // Restrict to max/min
57 if (output > max)
58 output = max;
59 else if (output < min)
60 output = min;
61
62 // Save error to previous error
63 preerror = error;
64
65 return output;
66 }
67
68 protected:
69 float dt = 1.0f;
70 float max = 0.0f;
71 float min = 0.0f;
72 float kp = 0.0f;
73 float kd = 0.0f;
74 float ki = 0.0f;
75 float preerror = 0.0f;
76 float integral = 0.0f;
77
78}; // namespace audiotools
79
80} // namespace audio_tools
A simple header only PID Controller.
Definition PIDController.h:15
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10