arduino-audio-tools
Loading...
Searching...
No Matches
src
AudioTools
CoreAudio
AudioBasic
PIDController.h
Go to the documentation of this file.
1
2
3
#include <cmath>
4
5
#pragma once
6
7
namespace
audio_tools
{
8
15
class
PIDController
{
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
assert
#define assert(T)
Definition
avr.h:10
audio_tools::PIDController
A simple header only PID Controller.
Definition
PIDController.h:15
audio_tools::PIDController::ki
float ki
Definition
PIDController.h:74
audio_tools::PIDController::calculate
float calculate(float target, float measured)
Definition
PIDController.h:36
audio_tools::PIDController::integral
float integral
Definition
PIDController.h:76
audio_tools::PIDController::kp
float kp
Definition
PIDController.h:72
audio_tools::PIDController::max
float max
Definition
PIDController.h:70
audio_tools::PIDController::preerror
float preerror
Definition
PIDController.h:75
audio_tools::PIDController::dt
float dt
Definition
PIDController.h:69
audio_tools::PIDController::min
float min
Definition
PIDController.h:71
audio_tools::PIDController::kd
float kd
Definition
PIDController.h:73
audio_tools::PIDController::begin
bool begin(float dt, float max, float min, float kp, float ki, float kd)
Definition
PIDController.h:23
audio_tools
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition
AudioCodecsBase.h:10
audio_tools::writeData
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition
AudioTypes.h:512
Generated by
1.9.8