2D example now working in macos and new version of matplotlib animation
commit
c9630679d9
@ -0,0 +1,22 @@
|
||||
import numpy as np
|
||||
from kfsims.tracker2d import run_sim
|
||||
from kfsims.kftracker2d import KalmanFilterModel
|
||||
|
||||
# Simulation Options
|
||||
sim_options = {'time_step': 0.01,
|
||||
'end_time': 120,
|
||||
'measurement_rate': 1,
|
||||
'measurement_noise_std': 10,
|
||||
'motion_type': 'random',
|
||||
'start_at_origin': False,
|
||||
'start_at_random_speed': True,
|
||||
'start_at_random_heading': True,
|
||||
'draw_plots': True,
|
||||
'draw_animation': True}
|
||||
|
||||
kf_options = {'accel_std':0.5, # Q Matrix Param
|
||||
'meas_std':10, # R Matrix
|
||||
'init_on_measurement':True}
|
||||
|
||||
# Run the Simulation
|
||||
run_sim(KalmanFilterModel, sim_options, kf_options)
|
@ -0,0 +1,103 @@
|
||||
import numpy as np
|
||||
from kfsims.tracker2d import run_sim
|
||||
from kfsims.kfmodels import KalmanFilterBase
|
||||
|
||||
# Simulation Options
|
||||
sim_options = {'time_step': 0.01,
|
||||
'end_time': 120,
|
||||
'measurement_rate': 1,
|
||||
'measurement_noise_std': 10,
|
||||
'motion_type': 'straight',
|
||||
'start_at_origin': False,
|
||||
'start_at_random_speed': True,
|
||||
'start_at_random_heading': True,
|
||||
'draw_plots': True,
|
||||
'draw_animation': True}
|
||||
|
||||
# Kalman Filter Model
|
||||
class KalmanFilterModel(KalmanFilterBase):
|
||||
|
||||
def initialise(self, time_step):
|
||||
|
||||
# Set Initial State and Covariance (COMMENT OUT FOR DELAYED)
|
||||
init_pos_std = 0
|
||||
init_vel_std = 10
|
||||
self.state = np.array([0,0,0,0])
|
||||
self.covariance = np.diag(np.array([init_pos_std*init_pos_std,
|
||||
init_pos_std*init_pos_std,
|
||||
init_vel_std*init_vel_std,
|
||||
init_vel_std*init_vel_std]))
|
||||
|
||||
# Setup the Model F Matrix
|
||||
dt = time_step
|
||||
self.F = np.array([[1,0,dt,0],
|
||||
[0,1,0,dt],
|
||||
[0,0,1,0],
|
||||
[0,0,0,1]])
|
||||
|
||||
# Set the Q Matrix
|
||||
accel_std = 0.0
|
||||
self.Q = np.diag(np.array([(0.5*dt*dt),(0.5*dt*dt),dt,dt]) * (accel_std*accel_std))
|
||||
|
||||
# Setup the Model H Matrix
|
||||
self.H = np.array([[1,0,0,0],[0,1,0,0]])
|
||||
|
||||
# Set the R Matrix
|
||||
meas_std = 10.0
|
||||
self.R = np.diag([meas_std*meas_std, meas_std*meas_std])
|
||||
|
||||
return
|
||||
|
||||
def prediction_step(self):
|
||||
# Make Sure Filter is Initialised
|
||||
if self.state is not None:
|
||||
x = self.state
|
||||
P = self.covariance
|
||||
|
||||
# Calculate Kalman Filter Prediction
|
||||
x_predict = np.matmul(self.F, x)
|
||||
P_predict = np.matmul(self.F, np.matmul(P, np.transpose(self.F))) + self.Q
|
||||
|
||||
# Save Predicted State
|
||||
self.state = x_predict
|
||||
self.covariance = P_predict
|
||||
|
||||
return
|
||||
|
||||
def update_step(self, measurement):
|
||||
|
||||
# Make Sure Filter is Initialised
|
||||
if self.state is not None and self.covariance is not None:
|
||||
x = self.state
|
||||
P = self.covariance
|
||||
H = self.H
|
||||
R = self.R
|
||||
|
||||
# Calculate Kalman Filter Update
|
||||
z = np.array([measurement[0],measurement[1]])
|
||||
z_hat = np.matmul(H, x)
|
||||
|
||||
y = z - z_hat
|
||||
S = np.matmul(H,np.matmul(P,np.transpose(H))) + R
|
||||
K = np.matmul(P,np.matmul(np.transpose(H),np.linalg.inv(S)))
|
||||
|
||||
x_update = x + np.matmul(K, y)
|
||||
P_update = np.matmul( (np.eye(4) - np.matmul(K,H)), P)
|
||||
|
||||
# Save Updated State
|
||||
self.innovation = y
|
||||
self.innovation_covariance = S
|
||||
self.state = x_update
|
||||
self.covariance = P_update
|
||||
|
||||
else:
|
||||
|
||||
# Set Initial State and Covariance
|
||||
return
|
||||
|
||||
|
||||
return
|
||||
|
||||
|
||||
# Run the Simulation
|
||||
run_sim(KalmanFilterModel, sim_options, {})
|
@ -0,0 +1,105 @@
|
||||
import numpy as np
|
||||
from kfsims.tracker2d import run_sim
|
||||
from kfsims.kfmodels import KalmanFilterBase
|
||||
|
||||
# Simulation Options
|
||||
sim_options = {'time_step': 0.01,
|
||||
'end_time': 120,
|
||||
'measurement_rate': 1,
|
||||
'measurement_noise_std': 10,
|
||||
'motion_type': 'straight',
|
||||
'start_at_origin': False,
|
||||
'start_at_random_speed': True,
|
||||
'start_at_random_heading': True,
|
||||
'draw_plots': True,
|
||||
'draw_animation': True}
|
||||
|
||||
# Kalman Filter Model
|
||||
class KalmanFilterModel(KalmanFilterBase):
|
||||
|
||||
def initialise(self, time_step):
|
||||
|
||||
# Set Initial State and Covariance (COMMENT OUT FOR DELAYED INIT)
|
||||
init_pos_std = 100
|
||||
init_vel_std = 10
|
||||
#self.state = np.array([0,0,0,0])
|
||||
#self.covariance = np.diag(np.array([init_pos_std*init_pos_std,
|
||||
# init_pos_std*init_pos_std,
|
||||
# init_vel_std*init_vel_std,
|
||||
# init_vel_std*init_vel_std]))
|
||||
|
||||
# Setup the Model F Matrix
|
||||
dt = time_step
|
||||
self.F = np.array([[1,0,dt,0],
|
||||
[0,1,0,dt],
|
||||
[0,0,1,0],
|
||||
[0,0,0,1]])
|
||||
|
||||
# Set the Q Matrix
|
||||
accel_std = 0.0
|
||||
self.Q = np.diag(np.array([(0.5*dt*dt),(0.5*dt*dt),dt,dt]) * (accel_std*accel_std))
|
||||
|
||||
# Setup the Model H Matrix
|
||||
self.H = np.array([[1,0,0,0],[0,1,0,0]])
|
||||
|
||||
# Set the R Matrix
|
||||
meas_std = 10.0
|
||||
self.R = np.diag([meas_std*meas_std, meas_std*meas_std])
|
||||
|
||||
return
|
||||
|
||||
def prediction_step(self):
|
||||
# Make Sure Filter is Initialised
|
||||
if self.state is not None:
|
||||
x = self.state
|
||||
P = self.covariance
|
||||
|
||||
# Calculate Kalman Filter Prediction
|
||||
x_predict = np.matmul(self.F, x)
|
||||
P_predict = np.matmul(self.F, np.matmul(P, np.transpose(self.F))) + self.Q
|
||||
|
||||
# Save Predicted State
|
||||
self.state = x_predict
|
||||
self.covariance = P_predict
|
||||
|
||||
return
|
||||
|
||||
def update_step(self, measurement):
|
||||
|
||||
# Make Sure Filter is Initialised
|
||||
if self.state is not None and self.covariance is not None:
|
||||
x = self.state
|
||||
P = self.covariance
|
||||
H = self.H
|
||||
R = self.R
|
||||
|
||||
# Calculate Kalman Filter Update
|
||||
z = np.array([measurement[0],measurement[1]])
|
||||
z_hat = np.matmul(H, x)
|
||||
|
||||
y = z - z_hat
|
||||
S = np.matmul(H,np.matmul(P,np.transpose(H))) + R
|
||||
K = np.matmul(P,np.matmul(np.transpose(H),np.linalg.inv(S)))
|
||||
|
||||
x_update = x + np.matmul(K, y)
|
||||
P_update = np.matmul( (np.eye(4) - np.matmul(K,H)), P)
|
||||
|
||||
# Save Updated State
|
||||
self.innovation = y
|
||||
self.innovation_covariance = S
|
||||
self.state = x_update
|
||||
self.covariance = P_update
|
||||
|
||||
else:
|
||||
|
||||
# Set Initial State and Covariance
|
||||
init_vel_std = 10
|
||||
|
||||
self.state = np.array([measurement[0],measurement[1],0,0])
|
||||
self.covariance = np.diag(np.array([self.R[0,0],self.R[1,1],init_vel_std*init_vel_std,init_vel_std*init_vel_std]))
|
||||
|
||||
return
|
||||
|
||||
|
||||
# Run the Simulation
|
||||
run_sim(KalmanFilterModel, sim_options, {})
|
@ -0,0 +1,62 @@
|
||||
import numpy as np
|
||||
from kfsims.tracker2d import run_sim
|
||||
from kfsims.kfmodels import KalmanFilterBase
|
||||
|
||||
# Simulation Options
|
||||
sim_options = {'time_step': 0.01,
|
||||
'end_time': 120,
|
||||
'measurement_rate': 1,
|
||||
'measurement_noise_std': 10,
|
||||
'motion_type': 'straight',
|
||||
'start_at_origin': True,
|
||||
'start_at_random_speed': False,
|
||||
'start_at_random_heading': False,
|
||||
'draw_plots': True,
|
||||
'draw_animation': True}
|
||||
|
||||
# Kalman Filter Model
|
||||
class KalmanFilterModel(KalmanFilterBase):
|
||||
|
||||
def initialise(self, time_step):
|
||||
|
||||
# Define a np.array 4x1 with the initial state (px,py,vx,vy)
|
||||
#self.state =
|
||||
|
||||
# Define a np.array 4x4 for the initial covariance
|
||||
#self.covariance =
|
||||
|
||||
# Setup the Model F Matrix
|
||||
#self.F =
|
||||
|
||||
# Set the Q Matrix
|
||||
#self.Q =
|
||||
|
||||
return
|
||||
|
||||
def prediction_step(self):
|
||||
# Make Sure Filter is Initialised
|
||||
if self.state is not None:
|
||||
x = self.state
|
||||
P = self.covariance
|
||||
|
||||
# Calculate Kalman Filter Prediction
|
||||
|
||||
# State Prediction: x_predict = F * x
|
||||
x_predict = x
|
||||
|
||||
# Covariance Prediction: P_predict = F * P * F' + Q
|
||||
P_predict = P
|
||||
|
||||
# Save Predicted State
|
||||
self.state = x_predict
|
||||
self.covariance = P_predict
|
||||
|
||||
return
|
||||
|
||||
def update_step(self, measurement):
|
||||
return
|
||||
|
||||
|
||||
|
||||
# Run the Simulation
|
||||
run_sim(KalmanFilterModel, sim_options, {})
|
@ -0,0 +1,64 @@
|
||||
import numpy as np
|
||||
from kfsims.tracker2d import run_sim
|
||||
from kfsims.kfmodels import KalmanFilterBase
|
||||
|
||||
# Simulation Options
|
||||
sim_options = {'time_step': 0.01,
|
||||
'end_time': 120,
|
||||
'measurement_rate': 1,
|
||||
'measurement_noise_std': 10,
|
||||
'motion_type': 'straight',
|
||||
'start_at_origin': True,
|
||||
'start_at_random_speed': False,
|
||||
'start_at_random_heading': False,
|
||||
'draw_plots': True,
|
||||
'draw_animation': True}
|
||||
|
||||
# Kalman Filter Model
|
||||
class KalmanFilterModel(KalmanFilterBase):
|
||||
|
||||
def initialise(self, time_step):
|
||||
|
||||
# Set Initial State and Covariance
|
||||
self.state = np.array([0,0,7.07,7.07])
|
||||
self.covariance = np.diag(np.array([0,0,0,0]))
|
||||
|
||||
# Setup the Model F Matrix
|
||||
dt = time_step
|
||||
self.F = np.array([[1,0,dt,0],
|
||||
[0,1,0,dt],
|
||||
[0,0,1,0],
|
||||
[0,0,0,1]])
|
||||
|
||||
# Set the Q Matrix
|
||||
accel_std = 0.1
|
||||
self.Q = np.diag(np.array([(0.5*dt*dt),(0.5*dt*dt),dt,dt]) * (accel_std*accel_std))
|
||||
|
||||
return
|
||||
|
||||
def prediction_step(self):
|
||||
# Make Sure Filter is Initialised
|
||||
if self.state is not None:
|
||||
x = self.state
|
||||
P = self.covariance
|
||||
|
||||
# Calculate Kalman Filter Prediction
|
||||
|
||||
# State Prediction: x_predict = F * x
|
||||
x_predict = np.matmul(self.F, x)
|
||||
|
||||
# Covariance Prediction: P_predict = F * P * F' + Q
|
||||
P_predict = np.matmul(self.F, np.matmul(P, np.transpose(self.F))) + self.Q
|
||||
|
||||
# Save Predicted State
|
||||
self.state = x_predict
|
||||
self.covariance = P_predict
|
||||
|
||||
return
|
||||
|
||||
def update_step(self, measurement):
|
||||
return
|
||||
|
||||
|
||||
# Run the Simulation
|
||||
run_sim(KalmanFilterModel, sim_options, {})
|
@ -0,0 +1,105 @@
|
||||
import numpy as np
|
||||
from kfsims.tracker2d import run_sim
|
||||
from kfsims.kfmodels import KalmanFilterBase
|
||||
|
||||
# Simulation Options
|
||||
sim_options = {'time_step': 0.01,
|
||||
'end_time': 120,
|
||||
'measurement_rate': 1,
|
||||
'measurement_noise_std': 10,
|
||||
'motion_type': 'straight',
|
||||
'start_at_origin': True,
|
||||
'start_at_random_speed': True,
|
||||
'start_at_random_heading': True,
|
||||
'draw_plots': True,
|
||||
'draw_animation': True}
|
||||
|
||||
# Kalman Filter Model
|
||||
class KalmanFilterModel(KalmanFilterBase):
|
||||
|
||||
def initialise(self, time_step):
|
||||
|
||||
# Set Initial State and Covariance
|
||||
init_pos_std = 0
|
||||
init_vel_std = 0
|
||||
self.state = np.array([0,0,0,0])
|
||||
self.covariance = np.diag(np.array([init_pos_std*init_pos_std,
|
||||
init_pos_std*init_pos_std,
|
||||
init_vel_std*init_vel_std,
|
||||
init_vel_std*init_vel_std]))
|
||||
|
||||
# Setup the Model F Matrix
|
||||
dt = time_step
|
||||
self.F = np.array([[1,0,dt,0],
|
||||
[0,1,0,dt],
|
||||
[0,0,1,0],
|
||||
[0,0,0,1]])
|
||||
|
||||
# Set the Q Matrix
|
||||
accel_std = 0.0
|
||||
self.Q = np.diag(np.array([(0.5*dt*dt),(0.5*dt*dt),dt,dt]) * (accel_std*accel_std))
|
||||
|
||||
# Setup the Model H Matrix
|
||||
#self.H =
|
||||
|
||||
# Set the R Matrix
|
||||
meas_std = 10.0
|
||||
#self.R =
|
||||
|
||||
return
|
||||
|
||||
def prediction_step(self):
|
||||
# Make Sure Filter is Initialised
|
||||
if self.state is not None:
|
||||
x = self.state
|
||||
P = self.covariance
|
||||
|
||||
# Calculate Kalman Filter Prediction
|
||||
x_predict = np.matmul(self.F, x)
|
||||
P_predict = np.matmul(self.F, np.matmul(P, np.transpose(self.F))) + self.Q
|
||||
|
||||
# Save Predicted State
|
||||
self.state = x_predict
|
||||
self.covariance = P_predict
|
||||
|
||||
return
|
||||
|
||||
def update_step(self, measurement):
|
||||
|
||||
# Make Sure Filter is Initialised
|
||||
if self.state is not None and self.covariance is not None:
|
||||
x = self.state
|
||||
P = self.covariance
|
||||
|
||||
# Calculate Kalman Filter Update
|
||||
z = np.array([measurement[0],measurement[1]])
|
||||
|
||||
# Predicted Measurement: z_hat = H * x
|
||||
# z_hat =
|
||||
|
||||
# Innovation: y = z - z_hat
|
||||
y = None
|
||||
|
||||
# Innovation Covariance: S = H * P * H' + R
|
||||
S = None
|
||||
|
||||
# Kalman Gain: K = P * H' * S^-1
|
||||
# K =
|
||||
|
||||
# Kalman State Update: x_update = x + K*y
|
||||
x_update = x
|
||||
|
||||
# Kalman Covariance Update: P_update = (I - K*H)*P
|
||||
P_update = P
|
||||
|
||||
# Save Updated State
|
||||
self.innovation = y
|
||||
self.innovation_covariance = S
|
||||
self.state = x_update
|
||||
self.covariance = P_update
|
||||
|
||||
return
|
||||
|
||||
|
||||
# Run the Simulation
|
||||
run_sim(KalmanFilterModel, sim_options, {})
|
@ -0,0 +1,97 @@
|
||||
import numpy as np
|
||||
from kfsims.tracker2d import run_sim
|
||||
from kfsims.kfmodels import KalmanFilterBase
|
||||
|
||||
# Simulation Options
|
||||
sim_options = {'time_step': 0.1,
|
||||
'end_time': 120,
|
||||
'measurement_rate': 1,
|
||||
'measurement_noise_std': 10,
|
||||
'motion_type': 'straight',
|
||||
'start_at_origin': True,
|
||||
'start_at_random_speed': True,
|
||||
'start_at_random_heading': True,
|
||||
'draw_plots': True,
|
||||
'draw_animation': True}
|
||||
|
||||
# Kalman Filter Model
|
||||
class KalmanFilterModel(KalmanFilterBase):
|
||||
|
||||
def initialise(self, time_step):
|
||||
|
||||
# Set Initial State and Covariance
|
||||
init_pos_std = 0
|
||||
init_vel_std = 0
|
||||
self.state = np.array([0,0,0,0])
|
||||
self.covariance = np.diag(np.array([init_pos_std*init_pos_std,
|
||||
init_pos_std*init_pos_std,
|
||||
init_vel_std*init_vel_std,
|
||||
init_vel_std*init_vel_std]))
|
||||
|
||||
# Setup the Model F Matrix
|
||||
dt = time_step
|
||||
self.F = np.array([[1,0,dt,0],
|
||||
[0,1,0,dt],
|
||||
[0,0,1,0],
|
||||
[0,0,0,1]])
|
||||
|
||||
# Set the Q Matrix
|
||||
accel_std = 0.1
|
||||
self.Q = np.diag(np.array([(0.5*dt*dt),(0.5*dt*dt),dt,dt]) * (accel_std*accel_std))
|
||||
|
||||
# Setup the Model H Matrix
|
||||
self.H = np.array([[1,0,0,0],[0,1,0,0]])
|
||||
|
||||
# Set the R Matrix
|
||||
meas_std = 10.0
|
||||
self.R = np.diag([meas_std*meas_std, meas_std*meas_std])
|
||||
|
||||
return
|
||||
|
||||
def prediction_step(self):
|
||||
# Make Sure Filter is Initialised
|
||||
if self.state is not None:
|
||||
x = self.state
|
||||
P = self.covariance
|
||||
|
||||
# Calculate Kalman Filter Prediction
|
||||
x_predict = np.matmul(self.F, x)
|
||||
P_predict = np.matmul(self.F, np.matmul(P, np.transpose(self.F))) + self.Q
|
||||
|
||||
# Save Predicted State
|
||||
self.state = x_predict
|
||||
self.covariance = P_predict
|
||||
|
||||
return
|
||||
|
||||
def update_step(self, measurement):
|
||||
|
||||
# Make Sure Filter is Initialised
|
||||
if self.state is not None and self.covariance is not None:
|
||||
x = self.state
|
||||
P = self.covariance
|
||||
H = self.H
|
||||
R = self.R
|
||||
|
||||
# Calculate Kalman Filter Update
|
||||
z = np.array([measurement[0],measurement[1]])
|
||||
z_hat = np.matmul(H, x)
|
||||
|
||||
y = z - z_hat
|
||||
S = np.matmul(H,np.matmul(P,np.transpose(H))) + R
|
||||
K = np.matmul(P,np.matmul(np.transpose(H),np.linalg.inv(S)))
|
||||
|
||||
x_update = x + np.matmul(K, y)
|
||||
P_update = np.matmul( (np.eye(4) - np.matmul(K,H)), P)
|
||||
|
||||
# Save Updated State
|
||||
self.innovation = y
|
||||
self.innovation_covariance = S
|
||||
self.state = x_update
|
||||
self.covariance = P_update
|
||||
|
||||
return
|
||||
|
||||
|
||||
# Run the Simulation
|
||||
run_sim(KalmanFilterModel, sim_options, {})
|
@ -0,0 +1,28 @@
|
||||
class KalmanFilterBase:
|
||||
|
||||
def __init__(self):
|
||||
self.state = None
|
||||
self.covariance = None
|
||||
self.innovation = None
|
||||
self.innovation_covariance = None
|
||||
|
||||
def get_current_state(self):
|
||||
if self.state is not None:
|
||||
return self.state.tolist()
|
||||
return None
|
||||
|
||||
def get_current_covariance(self):
|
||||
if self.covariance is not None:
|
||||
return self.covariance.tolist()
|
||||
return None
|
||||
|
||||
def get_last_innovation(self):
|
||||
if self.innovation is not None:
|
||||
return self.innovation.tolist()
|
||||
return None
|
||||
|
||||
def get_last_innovation_covariance(self):
|
||||
if self.innovation_covariance is not None:
|
||||
return self.innovation_covariance.tolist()
|
||||
return None
|
||||
|
@ -0,0 +1,77 @@
|
||||
import numpy as np
|
||||
from .kfmodels import KalmanFilterBase
|
||||
|
||||
# Kalman Filter Model
|
||||
class KalmanFilterModel(KalmanFilterBase):
|
||||
|
||||
def initialise(self, time_step, accel_std, meas_std, init_on_measurement=False, init_pos_std = 500, init_vel_std = 50):
|
||||
dt = time_step
|
||||
|
||||
# Set Model F and H Matrices
|
||||
self.F = np.array([[1,0,dt,0],
|
||||
[0,1,0,dt],
|
||||
[0,0,1,0],
|
||||
[0,0,0,1]])
|
||||
|
||||
self.H = np.array([[1,0,0,0],[0,1,0,0]])
|
||||
|
||||
# Set R and Q Matrices
|
||||
self.Q = np.diag(np.array([(0.5*dt*dt),(0.5*dt*dt),dt,dt]) * (accel_std*accel_std))
|
||||
self.R = np.diag([meas_std*meas_std, meas_std*meas_std])
|
||||
|
||||
# Set Initial State and Covariance
|
||||
if init_on_measurement is False:
|
||||
self.state = np.array([0,0,0,0]) # Assume we are at zero position and velocity
|
||||
self.covariance = np.diag(np.array([init_pos_std*init_pos_std,init_pos_std*init_pos_std,init_vel_std*init_vel_std,init_vel_std*init_vel_std]))
|
||||
|
||||
return
|
||||
|
||||
def prediction_step(self):
|
||||
# Make Sure Filter is Initialised
|
||||
if self.state is not None:
|
||||
x = self.state
|
||||
P = self.covariance
|
||||
|
||||
# Calculate Kalman Filter Prediction
|
||||
x_predict = np.matmul(self.F, x)
|
||||
P_predict = np.matmul(self.F, np.matmul(P, np.transpose(self.F))) + self.Q
|
||||
|
||||
# Save Predicted State
|
||||
self.state = x_predict
|
||||
self.covariance = P_predict
|
||||
|
||||
return
|
||||
|
||||
def update_step(self, measurement):
|
||||
|
||||
# Make Sure Filter is Initialised
|
||||
if self.state is not None and self.covariance is not None:
|
||||
x = self.state
|
||||
P = self.covariance
|
||||
H = self.H
|
||||
R = self.R
|
||||
|
||||
# Calculate Kalman Filter Update
|
||||
z = np.array([measurement[0],measurement[1]])
|
||||
z_hat = np.matmul(H, x)
|
||||
|
||||
y = z - z_hat
|
||||
S = np.matmul(H,np.matmul(P,np.transpose(H))) + R
|
||||
K = np.matmul(P,np.matmul(np.transpose(H),np.linalg.inv(S)))
|
||||
|
||||
x_update = x + np.matmul(K, y)
|
||||
P_update = np.matmul( (np.eye(4) - np.matmul(K,H)), P)
|
||||
|
||||
# Save Updated State
|
||||
self.innovation = y
|
||||
self.innovation_covariance = S
|
||||
self.state = x_update
|
||||
self.covariance = P_update
|
||||
|
||||
else:
|
||||
|
||||
# Set Initial State and Covariance
|
||||
self.state = np.array([measurement[0],measurement[1],0,0])
|
||||
self.covariance = np.diag(np.array([self.R[0,0],self.R[1,1],10,10])) # Assume we don't know our velocity
|
||||
|
||||
return
|
@ -0,0 +1,37 @@
|
||||
import numpy as np
|
||||
|
||||
class VehicleModel2D():
|
||||
|
||||
def __init__(self):
|
||||
self.x_pos = 0
|
||||
self.y_pos = 0
|
||||
self.vel = 0
|
||||
self.yaw = 0
|
||||
|
||||
def initialise(self, vehicle_params):
|
||||
self.x_pos = vehicle_params['initial_x_position']
|
||||
self.y_pos = vehicle_params['initial_y_position']
|
||||
self.yaw = vehicle_params['initial_heading']
|
||||
self.vel = vehicle_params['initial_speed']
|
||||
|
||||
def update_vehicle(self, time_step, accel, yaw_rate):
|
||||
self.vel = self.vel + accel * time_step
|
||||
self.yaw = self.yaw + yaw_rate * time_step
|
||||
self.x_pos = self.x_pos + self.vel * np.cos(self.yaw) * time_step
|
||||
self.y_pos = self.y_pos + self.vel * np.sin(self.yaw) * time_step
|
||||
return
|
||||
|
||||
def get_position(self):
|
||||
return [self.x_pos, self.y_pos]
|
||||
|
||||
def get_velocity(self):
|
||||
x_vel = self.vel * np.cos(self.yaw)
|
||||
y_vel = self.vel * np.sin(self.yaw)
|
||||
return [x_vel, y_vel]
|
||||
|
||||
def get_speed(self):
|
||||
return self.vel
|
||||
|
||||
def get_heading(self):
|
||||
return self.yaw
|
||||
|
Loading…
Reference in New Issue