-
Notifications
You must be signed in to change notification settings - Fork 3
/
quikplan_barrel_racing.py
227 lines (190 loc) · 5.91 KB
/
quikplan_barrel_racing.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
#!/usr/bin/env python3
import casadi as ca
import numpy as np
import pylab as plt
from constants import ControlVars, StateVars
from helpers import (
load_init_json,
anim_traj,
create_obstacles,
in2m,
interp_state_vector,
plot_traj,
rotate_around_origin,
plot_wheel_vel_accel_jerk,
plot_wheel_forces,
plot_total_force,
)
from robot import Robot
def plan(robot=Robot(), plot=False):
N = 200 # Number of control intervals
OBSTACLES = create_obstacles("barrel-racing")
FINISH_LINE_BUFFER = 0.1
# Setup Optimization
opti = ca.Opti()
# State variables
X = opti.variable(len(StateVars), N + 1)
xpos = X[StateVars.xIdx.value, :] # X position
ypos = X[StateVars.yIdx.value, :] # Y-position
theta = X[StateVars.thetaIdx.value, :] # Theta
vl = X[StateVars.vlIdx.value, :] # Left wheel velocity
vr = X[StateVars.vrIdx.value, :] # Right wheel velocity
al = X[StateVars.alIdx.value, :] # Left wheel acceleration
ar = X[StateVars.arIdx.value, :] # Right wheel acceleration
# Control variables
U = opti.variable(len(ControlVars), N)
jl = U[ControlVars.jlIdx.value, :] # Left wheel jerk
jr = U[ControlVars.jrIdx.value, :] # Right wheel jerk
# Total time variable
T = opti.variable()
dt = T / N # length of one control interval
# Minimize time
opti.minimize(T)
# Apply dynamic constriants
for k in range(N):
x_next = X[:, k] + robot.dynamics_model(X[:, k], U[:, k]) * dt
opti.subject_to(X[:, k + 1] == x_next)
# Wheel constraints
robot.apply_wheel_constraints(opti, vl, vr, al, ar, jl, jr)
# Boundary conditions
# Start
opti.subject_to(xpos[0] == in2m(60) - robot.LENGTH / 2)
opti.subject_to(ypos[0] == in2m(90))
opti.subject_to(theta[0] == 0)
opti.subject_to(vl[0] == 0)
opti.subject_to(vr[0] == 0)
opti.subject_to(al[0] == 0)
opti.subject_to(ar[0] == 0)
opti.subject_to(jl[0] == 0)
opti.subject_to(jr[0] == 0)
# End
robot.apply_finish_line_constraints(
opti,
xpos[-1],
ypos[-1],
theta[-1],
(
(in2m(60) - FINISH_LINE_BUFFER, in2m(60)),
(in2m(60) - FINISH_LINE_BUFFER, in2m(120)),
),
"left",
)
# Obstacles
robot.apply_obstacle_constraints(opti, xpos, ypos, theta, OBSTACLES)
# Time constraints
opti.subject_to(T >= 0)
# Compute initial guess from init traj
x_init, y_init, theta_init = load_init_json(
"init_traj/barrel_racing.json", (in2m(30), in2m(90), 0.0), N
)
# Initial guess
opti.set_initial(xpos, x_init)
opti.set_initial(ypos, y_init)
opti.set_initial(theta, theta_init)
opti.set_initial(vl, 0)
opti.set_initial(vr, 0)
opti.set_initial(al, 0)
opti.set_initial(ar, 0)
opti.set_initial(jl, 0)
opti.set_initial(jr, 0)
opti.set_initial(T, 10)
if plot:
# Plot initialization
plot_traj(
"Initial Trajectory",
x_init,
y_init,
theta_init,
OBSTACLES,
robot.GEOMETRY,
robot.AXIS_SIZE,
)
# Solve non-linear program
opti.solver("ipopt", {}, {"mu_init": 1e-3}) # set numerical backend
sol = opti.solve()
if plot:
# Plot result without wheel force limits
plot_traj(
"Before Wheel Force Limits",
sol.value(xpos),
sol.value(ypos),
sol.value(theta),
OBSTACLES,
robot.GEOMETRY,
robot.AXIS_SIZE,
)
# Solve the problem again, but this time with wheel force & friction limit constraints
robot.apply_wheel_force_constraints(opti, al, ar)
robot.apply_wheel_friction_constraints(opti, vl, vr, al, ar)
# Copy over X, U, and T to initialize
opti.set_initial(X, sol.value(X))
opti.set_initial(U, sol.value(U))
opti.set_initial(T, sol.value(T))
sol = opti.solve()
times = np.linspace(0, sol.value(T), N)
if plot:
# Plot final result
plot_traj(
"Final Result",
sol.value(xpos),
sol.value(ypos),
sol.value(theta),
OBSTACLES,
robot.GEOMETRY,
robot.AXIS_SIZE,
)
plt.figure()
plot_wheel_vel_accel_jerk(
times,
sol.value(vl)[:-1],
sol.value(vr)[:-1],
sol.value(al)[:-1],
sol.value(ar)[:-1],
sol.value(jl),
sol.value(jr),
)
lon_fl, lon_fr = robot.get_longitudinal_wheel_forces(al, ar)
lat_f = robot.get_lateral_wheel_force(vl, vr)
plt.figure()
plot_wheel_forces(
times,
sol.value(lon_fl)[:-1],
sol.value(lon_fr)[:-1],
sol.value(lat_f)[:-1],
)
plt.figure()
plot_total_force(
times,
np.sqrt(sol.value(lon_fl) ** 2 + sol.value(lat_f) ** 2)[:-1],
np.sqrt(sol.value(lon_fr) ** 2 + sol.value(lat_f) ** 2)[:-1],
)
interp_time = 0.02 # seconds
interp_x = interp_state_vector(times, sol.value(xpos), interp_time)
interp_y = interp_state_vector(times, sol.value(ypos), interp_time)
interp_theta = interp_state_vector(times, sol.value(theta), interp_time)
plot_traj(
"Interp",
interp_x,
interp_y,
interp_theta,
OBSTACLES,
robot.GEOMETRY,
robot.AXIS_SIZE,
save_png=True,
)
anim = anim_traj(
"Final Result",
interp_x,
interp_y,
interp_theta,
OBSTACLES,
robot.GEOMETRY,
robot.AXIS_SIZE,
20, # milliseconds
save_gif=False,
)
plt.show()
print(f"Trajectory time: {sol.value(T)} seconds")
if __name__ == "__main__":
robot = Robot()
plan(robot, plot=True)