-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
61 lines (53 loc) · 1.25 KB
/
main.js
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
// Real-time PID controller
//=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=//
// Initializing of PID
var Kp = 0 ;
var Ki = 9 ;
var Kd = 8 ;
var Ts = ;
var Isat = ;
var u_min = ;
var u_max = ;
var ref = ;
//pid controller configuration
var PID_value = [Kp,Ki,Kd,Ts,Isat];
var SRead1 = 0 ; // current state of the output
// PID controller
var U=0;
var err = 0;
var err_prev = 0;
var I_u = 0;
var ts, kp, ki, kd, Is;
var temp=0;
pid_control(ref,PID_value,SRead1);
function pid_control (ref,param,SRead1)
{
// pid controller initialization
kp =param[0] ;
ki = param[1];
kd = param[2];
ts = param[3];
Is = param[4];
var err = ref-SRead1;
if ( ki === 0.0 ){
I_u = 0.0; // Set the I part to zero if Ki == 0
}
else
{
temp = ki*err*ts;
I_u = I_u +temp;
}
/* Saturation of the I part */
if ( Is > 0.0 )
{
if ( I_u > Is ) I_u = Is;
if ( I_u < -Is ) I_u = -Is;
}
/* Add vertical PID parts: PID = P + I + D */
U = kp*err + I_u + kd*(err-err_prev)/ts ;
err_prev = err;
/* Check control limits */
if( U > u_max ) U = u_max;
if( U < u_min ) U = u_min;
console.log(U);
}