-
Notifications
You must be signed in to change notification settings - Fork 0
/
kalman_vel_ned.py
158 lines (136 loc) · 3.5 KB
/
kalman_vel_ned.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
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
# 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])
xyz_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])
W = np.array([[0, 1, 1],
[1, 0, 1],
[1, 1, 0]])
D_e = np.array([[ 0, 4, 2],
[-4, 0, -2],
[-2, 2, 0]])
D_n = np.array([[ 0, 0, 3.464],
[ 0, 0, 3.464],
[-3.464, -3.464, 0]])
# Simulation parameters
tf = 100
dt = 1e-2
time = np.linspace(0, tf, tf/dt)
it = 0
frames = 100
# Setting quads
q1 = quad.quadrotor(1, dt, m, l, J, CDl, CDr, kt, km, kw, \
att_0, pqr_0, xyz_0, v_ned_0, w_0, W, D_e, D_n)
# Data log
q1_log = quadlog.quadlog(time)
# Plots
quadcolor = ['k']
pl.close("all")
pl.ion()
fig = pl.figure(0)
axis3d = fig.add_subplot(111, projection='3d')
# Desired position and heading
v_2D_d = np.array([0, 0])
alt_d = -8
q1.yaw_d = np.pi/3
for t in time:
# Simulation
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.step(dt)
# Animation
if it%frames == 0:
pl.figure(0)
axis3d.cla()
ani.draw3d(axis3d, q1.xyz, q1.Rot_bn(), quadcolor[0])
axis3d.set_xlim(-10, 10)
axis3d.set_ylim(-10, 10)
axis3d.set_zlim(0, 15)
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()
# Log
q1_log.xyz_h[it, :] = q1.xyz
q1_log.w_h[it, :] = q1.w
q1_log.v_ned_h[it, :] = q1.v_ned
q1_log.xi_g_h[it] = q1.xi_g
q1_log.xi_CD_h[it] = q1.xi_CD
q1_log.b_h[it] = q1.c_q[2][0]
it+=1
# Stop if crash
if (q1.crashed == 1):
break
pl.figure(1)
pl.plot(time, q1_log.w_h[:, 0], label="w_1")
pl.plot(time, q1_log.w_h[:, 1], label="w_2")
pl.plot(time, q1_log.w_h[:, 2], label="w_3")
pl.plot(time, q1_log.w_h[:, 3], label="w_4")
pl.xlabel("Time [s]")
pl.ylabel("Motor angular velocity [rad/s]")
pl.grid()
pl.legend()
# pl.figure(2)
# pl.plot(time, q1_log.d[:, 0], label="roll")
# pl.plot(time, q1_log.d[:, 1], label="pitch")
# pl.plot(time, q1_log.d[:, 2], label="yaw")
# pl.xlabel("Time [s]")
# pl.ylabel("Attitude angle [rad]")
# pl.grid()
# pl.legend()
pl.figure(3)
pl.plot(time, -q1_log.xyz_h[:, 2], label="UP")
pl.plot(time, q1_log.xyz_h[:, 0], label="X")
pl.plot(time, q1_log.xyz_h[:, 1], label="Y")
pl.xlabel("Time [s]")
pl.ylabel("Position [m]")
pl.grid()
pl.legend()
pl.figure(4)
pl.plot(time, -q1_log.v_ned_h[:, 2], label="-V_z")
pl.plot(time, q1_log.v_ned_h[:, 0], label="V_x")
pl.plot(time, q1_log.v_ned_h[:, 1], label="V_y")
pl.xlabel("Time [s]")
pl.ylabel("Velocity [m/s]")
pl.grid()
pl.legend()
pl.figure(5)
pl.plot(time, q1_log.xi_g_h, label="${\\xi}_g$")
pl.plot(time, q1_log.xi_CD_h, label="${\\xi}_{CD}$")
pl.xlabel("Time [s]")
pl.ylabel("Estimators value")
pl.grid()
pl.legend()
pl.figure(6)
pl.plot(time, q1_log.b_h)
pl.xlabel("Time [s]")
pl.ylabel("Estimated bias [m/s]")
pl.grid()
pl.legend()
pl.pause(0)