-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpendulum.py
executable file
·161 lines (125 loc) · 4.28 KB
/
pendulum.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
##
#
# Swing-up control of a simple inverted pendulum
#
##
import numpy as np
from pydrake.all import *
from ilqr import IterativeLinearQuadraticRegulator
import time
####################################
# Parameters
####################################
T = 2.0 # total simulation time (S)
dt = 1e-2 # simulation timestep
# Solver method
# must be "ilqr" or "sqp"
method = "ilqr"
# Initial state
x0 = np.array([0,0])
# Target state
x_nom = np.array([np.pi,0])
# Quadratic cost int_{0^T} (x'Qx + u'Ru) + x_T*Qf*x_T
Q = 0.01*np.diag([0,1])
R = 0.01*np.eye(1)
Qf = 100*np.diag([1,1])
####################################
# Tools for system setup
####################################
def create_system_model(plant):
urdf = FindResourceOrThrow("drake/examples/pendulum/Pendulum.urdf")
robot = Parser(plant=plant).AddModelFromFile(urdf)
plant.Finalize()
return plant
####################################
# Create system diagram
####################################
builder = DiagramBuilder()
plant, scene_graph = AddMultibodyPlantSceneGraph(builder, dt)
plant = create_system_model(plant)
assert plant.geometry_source_is_registered()
controller = builder.AddSystem(ConstantVectorSource(np.zeros(1)))
builder.Connect(
controller.get_output_port(),
plant.get_actuation_input_port())
DrakeVisualizer().AddToBuilder(builder, scene_graph)
ConnectContactResultsToDrakeVisualizer(builder, plant, scene_graph)
diagram = builder.Build()
diagram_context = diagram.CreateDefaultContext()
plant_context = diagram.GetMutableSubsystemContext(
plant, diagram_context)
#####################################
# Solve Trajectory Optimization
#####################################
# Create system model for the solver to use
plant_ = MultibodyPlant(dt)
plant_ = create_system_model(plant_)
input_port_index = plant_.get_actuation_input_port().get_index()
#-----------------------------------------
# DDP method
#-----------------------------------------
if method == "ilqr":
# Set up the optimizer
num_steps = int(T/dt)
ilqr = IterativeLinearQuadraticRegulator(plant_, num_steps,
input_port_index=input_port_index)
# Define initial and target states
ilqr.SetInitialState(x0)
ilqr.SetTargetState(x_nom)
# Define cost function
ilqr.SetRunningCost(dt*Q, dt*R)
ilqr.SetTerminalCost(Qf)
# Set initial guess
u_guess = np.zeros((1,num_steps-1))
ilqr.SetInitialGuess(u_guess)
states, inputs, solve_time, optimal_cost = ilqr.Solve()
print(f"Solved in {solve_time} seconds using iLQR")
print(f"Optimal cost: {optimal_cost}")
timesteps = np.arange(0.0,T,dt)
#-----------------------------------------
# Direct Transcription method
#-----------------------------------------
elif method == "sqp":
context_ = plant_.CreateDefaultContext()
# Set up the solver object
trajopt = DirectTranscription(
plant_, context_,
input_port_index=input_port_index,
num_time_samples=int(T/dt))
# Add constraints
x = trajopt.state()
u = trajopt.input()
x_init = trajopt.initial_state()
trajopt.prog().AddConstraint(eq( x_init, x0 ))
x_err = x - x_nom
trajopt.AddRunningCost(x_err.T@Q@x_err + u.T@R@u)
trajopt.AddFinalCost(x_err.T@Qf@x_err)
# Solve the optimization problem
st = time.time()
res = Solve(trajopt.prog())
solve_time = time.time() - st
assert res.is_success(), "trajectory optimizer failed"
solver_name = res.get_solver_id().name()
optimal_cost = res.get_optimal_cost()
print(f"Solved in {solve_time} seconds using SQP via {solver_name}")
print(f"Optimal cost: {optimal_cost}")
# Extract the solution
timesteps = trajopt.GetSampleTimes(res)
states = trajopt.GetStateSamples(res)
inputs = trajopt.GetInputSamples(res)
else:
raise ValueError("Unknown method %s"%method)
#####################################
# Playback
#####################################
while True:
# Just keep playing back the trajectory
for i in range(len(timesteps)):
t = timesteps[i]
x = states[:,i]
diagram_context.SetTime(t)
plant.SetPositionsAndVelocities(plant_context, x)
diagram.ForcedPublish(diagram_context)
time.sleep(dt-3e-4)
time.sleep(1)