Skip to content

Commit

Permalink
adding base KF
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveMacenski committed Apr 29, 2019
1 parent 95cdaa4 commit 5105164
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/kalmon_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import numpy as np

# TODO tune values
# TODO add KF to project
# TODO add constant rate (10hz)

class ConstantVelocityMotionModel():
def __init__(self, dt):
self.dt = dt
self.A = np.array((1, 0, self.dt, 0), (0, 1, 0, self.dt), (0, 0, 1, 0), (0, 0, 0, 1))
self.H = np.array((1, 0, 0, 0), (0, 1, 0, 0))
self.I = np.array((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1))
return


class KalmonFilter(ConstantVelocityMotionModel):
def __init__(self, fps):
dt = 1.0/fps
super(KalmonFilter, self).__init__(dt)
self.P = np.array((100, 0, 0, 0), (0, 100, 0, 0), (0, 0, 100, 0), (0, 0, 0, 100))
self.Q = np.array((100, 0, 0, 0), (0, 100, 0, 0), (0, 0, 100, 0), (0, 0, 0, 100))
self.R = np.array((100, 0), (0, 100))
return

def initializeFilter(self, detection, missedUpdatesThresh = 5):
self.state = detection
self.missedUpdatesThreshold = missedUpdatesThresh
self.missedUpdates = 0
self.dirtyState = False
return

def predict(self):
self.dirtyState = True
self.missedUpdates = self.missedUpdates + 1

self.state = self.A * self.state
self.P = self.A * self.P * self.A.T() + self.Q
return

def correct(self, measurement):
self.dirtyState = False
self.missedUpdates = 0

K = self.P * self.H.T() * (self.H * self.P * self.H.T() + self.R).T()
self.state = self.state + K * (measurement - self.H * self.state)
self.P = (I - K * self.H) * self.P
return

def getState(self):
return self.state

def isValid(self):
return self.missedUpdates < self.missedUpdatesThreshold

0 comments on commit 5105164

Please sign in to comment.