-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLesson-2-extension-1-solution.py
More file actions
122 lines (96 loc) · 3.83 KB
/
Copy pathLesson-2-extension-1-solution.py
File metadata and controls
122 lines (96 loc) · 3.83 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import numpy as np
import matplotlib.pyplot as plt
from pydrake.all import (
plot_system_graphviz,
DiagramBuilder,
Simulator,
LeafSystem,
PidController,
ConstantVectorSource,
LogVectorOutput,
)
# double integrator system with a continuous state
class DoubleIntegrator(LeafSystem):
def __init__(self):
LeafSystem.__init__(self)
self.DeclareVectorInputPort("u", 1)
self.state_index = self.DeclareContinuousState(1,1,0)
self.DeclareStateOutputPort("y", self.state_index)
def DoCalcTimeDerivatives(self, context, derivatives):
# read the input
u = self.get_input_port().Eval(context)[0]
# read the output
state = context.get_continuous_state().get_vector()
x = state.GetAtIndex(0)
x_dot = state.GetAtIndex(1)
# state space equations
x_ddot = u
# update the derivatives for the continous-time integrator
derivatives.get_mutable_vector().SetFromVector(
np.array([x_dot, x_ddot])
)
if __name__ == "__main__":
# create the diagram
builder = DiagramBuilder()
plant = builder.AddSystem(DoubleIntegrator())
# read the documentation for PidController to see how to set it up
# https://drake.mit.edu/doxygen_cxx/classdrake_1_1systems_1_1controllers_1_1_pid_controller.html
controller = builder.AddSystem(PidController(kp=[10.0], ki=[0.0], kd=[5.0]))
reference = builder.AddSystem(ConstantVectorSource([1.0, 0.0]))
# read the documentation for PidController to see how to set it up
# https://drake.mit.edu/doxygen_cxx/classdrake_1_1systems_1_1controllers_1_1_pid_controller.html
builder.Connect(controller.get_output_port_control(), plant.get_input_port())
builder.Connect(plant.get_output_port(), controller.get_input_port_estimated_state())
builder.Connect(reference.get_output_port(), controller.get_input_port_desired_state())
logger = LogVectorOutput(plant.get_output_port(), builder)
logger2 = LogVectorOutput(controller.get_output_port_control(), builder)
diagram = builder.Build()
plot_system_graphviz(diagram)
plt.show()
# set initial conditions
context = diagram.CreateDefaultContext()
context.SetTime(0.0)
# the integral term in the PID controller is technically a state, too
# so it can be set like this (optional)
controller_context = controller.GetMyContextFromRoot(context)
controller_context.SetContinuousState(np.array([0]))
# when multiple systems have state, you need to be careful
# to set the state of the correct system. Use the GetMyContextFromRoot
# method to get the context for the specific system.
plant_context = plant.GetMyContextFromRoot(context)
plant_context.SetContinuousState(np.array([0, 0]))
# create the simulator
simulator = Simulator(diagram, context)
# run the simulation
print("Running simulation...")
simulator.AdvanceTo(5.0)
print("Simulation complete. Press Ctrl+C to exit.")
# create plots
log = logger.FindLog(context)
log2 = logger2.FindLog(context)
x = log.data()[0,:]
x_dot = log.data()[1,:]
plt.figure()
plt.plot(log.sample_times(), x, label="x")
plt.title("Position vs Time")
plt.xlabel("Time (s)")
plt.ylabel("Position (m)")
plt.grid()
plt.legend()
plt.show()
plt.figure()
plt.plot(log.sample_times(), x_dot, label="x_dot")
plt.title("Velocity vs Time")
plt.xlabel("Time (s)")
plt.ylabel("Velocity (m/s)")
plt.grid()
plt.legend()
plt.show()
plt.figure()
plt.plot(log2.sample_times(), log2.data()[0,:], label="u")
plt.title("Input vs Time")
plt.xlabel("Time (s)")
plt.ylabel("Input (m/s^2)")
plt.grid()
plt.legend()
plt.show()