-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_nominal_control.py
232 lines (177 loc) · 6.82 KB
/
run_nominal_control.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import sys
import numpy as np
import scipy.linalg
from acados_template import AcadosOcp, AcadosOcpSolver, AcadosSimSolver
from export_chain_mass_model import export_chain_mass_model
from export_disturbed_chain_mass_model import export_disturbed_chain_mass_model
from export_chain_mass_integrator import export_chain_mass_integrator
from plot_utils import *
from utils import *
import matplotlib.pyplot as plt
def run_nominal_control(chain_params):
# create ocp object to formulate the OCP
ocp = AcadosOcp()
# chain parameters
n_mass = chain_params["n_mass"]
M = chain_params["n_mass"] - 2 # number of intermediate masses
Ts = chain_params["Ts"]
Tsim = chain_params["Tsim"]
N = chain_params["N"]
u_init = chain_params["u_init"]
with_wall = chain_params["with_wall"]
yPosWall = chain_params["yPosWall"]
m = chain_params["m"]
D = chain_params["D"]
L = chain_params["L"]
perturb_scale = chain_params["perturb_scale"]
nlp_iter = chain_params["nlp_iter"]
nlp_tol = chain_params["nlp_tol"]
save_results = chain_params["save_results"]
show_plots = chain_params["show_plots"]
seed = chain_params["seed"]
np.random.seed(seed)
nparam = 3*M
W = perturb_scale * np.eye(nparam)
# export model
model = export_disturbed_chain_mass_model(n_mass, m, D, L)
# set model
ocp.model = model
nx = model.x.size()[0]
nu = model.u.size()[0]
ny = nx + nu
ny_e = nx
Tf = N * Ts
# initial state
xPosFirstMass = np.zeros((3,1))
xEndRef = np.zeros((3,1))
xEndRef[0] = L * (M+1) * 6
pos0_x = np.linspace(xPosFirstMass[0], xEndRef[0], n_mass)
xrest = compute_steady_state(n_mass, m, D, L, xPosFirstMass, xEndRef)
x0 = xrest
# set dimensions
ocp.dims.N = N
# set cost module
ocp.cost.cost_type = 'LINEAR_LS'
ocp.cost.cost_type_e = 'LINEAR_LS'
Q = 2*np.diagflat( np.ones((nx, 1)) )
q_diag = np.ones((nx,1))
strong_penalty = M+1
q_diag[3*M] = strong_penalty
q_diag[3*M+1] = strong_penalty
q_diag[3*M+2] = strong_penalty
Q = 2*np.diagflat( q_diag )
R = 2*np.diagflat( 1e-2 * np.ones((nu, 1)) )
ocp.cost.W = scipy.linalg.block_diag(Q, R)
ocp.cost.W_e = Q
ocp.cost.Vx = np.zeros((ny, nx))
ocp.cost.Vx[:nx,:nx] = np.eye(nx)
Vu = np.zeros((ny, nu))
Vu[nx:nx+nu, :] = np.eye(nu)
ocp.cost.Vu = Vu
ocp.cost.Vx_e = np.eye(nx)
# import pdb; pdb.set_trace()
yref = np.vstack((xrest, np.zeros((nu,1)))).flatten()
ocp.cost.yref = yref
ocp.cost.yref_e = xrest.flatten()
# set constraints
umax = 1*np.ones((nu,))
ocp.constraints.constr_type = 'BGH'
ocp.constraints.lbu = -umax
ocp.constraints.ubu = umax
ocp.constraints.x0 = x0.reshape((nx,))
ocp.constraints.idxbu = np.array(range(nu))
# disturbances
nparam = 3*M
ocp.parameter_values = np.zeros((nparam,))
# wall constraint
if with_wall:
nbx = M + 1
Jbx = np.zeros((nbx,nx))
for i in range(nbx):
Jbx[i, 3*i+1] = 1.0
ocp.constraints.Jbx = Jbx
ocp.constraints.lbx = yPosWall * np.ones((nbx,))
ocp.constraints.ubx = 1e9 * np.ones((nbx,))
# slacks
ocp.constraints.Jsbx = np.eye(nbx)
L2_pen = 1e3
L1_pen = 1
ocp.cost.Zl = L2_pen * np.ones((nbx,))
ocp.cost.Zu = L2_pen * np.ones((nbx,))
ocp.cost.zl = L1_pen * np.ones((nbx,))
ocp.cost.zu = L1_pen * np.ones((nbx,))
# solver options
ocp.solver_options.qp_solver = 'PARTIAL_CONDENSING_HPIPM' # FULL_CONDENSING_QPOASES
ocp.solver_options.hessian_approx = 'GAUSS_NEWTON'
ocp.solver_options.integrator_type = 'IRK'
ocp.solver_options.nlp_solver_type = 'SQP' # SQP_RTI
ocp.solver_options.nlp_solver_max_iter = nlp_iter
ocp.solver_options.sim_method_num_stages = 2
ocp.solver_options.sim_method_num_steps = 2
ocp.solver_options.qp_solver_cond_N = N
ocp.solver_options.qp_tol = nlp_tol
ocp.solver_options.tol = nlp_tol
# ocp.solver_options.nlp_solver_tol_eq = 1e-9
# set prediction horizon
ocp.solver_options.tf = Tf
acados_ocp_solver = AcadosOcpSolver(ocp, json_file = 'acados_ocp_' + model.name + '.json')
# acados_integrator = AcadosSimSolver(ocp, json_file = 'acados_ocp_' + model.name + '.json')
acados_integrator = export_chain_mass_integrator(n_mass, m, D, L)
#%% get initial state from xrest
xcurrent = x0.reshape((nx,))
for i in range(5):
acados_integrator.set("x", xcurrent)
acados_integrator.set("u", u_init)
status = acados_integrator.solve()
if status != 0:
raise Exception('acados integrator returned status {}. Exiting.'.format(status))
# update state
xcurrent = acados_integrator.get("x")
#%% actual simulation
N_sim = int(np.floor(Tsim/Ts))
simX = np.ndarray((N_sim+1, nx))
simU = np.ndarray((N_sim, nu))
wall_dist = np.zeros((N_sim,))
timings = np.zeros((N_sim,))
simX[0,:] = xcurrent
# closed loop
for i in range(N_sim):
# solve ocp
acados_ocp_solver.set(0, "lbx", xcurrent)
acados_ocp_solver.set(0, "ubx", xcurrent)
status = acados_ocp_solver.solve()
timings[i] = acados_ocp_solver.get_stats("time_tot")[0]
if status != 0:
raise Exception('acados acados_ocp_solver returned status {} in time step {}. Exiting.'.format(status, i))
simU[i,:] = acados_ocp_solver.get(0, "u")
print("control at time", i, ":", simU[i,:])
# simulate system
acados_integrator.set("x", xcurrent)
acados_integrator.set("u", simU[i,:])
pertubation = sampleFromEllipsoid(np.zeros((nparam,)), W)
acados_integrator.set("p", pertubation)
status = acados_integrator.solve()
if status != 0:
raise Exception('acados integrator returned status {}. Exiting.'.format(status))
# update state
xcurrent = acados_integrator.get("x")
simX[i+1,:] = xcurrent
# xOcpPredict = acados_ocp_solver.get(1, "x")
# print("model mismatch = ", str(np.max(xOcpPredict - xcurrent)))
yPos = xcurrent[range(1,3*M+1,3)]
wall_dist[i] = np.min(yPos - yPosWall)
print("time i = ", str(i), " dist2wall ", str(wall_dist[i]))
print("dist2wall (minimum over simulation) ", str(np.min(wall_dist)))
#%% plot results
if show_plots:
plot_chain_control_traj(simU)
plot_chain_position_traj(simX, yPosWall=yPosWall)
plot_chain_velocity_traj(simX)
# plot_chain_position(simX[-1,:], xPosFirstMass)
animate_chain_position(simX, xPosFirstMass, yPosWall=yPosWall)
plt.show()
#%% save results
if save_results:
ID = "nominal"
timings_Pprop = np.zeros((N_sim,))
save_closed_loop_results_as_json(ID, timings, timings_Pprop, wall_dist, chain_params)