-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLesson-1-starter-code.py
More file actions
128 lines (98 loc) · 3.69 KB
/
Copy pathLesson-1-starter-code.py
File metadata and controls
128 lines (98 loc) · 3.69 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
123
124
125
126
127
128
import numpy as np
import matplotlib.pyplot as plt
from pydrake.all import (
plot_system_graphviz,
ConstantVectorSource,
DiagramBuilder,
Simulator,
LeafSystem,
LogVectorOutput,
)
# double integrator system with a continuous state
class DoubleIntegrator(LeafSystem):
def __init__(self):
LeafSystem.__init__(self)
# declare the input port (1-dimensional)
self.DeclareVectorInputPort("u", 1)
# declare the continuous state (1 position state, 1 velocity state, 0 abstract states)
self.state_index = self.DeclareContinuousState(1,1,0)
# declare the output port to be the state vector
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 = 1/2 * (-0.5 * x_dot + u)
# update the derivatives for the continous-time integrator
# print(f"DoCalcTimeDerivatives(): t={context.get_time()}, u={u}, x={x}, x_dot={x_dot}, x_ddot={x_ddot}")
derivatives.get_mutable_vector().SetFromVector(
np.array([x_dot, x_ddot])
)
class Controller(LeafSystem):
def __init__(self):
LeafSystem.__init__(self)
self.DeclareVectorOutputPort("u", 1, self.MyOutput)
def MyOutput(self, context, output):
t = context.get_time()
u = np.sin(t) # modify this line
output.SetFromVector([u])
if __name__ == "__main__":
# create the diagram
builder = DiagramBuilder()
plant = builder.AddSystem(DoubleIntegrator())
plant.set_name("double integrator")
controller = builder.AddSystem(Controller())
builder.Connect(controller.get_output_port(), plant.get_input_port())
logger = LogVectorOutput(plant.get_output_port(), builder)
# logger = LogVectorOutput(plant.get_output_port(), builder, publish_period=0.1) # if you want to log only at specific intervals
logger.set_name("output state logger")
input_logger = LogVectorOutput(controller.get_output_port(), builder)
diagram = builder.Build()
diagram.set_name("Double Integrator System")
# visualize the diagram (optional)
# plot_system_graphviz(diagram)
# plt.show()
# set initial conditions
context = diagram.CreateDefaultContext()
context.SetTime(0.0)
context.SetContinuousState(np.array([0.0, 0.0]))
# create the simulator
simulator = Simulator(diagram, context)
# run the simulation
print("Running simulation...")
simulator.AdvanceTo(4*np.pi)
print("Simulation complete. Press Ctrl+C to exit.")
# create plots
log = logger.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()
input_log = input_logger.FindLog(context)
u = input_log.data()[0,:]
plt.figure()
plt.plot(input_log.sample_times(), u, label="u")
plt.title("Control Input vs Time")
plt.xlabel("Time (s)")
plt.ylabel("Control Input (N)")
plt.grid()
plt.legend()
plt.show()