-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadcopter.py
121 lines (107 loc) · 4.09 KB
/
quadcopter.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
from ..formulations.ocp import OCProblem
import numpy as np
import casadi as cs
class Problem(OCProblem):
def __init__(self, Ts: float = 0.1, N: int = 30):
self.Ts = Ts
self.N = N
# Parameters
atmin = 0
atmax = 9.81 * 5
tiltmax = 1.1 / 2
dtiltmax = 0.1
# Dynamics
p = cs.SX.sym("p", 3)
v = cs.SX.sym("v", 3)
θ = cs.SX.sym("theta", 3)
at = cs.SX.sym("at", 1)
ω = cs.SX.sym("omega", 3)
self.state_var = state = cs.vertcat(p, v, θ)
self.input_var = input = cs.vertcat(at, ω)
self.state_names = ["$p_x$", "$p_y$", "$p_z$",
"$v_x$", "$v_y$", "$v_z$",
r"$\theta_x$", r"$\theta_y$", r"$\theta_z$"]
self.input_names = [r"$a_t$",
r"$\omega_x$", r"$\omega_y$", r"$\omega_z$"]
cr = np.cos(θ[0])
sr = np.sin(θ[0])
cp = np.cos(θ[1])
sp = np.sin(θ[1])
cy = np.cos(θ[2])
sy = np.sin(θ[2])
R = cs.vertcat(cs.horzcat(cy*cp, cy*sp*sr-sy*cr, cy*sp*cr + sy*sr),
cs.horzcat(sy*cp, sy*sp*sr + cy*cr, sy*sp*cr - cy*sr),
cs.horzcat(-sp, cp*sr, cp*cr))
at_ = R @ cs.vertcat(0, 0, at)
g = cs.vertcat(0, 0, -9.81)
a = at_ + g
self.f_c = cs.Function("f_c", [state, input], [cs.vertcat(v, a, ω)])
# Discretization
k1 = self.f_c(state, input)
k2 = self.f_c(state + Ts * k1 / 2, input)
k3 = self.f_c(state + Ts * k2 / 2, input)
k4 = self.f_c(state + Ts * k3, input)
f_d_expr = state + (Ts / 6) * (k1 + 2 * k2 + 2 * k3 + k4)
self.f_dynamics = cs.Function("f", [state, input], [f_d_expr])
# Input constraints
self.input_constr_box = (
np.array([atmin, -dtiltmax, -dtiltmax, -dtiltmax]),
np.array([atmax, +dtiltmax, +dtiltmax, +dtiltmax]),
)
# State constraints
self.stage_constr = cs.Function("c", [state], [cs.vertcat(
θ[0],
θ[1],
cs.cos(θ[0]) * cs.cos(θ[1]),
# 1e4 * cs.fmax(0, 0.1**2 - p[0]**2) * cs.fmax(0, 0.1**2 - p[1]**2),
# x²+y²>r² ~ r²-x²-y²<0
0.1**2 - p[0]**2 - p[1]**2,
)])
self.stage_constr_box = (
np.array([-np.pi/2, -np.pi/2, np.cos(tiltmax), -np.inf]),
np.array([+np.pi/2, +np.pi/2, +np.inf, 0]),
)
self.term_constr = self.stage_constr
self.term_constr_box = self.stage_constr_box
# Initial state
self.p0 = p0 = np.array([-0.2, -0.25, 0.5])
# self.v0 = v0 = np.array([1., 1., 1.])
self.v0 = v0 = np.array([0., 0., 0.])
# self.θ0 = θ0 = np.array([np.pi/10, np.pi/10, np.pi/10])
self.θ0 = θ0 = np.array([0., 0., 0.])
self.initial_state = np.concatenate((p0, v0, θ0))
self.pf = pf = np.array([0.25, 0.25, 0.5])
# Objective
alpha = 1.
beta = 10.
gamma = 10.
delta = 1.
self.stage_cost_state = cs.Function("lx", [state], [
alpha * cs.sum1(v**2) +
gamma * cs.sum1((p - pf)**2) +
delta * cs.sum1(θ**2) +
0
])
self.stage_cost_input = cs.Function("lu", [input], [
1e-4 * input.T @ input +
beta * cs.sum1(ω**2) +
0
])
self.stage_cost = cs.Function("l", [cs.vertcat(state, input)], [
self.stage_cost_input(input) + self.stage_cost_state(state)
])
self.term_cost = cs.Function("l_N", [state], [
25 * alpha * cs.sum1(v**2) +
25 * gamma * cs.sum1((p - pf)**2) +
10 * delta * cs.sum1(θ**2) +
0
])
self.initial_guess = np.tile(np.array([9.81, 0, 0, 0]), N)
self.plot_2d = True
self.plot_figsize = (4, 4)
self.plot_x = 0
self.plot_y = 1
self.plot_collision_constr = (3,)
self.plot_constr_xlim = (-0.35, 0.35)
self.plot_constr_ylim = (-0.35, 0.35)
self.plot_constr_num = 200