-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathexample_pid.py
More file actions
62 lines (44 loc) · 1.46 KB
/
example_pid.py
File metadata and controls
62 lines (44 loc) · 1.46 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
50
51
52
53
54
55
56
57
58
59
60
61
62
#########################################################################################
##
## PathSim Example for PID-controller
##
#########################################################################################
# IMPORTS ===============================================================================
import numpy as np
import matplotlib.pyplot as plt
from pathsim import Simulation, Connection
from pathsim.blocks import Source, Integrator, Amplifier, Adder, Scope, PID
from pathsim.solvers import RKCK54, RKBS32
# SYSTEM SETUP AND SIMULATION ===========================================================
#plant gain
K = 0.4
#pid parameters
Kp, Ki, Kd = 1.5, 0.5, 0.1
#source function
def f_s(t):
if t>60: return 0.5
elif t>20: return 1
else: return 0
#blocks
spt = Source(f_s)
err = Adder("+-")
pid = PID(Kp, Ki, Kd, f_max=10)
pnt = Integrator()
pgn = Amplifier(K)
sco = Scope(labels=["s(t)", "x(t)", r"$\epsilon(t)$"])
blocks = [spt, err, pid, pnt, pgn, sco]
connections = [
Connection(spt, err, sco[0]),
Connection(pgn, err[1], sco[1]),
Connection(err, pid, sco[2]),
Connection(pid, pnt),
Connection(pnt, pgn)
]
#simulation initialization
Sim = Simulation(blocks, connections, Solver=RKCK54)
# Run Example ===========================================================================
if __name__ == "__main__":
#run the simulation for some time
Sim.run(100)
sco.plot(lw=2)
plt.show()