-
Notifications
You must be signed in to change notification settings - Fork 0
/
kalman_triangle_2D.py
238 lines (204 loc) · 6.55 KB
/
kalman_triangle_2D.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
from scipy import linalg as la
import matplotlib.pyplot as pl
import numpy as np
import quadrotor as quad
import quadlog
import animation as ani
from time import time as tm
# Quadrotor
m = 0.65 # Kg
l = 0.23 # m
Jxx = 7.5e-3 # Kg/m^2
Jyy = Jxx
Jzz = 1.3e-2
Jxy = 0
Jxz = 0
Jyz = 0
J = np.array([[Jxx, Jxy, Jxz], \
[Jxy, Jyy, Jyz], \
[Jxz, Jyz, Jzz]])
CDl = 9e-3
CDr = 9e-4
kt = 3.13e-5 # Ns^2
km = 7.5e-7 # Ns^2
kw = 1/0.18 # rad/s
# Initial conditions
att_0 = np.array([0.0, 0.0, 0.0])
pqr_0 = np.array([0.0, 0.0, 0.0])
xyz1_0 = np.array([1.0, 1.2, 0.0])
xyz2_0 = np.array([1.2, 2.0, 0.0])
xyz3_0 = np.array([-1.1, 2.6, 0.0])
# xyz1_0 = np.array([0.0, 0.0, 0.0])
# xyz2_0 = np.array([0.0, 0.0, 0.0])
# xyz3_0 = np.array([0.0, 0.0, 0.0])
v_ned_0 = np.array([0.0, 0.0, 0.0])
w_0 = np.array([0.0, 0.0, 0.0, 0.0])
# Formation information
B = np.array([[-1, 0],
[ 0, 1],
[ 1,-1]])
D_e = np.array([[2],
[2]])
D_n = np.array([[ 3.464],
[-3.464]])
# Simulation parameters
tf = 400
dt = 1e-2
time = np.linspace(0, tf, tf/dt)
it = 0
frames = 1000
# Setting quads
q1 = quad.quadrotor(1, dt, m, l, J, CDl, CDr, kt, km, kw, \
att_0, pqr_0, xyz1_0, v_ned_0, w_0, B, D_e, D_n)
q2 = quad.quadrotor(2, dt, m, l, J, CDl, CDr, kt, km, kw, \
att_0, pqr_0, xyz2_0, v_ned_0, w_0, B, D_e, D_n)
q3 = quad.quadrotor(3, dt, m, l, J, CDl, CDr, kt, km, kw, \
att_0, pqr_0, xyz3_0, v_ned_0, w_0, B, D_e, D_n)
# Data log
q1_log = quadlog.quadlog(time)
q2_log = quadlog.quadlog(time)
q3_log = quadlog.quadlog(time)
# Plots
quadcolor = ['r', 'g', 'b']
pl.close("all")
pl.ion()
fig = pl.figure(0)
axis3d = fig.add_subplot(111, projection='3d')
init_area = 5
s = 2
# Desired altitude and heading
v_2D_d = np.array([0, 0])
alt_d = -4
# q1.yaw_d = 0
timer = tm()
for t in time:
# Simulation
# bias1 = np.array([q1.c_q[2][0], q1.c_q[2][0]])
# bias2 = np.array([q2.c_q[2][0], q2.c_q[2][0]])
# bias3 = np.array([q3.c_q[2][0], q3.c_q[2][0]])
#
# q1.set_v_2D_alt_lya(v_2D_d - bias1, alt_d)
# q2.set_v_2D_alt_lya(v_2D_d - bias2, alt_d)
# q3.set_v_2D_alt_lya(v_2D_d - bias3, alt_d)
n_all = np.array([q1.xyz[0], q2.xyz[0], q3.xyz[0]])
e_all = np.array([q1.xyz[1], q2.xyz[1], q3.xyz[1]])
p_n = np.array([[q1.xyz[0]],
[q2.xyz[0]],
[q3.xyz[0]]])
p_e = np.array([[q1.xyz[1]],
[q2.xyz[1]],
[q3.xyz[1]]])
# bias = np.array([q1.c_q[2][0], q1.c_q[2][0]])
# q1.set_v_2D_alt_lya(v_2D_d - bias,alt_d)
q1.set_auto_formation(p_n, p_e, alt_d)
q2.set_auto_formation(p_n, p_e, alt_d)
q3.set_auto_formation(p_n, p_e, alt_d)
q1.step(dt)
q2.step(dt)
q3.step(dt)
# Animation
if it%frames == 0:
pl.figure(0)
axis3d.cla()
ani.draw3d(axis3d, q1.xyz, q1.Rot_bn(), quadcolor[0])
ani.draw3d(axis3d, q2.xyz, q2.Rot_bn(), quadcolor[1])
ani.draw3d(axis3d, q3.xyz, q3.Rot_bn(), quadcolor[2])
axis3d.set_xlim(-5, 5)
axis3d.set_ylim(-5, 5)
axis3d.set_zlim(0, 10)
axis3d.set_xlabel('South [m]')
axis3d.set_ylabel('East [m]')
axis3d.set_zlabel('Up [m]')
axis3d.set_title("Time %.3f s" %t)
# pl.pause(0.001)
pl.draw()
#namepic = '%i'%it
#digits = len(str(it))
#for j in range(0, 5-digits):
# namepic = '0' + namepic
#pl.savefig("./images/%s.png"%namepic)
pl.figure(1)
pl.clf()
for i in range(len(quadcolor)):
pl.plot(e_all[i], n_all[i], 'o' + quadcolor[i])
# ani.draw2d(1, X, fc, quadcolor)
# ani.draw_edges(1, X, fc, -1)
pl.plot(np.array([q1.xyz[1], q2.xyz[1], q3.xyz[1],q1.xyz[1]]),np.array([q1.xyz[0], q2.xyz[0], q3.xyz[0],q1.xyz[0]]), 'k--', lw=2)
pl.xlabel('South [m]')
pl.ylabel('West [m]')
pl.title('2D Map')
pl.axis('equal')
pl.axis([-s*init_area, s*init_area, -s*init_area, s*init_area])
# pl.xlim(-s*init_area, s*init_area)
# pl.ylim(-s*init_area, s*init_area)
pl.grid()
pl.pause(0.001)
pl.draw()
q1p = np.array([q1.xyz[0], q1.xyz[1]])
q2p = np.array([q2.xyz[0], q2.xyz[1]])
q3p = np.array([q3.xyz[0], q3.xyz[1]])
# Log
q1_log.xyz_h[it, :] = q1.xyz
q1_log.d[it] = np.linalg.norm(q2p - q1p)
q1_log.w_h[it, :] = q1.w
q1_log.v_ned_h[it, :] = q1.dB
q1_log.b_h[it] = q1.c_q[2][0]
q2_log.xyz_h[it, :] = q2.xyz
q2_log.d[it] = np.linalg.norm(q3p - q2p)
q2_log.w_h[it, :] = q2.w
q2_log.v_ned_h[it, :] = q2.dB
q2_log.b_h[it] = q2.c_q[2][0]
q3_log.xyz_h[it, :] = q3.xyz
q3_log.d[it] = np.linalg.norm(q1p - q3p)
q3_log.w_h[it, :] = q3.w
q3_log.v_ned_h[it, :] = q3.dB
q3_log.b_h[it] = q3.c_q[2][0]
it+=1
# Stop if crash
if (q1.crashed == 1 or q2.crashed == 1 or q3.crashed == 1):
break
print(tm()-timer)
pl.figure(1)
pl.clf()
for i in range(len(quadcolor)):
pl.plot(e_all[i], n_all[i], 'o' + quadcolor[i])
pl.plot(q1_log.xyz_h[:, 1], q1_log.xyz_h[:, 0], label="q1", color=quadcolor[0])
pl.plot(q2_log.xyz_h[:, 1], q2_log.xyz_h[:, 0], label="q2", color=quadcolor[1])
pl.plot(q3_log.xyz_h[:, 1], q3_log.xyz_h[:, 0], label="q3", color=quadcolor[2])
pl.plot(np.array([q1.xyz[1], q2.xyz[1], q3.xyz[1], q1.xyz[1]]), np.array([q1.xyz[0], q2.xyz[0], q3.xyz[0], q1.xyz[0]]),
'k--', lw=2)
pl.xlabel("South")
pl.ylabel("West")
pl.title("2D Position [m]")
pl.axis('equal')
pl.axis([-s*init_area, s*init_area, -s*init_area, s*init_area])
pl.grid()
pl.legend()
pl.figure(2)
pl.plot(time, q1_log.v_ned_h[:, 0], label="q1",color=quadcolor[0])
pl.plot(time, q2_log.v_ned_h[:, 0], label="q2",color=quadcolor[1])
pl.plot(time, q3_log.v_ned_h[:, 0], label="q3",color=quadcolor[2])
pl.xlabel("Time [s]")
pl.ylabel("Derivation of the estimated bias [m/s^2]")
pl.grid()
pl.legend()
pl.figure(3)
pl.plot(time, q1_log.d[:], label="$||q_2 - q_1||$", color='y')
pl.plot(time, q2_log.d[:], label="$||q_3 - q_2||$", color='c')
pl.plot(time, q3_log.d[:], label="$||q_1 - q_3||$", color='m')
pl.xlabel("Time [s]")
pl.ylabel("Triangle sides lengths [m]")
pl.grid()
pl.legend(loc=2)
pl.figure(4)
pl.plot(time, q1_log.b_h, label="bias q1", color=quadcolor[0])
pl.plot(time, q2_log.b_h, label="bias q2", color=quadcolor[1])
pl.plot(time, q3_log.b_h, label="bias q3", color=quadcolor[2])
pl.xlabel("Time [s]")
pl.ylabel("Estimated bias [m/s]")
pl.grid()
pl.legend()
print(np.linalg.norm(q2p-q1p))
print(np.linalg.norm(q3p-q2p))
print(np.linalg.norm(q1p-q3p))
pl.pause(0)