1 |
import jtt.*; // Import the JTT package
|
2 |
public class MyProcess { |
3 |
// Initialize the process |
4 |
public MyProcess ( ) { |
5 |
// create a task (a PID controller)
|
6 |
Task task = new Task ( ); |
7 |
task.setPeriod (0.012); // period = 12 ms
|
8 |
// add code to task
|
9 |
task.addCode (new CodeSegment ( ) { |
10 |
public double code ( ) { |
11 |
controlAction = calculate (reference, output); |
12 |
return 0.02; // 20 ms
|
13 |
} |
14 |
}); |
15 |
task.addCode (new CodeSegment ( ) { |
16 |
public double code ( ) { |
17 |
input = controlAction; |
18 |
return 0; // 0 ms
|
19 |
} |
20 |
}); |
21 |
// create kernel and add it the task . . .
|
22 |
} |
23 |
// Gets rate of the ODE model
|
24 |
public void getRate (double [] state, double [] rate) { |
25 |
rate [0] = state [1]; |
26 |
rate [1]= − state [1] + 1000 * input; |
27 |
rate [2] = 1; |
28 |
} |
29 |
// Step the process for an increment of time
|
30 |
public void step (double dt) { |
31 |
getRate (state, rates1); |
32 |
for (int i = 0; i <numEqn; i++) |
33 |
k1 [i]= state [i]+ stepSize * rates 1 [i]/2.0; |
34 |
getRate (k1, rates 2); |
35 |
for (int i = 0; i <numEqn; i++) |
36 |
k2 [i]= state [i]+ stepSize * rates 2 [i]/2.0; |
37 |
getRate (k2, rates 3); |
38 |
for (int i = 0; i <numEqn; i++) |
39 |
k3 [i]= state [i]+ stepSize*rates3 [i]; |
40 |
getRate (k3, rates4); |
41 |
for (int i = 0; i <numEqn; i++) |
42 |
state [i]= state [i]+stepSize*(rates1[i]+2*rates2[i]+2*rates3[i]+rates4[i])/6.0; |
43 |
} |
44 |
// Computes the PID
|
45 |
public double calculate (r, y) { |
46 |
P = Kp*(beta*r−y); I = Iold; D = Td/(N*h+Td)*Dold+N*Kp*Td/(N*h+Td)*(yold−y); |
47 |
Iold = Iold + Kp*h/Ti*(r−y); Dold = D; yold = y; |
48 |
return (P + I + D); |
49 |
} |
50 |
// Simulate the process
|
51 |
. . . |
52 |
} // end of class
|