Skip to content

Commit 30d1ff8

Browse files
committed
add code
1 parent c704c67 commit 30d1ff8

File tree

2 files changed

+65
-71
lines changed

2 files changed

+65
-71
lines changed

Diff for: mpc_modeling/mpc_modeling_with_ECOS.py

+50-56
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def use_modeling_tool(A, B, N, Q, R, P, x0, umax=None, umin=None, xmin=None, xma
2727
(nx, nu) = B.shape
2828

2929
# mpc calculation
30-
x = cvxpy.Variable(nx, N + 1)
31-
u = cvxpy.Variable(nu, N)
30+
x = cvxpy.Variable((nx, N + 1))
31+
u = cvxpy.Variable((nu, N))
3232

3333
costlist = 0.0
3434
constrlist = []
@@ -40,23 +40,23 @@ def use_modeling_tool(A, B, N, Q, R, P, x0, umax=None, umin=None, xmin=None, xma
4040
constrlist += [x[:, t + 1] == A * x[:, t] + B * u[:, t]]
4141

4242
if xmin is not None:
43-
constrlist += [x[:, t] >= xmin]
43+
constrlist += [x[:, t] >= xmin[:, 0]]
4444
if xmax is not None:
45-
constrlist += [x[:, t] <= xmax]
45+
constrlist += [x[:, t] <= xmax[:, 0]]
4646

4747
costlist += 0.5 * cvxpy.quad_form(x[:, N], P) # terminal cost
4848
if xmin is not None:
49-
constrlist += [x[:, N] >= xmin]
49+
constrlist += [x[:, N] >= xmin[:, 0]]
5050
if xmax is not None:
51-
constrlist += [x[:, N] <= xmax]
51+
constrlist += [x[:, N] <= xmax[:, 0]]
5252

53-
prob = cvxpy.Problem(cvxpy.Minimize(costlist), constrlist)
54-
55-
prob.constraints += [x[:, 0] == x0] # inital state constraints
53+
constrlist += [x[:, 0] == x0[:, 0]] # inital state constraints
5654
if umax is not None:
57-
prob.constraints += [u <= umax] # input constraints
55+
constrlist += [u <= umax] # input constraints
5856
if umin is not None:
59-
prob.constraints += [u >= umin] # input constraints
57+
constrlist += [u >= umin] # input constraints
58+
59+
prob = cvxpy.Problem(cvxpy.Minimize(costlist), constrlist)
6060

6161
prob.solve(verbose=True)
6262

@@ -130,7 +130,7 @@ def opt_mpc_with_state_constr(A, B, N, Q, R, P, x0, xmin=None, xmax=None, umax=N
130130
# print(Ae.shape)
131131

132132
# calc be
133-
be = np.vstack((A, np.zeros(((N - 1) * nx, nx)))) * x0
133+
be = np.vstack((A, np.zeros(((N - 1) * nx, nx)))) @ x0
134134
# print(be)
135135
# print(be.shape)
136136

@@ -156,11 +156,10 @@ def opt_mpc_with_state_constr(A, B, N, Q, R, P, x0, xmin=None, xmax=None, umax=N
156156
sol = pyecosqp.ecosqp(H, q, A=G, B=h, Aeq=Ae, Beq=be)
157157

158158
# print(sol)
159-
fx = np.matrix(sol["x"])
160-
# print(fx)
159+
fx = np.array(sol["x"])
161160

162-
u = fx[0, 0:N * nu].reshape(N, nu).T
163-
x = fx[0, -N * nx:].reshape(N, nx).T
161+
u = fx[0:N * nu].reshape(N, nu).T
162+
x = fx[-N * nx:].reshape(N, nx).T
164163
x = np.hstack((x0, x))
165164
# print(x)
166165
# print(u)
@@ -170,8 +169,8 @@ def opt_mpc_with_state_constr(A, B, N, Q, R, P, x0, xmin=None, xmax=None, umax=N
170169

171170
def test3():
172171
print("start!!")
173-
A = np.matrix([[0.8, 1.0], [0, 0.9]])
174-
B = np.matrix([[-1.0], [2.0]])
172+
A = np.array([[0.8, 1.0], [0, 0.9]])
173+
B = np.array([[-1.0], [2.0]])
175174
(nx, nu) = B.shape
176175

177176
N = 10 # number of horizon
@@ -181,7 +180,7 @@ def test3():
181180
umax = 0.7
182181
umin = -0.7
183182

184-
x0 = np.matrix([[1.0], [2.0]]) # init state
183+
x0 = np.array([[1.0], [2.0]]) # init state
185184
x, u = use_modeling_tool(A, B, N, Q, R, P, x0, umax=umax, umin=umin)
186185

187186
rx1 = np.array(x[0, :]).flatten()
@@ -217,16 +216,16 @@ def test3():
217216

218217
def test4():
219218
print("start!!")
220-
A = np.matrix([[0.8, 1.0], [0, 0.9]])
221-
B = np.matrix([[-1.0], [2.0]])
219+
A = np.array([[0.8, 1.0], [0, 0.9]])
220+
B = np.array([[-1.0], [2.0]])
222221
(nx, nu) = B.shape
223222

224223
N = 10 # number of horizon
225224
Q = np.eye(nx)
226225
R = np.eye(nu)
227226
P = np.eye(nx)
228227

229-
x0 = np.matrix([[1.0], [2.0]]) # init state
228+
x0 = np.array([[1.0], [2.0]]) # init state
230229

231230
x, u = use_modeling_tool(A, B, N, Q, R, P, x0)
232231

@@ -262,16 +261,16 @@ def test4():
262261

263262
def test5():
264263
print("start!!")
265-
A = np.matrix([[0.8, 1.0], [0, 0.9]])
266-
B = np.matrix([[-1.0], [2.0]])
264+
A = np.array([[0.8, 1.0], [0, 0.9]])
265+
B = np.array([[-1.0], [2.0]])
267266
(nx, nu) = B.shape
268267

269268
N = 10 # number of horizon
270269
Q = np.eye(nx)
271270
R = np.eye(nu)
272271
P = np.eye(nx)
273272

274-
x0 = np.matrix([[1.0], [2.0]]) # init state
273+
x0 = np.array([[1.0], [2.0]]) # init state
275274
umax = 0.7
276275

277276
x, u = use_modeling_tool(A, B, N, Q, R, P, x0, umax=umax)
@@ -308,23 +307,23 @@ def test5():
308307

309308
def test6():
310309
print("start!!")
311-
A = np.matrix([[0.8, 1.0], [0, 0.9]])
312-
B = np.matrix([[-1.0], [2.0]])
310+
A = np.array([[0.8, 1.0], [0, 0.9]])
311+
B = np.array([[-1.0], [2.0]])
313312
(nx, nu) = B.shape
314313

315314
N = 10 # number of horizon
316315
Q = np.eye(nx)
317316
R = np.eye(nu)
318317
P = np.eye(nx)
319318

320-
x0 = np.matrix([[1.0], [2.0]]) # init state
319+
x0 = np.array([[1.0], [2.0]]) # init state
321320
umax = 0.7
322321
umin = -0.7
323322

324-
x0 = np.matrix([[1.0], [2.0]]) # init state
323+
x0 = np.array([[1.0], [2.0]]) # init state
325324

326-
xmin = np.matrix([[-3.5], [-0.5]]) # state constraints
327-
xmax = np.matrix([[3.5], [2.0]]) # state constraints
325+
xmin = np.array([[-3.5], [-0.5]]) # state constraints
326+
xmax = np.array([[3.5], [2.0]]) # state constraints
328327

329328
x, u = use_modeling_tool(A, B, N, Q, R, P, x0,
330329
umax=umax, umin=umin, xmin=xmin, xmax=xmax)
@@ -363,27 +362,22 @@ def test6():
363362
def test7():
364363
print("start!!")
365364

366-
A = np.matrix([[0.8, 1.0], [0, 0.9]])
367-
B = np.matrix([[-1.0], [2.0]])
365+
A = np.array([[0.8, 1.0], [0, 0.9]])
366+
B = np.array([[-1.0], [2.0]])
368367
(nx, nu) = B.shape
369368

370369
N = 3 # number of horizon
371370
Q = np.eye(nx)
372371
R = np.eye(nu)
373372
P = np.eye(nx)
374373

375-
x0 = np.matrix([[1.0], [2.0]]) # init state
374+
x0 = np.array([[1.0], [2.0]]) # init state
376375
umax = 0.7
377376
umin = -0.7
378377

379-
x0 = np.matrix([[1.0], [2.0]]) # init state
380-
381-
# xmin = np.matrix([[-3.5], [-0.5]]) # state constraints
382-
# xmax = np.matrix([[3.5], [2.0]]) # state constraints
378+
x0 = np.array([[1.0], [2.0]]) # init state
383379

384380
x, u = use_modeling_tool(A, B, N, Q, R, P, x0, umax=umax, umin=umin)
385-
# x, u = use_modeling_tool(A, B, N, Q, R, P, x0, umax=umax, umin=umin, xmin=xmin, xmax=xmax)
386-
# x, u = use_modeling_tool(A, B, N, Q, R, P, x0)
387381

388382
rx1 = np.array(x[0, :]).flatten()
389383
rx2 = np.array(x[1, :]).flatten()
@@ -422,33 +416,33 @@ def test7():
422416
def test8():
423417
print("start!!")
424418

425-
A = np.matrix([[0.8, 1.0], [0, 0.9]])
426-
B = np.matrix([[-1.0], [2.0]])
419+
A = np.array([[0.8, 1.0], [0, 0.9]])
420+
B = np.array([[-1.0], [2.0]])
427421
(nx, nu) = B.shape
428422

429423
N = 5 # number of horizon
430424
Q = np.eye(nx)
431425
R = np.eye(nu)
432426
P = np.eye(nx)
433427

434-
x0 = np.matrix([[1.0], [2.0]]) # init state
428+
x0 = np.array([[1.0], [2.0]]) # init state
435429
umax = 0.7
436430
umin = -0.7
437431

438-
x0 = np.matrix([[1.0], [2.0]]) # init state
432+
x0 = np.array([[1.0], [2.0]]) # init state
439433

440434
start = time.time()
441435

442-
xmin = np.matrix([[-3.5], [-0.5]]) # state constraints
443-
xmax = np.matrix([[3.5], [2.0]]) # state constraints
436+
xmin = np.array([[-3.5], [-0.5]]) # state constraints
437+
xmax = np.array([[3.5], [2.0]]) # state constraints
444438

445439
# x, u = use_modeling_tool(A, B, N, Q, R, P, x0, umax=umax, umin=umin)
446440
x, u = use_modeling_tool(A, B, N, Q, R, P, x0,
447441
umax=umax, umin=umin, xmin=xmin, xmax=xmax)
448442

449443
# x, u = use_modeling_tool(A, B, N, Q, R, P, x0)
450444
elapsed_time = time.time() - start
451-
print ("modeling tool modeling elapsed_time:{0}".format(
445+
print("modeling tool modeling elapsed_time:{0}".format(
452446
elapsed_time) + "[sec]")
453447

454448
rx1 = np.array(x[0, :]).flatten()
@@ -470,7 +464,7 @@ def test8():
470464
# A, B, N, Q, R, P, x0, umax=umax, umin=umin)
471465
# x, u = opt_mpc_with_state_constr(A, B, N, Q, R, P, x0)
472466
elapsed_time = time.time() - start
473-
print ("hand modeling elapsed_time:{0}".format(elapsed_time) + "[sec]")
467+
print("hand modeling elapsed_time:{0}".format(elapsed_time) + "[sec]")
474468

475469
# print(x)
476470
print(u)
@@ -498,22 +492,22 @@ def test_output_check(rx1, rx2, ru, x1, x2, u):
498492
print("test x1")
499493
for (i, j) in zip(rx1, x1):
500494
print(i, j)
501-
assert (i - j) <= 0.001, "Error"
495+
assert (i - j) <= 0.01, "Error"
502496
print("test x2")
503497
for (i, j) in zip(rx2, x2):
504498
print(i, j)
505-
assert (i - j) <= 0.001, "Error"
499+
assert (i - j) <= 0.01, "Error"
506500
print("test u")
507501
for (i, j) in zip(ru, u):
508502
print(i, j)
509-
assert (i - j) <= 0.001, "Error"
503+
assert (i - j) <= 0.01, "Error"
510504

511505

512506
if __name__ == '__main__':
513507
DEBUG_ = True
514-
# test3()
515-
# test4()
516-
# test5()
517-
# test6()
518-
# test7()
508+
test3()
509+
test4()
510+
test5()
511+
test6()
512+
test7()
519513
test8()

Diff for: mpc_path_tracking/main.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#! /usr/bin/python
2-
# -*- coding: utf-8 -*-
3-
u"""
1+
"""
42
MPC driving simulation to target point
53
64
author Atsushi Sakai
@@ -23,7 +21,7 @@
2321

2422

2523
def LinealizeCarModel(xb, u, dt, lr):
26-
u"""
24+
"""
2725
TODO conplete model
2826
"""
2927

@@ -57,7 +55,9 @@ def LinealizeCarModel(xb, u, dt, lr):
5755
tm[2, 0] = a * dt
5856
tm[3, 0] = v / lr * sin(beta) * dt
5957
C = xb + tm
60-
C = C - A * xb - B * u
58+
C = C - A @ xb - B @ u
59+
60+
# print(A, B, C)
6161

6262
return A, B, C
6363

@@ -75,15 +75,13 @@ def NonlinearModel(x, u, dt, lr):
7575
def CalcInput(A, B, C, x, u):
7676

7777
x_0 = x[:]
78-
x = Variable(x.shape[0], T + 1)
79-
u = Variable(u.shape[0], T)
78+
x = Variable((x.shape[0], T + 1))
79+
u = Variable((u.shape[0], T))
8080

8181
# MPC controller
8282
states = []
8383
for t in range(T):
84-
# constr = [x[:,t+1] == A*x[:,t] + B*u[:,t]+C, abs(u[:,t])<=0.5, x[2,t+1]<= max_speed, x[2,t+1] >= min_speed]
8584
constr = [x[:, t + 1] == A * x[:, t] + B * u[:, t] + C]
86-
# constr = [x[:,t+1] == NonlinearModel(x[:,t],u,dt,lr)]
8785
constr += [abs(u[:, t]) <= 0.5]
8886
constr += [x[2, t + 1] <= max_speed]
8987
constr += [x[2, t + 1] >= min_speed]
@@ -103,8 +101,8 @@ def CalcInput(A, B, C, x, u):
103101
# result=prob.solve(verbose=True)
104102
result = prob.solve()
105103
elapsed_time = time.time() - start
106-
print ("calc time:{0}".format(elapsed_time) + "[sec]")
107-
print (prob.value)
104+
print("calc time:{0}".format(elapsed_time) + "[sec]")
105+
print(prob.value)
108106

109107
if prob.status != OPTIMAL:
110108
print("Cannot calc opt")
@@ -118,9 +116,10 @@ def GetListFromMatrix(x):
118116

119117

120118
def Main():
121-
x0 = np.matrix([0.0, 0.0, 0.0, 0.0]).T # [x,y,v theta]
119+
x0 = np.array([[0.0, 0.0, 0.0, 0.0]]).T # [x,y,v theta]
120+
print(x0)
122121
x = x0
123-
u = np.matrix([0.0, 0.00]).T # [a,beta]
122+
u = np.array([[0.0, 0.0]]).T # [a,beta]
124123
plt.figure(num=None, figsize=(12, 12))
125124

126125
mincost = 100000
@@ -132,12 +131,13 @@ def Main():
132131
u[0, 0] = GetListFromMatrix(ustar.value[0, :])[0]
133132
u[1, 0] = float(ustar[1, 0].value)
134133

135-
x = A * x + B * u
134+
x = A @ x + B @ u
136135

137136
plt.subplot(3, 1, 1)
138137
plt.plot(target[0], target[1], 'xb')
139138
plt.plot(x[0], x[1], '.r')
140-
plt.plot(GetListFromMatrix(xstar.value[0, :]), GetListFromMatrix(xstar.value[1, :]), '-b')
139+
plt.plot(GetListFromMatrix(xstar.value[0, :]), GetListFromMatrix(
140+
xstar.value[1, :]), '-b')
141141
plt.axis("equal")
142142
plt.xlabel("x[m]")
143143
plt.ylabel("y[m]")

0 commit comments

Comments
 (0)