-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
93 lines (74 loc) · 2.12 KB
/
main.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
import sys
import time, os
import numpy as np
from acados_settings import *
from plotFcn import *
from tracks.readDataFcn import getTrack
import matplotlib.pyplot as plt
np.set_printoptions(threshold=sys.maxsize)
track = "buggy_track.txt"
[Sref, _, _, _, _] = getTrack(track)
Tf = 2.5 # prediction horizon
N = 20 # number of discretization steps
T = 200 # maximum simulation time[s]
# load model
constraint, model, acados_solver = acados_settings(Tf, N, track)
# dimensions
nx = model.x.rows()
nu = model.u.rows()
ny = nx + nu
Nsim = int(T * N / Tf)
# initialize data structs
simX = np.zeros((Nsim, nx))
simU = np.zeros((Nsim, nu))
x0 = model.x0
s0 = x0[0]
tcomp_sum = 0
tcomp_max = 0
# simulate
for i in range(Nsim):
# update reference
sref = s0 + Tf * model.vx_max
for j in range(N):
yref = np.array([sref, 0, 0, 0, 0, 0, 0, 0, 0])
acados_solver.set(j, "yref", yref)
yref_N = np.array([sref, 0, 0, 0, 0, 0, 0])
acados_solver.set(N, "yref", yref_N)
# solve ocp
t = time.time()
status = acados_solver.solve()
elapsed = time.time() - t
# manage timings
tcomp_sum += elapsed
if elapsed > tcomp_max:
tcomp_max = elapsed
# get solution
x0 = acados_solver.get(0, "x")
u0 = acados_solver.get(0, "u")
for j in range(nx):
simX[i, j] = x0[j]
for j in range(nu):
simU[i, j] = u0[j]
# update initial condition
x0 = acados_solver.get(1, "x")
print(x0)
acados_solver.set(0, "lbx", x0)
acados_solver.set(0, "ubx", x0)
s0 = x0[0]
# check if one lap is done
if x0[0] > Sref[-1] + 0.1:
break
if status != 0:
print("acados returned status {} in closed loop iteration {}.".format(status, i))
break
# Plot Results
t = np.linspace(0.0, Nsim * Tf / N, Nsim)
plotRes(simX, simU, t)
plotTrackProj(simX, track)
# Print some stats
print("Average computation time: {}".format(tcomp_sum / Nsim))
print("Maximum computation time: {}".format(tcomp_max))
print("Average speed:{}m/s".format(np.average(simX[:, 3])))
print("Lap time: {}s".format(Tf * Nsim / N))
if os.environ.get("ACADOS_ON_CI") is None:
plt.show()