forked from simorxb/PID-Controller-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
76 lines (57 loc) · 1.48 KB
/
main.py
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
import numpy as np
from lib import Car, PID
import matplotlib.pyplot as plt
def main():
# -------- Configuration --------
# Simulation parameters
time_step = 0.1
end_time = 25
length = round(end_time/time_step)
t = np.zeros(length)
stp = np.zeros(length)
v = np.zeros(length)
command = np.zeros(length)
# Car parameters
m = 2140
b = 0.33
F_max_0 = 22000
F_max_max = 1710
v_max = 72
# PID parameters
Kp = 800.0
Ki = 70.0
Kaw = 1.0
Kd = 20.0
T_C = 1.0
# Initialize PID controller
pid = PID(Kp, Ki, Kd, Kaw, T_C, time_step, F_max_0, 0, 30000)
# Initialize car with given parameters
car = Car(m, b, F_max_0, F_max_max, v_max, time_step)
# Iterate through time steps
for idx in range(0, length):
t[idx] = idx*time_step
# Set setpoint
stp[idx] = 42
# Execute the control loop
v[idx] = car.v
pid.Step(v[idx], stp[idx])
command[idx] = pid.command_sat
car.Step(command[idx])
# Plot speed response
plt.subplot(2, 1, 1)
plt.plot(t, v, label="Response")
plt.plot(t, stp, '--', label="Setpoint")
plt.xlabel("Time [s]")
plt.ylabel("Speed [m/s]")
plt.legend()
plt.grid()
# Plot command force
plt.subplot(2, 1, 2)
plt.plot(t, command, label="Command")
plt.xlabel("Time [s]")
plt.ylabel("Force [N]")
plt.legend()
plt.grid()
# Display the plots
plt.show()
main()