-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuadcopter.py
179 lines (128 loc) · 4.79 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
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
import numpy as np
from scipy.integrate import RK45
from utils import *
class Quadcopter:
def __init__(self, parameters):
'''
Initialise Quadcopter object with parameters
'''
# Distance of propellers from body-X-axis
self.x_1 = parameters[0]
self.x_2 = parameters[1]
self.x_3 = parameters[2]
self.x_4 = parameters[3]
# Distance of propellers from body-Y-axis
self.y_1 = parameters[4]
self.y_2 = parameters[5]
self.y_3 = parameters[6]
self.y_4 = parameters[7]
# Torque to thrust correlation coeffecient for each propeller
self.c_1 = parameters[8]
self.c_2 = parameters[9]
self.c_3 = parameters[10]
self.c_4 = parameters[11]
# Moment of Inertia
self.J = np.diag(parameters[12:15])
# Mass of quadcopter
self.m = parameters[15]
# Gravity
self.g = 9.8
# M_layout matrix
self.M = np.array([[1, 1, 1, 1],
[self.y_1, self.y_2, self.y_3, self.y_4],
[-self.x_1, -self.x_2, -self.x_3, -self.x_4],
[self.c_1, self.c_2, self.c_3, self.c_4]
])
# Initialise full-state with a column vector of 12 zeros
self.full_state = np.zeros(12)
self.full_state = self.full_state[:, np.newaxis]
# Initialise control-input as a column vector of 4 zeros
self.u = np.zeros(4)
self.u = self.u[:, np.newaxis]
def dynamics(self, X, U):
'''
X: Column vector of full state (12 elements)
U: Column vector of control inputs (4 elements). U accepts the 'actuator inputs' as defined in the book and not thrusts
Returns X_dot
'''
p = X[0:3, :]
p_dot = X[3:6, :]
psi = X[6:9, :]
psi_dot = X[9:, :]
# Define vectors for convenience
G = np.array([0, 0, -self.m*self.g])
G = G[:, np.newaxis]
U1 = np.array([0, 0, U[0, 0]])
U1 = U1[:, np.newaxis]
U2 = U[1:, :]
# Calculate Matrices for convenience
R = Rot_B_I(psi)
T_psi = T(psi)
T_psi_inv = T_inv(psi)
T_psi_dot = T_dot(np.vstack((psi, psi_dot)))
# Dynamics
w = T_psi @ psi_dot
w_dot = np.linalg.inv(self.J)@(U2 \
- np.squeeze(np.cross(w.T, (self.J @ w).T))[:, np.newaxis])
psi_ddot = T_psi_inv@(w_dot - T_psi_dot@psi_dot)
p_ddot = (R@U1 + G)/self.m
return np.vstack((p_dot, p_ddot, psi_dot, psi_ddot))
def modelRK(self, t, X_U):
'''
For ease to interface with RK45, takes inputs in the form of row vector and returns a row vector
Concatenated X and U together
Takes time as input but not uses it because dynamics does not depend on time
'''
X = X_U[:12]
U = X_U[12:]
X = X[:, np.newaxis]
U = U[:, np.newaxis]
X_dot = self.dynamics(X, U)
U_dot = np.zeros((4,1))
return np.squeeze(np.vstack((X_dot, U_dot)))
def HoverDynamics(self):
'''
Returns A and B about equilibrium point assuming hover
'''
b1 = np.zeros((3,3))
b2 = np.eye(3)
b3 = np.array([[0, self.g, 0],
[-self.g, 0, 0],
[0, 0, 0]])
b4 = np.array([[0, 0, 0, 0],
[0, 0, 0, 0],
[1/self.m, 1/self.m, 1/self.m, 1/self.m]])
b5 = np.linalg.inv(self.J)@(self.M[1:, :])
A_hover = np.block([[b1, b2, b1, b1],
[b1, b1, b3, b1],
[b1, b1, b1, b2],
[b1, b1, b1, b1]])
B_hover = np.vstack((np.zeros((3, 4)), b4, np.zeros((3, 4)), b5))
return A_hover, B_hover
def HoverThrusts(self):
'''
Returns row vector of thrusts to keep hovering
'''
f = np.array([self.m*self.g, 0, 0, 0])
thrusts = np.linalg.solve(self.M, f)
return thrusts
class QuadcopterSimulator:
def __init__(self, qc_obj:Quadcopter, Ts=0.05):
'''
Initialise with a Quadcopter object and a sampling time Ts
Ts must be greater than 0.05
'''
self.qc_obj = qc_obj
self.Ts = Ts
def step(self, X, U):
'''
Takes row/column vectors X(t) and U(t) as input
Returns row vector X(t+Ts)
'''
X0_U0 = np.concatenate((np.squeeze(X), np.squeeze(U)))
qc_sol = RK45(fun=self.qc_obj.modelRK, t0=0, y0=X0_U0, t_bound=self.Ts, max_step=0.01, rtol=1e-5)
status = 'running'
while status=='running':
qc_sol.step()
status = qc_sol.status
return (qc_sol.y[:12])