-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Cython KF1D to Python * cleanup * set x * less nesting * fix release * Revert "fix release" This reverts commit 97e5d0f.
- Loading branch information
1 parent
7aeefaa
commit 1421551
Showing
15 changed files
with
95 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import numpy as np | ||
|
||
|
||
def get_kalman_gain(dt, A, C, Q, R, iterations=100): | ||
P = np.zeros_like(Q) | ||
for _ in range(iterations): | ||
P = A.dot(P).dot(A.T) + dt * Q | ||
S = C.dot(P).dot(C.T) + R | ||
K = P.dot(C.T).dot(np.linalg.inv(S)) | ||
P = (np.eye(len(P)) - K.dot(C)).dot(P) | ||
return K | ||
|
||
|
||
class KF1D: | ||
# this EKF assumes constant covariance matrix, so calculations are much simpler | ||
# the Kalman gain also needs to be precomputed using the control module | ||
|
||
def __init__(self, x0, A, C, K): | ||
self.x0_0 = x0[0][0] | ||
self.x1_0 = x0[1][0] | ||
self.A0_0 = A[0][0] | ||
self.A0_1 = A[0][1] | ||
self.A1_0 = A[1][0] | ||
self.A1_1 = A[1][1] | ||
self.C0_0 = C[0] | ||
self.C0_1 = C[1] | ||
self.K0_0 = K[0][0] | ||
self.K1_0 = K[1][0] | ||
|
||
self.A_K_0 = self.A0_0 - self.K0_0 * self.C0_0 | ||
self.A_K_1 = self.A0_1 - self.K0_0 * self.C0_1 | ||
self.A_K_2 = self.A1_0 - self.K1_0 * self.C0_0 | ||
self.A_K_3 = self.A1_1 - self.K1_0 * self.C0_1 | ||
|
||
# K matrix needs to be pre-computed as follow: | ||
# import control | ||
# (x, l, K) = control.dare(np.transpose(self.A), np.transpose(self.C), Q, R) | ||
# self.K = np.transpose(K) | ||
|
||
def update(self, meas): | ||
#self.x = np.dot(self.A_K, self.x) + np.dot(self.K, meas) | ||
x0_0 = self.A_K_0 * self.x0_0 + self.A_K_1 * self.x1_0 + self.K0_0 * meas | ||
x1_0 = self.A_K_2 * self.x0_0 + self.A_K_3 * self.x1_0 + self.K1_0 * meas | ||
self.x0_0 = x0_0 | ||
self.x1_0 = x1_0 | ||
return [self.x0_0, self.x1_0] | ||
|
||
@property | ||
def x(self): | ||
return [[self.x0_0], [self.x1_0]] | ||
|
||
def set_x(self, x): | ||
self.x0_0 = x[0][0] | ||
self.x1_0 = x[1][0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import unittest | ||
|
||
from openpilot.common.simple_kalman import KF1D | ||
|
||
|
||
class TestSimpleKalman(unittest.TestCase): | ||
def setUp(self): | ||
dt = 0.01 | ||
x0_0 = 0.0 | ||
x1_0 = 0.0 | ||
A0_0 = 1.0 | ||
A0_1 = dt | ||
A1_0 = 0.0 | ||
A1_1 = 1.0 | ||
C0_0 = 1.0 | ||
C0_1 = 0.0 | ||
K0_0 = 0.12287673 | ||
K1_0 = 0.29666309 | ||
|
||
self.kf = KF1D(x0=[[x0_0], [x1_0]], | ||
A=[[A0_0, A0_1], [A1_0, A1_1]], | ||
C=[C0_0, C0_1], | ||
K=[[K0_0], [K1_0]]) | ||
|
||
def test_getter_setter(self): | ||
self.kf.set_x([[1.0], [1.0]]) | ||
self.assertEqual(self.kf.x, [[1.0], [1.0]]) | ||
|
||
def update_returns_state(self): | ||
x = self.kf.update(100) | ||
self.assertEqual(x, self.kf.x) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters