-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpid_controller.m
More file actions
49 lines (39 loc) · 1.09 KB
/
pid_controller.m
File metadata and controls
49 lines (39 loc) · 1.09 KB
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
n = 100000; % Number of time steps
t = 0.001; % Time step
x = zeros(1,n); % Vector storing state
v = 0; % Velocity
a = 0; % Acceleration
x(1) = 5; % Initial state
kP = 0.3; % Proportional gain
kI = 0.05; % Integral gain
kD = 0.7; % Derrivative gain
g = 0; % Acceleration due to gravity
rho = 0.01; % Air resistance constant
set_state = 0; % Initial set stae
% Initialise variables
sum_error = 0;
prev_error = 0;
error = 0;
for i=2:n
% Calculate new state
v = v + a*t;
x(i) = x(i-1) + v*t;
% Calculate new control signal
prev_error = error;
error = set_state - x(i);
sum_error = sum_error + error*t;
diff_error = (error-prev_error)/t;
control = kP * error + kI * sum_error + kD * diff_error;
% Update acceleration
a = control + g - rho*v*v*sign(v);
end
% Plot the simulation
figure
plot(x)
% Plot the set state
y = zeros(1,n);
hold;
plot(y,':')
% Axis labels
xlabel("Time t");
ylabel("State x");