Skip to content

Commit 7560cbd

Browse files
committed
add @ operator instead of dot function
1 parent 1ad025a commit 7560cbd

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

Localization/extended_kalman_filter/extended_kalman_filter.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def motion_model(x, u):
6262
[0.0, DT],
6363
[1.0, 0.0]])
6464

65-
x = F.dot(x) + B.dot(u)
65+
x = F@x + B@u
6666

6767
return x
6868

@@ -74,7 +74,7 @@ def observation_model(x):
7474
[0, 1, 0, 0]
7575
])
7676

77-
z = H.dot(x)
77+
z = H@x
7878

7979
return z
8080

@@ -120,16 +120,16 @@ def ekf_estimation(xEst, PEst, z, u):
120120
# Predict
121121
xPred = motion_model(xEst, u)
122122
jF = jacobF(xPred, u)
123-
PPred = jF.dot(PEst).dot(jF.T) + R
123+
PPred = jF@PEst@jF.T + R
124124

125125
# Update
126126
jH = jacobH(xPred)
127127
zPred = observation_model(xPred)
128128
y = z.T - zPred
129-
S = jH.dot(PPred).dot(jH.T) + Q
130-
K = PPred.dot(jH.T).dot(np.linalg.inv(S))
131-
xEst = xPred + K.dot(y)
132-
PEst = (np.eye(len(xEst)) - K.dot(jH)).dot(PPred)
129+
S = jH@PPred@jH.T + Q
130+
K = PPred@jH.T@np.linalg.inv(S)
131+
xEst = xPred + K@y
132+
PEst = (np.eye(len(xEst)) - K@jH)@PPred
133133

134134
return xEst, PEst
135135

@@ -153,7 +153,7 @@ def plot_covariance_ellipse(xEst, PEst):
153153
angle = math.atan2(eigvec[bigind, 1], eigvec[bigind, 0])
154154
R = np.array([[math.cos(angle), math.sin(angle)],
155155
[-math.sin(angle), math.cos(angle)]])
156-
fx = R.dot(np.array([[x, y]]))
156+
fx = R@(np.array([x, y]))
157157
px = np.array(fx[0, :] + xEst[0, 0]).flatten()
158158
py = np.array(fx[1, :] + xEst[1, 0]).flatten()
159159
plt.plot(px, py, "--r")

Localization/extended_kalman_filter/extended_kalman_filter_localization.ipynb

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
"cell_type": "markdown",
2626
"metadata": {},
2727
"source": [
28-
"### Filter Design\n",
28+
"### Filter design\n",
2929
"\n",
3030
"In this simulation, the robot has a state vector includes 4 states at time $t$.\n",
3131
"\n",
3232
"$$\\textbf{x}_t=[x_t, y_t, \\theta_t, v_t]$$\n",
3333
"\n",
3434
"x, y are a 2D x-y position, $\\theta$ is orientation, and v is velocity.\n",
3535
"\n",
36-
"In the code, xEst means the state vector. [code](https://github.com/AtsushiSakai/PythonRobotics/blob/916b4382de090de29f54538b356cef1c811aacce/Localization/extended_kalman_filter/extended_kalman_filter.py#L168)\n",
36+
"In the code, \"xEst\" means the state vector. [code](https://github.com/AtsushiSakai/PythonRobotics/blob/916b4382de090de29f54538b356cef1c811aacce/Localization/extended_kalman_filter/extended_kalman_filter.py#L168)\n",
3737
"\n",
3838
" \n",
3939
"\n",
@@ -49,7 +49,7 @@
4949
"\n",
5050
"The input and observation vector includes sensor noise.\n",
5151
"\n",
52-
"In the code, observation function generates the input and observation vector [code](https://github.com/AtsushiSakai/PythonRobotics/blob/916b4382de090de29f54538b356cef1c811aacce/Localization/extended_kalman_filter/extended_kalman_filter.py#L34-L50)\n",
52+
"In the code, \"observation\" function generates the input and observation vector with noise [code](https://github.com/AtsushiSakai/PythonRobotics/blob/916b4382de090de29f54538b356cef1c811aacce/Localization/extended_kalman_filter/extended_kalman_filter.py#L34-L50)\n",
5353
"\n",
5454
"\n",
5555
"\n",
@@ -58,6 +58,54 @@
5858
"\n"
5959
]
6060
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"### Motion Model\n",
66+
"\n",
67+
"The robot model is \n",
68+
"\n",
69+
"$$ \\dot{x} = vcos(\\phi)$$\n",
70+
"\n",
71+
"$$ \\dot{y} = vsin((\\phi)$$\n",
72+
"\n",
73+
"$$ \\dot{\\phi} = \\omega$$\n",
74+
"\n",
75+
"\n",
76+
"So, the motion model is\n",
77+
"\n",
78+
"$$\\textbf{x}_{t+1} = F\\textbf{x}+B\\textbf{u}$$\n",
79+
"\n",
80+
"where\n",
81+
"\n",
82+
"$\\begin{equation*}\n",
83+
"F=\n",
84+
"\\begin{bmatrix}\n",
85+
"1 & 0 & 0 & 0\\\\\n",
86+
"0 & 1 & 0 & 0\\\\\n",
87+
"0 & 0 & 1 & 0 \\\\\n",
88+
"0 & 0 & 0 & 0 \\\\\n",
89+
"\\end{bmatrix}\n",
90+
"\\end{equation*}$\n",
91+
"\n",
92+
"$\\begin{equation*}\n",
93+
"B=\n",
94+
"\\begin{bmatrix}\n",
95+
"sin(\\phi)dt & 0\\\\\n",
96+
"cos(\\phi)dt & 0\\\\\n",
97+
"0 & dt\\\\\n",
98+
"1 & 0\\\\\n",
99+
"\\end{bmatrix}\n",
100+
"\\end{equation*}$\n",
101+
"\n",
102+
"$dt$ is a time interval.\n",
103+
"\n",
104+
"This is implemented at [code](https://github.com/AtsushiSakai/PythonRobotics/blob/916b4382de090de29f54538b356cef1c811aacce/Localization/extended_kalman_filter/extended_kalman_filter.py#L53-L67)\n",
105+
"\n",
106+
"\n"
107+
]
108+
},
61109
{
62110
"cell_type": "markdown",
63111
"metadata": {},

0 commit comments

Comments
 (0)