-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #295 from LAMPSPUC/gb/veh-track2
Vehicle Tracking model
- Loading branch information
Showing
7 changed files
with
124 additions
and
0 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 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
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,59 @@ | ||
@doc raw""" | ||
VehicleTracking(y::Matrix{Fl}, ρ::Fl, H::Matrix{Fl}, Q::Matrix{Fl}) where Fl | ||
The vehicle tracking example illustrates a model where there are no hyperparameters, | ||
the user defines the parameters ``\rho``, ``H`` and ``Q`` and the model gives the predicted and filtered speed and | ||
position. In this case, $y_t$ is a $2 \times 1$ observation vector representing the corrupted measurements | ||
of the vehicle's position on the two-dimensional plane in instant $t$. | ||
The position and speed in each dimension compose the state of the vehicle. Let us refer to ``x_t^{(d)}`` as | ||
the position on the axis $d$ and to ``\dot{x}^{(d)}_t`` as the speed on the axis $d$ in instant ``t``. Additionally, | ||
let ``\eta^{(d)}_t`` be the input drive force on the axis ``d``, which acts as state noise. For a single dimension, | ||
we can describe the vehicle dynamics as | ||
```math | ||
\begin{equation} | ||
\begin{aligned} | ||
& x_{t+1}^{(d)} = x_t^{(d)} + \Big( 1 - \frac{\rho \Delta_t}{2} \Big) \Delta_t \dot{x}^{(d)}_t + \frac{\Delta^2_t}{2} \eta_t^{(d)}, \\ | ||
& \dot{x}^{(d)}_{t+1} = (1 - \rho) \dot{x}^{(d)}_{t} + \Delta_t \eta^{(d)}_t, | ||
\end{aligned}\label{eq_control} | ||
\end{equation} | ||
``` | ||
We can cast the dynamical system as a state-space model in the following manner: | ||
```math | ||
\begin{align*} | ||
y_t &= \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \end{bmatrix} \alpha_{t+1} + \varepsilon_t, \\ | ||
\alpha_{t+1} &= \begin{bmatrix} 1 & (1 - \tfrac{\rho \Delta_t}{2}) \Delta_t & 0 & 0 \\ 0 & (1 - \rho) & 0 & 0 \\ 0 & 0 & 1 & (1 - \tfrac{\rho \Delta_t}{2}) \\ 0 & 0 & 0 & (1 - \rho) \end{bmatrix} \alpha_{t} + \begin{bmatrix} \tfrac{\Delta^2_t}{2} & 0 \\ \Delta_t & 0 \\ 0 & \tfrac{\Delta^2_t}{2} \\ 0 & \Delta_t \end{bmatrix} \eta_{t}, | ||
\end{align*} | ||
``` | ||
See more on [Vehicle tracking](@ref) | ||
""" | ||
mutable struct VehicleTracking <: StateSpaceModel | ||
system::LinearMultivariateTimeInvariant | ||
|
||
function VehicleTracking(y::Matrix{Fl}, ρ::Fl, H::Matrix{Fl}, Q::Matrix{Fl}) where Fl | ||
p = 2 | ||
Z = kron(Matrix{Fl}(I, p, p), [1.0 0.0]) | ||
T = kron(Matrix{Fl}(I, p, p), [1 (1 - ρ / 2); 0 (1 - ρ)]) | ||
R = kron(Matrix{Fl}(I, p, p), [0.5; 1]) | ||
d = zeros(Fl, p) | ||
c = zeros(Fl, 4) | ||
H = H | ||
Q = Q | ||
|
||
system = LinearMultivariateTimeInvariant{Fl}(y, Z, T, R, d, c, H, Q) | ||
|
||
return new(system) | ||
end | ||
end | ||
|
||
function default_filter(model::VehicleTracking) | ||
Fl = typeof_model_elements(model) | ||
steadystate_tol = Fl(1e-5) | ||
a1 = zeros(Fl, num_states(model)) | ||
skip_llk_instants = length(a1) | ||
P1 = Fl(1e6) .* Matrix{Fl}(I, num_states(model), num_states(model)) | ||
return MultivariateKalmanFilter(2, a1, P1, skip_llk_instants, steadystate_tol) | ||
end |
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,21 @@ | ||
@testset "Vehicle tracking" begin | ||
n = 100 | ||
H = [1 0 | ||
0 1.0] | ||
Q = [1 0 | ||
0 1.0] | ||
rho = 0.1 | ||
model = VehicleTracking(rand(n, 2), rho, H, Q) | ||
|
||
# Not possible to fit | ||
@test !has_fit_methods(VehicleTracking) | ||
|
||
# Simply test if it runs | ||
initial_state = [0.0, 0, 0, 0] | ||
sim = simulate(model.system, initial_state, n) | ||
|
||
model = VehicleTracking(sim, 0.1, H, Q) | ||
kalman_filter(model) | ||
pos_pred = get_predictive_state(model) | ||
pos_filtered = get_filtered_state(model) | ||
end |
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