1from typing
import Union
4from casadi
import vertcat
as vc
8 def __init__(self, N: int, dim: int):
12 self.
y1y1 = cs.SX.sym(
"y1", dim * N, 1)
13 self.
y2y2 = cs.SX.sym(
"y2", dim * N, 1)
14 self.
y3y3 = cs.SX.sym(
"y3", dim, 1)
15 self.
uu = cs.SX.sym(
"u", dim, 1)
16 self.
yy = vc(self.
y1y1, self.
y2y2, self.
y3y3)
18 self.
mm = cs.SX.sym(
"m")
19 self.
DD = cs.SX.sym(
"D")
20 self.
LL = cs.SX.sym(
"L")
24 self.
gg = np.array([0, 0, -9.81]
if dim == 3
else [0, -9.81])
25 self.
x0x0 = np.zeros((dim, ))
26 self.
x_endx_end = np.eye(1, dim, 0).ravel()
29 y, y1, y2, y3, u = self.
yy, self.
y1y1, self.
y2y2, self.
y3y3, self.
uu
30 dist =
lambda xa, xb: cs.norm_2(xa - xb)
31 N, d = self.
NN, self.
dimdim
39 xi = y1[d * i:d * i + d]
40 xip1 = y1[d * i + d:d * i + d * 2]
if i < N - 1
else y3
41 Fiip1 = self.
DD * (1 - self.
LL / dist(xip1, xi)) * (xip1 - xi)
42 xim1 = y1[d * i - d:d * i]
if i > 0
else self.
x0x0
43 Fim1i = self.
DD * (1 - self.
LL / dist(xi, xim1)) * (xi - xim1)
44 fi = (Fiip1 - Fim1i) / self.
mm + self.
gg
48 f_expr = vc(*f1, *f2, *f3)
49 self.
ff = cs.Function(
"f", [y, u, p], [f_expr], [
"y",
"u",
"p"], [
"y'"])
53 opt = {
"tf": Ts,
"simplify":
True,
"number_of_finite_elements": 4}
54 intg = cs.integrator(
"intg",
"rk", {
60 f_d_expr = intg(x0=y, p=vc(u, p))[
"xf"]
61 self.
f_df_d = cs.Function(
"f_d", [y, u, p], [f_d_expr], [
"y",
"u",
"p"],
67 N, d = self.
NN, self.
dimdim
68 rav =
lambda x: np.array(x).ravel()
69 xdim =
lambda y, i: np.concatenate(
70 ([0], rav(y[i:d * N:d]), rav(y[-d + i])))
72 return (xdim(y, 0), xdim(y, 1), xdim(y, 2))
74 return (xdim(y, 0), xdim(y, 1), np.zeros((N + 1, )))
78 Reshape the input signal from a vector into a dim × N_horiz matrix (note
79 that CasADi matrices are stored column-wise
and NumPy arrays row-wise)
81 if isinstance(u, np.ndarray):
82 return u.reshape((self.
dimdim, u.shape[0] // self.
dimdim), order=
'F')
84 return u.reshape((self.
dimdim, u.shape[0] // self.
dimdim))
86 def simulate(self, N_sim: int, y_0: np.ndarray, u: Union[np.ndarray, list,
88 p: Union[np.ndarray, list, cs.SX.sym]):
89 if isinstance(u, list):
91 if isinstance(u, np.ndarray):
92 if u.ndim == 1
or (u.ndim == 2
and u.shape[1] == 1):
93 if u.shape[0] == self.
dimdim:
94 u = np.tile(u, (N_sim, 1)).T
95 return self.
f_df_d.mapaccum(N_sim)(y_0, u, p)
98 N, d = self.
NN, self.
dimdim
99 y1_0 = np.zeros((d * N))
100 y1_0[0::d] = np.arange(1, N + 1) / (N + 1)
101 y2_0 = np.zeros((d * N))
102 y3_0 = np.zeros((d, ))
105 y_null = np.concatenate((y1_0, y2_0, y3_0))
106 u_null = np.zeros((d, ))
108 return y_null, u_null
111 N, d = self.
NN, self.
dimdim
112 y1t = cs.SX.sym(
"y1t", d * N, 1)
113 y2t = cs.SX.sym(
"y2t", d * N, 1)
114 y3t = cs.SX.sym(
"y3t", d, 1)
115 ut = cs.SX.sym(
"ut", d, 1)
116 yt = cs.vertcat(y1t, y2t, y3t)
118 L_cost = α * cs.sumsqr(y3t - self.
x_endx_end) + γ * cs.sumsqr(ut)
120 xdi = y2t[d * i:d * i + d]
121 L_cost += β * cs.sumsqr(xdi)
122 return cs.Function(
"L_cost", [yt, ut], [L_cost])
def generate_cost_fun(self, α=25, β=1, γ=0.01)
def simulate(self, int N_sim, np.ndarray y_0, Union[np.ndarray, list, cs.SX.sym] u, Union[np.ndarray, list, cs.SX.sym] p)
def dynamics(self, Ts=0.05)
def state_to_pos(self, y)
def input_to_matrix(self, u)