Skip to content

Commit cfe1e30

Browse files
Addition of velocity scale factor estimation to EKF (AtsushiSakai#937)
* Addition of velocity scale factor estimation to EKF * Format * Add a scale factor motion model in the Jacobian function description * Fix Jacobian function description * New script: 'ekf_with_velocity_correction.py' * Add doc * Fix doc * Correct the parts where the first letter of the sentence is lowercase * Fix doc title * Fix script title * Do grouping * Fix wrong motion function in ekf doc * Update docs/modules/localization/extended_kalman_filter_localization_files/extended_kalman_filter_localization_main.rst --------- Co-authored-by: Atsushi Sakai <[email protected]>
1 parent 4fcab6a commit cfe1e30

File tree

3 files changed

+303
-0
lines changed

3 files changed

+303
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
"""
2+
3+
Extended kalman filter (EKF) localization with velocity correction sample
4+
5+
author: Atsushi Sakai (@Atsushi_twi)
6+
modified by: Ryohei Sasaki (@rsasaki0109)
7+
8+
"""
9+
import sys
10+
import pathlib
11+
12+
sys.path.append(str(pathlib.Path(__file__).parent.parent.parent))
13+
14+
import math
15+
import matplotlib.pyplot as plt
16+
import numpy as np
17+
18+
from utils.plot import plot_covariance_ellipse
19+
20+
# Covariance for EKF simulation
21+
Q = np.diag([
22+
0.1, # variance of location on x-axis
23+
0.1, # variance of location on y-axis
24+
np.deg2rad(1.0), # variance of yaw angle
25+
0.4, # variance of velocity
26+
0.1 # variance of scale factor
27+
]) ** 2 # predict state covariance
28+
R = np.diag([0.1, 0.1]) ** 2 # Observation x,y position covariance
29+
30+
# Simulation parameter
31+
INPUT_NOISE = np.diag([0.1, np.deg2rad(5.0)]) ** 2
32+
GPS_NOISE = np.diag([0.05, 0.05]) ** 2
33+
34+
DT = 0.1 # time tick [s]
35+
SIM_TIME = 50.0 # simulation time [s]
36+
37+
show_animation = True
38+
39+
40+
def calc_input():
41+
v = 1.0 # [m/s]
42+
yawrate = 0.1 # [rad/s]
43+
u = np.array([[v], [yawrate]])
44+
return u
45+
46+
47+
def observation(xTrue, xd, u):
48+
xTrue = motion_model(xTrue, u)
49+
50+
# add noise to gps x-y
51+
z = observation_model(xTrue) + GPS_NOISE @ np.random.randn(2, 1)
52+
53+
# add noise to input
54+
ud = u + INPUT_NOISE @ np.random.randn(2, 1)
55+
56+
xd = motion_model(xd, ud)
57+
58+
return xTrue, z, xd, ud
59+
60+
61+
def motion_model(x, u):
62+
F = np.array([[1.0, 0, 0, 0, 0],
63+
[0, 1.0, 0, 0, 0],
64+
[0, 0, 1.0, 0, 0],
65+
[0, 0, 0, 0, 0],
66+
[0, 0, 0, 0, 1.0]])
67+
68+
B = np.array([[DT * math.cos(x[2, 0]) * x[4, 0], 0],
69+
[DT * math.sin(x[2, 0]) * x[4, 0], 0],
70+
[0.0, DT],
71+
[1.0, 0.0],
72+
[0.0, 0.0]])
73+
74+
x = F @ x + B @ u
75+
76+
return x
77+
78+
79+
def observation_model(x):
80+
H = np.array([
81+
[1, 0, 0, 0, 0],
82+
[0, 1, 0, 0, 0]
83+
])
84+
z = H @ x
85+
86+
return z
87+
88+
89+
def jacob_f(x, u):
90+
"""
91+
Jacobian of Motion Model
92+
93+
motion model
94+
x_{t+1} = x_t+v*s*dt*cos(yaw)
95+
y_{t+1} = y_t+v*s*dt*sin(yaw)
96+
yaw_{t+1} = yaw_t+omega*dt
97+
v_{t+1} = v{t}
98+
s_{t+1} = s{t}
99+
so
100+
dx/dyaw = -v*s*dt*sin(yaw)
101+
dx/dv = dt*s*cos(yaw)
102+
dx/ds = dt*v*cos(yaw)
103+
dy/dyaw = v*s*dt*cos(yaw)
104+
dy/dv = dt*s*sin(yaw)
105+
dy/ds = dt*v*sin(yaw)
106+
"""
107+
yaw = x[2, 0]
108+
v = u[0, 0]
109+
s = x[4, 0]
110+
jF = np.array([
111+
[1.0, 0.0, -DT * v * s * math.sin(yaw), DT * s * math.cos(yaw), DT * v * math.cos(yaw)],
112+
[0.0, 1.0, DT * v * s * math.cos(yaw), DT * s * math.sin(yaw), DT * v * math.sin(yaw)],
113+
[0.0, 0.0, 1.0, 0.0, 0.0],
114+
[0.0, 0.0, 0.0, 1.0, 0.0],
115+
[0.0, 0.0, 0.0, 0.0, 1.0]])
116+
return jF
117+
118+
119+
def jacob_h():
120+
jH = np.array([[1, 0, 0, 0, 0],
121+
[0, 1, 0, 0, 0]])
122+
return jH
123+
124+
125+
def ekf_estimation(xEst, PEst, z, u):
126+
# Predict
127+
xPred = motion_model(xEst, u)
128+
jF = jacob_f(xEst, u)
129+
PPred = jF @ PEst @ jF.T + Q
130+
131+
# Update
132+
jH = jacob_h()
133+
zPred = observation_model(xPred)
134+
y = z - zPred
135+
S = jH @ PPred @ jH.T + R
136+
K = PPred @ jH.T @ np.linalg.inv(S)
137+
xEst = xPred + K @ y
138+
PEst = (np.eye(len(xEst)) - K @ jH) @ PPred
139+
return xEst, PEst
140+
141+
142+
def main():
143+
print(__file__ + " start!!")
144+
145+
time = 0.0
146+
147+
# State Vector [x y yaw v s]'
148+
xEst = np.zeros((5, 1))
149+
xEst[4, 0] = 1.0 # Initial scale factor
150+
xTrue = np.zeros((5, 1))
151+
true_scale_factor = 0.9 # True scale factor
152+
xTrue[4, 0] = true_scale_factor
153+
PEst = np.eye(5)
154+
155+
xDR = np.zeros((5, 1)) # Dead reckoning
156+
157+
# history
158+
hxEst = xEst
159+
hxTrue = xTrue
160+
hxDR = xTrue
161+
hz = np.zeros((2, 1))
162+
163+
while SIM_TIME >= time:
164+
time += DT
165+
u = calc_input()
166+
167+
xTrue, z, xDR, ud = observation(xTrue, xDR, u)
168+
169+
xEst, PEst = ekf_estimation(xEst, PEst, z, ud)
170+
171+
# store data history
172+
hxEst = np.hstack((hxEst, xEst))
173+
hxDR = np.hstack((hxDR, xDR))
174+
hxTrue = np.hstack((hxTrue, xTrue))
175+
hz = np.hstack((hz, z))
176+
estimated_scale_factor = hxEst[4, -1]
177+
if show_animation:
178+
plt.cla()
179+
# for stopping simulation with the esc key.
180+
plt.gcf().canvas.mpl_connect('key_release_event',
181+
lambda event: [exit(0) if event.key == 'escape' else None])
182+
plt.plot(hz[0, :], hz[1, :], ".g")
183+
plt.plot(hxTrue[0, :].flatten(),
184+
hxTrue[1, :].flatten(), "-b")
185+
plt.plot(hxDR[0, :].flatten(),
186+
hxDR[1, :].flatten(), "-k")
187+
plt.plot(hxEst[0, :].flatten(),
188+
hxEst[1, :].flatten(), "-r")
189+
plt.text(0.45, 0.85, f"True Velocity Scale Factor: {true_scale_factor:.2f}", ha='left', va='top', transform=plt.gca().transAxes)
190+
plt.text(0.45, 0.95, f"Estimated Velocity Scale Factor: {estimated_scale_factor:.2f}", ha='left', va='top', transform=plt.gca().transAxes)
191+
plot_covariance_ellipse(xEst[0, 0], xEst[1, 0], PEst)
192+
plt.axis("equal")
193+
plt.grid(True)
194+
plt.pause(0.001)
195+
196+
197+
if __name__ == '__main__':
198+
main()

docs/modules/localization/extended_kalman_filter_localization_files/extended_kalman_filter_localization_main.rst

+105
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
Extended Kalman Filter Localization
33
-----------------------------------
44

5+
Position Estimation Kalman Filter
6+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7+
58
.. image:: extended_kalman_filter_localization_1_0.png
69
:width: 600px
710

811

912

1013
.. figure:: https://github.com/AtsushiSakai/PythonRoboticsGifs/raw/master/Localization/extended_kalman_filter/animation.gif
1114

15+
1216
This is a sensor fusion localization with Extended Kalman Filter(EKF).
1317

1418
The blue line is true trajectory, the black line is dead reckoning
@@ -22,9 +26,26 @@ The red ellipse is estimated covariance ellipse with EKF.
2226
Code: `PythonRobotics/extended_kalman_filter.py at master ·
2327
AtsushiSakai/PythonRobotics <https://github.com/AtsushiSakai/PythonRobotics/blob/master/Localization/extended_kalman_filter/extended_kalman_filter.py>`__
2428

29+
Kalman Filter with Speed Scale Factor Correction
30+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
32+
33+
.. image:: ekf_with_velocity_correction_1_0.png
34+
:width: 600px
35+
36+
This is a Extended kalman filter (EKF) localization with velocity correction.
37+
38+
This is for correcting the vehicle speed measured with scale factor errors due to factors such as wheel wear.
39+
40+
Code: `PythonRobotics/extended_kalman_ekf_with_velocity_correctionfilter.py
41+
AtsushiSakai/PythonRobotics <https://github.com/AtsushiSakai/PythonRobotics/blob/master/Localization/extended_kalman_filter/extended_kalman_ekf_with_velocity_correctionfilter.py>`__
42+
2543
Filter design
2644
~~~~~~~~~~~~~
2745

46+
Position Estimation Kalman Filter
47+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
2849
In this simulation, the robot has a state vector includes 4 states at
2950
time :math:`t`.
3051

@@ -61,9 +82,28 @@ In the code, “observation” function generates the input and observation
6182
vector with noise
6283
`code <https://github.com/AtsushiSakai/PythonRobotics/blob/916b4382de090de29f54538b356cef1c811aacce/Localization/extended_kalman_filter/extended_kalman_filter.py#L34-L50>`__
6384

85+
Kalman Filter with Speed Scale Factor Correction
86+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
87+
88+
In this simulation, the robot has a state vector includes 5 states at
89+
time :math:`t`.
90+
91+
.. math:: \textbf{x}_t=[x_t, y_t, \phi_t, v_t, s_t]
92+
93+
x, y are a 2D x-y position, :math:`\phi` is orientation, v is
94+
velocity, and s is a scale factor of velocity.
95+
96+
In the code, “xEst” means the state vector.
97+
`code <https://github.com/AtsushiSakai/PythonRobotics/blob/916b4382de090de29f54538b356cef1c811aacce/Localization/extended_kalman_filter/extended_kalman_ekf_with_velocity_correctionfilter.py#L163>`__
98+
99+
The rest is the same as the Position Estimation Kalman Filter.
100+
64101
Motion Model
65102
~~~~~~~~~~~~
66103

104+
Position Estimation Kalman Filter
105+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
106+
67107
The robot model is
68108

69109
.. math:: \dot{x} = v \cos(\phi)
@@ -97,9 +137,50 @@ Its Jacobian matrix is
97137

98138
:math:`\begin{equation*}  = \begin{bmatrix} 1& 0 & -v \sin(\phi) \Delta t & \cos(\phi) \Delta t\\ 0 & 1 & v \cos(\phi) \Delta t & \sin(\phi) \Delta t\\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \end{equation*}`
99139

140+
141+
Kalman Filter with Speed Scale Factor Correction
142+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
143+
144+
The robot model is
145+
146+
.. math:: \dot{x} = v s \cos(\phi)
147+
148+
.. math:: \dot{y} = v s \sin(\phi)
149+
150+
.. math:: \dot{\phi} = \omega
151+
152+
So, the motion model is
153+
154+
.. math:: \textbf{x}_{t+1} = f(\textbf{x}_t, \textbf{u}_t) = F\textbf{x}_t+B\textbf{u}_t
155+
156+
where
157+
158+
:math:`\begin{equation*} F= \begin{bmatrix} 1 & 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 1\\ \end{bmatrix} \end{equation*}`
159+
160+
:math:`\begin{equation*} B= \begin{bmatrix} cos(\phi) \Delta t s & 0\\ sin(\phi) \Delta t s & 0\\ 0 & \Delta t\\ 1 & 0\\ 0 & 0\\ \end{bmatrix} \end{equation*}`
161+
162+
:math:`\Delta t` is a time interval.
163+
164+
This is implemented at
165+
`code <https://github.com/AtsushiSakai/PythonRobotics/blob/916b4382de090de29f54538b356cef1c811aacce/Localization/extended_kalman_filter/extended_kalman_filter.py#L61-L76>`__
166+
167+
The motion function is that
168+
169+
:math:`\begin{equation*} \begin{bmatrix} x' \\ y' \\ w' \\ v' \end{bmatrix} = f(\textbf{x}, \textbf{u}) = \begin{bmatrix} x + v s \cos(\phi)\Delta t \\ y + v s \sin(\phi)\Delta t \\ \phi + \omega \Delta t \\ v \end{bmatrix} \end{equation*}`
170+
171+
Its Jacobian matrix is
172+
173+
:math:`\begin{equation*} J_f = \begin{bmatrix} \frac{\partial x'}{\partial x}& \frac{\partial x'}{\partial y} & \frac{\partial x'}{\partial \phi} & \frac{\partial x'}{\partial v} & \frac{\partial x'}{\partial s}\\ \frac{\partial y'}{\partial x}& \frac{\partial y'}{\partial y} & \frac{\partial y'}{\partial \phi} & \frac{\partial y'}{\partial v} & \frac{\partial y'}{\partial s}\\ \frac{\partial \phi'}{\partial x}& \frac{\partial \phi'}{\partial y} & \frac{\partial \phi'}{\partial \phi} & \frac{\partial \phi'}{\partial v} & \frac{\partial \phi'}{\partial s}\\ \frac{\partial v'}{\partial x}& \frac{\partial v'}{\partial y} & \frac{\partial v'}{\partial \phi} & \frac{\partial v'}{\partial v} & \frac{\partial v'}{\partial s} \\ \frac{\partial s'}{\partial x}& \frac{\partial s'}{\partial y} & \frac{\partial s'}{\partial \phi} & \frac{\partial s'}{\partial v} & \frac{\partial s'}{\partial s} \end{bmatrix} \end{equation*}`
174+
175+
:math:`\begin{equation*}  = \begin{bmatrix} 1& 0 & -v s \sin(\phi) \Delta t & s \cos(\phi) \Delta t & \cos(\phi) v \Delta t\\ 0 & 1 & v s \cos(\phi) \Delta t & s \sin(\phi) \Delta t & v \sin(\phi) \Delta t\\ 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \end{bmatrix} \end{equation*}`
176+
177+
100178
Observation Model
101179
~~~~~~~~~~~~~~~~~
102180

181+
Position Estimation Kalman Filter
182+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
183+
103184
The robot can get x-y position infomation from GPS.
104185

105186
So GPS Observation model is
@@ -120,6 +201,30 @@ Its Jacobian matrix is
120201

121202
:math:`\begin{equation*}  = \begin{bmatrix} 1& 0 & 0 & 0\\ 0 & 1 & 0 & 0\\ \end{bmatrix} \end{equation*}`
122203

204+
Kalman Filter with Speed Scale Factor Correction
205+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
206+
207+
The robot can get x-y position infomation from GPS.
208+
209+
So GPS Observation model is
210+
211+
.. math:: \textbf{z}_{t} = g(\textbf{x}_t) = H \textbf{x}_t
212+
213+
where
214+
215+
:math:`\begin{equation*} H = \begin{bmatrix} 1 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 \\ \end{bmatrix} \end{equation*}`
216+
217+
The observation function states that
218+
219+
:math:`\begin{equation*} \begin{bmatrix} x' \\ y' \end{bmatrix} = g(\textbf{x}) = \begin{bmatrix} x \\ y \end{bmatrix} \end{equation*}`
220+
221+
Its Jacobian matrix is
222+
223+
:math:`\begin{equation*} J_g = \begin{bmatrix} \frac{\partial x'}{\partial x} & \frac{\partial x'}{\partial y} & \frac{\partial x'}{\partial \phi} & \frac{\partial x'}{\partial v} & \frac{\partial x'}{\partial s}\\ \frac{\partial y'}{\partial x}& \frac{\partial y'}{\partial y} & \frac{\partial y'}{\partial \phi} & \frac{\partial y'}{ \partial v} & \frac{\partial y'}{ \partial s}\\ \end{bmatrix} \end{equation*}`
224+
225+
:math:`\begin{equation*}  = \begin{bmatrix} 1& 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0\\ \end{bmatrix} \end{equation*}`
226+
227+
123228
Extended Kalman Filter
124229
~~~~~~~~~~~~~~~~~~~~~~
125230

0 commit comments

Comments
 (0)