-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample4.py
More file actions
68 lines (41 loc) · 1.49 KB
/
example4.py
File metadata and controls
68 lines (41 loc) · 1.49 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
63
64
65
66
67
68
# -*- coding: utf-8 -*-
from pyblocksim import *
mainprint(
"""
Example4:
Flatness based control of a linear plant
(another academic exercise)
"""
)
# parameters
k = 20 # controller gain
y_ref, u_traj, fb = inputs("y_ref, u_traj, fb") # external force and feedback
SUM = Blockfnc((y_ref - fb) * k + u_traj)
# Plant
P_tf = 2 * (s + 1) / ((s**2 - 2 * s + 2) * (s + 5))
PLANT = TFBlock(P_tf, SUM.Y) # double integrator x_ddot -> x
# close the loop
loop(PLANT.Y, fb)
# input signals
# the flat output is called q here
q_A = 0 # beginning
q_E = -16 # end
T = 1 # Transition Time
traj_expr1 = q_A + (q_E - q_A) * ((t / T) ** 3 * (10 - 15 * t / T + 6 * (t / T) ** 2))
traj_expr2 = sp.Piecewise((0, t < 0), (traj_expr1, t <= T), (q_E, True))
traj = Trajectory(traj_expr2, 3)
y_ref_fnc = traj.combined_trajectories(2 * (s + 1)) # the reference of PLANT.Y
# y_ref_fnc = stepfnc(0, -32) # for comparism
u_traj_fnc = traj.combined_trajectories((s**2 - 2 * s + 2) * (s + 5))
t, states = blocksimulation(10, {u_traj: u_traj_fnc, y_ref: y_ref_fnc}) # simulate 10 seconds
t = t.flatten()
bo = compute_block_outputs(states)
if __name__ == "__main__":
# desired and simulated values are quite close
# (the combination of feed forward and feedback controller works good)
pl.plot(t, bo[PLANT], label="simulation")
pl.plot(t, [y_ref_fnc(ti) for ti in t], label="desired")
pl.legend()
pl.grid()
# pl.plot(t, [u_traj_fnc(ti) for ti in t]) # the precalculated input
pl.show()