WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
EkfDynamics.hpp
Go to the documentation of this file.
1/******************************************************************************
2* Copyright (c) ATTX LLC 2024. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, LLC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, LLC.
15******************************************************************************/
16
17#ifndef GNCUTILS_EKF_EKFDYNAMICS_HPP
18#define GNCUTILS_EKF_EKFDYNAMICS_HPP
19
21#include "core/matrixmath.hpp"
22
23namespace warpos {
24
38 template <uint32 N, uint32 O, uint32 D>
39 class EkfDynamics : public Rates<N+N*N+O>{
40 public:
48 virtual int16 calculateDynamics(floating_point time,
49 const std::array<floating_point, N> &tar_state,
50 const std::array<floating_point, O> &obs_state,
51 std::array<floating_point, N> &tar_state_dot) {
53 }
54
62 virtual int16 calculateDynamicsJacobian(floating_point time,
63 const std::array<floating_point, N> &tar_state,
64 const std::array<floating_point, O> &obs_state,
65 std::array<floating_point, N*N> &dynamics_Jacobian) {
67 }
68
76 virtual int16 calculateDynamicsNoiseMappingMatrix(floating_point time,
77 const std::array<floating_point, N> &tar_state,
78 const std::array<floating_point, O> &obs_state,
79 std::array<floating_point, N*D> &dynamics_mapping) {
81 }
82
90 virtual int16 calculateNoiseCovariance(floating_point time,
91 const std::array<floating_point, N> &tar_state,
92 const std::array<floating_point, O> &obs_state,
93 std::array<floating_point, D*D> &process_covariance) {
95 }
96
104 virtual int16 calculateObserverDynamics(floating_point time,
105 const std::array<floating_point, N> &tar_state,
106 const std::array<floating_point, O> &obs_state,
107 std::array<floating_point, O> &obs_state_dot) {
109 }
110
115 int16 calculateRates(floating_point time,
116 const std::array<floating_point, N+N*N+O> &state,
117 std::array<floating_point, N+N*N+O> &out_rates) override {
118 // Split input into state vector, state covariance, and observer state
119 for (uint32 i = 0; i < N; ++i) {
120 _state_vector[i] = state[i];
121 }
122 _PP.setFromArray(&state[N]);
123 for (uint32 i = 0; i < O; ++i) {
124 _observer_vector[i] = state[N+N*N+i];
125 }
126
127 // ----- First get dynamics function outputs ----- //
128 // Dynamics function Jacobian and transpose
129 _error = calculateDynamicsJacobian(time, _state_vector, _observer_vector, _tmp_F_array);
130 if (_error > 0) {return _error;} else {_saved_warning = _error;}
131 _F.setFromArray(&_tmp_F_array[0]);
132 _F.transpose(_F_tpose);
133 // Dynamics mapping matrix and transpose
134 _error = calculateDynamicsNoiseMappingMatrix(time, _state_vector, _observer_vector, _tmp_M_array);
135 if (_error > 0) {return _error;} else {_saved_warning = _error;}
136 _M.setFromArray(&_tmp_M_array[0]);
137 _M.transpose(_M_tpose);
138 // Process noise covariance matrix
139 _error = calculateNoiseCovariance(time, _state_vector, _observer_vector, _tmp_Q_array);
140 if (_error > 0) {return _error;} else {_saved_warning = _error;}
141 _Q.setFromArray(&_tmp_Q_array[0]);
142
143 // Compute mean state derivative
144 _error = calculateDynamics(time, _state_vector, _observer_vector, _m_dot);
145 if (_error > 0) {return _error;} else {_saved_warning = _error;}
146 for (uint32 i = 0; i < N; ++i) {
147 out_rates[i] = _m_dot[i];
148 }
149
150 // Compute state covariance derivative
151 _P_dot = _F*_PP + _PP*_F_tpose + _M*_Q*_M_tpose;
152 _P_dot.getAsArray(&out_rates[N]);
153
154 // Compute the observer state derivative
155 _error = calculateObserverDynamics(time, _state_vector, _observer_vector, _o_dot);
156 if (_error > 0) {return _error;} else {_saved_warning = _error;}
157 for (uint32 i = 0; i < O; ++i) {
158 out_rates[i+N+N*N] = _o_dot[i];
159 }
160
161 return _saved_warning;
162 }
163 private:
164 // Internal variable for tracking errors
165 int16 _error;
166 int16 _saved_warning;
167
168 // State vector and its derivative
169 std::array<floating_point, N> _state_vector, _m_dot;
170 // Observer vector and its derivative
171 std::array<floating_point, O> _observer_vector, _o_dot;
172
173 // F matrix (dynamics function jacobian) for internal use
174 std::array<floating_point, N*N> _tmp_F_array;
175 clockwerk::Matrix<N, N> _F, _F_tpose;
176
177 // M matrix (mapping from noise space to state space) for internal use
178 std::array<floating_point, N*D> _tmp_M_array;
181
182 // Q matrix (process noise covariance) for internal use
183 std::array<floating_point, D*D> _tmp_Q_array;
185
186 // Temporary variables for covariance and state
187 clockwerk::Matrix<N, N> _PP, _P_dot;
188 };
189
190}
191
192#endif
Matrix math implementation.
Definition Matrix.hpp:55
Definition EkfDynamics.hpp:39
virtual int16 calculateNoiseCovariance(floating_point time, const std::array< floating_point, N > &tar_state, const std::array< floating_point, O > &obs_state, std::array< floating_point, D *D > &process_covariance)
Function to calculate the covariance matrix of a given process with a specific noise profile.
Definition EkfDynamics.hpp:90
int16 calculateRates(floating_point time, const std::array< floating_point, N+N *N+O > &state, std::array< floating_point, N+N *N+O > &out_rates) override
Function to calculate rates from current state of system.
Definition EkfDynamics.hpp:115
virtual int16 calculateDynamicsJacobian(floating_point time, const std::array< floating_point, N > &tar_state, const std::array< floating_point, O > &obs_state, std::array< floating_point, N *N > &dynamics_Jacobian)
Function to calculate the dynamics function Jacobian from current state of system.
Definition EkfDynamics.hpp:62
virtual int16 calculateObserverDynamics(floating_point time, const std::array< floating_point, N > &tar_state, const std::array< floating_point, O > &obs_state, std::array< floating_point, O > &obs_state_dot)
Function to calculate the observer state vector time vector.
Definition EkfDynamics.hpp:104
virtual int16 calculateDynamicsNoiseMappingMatrix(floating_point time, const std::array< floating_point, N > &tar_state, const std::array< floating_point, O > &obs_state, std::array< floating_point, N *D > &dynamics_mapping)
Function to calculate the matrix that maps the noise configuration space to the state configuration s...
Definition EkfDynamics.hpp:76
virtual int16 calculateDynamics(floating_point time, const std::array< floating_point, N > &tar_state, const std::array< floating_point, O > &obs_state, std::array< floating_point, N > &tar_state_dot)
Function to calculate a state time derivative from current state of system.
Definition EkfDynamics.hpp:48
Definition Rates.hpp:28
#define WARNING_NOT_OVERRIDDEN
Warning when a virtual function is not called but not overwridden and should not result in fault wort...
Definition clockwerkerrors.h:172
Definition DeadReckon.cpp:20