77"""
88
99import math
10- import numpy as np
10+
1111import matplotlib .pyplot as plt
12+ import numpy as np
1213
1314# Covariance for EKF simulation
1415Q = np .diag ([
15- 0.1 , # variance of location on x-axis
16- 0.1 , # variance of location on y-axis
17- np .deg2rad (1.0 ), # variance of yaw angle
18- 1.0 # variance of velocity
19- ]) ** 2 # predict state covariance
20- R = np .diag ([1.0 , 1.0 ])** 2 # Observation x,y position covariance
16+ 0.1 , # variance of location on x-axis
17+ 0.1 , # variance of location on y-axis
18+ np .deg2rad (1.0 ), # variance of yaw angle
19+ 1.0 # variance of velocity
20+ ]) ** 2 # predict state covariance
21+ R = np .diag ([1.0 , 1.0 ]) ** 2 # Observation x,y position covariance
2122
2223# Simulation parameter
23- INPUT_NOISE = np .diag ([1.0 , np .deg2rad (30.0 )])** 2
24- GPS_NOISE = np .diag ([0.5 , 0.5 ])** 2
24+ INPUT_NOISE = np .diag ([1.0 , np .deg2rad (30.0 )]) ** 2
25+ GPS_NOISE = np .diag ([0.5 , 0.5 ]) ** 2
2526
2627DT = 0.1 # time tick [s]
2728SIM_TIME = 50.0 # simulation time [s]
@@ -37,7 +38,6 @@ def calc_input():
3738
3839
3940def observation (xTrue , xd , u ):
40-
4141 xTrue = motion_model (xTrue , u )
4242
4343 # add noise to gps x-y
@@ -52,7 +52,6 @@ def observation(xTrue, xd, u):
5252
5353
5454def motion_model (x , u ):
55-
5655 F = np .array ([[1.0 , 0 , 0 , 0 ],
5756 [0 , 1.0 , 0 , 0 ],
5857 [0 , 0 , 1.0 , 0 ],
@@ -116,20 +115,19 @@ def jacobH(x):
116115
117116
118117def ekf_estimation (xEst , PEst , z , u ):
119-
120118 # Predict
121119 xPred = motion_model (xEst , u )
122120 jF = jacobF (xPred , u )
123- PPred = jF @ PEst @ jF .T + Q
121+ PPred = jF @ PEst @ jF .T + Q
124122
125123 # Update
126124 jH = jacobH (xPred )
127125 zPred = observation_model (xPred )
128126 y = z - zPred
129- S = jH @ PPred @ jH .T + R
130- K = PPred @ jH .T @ np .linalg .inv (S )
131- xEst = xPred + K @ y
132- PEst = (np .eye (len (xEst )) - K @ jH )@ PPred
127+ S = jH @ PPred @ jH .T + R
128+ K = PPred @ jH .T @ np .linalg .inv (S )
129+ xEst = xPred + K @ y
130+ PEst = (np .eye (len (xEst )) - K @ jH ) @ PPred
133131
134132 return xEst , PEst
135133
@@ -153,7 +151,7 @@ def plot_covariance_ellipse(xEst, PEst): # pragma: no cover
153151 angle = math .atan2 (eigvec [bigind , 1 ], eigvec [bigind , 0 ])
154152 R = np .array ([[math .cos (angle ), math .sin (angle )],
155153 [- math .sin (angle ), math .cos (angle )]])
156- fx = R @ (np .array ([x , y ]))
154+ fx = R @ (np .array ([x , y ]))
157155 px = np .array (fx [0 , :] + xEst [0 , 0 ]).flatten ()
158156 py = np .array (fx [1 , :] + xEst [1 , 0 ]).flatten ()
159157 plt .plot (px , py , "--r" )
0 commit comments