-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompute.m
54 lines (36 loc) · 1.11 KB
/
compute.m
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
function co= compute(lastTime,SampleTime,ITerm,dInput,ki,kp,kd,Setpoint,outMax,outMin,lastInput,Input,a)%,Output)
%err=1;
now = toc;
timeChange = (now - lastTime);
% if(timeChange>=SampleTime)
%/*Compute all the working error variables*/
err = (Setpoint - Input);
ITerm= ITerm+(ki * err);
if(ITerm> outMax)
ITerm= outMax;
elseif(ITerm< outMin)
ITerm= outMin;
dInput = (Input - lastInput);
end
%/*Compute PID Output*/
Output = kp * err + ITerm- kd * dInput;
if(Output > outMax)
Output = outMax;
elseif(Output < outMin)
Output = outMin;
end
m1speed=0.3-(Output);
m2speed=0.3+(Output);
writePWMDutyCycle(a,'D3',m1speed);
writePWMDutyCycle(a,'D10',m2speed);
pause(0.3);
%
writePWMDutyCycle(a,'D3',0);
writePWMDutyCycle(a,'D10',0);
%/*Remember some variables for next time*/
lastInput = Input;
lastTime = now;
%end
display(Output);
co=[Output ITerm lastTime lastInput dInput];
end