1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
'use strict';
var classes = classes || {};
classes.PidController = function () {
var self = {},
privateScope = {};
/**
*
* @type {number}
*/
privateScope.target = null;
/**
*
* @type {{P: null, I: null, D: null}}
*/
privateScope.gains = {
P: null,
I: null,
D: null
};
/**
*
* @type {number}
*/
privateScope.Iterm = 0;
/**
*
* @type {{min: number, max: number}}
*/
privateScope.ItermLimit = {
min: -1000,
max: 1000
};
/**
*
* @type {number}
*/
privateScope.previousError = 0;
/**
*
* @type {{min: number, max: number, minThreshold: number}}
*/
privateScope.output = {
min: null,
max: null,
minThreshold: null
};
/**
*
* @param {number} value
*/
self.setTarget = function (value) {
privateScope.target = value;
};
/**
* @param {number} Pgain
* @param {number} Igain
* @param {number} Dgain
*/
self.setGains = function (Pgain, Igain, Dgain) {
privateScope.gains.P = Pgain;
privateScope.gains.I = Igain;
privateScope.gains.D = Dgain;
};
/**
* Sets min and max value for output
* @param {number} min
* @param {number} max
* @param {number} minThreshold if output is below this value, [min] is returned
*/
self.setOutput = function (min, max, minThreshold) {
privateScope.output.min = min;
privateScope.output.max = max;
privateScope.output.minThreshold = minThreshold;
};
/**
* Sets upper and lower limit for Iterm accumulator
* @param {number} min
* @param {number} max
*/
self.setItermLimit = function (min, max) {
privateScope.ItermLimit.min = min;
privateScope.ItermLimit.max = max;
};
/**
* Executes PID controller based on current value and target
* @param {number} current
* @returns {number}
*/
self.run = function (current) {
var error = current - privateScope.target,
Pterm = error * privateScope.gains.P,
Dterm = (error - privateScope.previousError) * privateScope.gains.D,
output;
privateScope.previousError = error;
privateScope.Iterm += error * privateScope.gains.I;
if (privateScope.Iterm > privateScope.ItermLimit.max) {
privateScope.Iterm = privateScope.ItermLimit.max;
} else if (privateScope.Iterm < privateScope.ItermLimit.min) {
privateScope.Iterm = privateScope.ItermLimit.min;
}
output = Pterm + privateScope.Iterm + Dterm;
if (output < privateScope.output.minThreshold) {
output = privateScope.output.min;
} else if (output > privateScope.output.max) {
output = privateScope.output.max;
}
return output;
};
return self;
};
|