-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_speed.py
278 lines (217 loc) · 6.62 KB
/
test_speed.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from time import time
import casadi as ca
import numpy as np
from casadi import sin, cos, pi
# setting matrix_weights' variables
Q_x = 5
Q_y = 5
Q_theta = 0.0
R1 = 0.0
R2 = 10
step_horizon = 0.1 # time between steps in seconds
N = 10 # number of look ahead steps
rob_diam = 0.3 # diameter of the robot
wheel_radius = 1 # wheel radius
Lx = 0.3 # L in J Matrix (half robot x-axis length)
Ly = 0.3 # l in J Matrix (half robot y-axis length)
sim_time = 100 # simulation time
# specs
x_init = 0
y_init = 0
theta_init = 0
x_target = 10
y_target = 15
theta_target = 0.0
v_max = 1.0
v_min = 0
w_max = 0.3
w_min = -0.3
def shift_timestep(step_horizon, t0, state_init, u, f):
f_value = f(state_init, u[:, 0])
next_state = ca.DM.full(state_init (step_horizon * f_value))
t0 = t0 step_horizon
u0 = ca.horzcat(
u[:, 1:],
ca.reshape(u[:, -1], -1, 1)
)
return t0, next_state, u0
def DM2Arr(dm):
return np.array(dm.full())
# state symbolic variables
x = ca.SX.sym('x')
y = ca.SX.sym('y')
theta = ca.SX.sym('theta')
states = ca.vertcat(
x,
y,
theta
)
n_states = states.numel()
# 3
v_ = ca.SX.sym('v_')
w_ = ca.SX.sym('w_')
controls = ca.vertcat(v_, w_)
n_controls = controls.numel()
# matrix containing all states over all time steps 1 (each column is a state vector)
X = ca.SX.sym('X', n_states, N 1)
# matrix containing all control actions over all time steps (each column is an action vector)
U = ca.SX.sym('U', n_controls, N)
# coloumn vector for storing initial state and target state
P = ca.SX.sym('P', n_states n_states)
# state weights matrix (Q_X, Q_Y, Q_THETA)
Q = ca.diagcat(Q_x, Q_y, Q_theta)
###############
R = ca.diagcat(R1, R2)
RHS = ca.vertcat(
v_*cos(theta) - wheel_radius*w_*sin(theta),
v_*sin(theta) wheel_radius*w_*cos(theta),
w_
)
# maps controls from [va, vb, vc, vd].T to [vx, vy, omega].T
f = ca.Function('f', [states, controls], [RHS])
cost_fn = 0 # cost function
g = X[:, 0] - P[:n_states] # constraints in the equation
# runge kutta
for k in range(N):
st = X[:, k]
con = U[:, k]
cost_fn = cost_fn \
(st - P[n_states:]).T @ Q @ (st - P[n_states:]) \
con.T @ R @ con \
## CM(st)
st_next = X[:, k 1]
st_next_shift = st f(st, con)*step_horizon
g = ca.vertcat(g, st_next - st_next_shift)
# # circle obstackle
# n_obs = 1
# obs_x = np.ones((n_obs, 1))*5
# obs_y = np.linspace(5, 15, n_obs)
# obs_diam = 2
# for k in range(N 1):
# for j in range(n_obs):
# g = ca.vertcat(g, -ca.sqrt((X[0, k]-obs_x[j])**2
# (X[1, k]-obs_y[j])**2) rob_diam/2 obs_diam/2)
# ellipse obstackle
n_obs = 1
center = [5, 5]
scale = [5 rob_diam/2, 0.1 rob_diam/2]
rot = 3*pi/4
cR = cos(rot)
sR = sin(rot)
for k in range(N 1):
for j in range(n_obs):
g = ca.vertcat(g, 1 - (((X[0,k]-center[0])*cR (X[1,k]-center[1])*sR)/scale[0])**2 - (((X[0,k]-center[0])*sR-(X[1,k]-center[1])*cR)/scale[1])**2)
OPT_variables = ca.vertcat(
X.reshape((-1, 1)), # Example: 3x11 ---> 33x1 where 3=states, 11=N 1
U.reshape((-1, 1))
)
nlp_prob = {
'f': cost_fn,
'x': OPT_variables,
'g': g,
'p': P
}
opts = {
'ipopt': {
'max_iter': 2000,
'print_level': 0,
'acceptable_tol': 1e-8,
'acceptable_obj_change_tol': 1e-6
},
'print_time': 0
}
solver = ca.nlpsol('solver', 'ipopt', nlp_prob, opts)
lbx = ca.DM.zeros((n_states*(N 1) n_controls*N, 1))
ubx = ca.DM.zeros((n_states*(N 1) n_controls*N, 1))
lbx[0: n_states*(N 1): n_states] = -ca.inf # X lower bound
lbx[1: n_states*(N 1): n_states] = -ca.inf # Y lower bound
lbx[2: n_states*(N 1): n_states] = -ca.inf # theta lower bound
ubx[0: n_states*(N 1): n_states] = ca.inf # X upper bound
ubx[1: n_states*(N 1): n_states] = ca.inf # Y upper bound
ubx[2: n_states*(N 1): n_states] = ca.inf # theta upper bound
lbg = ca.DM.zeros((n_states*(N 1), 1)) # equal constraints
ubg = ca.DM.zeros((n_states*(N 1), 1)) # equal constraints
lbg = ca.vertcat(lbg, -ca.inf*ca.DM.ones(n_obs*(N 1), 1)
) # inequal constraints
ubg = ca.vertcat(ubg, ca.DM.zeros(n_obs*(N 1), 1)) # inequal constraints
k = n_states*(N 1)
while k < n_states*(N 1) n_controls*N:
lbx[k] = v_min
lbx[k 1] = w_min
ubx[k] = v_max
ubx[k 1] = w_max
k = 2
args = {
'lbg': lbg,
'ubg': ubg,
'lbx': lbx,
'ubx': ubx
}
t0 = 0
state_init = ca.DM([x_init, y_init, theta_init]) # initial state
state_target = ca.DM([x_target, y_target, theta_target]) # target state
# xx = DM(state_init)
t = ca.DM(t0)
u0 = ca.DM.zeros((n_controls, N)) # initial control
X0 = ca.repmat(state_init, 1, N 1) # initial state full
mpc_iter = 0
cat_states = DM2Arr(X0)
cat_controls = DM2Arr(u0[:, 0])
times = np.array([[0]])
###############################################################################
if __name__ == '__main__':
main_loop = time() # return time in sec
while (ca.norm_2(state_init - state_target) > 1e-1) and (mpc_iter * step_horizon < sim_time):
t1 = time()
args['p'] = ca.vertcat(
state_init, # current state
state_target # target state
)
# optimization variable current state
args['x0'] = ca.vertcat(
ca.reshape(X0, n_states*(N 1), 1),
ca.reshape(u0, n_controls*N, 1)
)
sol = solver(
x0=args['x0'],
lbx=args['lbx'],
ubx=args['ubx'],
lbg=args['lbg'],
ubg=args['ubg'],
p=args['p']
)
u = ca.reshape(sol['x'][n_states * (N 1):], n_controls, N)
X0 = ca.reshape(sol['x'][: n_states * (N 1)], n_states, N 1)
cat_states = np.dstack((
cat_states,
DM2Arr(X0)
))
cat_controls = np.vstack((
cat_controls,
DM2Arr(u[:, 0])
))
t = np.vstack((
t,
t0
))
t0, state_init, u0 = shift_timestep(step_horizon, t0, state_init, u, f)
# print(X0)
X0 = ca.horzcat(
X0[:, 1:],
ca.reshape(X0[:, -1], -1, 1)
)
# xx ...
t2 = time()
# print(mpc_iter)
# print(t2-t1)
times = np.vstack((
times,
t2-t1
))
mpc_iter = mpc_iter 1
main_loop_time = time()
ss_error = ca.norm_2(state_init[:2] - state_target[:2])
print('\n\n')
print('Total time: ', main_loop_time - main_loop)
print('avg iteration time: ', np.array(times).mean() * 1000, 'ms')
print('final error: ', ss_error)