-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckIn6
192 lines (135 loc) · 5.1 KB
/
CheckIn6
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
import numpy as np
from casadi import *
import matplotlib.pyplot as plt
class DronePositionEstimator:
def __init__(self):
self.nx = 9
self.nz = 11
self.x = SX.sym('x', self.nx)
self.u = SX.sym('u', 6)
self.z = SX.sym('z', self.nz)
self.pos = self.x[0:3]
self.vel = self.x[3:6]
self.att = self.x[6:9]
self.g = 9.81
self.mass = 1.5
self.dt = 0.01
self._initialize_covariances()
self._setup_process_model()
self._setup_measurement_model()
def _initialize_covariances(self):
self.Q = np.diag([
0.1, 0.1, 0.1,
0.2, 0.2, 0.2,
0.1, 0.1, 0.1
])
self.R = np.diag([
2.0, 2.0, 3.0,
0.1, 0.1, 0.1,
0.01, 0.01, 0.01,
0.5,
0.1
])
def _rotation_matrix(self, phi, theta, psi):
R_x = vertcat(
horzcat(1, 0, 0),
horzcat(0, cos(phi), -sin(phi)),
horzcat(0, sin(phi), cos(phi))
)
R_y = vertcat(
horzcat(cos(theta), 0, sin(theta)),
horzcat(0, 1, 0),
horzcat(-sin(theta), 0, cos(theta))
)
R_z = vertcat(
horzcat(cos(psi), -sin(psi), 0),
horzcat(sin(psi), cos(psi), 0),
horzcat(0, 0, 1)
)
return R_z @ R_y @ R_x
def _setup_process_model(self):
"""Set up the nonlinear process model"""
phi, theta, psi = self.att[0], self.att[1], self.att[2]
R = self._rotation_matrix(phi, theta, psi)
thrust = self.u[0]
omega = self.u[1:4]
a_body = vertcat(0, 0, thrust/self.mass)
a_inertial = R @ a_body + vertcat(0, 0, -self.g)
pos_dot = self.vel
vel_dot = a_inertial
att_dot = omega
x_dot = vertcat(pos_dot, vel_dot, att_dot)
k1 = x_dot
k2 = substitute(x_dot, self.x, self.x + self.dt/2 * k1)
k3 = substitute(x_dot, self.x, self.x + self.dt/2 * k2)
k4 = substitute(x_dot, self.x, self.x + self.dt * k3)
f = self.x + self.dt/6 * (k1 + 2*k2 + 2*k3 + k4)
self.f_func = Function('f', [self.x, self.u], [f])
self.F_func = Function('F', [self.x, self.u], [jacobian(f, self.x)])
def _setup_measurement_model(self):
h_gps = self.pos
phi, theta, psi = self.att[0], self.att[1], self.att[2]
R = self._rotation_matrix(phi, theta, psi)
f_b = transpose(R) @ (self.vel - vertcat(0, 0, -self.g))
omega = self.att
h_baro = self.pos[2]
h_mag = self.att[2]
h = vertcat(h_gps, f_b, omega, h_baro, h_mag)
self.h_func = Function('h', [self.x], [h])
self.H_func = Function('H', [self.x], [jacobian(h, self.x)])
def predict(self, x_prev, P_prev, u):
x_pred = np.array(self.f_func(x_prev, u)).flatten()
F = np.array(self.F_func(x_prev, u))
P_pred = F @ P_prev @ F.T + self.Q
return x_pred, P_pred
def update(self, x_pred, P_pred, z_meas):
z_pred = np.array(self.h_func(x_pred)).flatten()
H = np.array(self.H_func(x_pred))
y = z_meas - z_pred
S = H @ P_pred @ H.T + self.R
K = P_pred @ H.T @ np.linalg.inv(S)
x_post = x_pred + K @ y
P_post = (np.eye(self.nx) - K @ H) @ P_pred
return x_post, P_post
def normalize_angles(self, x):
x[6:9] = np.mod(x[6:9] + np.pi, 2 * np.pi) - np.pi
return x
def run_drone_estimator():
estimator = DronePositionEstimator()
x = np.zeros(9)
P = np.eye(9) * 0.1
z = np.array([
0.1, 0.2, 0.3,
0.0, 0.0, 9.81,
0.0, 0.0, 0.0,
0.3,
0.0
])
u = np.array([
9.81 * 1.5,
0.0, 0.0, 0.0,
0.0, 0.0
])
x_pred, P_pred = estimator.predict(x, P, u)
x_post, P_post = estimator.update(x_pred, P_pred, z)
x_post = estimator.normalize_angles(x_post)
return x_post, P_post
def simulate_drone_position():
timesteps = 100
dt = 0.1
time = np.linspace(0, timesteps * dt, timesteps)
x = np.sin(time)
y = np.cos(time)
z = 0.1 * time
return time, x, y, z
time, x, y, z = simulate_drone_position()
plt.figure(figsize=(10, 6))
plt.plot(time, x, label='x (Position along x-axis)', color='red')
plt.plot(time, y, label='y (Position along y-axis)', color='blue')
plt.plot(time, z, label='z (Position along z-axis)', color='green')
plt.title('Simulated Drone Position Over Time')
plt.xlabel('Time (s)')
plt.ylabel('Position (m)')
plt.legend()
plt.grid(True)
plt.show()