-
Notifications
You must be signed in to change notification settings - Fork 0
/
pvtol_model.py
252 lines (187 loc) · 6.35 KB
/
pvtol_model.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
""" pvtol model
(c) Martin Doff-Sotta, University of Oxford (martin.doff-sotta@eng.ox.ac.uk)
"""
import numpy as np
import cvxpy as cp
## Dynamics
def f_full(x, u, p):
""" PVTOL full dynamics
Let (x1, x2, x3, x4) = (alpha, y', z', alpha'), the state space model is:
x1' = x4
x2' = (g + u1)*sin(x1)
x3' = (g + u1)*cos(x1) - g
x4' = u2
"""
da = x[3]
ddy = (u[0] + p.g)*np.sin(x[0])
ddz = (u[0] + p.g)*np.cos(x[0]) - p.g
dda = u[1]
return np.array([da, ddy, ddz, dda])
def f(alpha, u, p):
""" PVTOL nonlinear dynamics """
ddy = (u + p.g)*np.sin(alpha)
ddz = (u + p.g)*np.cos(alpha) - p.g
return np.vstack([ddy, ddz])
def ddy(alpha, u, p):
""" PVTOL nonlinear y-axis dynamics """
return (u + p.g)*np.sin(alpha)
def ddz(alpha, u, p):
""" PVTOL nonlinear z-axis dynamics """
return (u + p.g)*np.cos(alpha) - p.g
## Linearise model
def derivative_weight(x, sigma, dsigma, weights, N_state):
""" Derivative of model from weights
N_state: number of states in x =[x_k, u_k]
"""
# First layer
x0 = x
W = weights[0].T
b = weights[1].T
z = W @ x + b[:, None]
x = sigma(z)
# Derivative
A = dsigma(z[:, 0]) @ W[:, 0:N_state]
B = dsigma(z[:, 0]) @ W[:, N_state:]
# Internal layers
N = (len(weights)-4)//4
for i in range(N):
Wx = weights[2+i*4].T
bx = weights[2+i*4+1].T
W0 = weights[2+i*4+2].T
b0 = weights[2+i*4+3].T
z = Wx @ x + bx[:, None] + W0 @ x0 + b0[:, None]
x = sigma(z)
# Derivative
A = dsigma(z[:, 0]) @ (Wx @ A + W0[:, 0:N_state])
B = dsigma(z[:, 0]) @ (Wx @ B + W0[:, N_state:])
# Last layer
W = weights[-2].T
b = weights[-1].T
z = W @ x + b[:, None]
# Derivative
A = W @ A # no dsigma(z) @ because no activation on last layer
B = W @ B # no dsigma(z) @ because no activation on last layer
return A, B
def linearise(x_0, u_0, weights_g, weights_h, sigma, dsigma):
"""
Form the DC linearised continuous-time model of the PVTOL around x_0, u_0
dx/dt = (A1 - A2) x + (B1 - B2) u
"""
# Dimensions
N_state = x_0.shape[0]
N_input = u_0.shape[0]
N = u_0.shape[1]
# Initialisation
A1 = np.zeros((N, N_state, N_state))
A2 = np.zeros((N, N_state, N_state))
B1 = np.zeros((N, N_state, N_input))
B2 = np.zeros((N, N_state, N_input))
x_ = np.hstack([x_0[0, None, :].T, u_0[0, None, :].T])
x_ = np.vstack([x_0[0, None, :], u_0[0, None, :]])
N_state_inner = 1 # number of states for nonlinear dynamics
N_input_inner = x_.shape[0] - N_state_inner # number of input for nonlinear dynamics
A_g = np.zeros((N, x_.shape[0], N_state_inner))
B_g = np.zeros((N, x_.shape[0], N_input_inner))
A_h = np.zeros((N, x_.shape[0], N_state_inner))
B_h = np.zeros((N, x_.shape[0], N_input_inner))
for i in range(N):
A_g[i, :, :], B_g[i, :, :] = derivative_weight(x_[:, i, None], sigma, dsigma, weights_g, N_state_inner)
A_h[i, :, :], B_h[i, :, :] = derivative_weight(x_[:, i, None], sigma, dsigma, weights_h, N_state_inner)
# A
A1[:, 0, 3] = 1
A1[:, 1, 0] = A_g[:, 0, 0]
A1[:, 2, 0] = A_g[:, 1, 0]
A2[:, 1, 0] = A_h[:, 0, 0]
A2[:, 2, 0] = A_h[:, 1, 0]
# B
B1[:, 3, 1] = 1
B1[:, 1, 0] = B_g[:, 0, 0]
B1[:, 2, 0] = B_g[:, 1, 0]
B2[:, 1, 0] = B_h[:, 0, 0]
B2[:, 2, 0] = B_h[:, 1, 0]
return A1, B1, A2, B2
def discretise(A, B, delta):
"""
Convert a continuous-time state space model into a discrete-time state space model
Input:
- A, B: continuous-time state space model
- delta: time step
Output:
- A_d, B_d: discrete-time state space model
"""
# Dimensions
N_state = A.shape[1]
# Linearised discrete-time model
A_d = np.eye(N_state) + delta*A
B_d = delta*B
return A_d, B_d
def linearise_true(x_0, u_0, p):
"""Form the linearised continuous-time model of the PVTOL around x_0, u_0
dx/dt = A x + B u
A = [ 0 0 0 1
(g+u1)*cos(x1) 0 0 0
-(g+u1)*sin(x1) 0 0 0
0 0 0 0],
B = [ 0 0
sin(x1) 0
cos(x1) 0
0 1],
and I is the identity.
Input:
- x_0: guess state trajectory
- u_0: guess input trajectory
- p: structure of parameters
Output:
- A, B: continuous-time matrices"""
# Dimensions
N_state = x_0.shape[0]
N_input = u_0.shape[0]
N = u_0.shape[1]
# Initialisation
A = np.zeros((N, N_state, N_state))
B = np.zeros((N, N_state, N_input))
# A
A[:, 0, 3] = 1
A[:, 1, 0] = (p.g + u_0[0, :])*np.cos(x_0[0, :])
A[:, 2, 0] = -(p.g + u_0[0, :])*np.sin(x_0[0, :])
# B
B[:, 1, 0] = np.sin(x_0[0, :])
B[:, 2, 0] = np.cos(x_0[0, :])
B[:, 3, 1] = 1
return A, B
def feasibility(f, x_0, x_r, delta, N, param):
""" Generate a feasible trajectory for the PVTOL """
# Initialisation
N_state = x_0.shape[0]
N_input = 2
u = np.zeros((N_input, N)) # control input
x = np.zeros((N_state, N+1))
t = np.zeros((N+1, ))
# Reference
zr = 0
ar = param.h_r[0]
# Compute trajectory
x[:, 0] = x_0
h = -1 # height
for i in range(N):
# Feedback linearisation
u1 = (1*(zr-h + 1*-x[2, i])+param.g)/np.cos(x[0, i]) -param.g
u2 = 3*(ar-x[0, i]) + 5*-x[3, i]
u[:, i] = np.array([u1, u2])
# Dynamic update
x[:, i+1] = x[:, i] + delta*(f_full(x[:, i], u[:, i], param))
t[i+1] = t[i] + delta
# height update
h = h + delta*x[2, i]
return x, u, t
def interp_feas(t_0, t_feas, x_feas, u_feas):
N = t_0.shape[0]-1
N_state = x_feas.shape[0]
N_input = u_feas.shape[0]
x_0 = np.zeros((N_state, N+1))
u_0 = np.zeros((N_input, N))
for i in range(N_state):
x_0[i, :] = np.interp(t_0, t_feas, x_feas[i, :])
for i in range(N_input):
u_0[i, :] = np.interp(t_0[:-1], t_feas[:-1], u_feas[i, :])
return x_0, u_0