mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-25 08:40:30 +08:00
perf(ekf2): cut covariance kernel cost without changing the estimate (#29242)
* perf(ekf2): skip the zero entries of H in P * H Most observation Jacobians depend on a handful of states only, yet the Kalman gain and both P * H products of the Joseph update ran a dense 24x24 matrix-vector product. Accumulating only the non-zero entries of H, in the same order, leaves every partial sum unchanged, so P and the gains stay bit-identical while the products drop from 576 multiply-adds to 24 per non-zero entry. Add Ekf::computeKalmanGain() and use it at the aid sources that built K as P * H / innov_var. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org> * perf(ekf2): evaluate the Joseph update in a single pass Step 1 of the Joseph covariance update stored the whole 24x24 P_temp = P - K * PH^T, and step 2 read it back for P_temp * H and the stabilized update of the lower triangle, overwriting the rest. Evaluate each P_temp entry where it is used instead: its columns selected by H for P_temp * H first, then in a single pass over the lower triangle, mirrored to the upper one. The arithmetic of every entry is unchanged, so P stays bit-identical, while the temporary is never written or read back. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org> * perf(ekf2): only derive the covariance prediction of the kinematic states The gyro bias, accel bias, mag, wind and terrain states have an identity process model, so 120 of the 300 outputs of the generated PredictCovariance() were plain copies of P, which predictCovariance() then copied back into P and mirrored in full. Return only the rows of the quaternion, velocity and position states and update P in place. Regenerated with symforce 0.9.0, the version that reproduces the committed headers byte-for-byte: the intermediate terms and the 180 remaining outputs are unchanged, so P stays bit-identical. The function is 2.8 KB smaller (15.5 to 12.8 KB on Cortex-M7), which matters on boards executing from external flash, and its stack temporary drops from 2304 to 864 bytes. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org> * refactor(ekf2): take the predicted covariance row count from its type sizeof(P_pred) gives the row count only while matrix::Matrix holds nothing but its data. The predict_covariance return annotation still described the full 24x24 matrix; symforce 0.9.0 regenerates the committed headers unchanged. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com> * perf(ekf2): run computeKalmanGain from ITCM Every fusion now calls it, and on the boards that keep measurementUpdate and controlGravityFusion in ITCM it sat in flash, reached through a veneer. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com> --------- Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org> Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com> Co-authored-by: Jacob Dahl <dahl.jakejacob@gmail.com>
This commit is contained in:
1 parent
3b65082449
commit
1eedf0c8e7
21 files changed
+137
-176
No files matched your search
@@ -433,6 +433,7 @@
|
||||
*(.text.nxsig_timeout)
|
||||
*(.text._ZN11RateControl19setSaturationStatusERKN6matrix7Vector3IbEES4_)
|
||||
*(.text._ZN3Ekf17measurementUpdateERN6matrix6VectorIfLj24EEERKS2_ff)
|
||||
*(.text._ZNK3Ekf17computeKalmanGainERKN6matrix6VectorIfLj24EEEf)
|
||||
*(.text._ZN19MavlinkStreamVFRHUD4sendEv)
|
||||
*(.text.hrt_call_reschedule)
|
||||
*(.text.nxsem_boost_priority)
|
||||
|
||||
@@ -421,6 +421,7 @@
|
||||
*(.text.nxsig_timeout)
|
||||
*(.text._ZN11RateControl19setSaturationStatusERKN6matrix7Vector3IbEES4_)
|
||||
*(.text._ZN3Ekf17measurementUpdateERN6matrix6VectorIfLj24EEERKS2_ff)
|
||||
*(.text._ZNK3Ekf17computeKalmanGainERKN6matrix6VectorIfLj24EEEf)
|
||||
*(.text._ZN19MavlinkStreamVFRHUD4sendEv)
|
||||
*(.text.hrt_call_reschedule)
|
||||
*(.text.nxsem_boost_priority)
|
||||
|
||||
@@ -426,6 +426,7 @@
|
||||
*(.text.nxsig_timeout)
|
||||
*(.text._ZN11RateControl19setSaturationStatusERKN6matrix7Vector3IbEES4_)
|
||||
*(.text._ZN3Ekf17measurementUpdateERN6matrix6VectorIfLj24EEERKS2_ff)
|
||||
*(.text._ZNK3Ekf17computeKalmanGainERKN6matrix6VectorIfLj24EEEf)
|
||||
*(.text._ZN19MavlinkStreamVFRHUD4sendEv)
|
||||
*(.text.hrt_call_reschedule)
|
||||
*(.text.nxsem_boost_priority)
|
||||
|
||||
@@ -426,6 +426,7 @@
|
||||
*(.text.nxsig_timeout)
|
||||
*(.text._ZN11RateControl19setSaturationStatusERKN6matrix7Vector3IbEES4_)
|
||||
*(.text._ZN3Ekf17measurementUpdateERN6matrix6VectorIfLj24EEERKS2_ff)
|
||||
*(.text._ZNK3Ekf17computeKalmanGainERKN6matrix6VectorIfLj24EEEf)
|
||||
*(.text._ZN19MavlinkStreamVFRHUD4sendEv)
|
||||
*(.text.hrt_call_reschedule)
|
||||
*(.text.nxsem_boost_priority)
|
||||
|
||||
@@ -13,3 +13,4 @@
|
||||
*(.text.param_get_default_value)
|
||||
*(.text._Z12get_orb_meta6ORB_ID)
|
||||
*(.text._ZN3Ekf17measurementUpdateERN6matrix6VectorIfLj24EEERKS2_ff)
|
||||
*(.text._ZNK3Ekf17computeKalmanGainERKN6matrix6VectorIfLj24EEEf)
|
||||
@@ -433,6 +433,7 @@
|
||||
*(.text.nxsig_timeout)
|
||||
*(.text._ZN11RateControl19setSaturationStatusERKN6matrix7Vector3IbEES4_)
|
||||
*(.text._ZN3Ekf17measurementUpdateERN6matrix6VectorIfLj24EEERKS2_ff)
|
||||
*(.text._ZNK3Ekf17computeKalmanGainERKN6matrix6VectorIfLj24EEEf)
|
||||
*(.text._ZN19MavlinkStreamVFRHUD4sendEv)
|
||||
*(.text.hrt_call_reschedule)
|
||||
*(.text.nxsem_boost_priority)
|
||||
|
||||
@@ -222,7 +222,7 @@ void Ekf::fuseAirspeed(const airspeedSample &airspeed_sample, estimator_aid_sour
|
||||
_fault_status.flags.bad_airspeed = false;
|
||||
|
||||
const VectorState H = sym::ComputeAirspeedH(_state.vector(), FLT_EPSILON);
|
||||
VectorState K = P * H / aid_src.innovation_variance;
|
||||
VectorState K = computeKalmanGain(H, aid_src.innovation_variance);
|
||||
|
||||
if (update_wind_only) {
|
||||
const Vector2f K_wind = K.slice<State::wind_vel.dof, 1>(State::wind_vel.idx, 0);
|
||||
|
||||
@@ -171,7 +171,7 @@ void Ekf::fuseDrag(const dragSample &drag_sample)
|
||||
&& (test_ratio < 1.f)
|
||||
) {
|
||||
|
||||
VectorState K = P * H / innovation_variance(axis_index);
|
||||
VectorState K = computeKalmanGain(H, innovation_variance(axis_index));
|
||||
|
||||
measurementUpdate(K, H, R_ACC, innovation(axis_index));
|
||||
}
|
||||
|
||||
@@ -193,7 +193,7 @@ void Ekf::fuseBodyFrameVelocity(estimator_aid_source3d_s &aid_src, const uint64_
|
||||
|
||||
aid_src.innovation[index] = Vector3f(_R_to_earth.transpose().row(index)) * _state.vel - measurement(index);
|
||||
|
||||
VectorState Kfusion = P * H[index] / aid_src.innovation_variance[index];
|
||||
VectorState Kfusion = computeKalmanGain(H[index], aid_src.innovation_variance[index]);
|
||||
measurementUpdate(Kfusion, H[index], aid_src.observation_variance[index], aid_src.innovation[index]);
|
||||
}
|
||||
|
||||
|
||||
@@ -229,7 +229,7 @@ void Ekf::fuseGnssYaw(float antenna_yaw_offset)
|
||||
|
||||
// calculate the Kalman gains
|
||||
// only calculate gains for states we are using
|
||||
VectorState Kfusion = P * H / aid_src.innovation_variance;
|
||||
VectorState Kfusion = computeKalmanGain(H, aid_src.innovation_variance);
|
||||
|
||||
measurementUpdate(Kfusion, H, aid_src.observation_variance, aid_src.innovation);
|
||||
|
||||
|
||||
@@ -108,7 +108,7 @@ void Ekf::controlGravityFusion(const imuSample &imu)
|
||||
const bool accel_clipping = imu.delta_vel_clipping[0] || imu.delta_vel_clipping[1] || imu.delta_vel_clipping[2];
|
||||
|
||||
if (_control_status.flags.gravity_vector && !_aid_src_gravity.innovation_rejected && !accel_clipping) {
|
||||
VectorState K = P * H / _aid_src_gravity.innovation_variance[index];
|
||||
VectorState K = computeKalmanGain(H, _aid_src_gravity.innovation_variance[index]);
|
||||
|
||||
fused[index] = measurementUpdate(K, H,
|
||||
_aid_src_gravity.observation_variance[index], _aid_src_gravity.innovation[index]);
|
||||
|
||||
@@ -107,7 +107,7 @@ bool Ekf::fuseMag(const Vector3f &mag, const float R_MAG, VectorState &H, estima
|
||||
return false;
|
||||
}
|
||||
|
||||
VectorState Kfusion = P * H / aid_src.innovation_variance[index];
|
||||
VectorState Kfusion = computeKalmanGain(H, aid_src.innovation_variance[index]);
|
||||
|
||||
if (update_all_states) {
|
||||
if (!update_tilt) {
|
||||
@@ -179,7 +179,7 @@ bool Ekf::fuseDeclination(float decl_measurement_rad, float R, bool update_all_s
|
||||
}
|
||||
|
||||
// Calculate the Kalman gains
|
||||
VectorState Kfusion = P * H / innovation_variance;
|
||||
VectorState Kfusion = computeKalmanGain(H, innovation_variance);
|
||||
|
||||
if (update_all_states) {
|
||||
if (!update_tilt) {
|
||||
|
||||
@@ -85,7 +85,7 @@ bool Ekf::fuseOptFlow(VectorState &H, const bool update_terrain)
|
||||
return false;
|
||||
}
|
||||
|
||||
VectorState Kfusion = P * H / _aid_src_optical_flow.innovation_variance[index];
|
||||
VectorState Kfusion = computeKalmanGain(H, _aid_src_optical_flow.innovation_variance[index]);
|
||||
|
||||
if (!update_terrain) {
|
||||
Kfusion(State::terrain.idx) = 0.f;
|
||||
|
||||
@@ -45,7 +45,7 @@ bool Ekf::fuseHaglRng(estimator_aid_source1d_s &aid_src, bool update_height, boo
|
||||
sym::ComputeHaglH(&H);
|
||||
|
||||
// calculate the Kalman gain
|
||||
VectorState K = P * H / aid_src.innovation_variance;
|
||||
VectorState K = computeKalmanGain(H, aid_src.innovation_variance);
|
||||
|
||||
if (!update_terrain) {
|
||||
K(State::terrain.idx) = 0.f;
|
||||
|
||||
@@ -117,7 +117,7 @@ void Ekf::fuseRangingBeacon(const rangingBeaconSample &sample)
|
||||
return;
|
||||
}
|
||||
|
||||
VectorState K = P * H / innov_var;
|
||||
VectorState K = computeKalmanGain(H, innov_var);
|
||||
K(State::pos.idx + 2) = 0.f; // altitude is handled by height reference
|
||||
measurementUpdate(K, H, R, innovation);
|
||||
|
||||
|
||||
@@ -128,7 +128,7 @@ bool Ekf::fuseSideslip(estimator_aid_source1d_s &sideslip)
|
||||
const float epsilon = 1e-3f;
|
||||
|
||||
const VectorState H = sym::ComputeSideslipH(_state.vector(), epsilon);
|
||||
VectorState K = P * H / sideslip.innovation_variance;
|
||||
VectorState K = computeKalmanGain(H, sideslip.innovation_variance);
|
||||
|
||||
if (update_wind_only) {
|
||||
const Vector2f K_wind = K.slice<State::wind_vel.dof, 1>(State::wind_vel.idx, 0);
|
||||
|
||||
@@ -47,6 +47,15 @@
|
||||
#include <math.h>
|
||||
#include <mathlib/mathlib.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
template<typename Type, size_t M, size_t N>
|
||||
constexpr size_t numRows(const matrix::Matrix<Type, M, N> &)
|
||||
{
|
||||
return M;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// Sets initial values for the covariance matrix
|
||||
// Do not call before quaternion states have been initialised
|
||||
void Ekf::initialiseCovariance()
|
||||
@@ -133,11 +142,22 @@ void Ekf::predictCovariance(const imuSample &imu_delayed)
|
||||
}
|
||||
}
|
||||
|
||||
// calculate variances and upper diagonal covariances for quaternion, velocity, position and gyro bias states
|
||||
P = sym::PredictCovariance(_state.vector(), P,
|
||||
imu_delayed.delta_vel / imu_delayed.delta_vel_dt, accel_var,
|
||||
imu_delayed.delta_ang / imu_delayed.delta_ang_dt, gyro_var,
|
||||
dt);
|
||||
// calculate variances and upper diagonal covariances for the quaternion, velocity and position states.
|
||||
// The other states have an identity process model: their rows of the upper triangle are unchanged.
|
||||
const auto P_pred = sym::PredictCovariance(_state.vector(), P,
|
||||
imu_delayed.delta_vel / imu_delayed.delta_vel_dt, accel_var,
|
||||
imu_delayed.delta_ang / imu_delayed.delta_ang_dt, gyro_var,
|
||||
dt);
|
||||
|
||||
constexpr unsigned predicted_rows = numRows(P_pred);
|
||||
|
||||
// covariance matrix is symmetrical, so copy upper half to lower half
|
||||
for (unsigned row = 0; row < predicted_rows; row++) {
|
||||
for (unsigned column = row; column < State::size; column++) {
|
||||
P(row, column) = P_pred(row, column);
|
||||
P(column, row) = P_pred(row, column);
|
||||
}
|
||||
}
|
||||
|
||||
if (!_control_status.flags.heading_observable) {
|
||||
// Zero heading correlations to prevent unintended heading corrections when heading is not observable
|
||||
@@ -236,13 +256,6 @@ void Ekf::predictCovariance(const imuSample &imu_delayed)
|
||||
|
||||
#endif // CONFIG_EKF2_TERRAIN
|
||||
|
||||
// covariance matrix is symmetrical, so copy upper half to lower half
|
||||
for (unsigned row = 0; row < State::size; row++) {
|
||||
for (unsigned column = 0; column < row; column++) {
|
||||
P(row, column) = P(column, row);
|
||||
}
|
||||
}
|
||||
|
||||
constrainStateVariances();
|
||||
}
|
||||
|
||||
|
||||
@@ -284,6 +284,9 @@ public:
|
||||
void fuseDirectStateMeasurement(const float innov, const float innov_var, const float R, const int state_index,
|
||||
bool constrain_variances = true);
|
||||
|
||||
// Kalman gain P * H / innov_var for a scalar observation with Jacobian H
|
||||
VectorState computeKalmanGain(const VectorState &H, const float innov_var) const;
|
||||
|
||||
bool measurementUpdate(VectorState &K, const VectorState &H, const float R, const float innovation);
|
||||
|
||||
// gyro bias
|
||||
@@ -926,7 +929,7 @@ private:
|
||||
bool fuseEvVelocity(estimator_aid_source3d_s &aid_src, const extVisionSample &ev_sample);
|
||||
void fuseBodyVelocity(estimator_aid_source1d_s &aid_src, float &innov_var, VectorState &H)
|
||||
{
|
||||
VectorState Kfusion = P * H / innov_var;
|
||||
VectorState Kfusion = computeKalmanGain(H, innov_var);
|
||||
measurementUpdate(Kfusion, H, aid_src.observation_variance, aid_src.innovation);
|
||||
aid_src.fused = true;
|
||||
}
|
||||
|
||||
@@ -45,6 +45,46 @@
|
||||
#include <lib/world_magnetic_model/geo_mag_declination.h>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace
|
||||
{
|
||||
// Indices of the non-zero entries of an observation Jacobian, in increasing order.
|
||||
// Most observations depend on a handful of states only. Skipping the zero terms
|
||||
// of P * H while keeping the summation order leaves every partial sum unchanged
|
||||
// (s + P(i, j) * 0 == s for finite P), so the sparse product is bit-identical
|
||||
// to the dense one.
|
||||
struct NonZeroIndices {
|
||||
explicit NonZeroIndices(const Ekf::VectorState &v)
|
||||
{
|
||||
for (unsigned i = 0; i < State::size; i++) {
|
||||
// excludes +0 and -0 only: NaN entries are kept, as in the dense product
|
||||
if (!(fabsf(v(i)) <= 0.f)) {
|
||||
idx[count++] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t idx[State::size];
|
||||
uint8_t count{0};
|
||||
};
|
||||
|
||||
Ekf::VectorState multiplySparse(const Ekf::SquareMatrixState &P, const Ekf::VectorState &H, const NonZeroIndices &nz)
|
||||
{
|
||||
Ekf::VectorState PH;
|
||||
|
||||
for (unsigned i = 0; i < State::size; i++) {
|
||||
float sum = 0.f;
|
||||
|
||||
for (unsigned k = 0; k < nz.count; k++) {
|
||||
sum += P(i, nz.idx[k]) * H(nz.idx[k]);
|
||||
}
|
||||
|
||||
PH(i) = sum;
|
||||
}
|
||||
|
||||
return PH;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool Ekf::isHeightResetRequired() const
|
||||
{
|
||||
// check if height is continuously failing because of accel errors
|
||||
@@ -1065,24 +1105,22 @@ void Ekf::fuseDirectStateMeasurement(const float innov, const float innov_var, c
|
||||
// = P_temp * (I - H.T * K.T) + K * R * K.T
|
||||
// = P_temp - P_temp * H.T * K.T + K * R * K.T
|
||||
|
||||
// Step 1: conventional update
|
||||
// Compute P_temp and store it in P to avoid allocating more memory
|
||||
// P_temp = P - K * PH^T is never stored: each entry is evaluated where it is used, first in
|
||||
// P_temp * H (its column state_index), then in the stabilized update of the lower triangle,
|
||||
// which is mirrored to the upper one. This is the same arithmetic as storing P_temp first.
|
||||
// P is symmetric, so PH == H.T * P.T == H.T * P. Taking the row is faster as matrices are row-major
|
||||
VectorState PH = P.row(state_index);
|
||||
const VectorState PH = P.row(state_index);
|
||||
VectorState P_temp_H;
|
||||
|
||||
for (unsigned i = 0; i < State::size; i++) {
|
||||
for (unsigned j = 0; j < State::size; j++) {
|
||||
P(i, j) -= K(i) * PH(j); // P is now not symmetric if K is not optimal (e.g.: some gains have been zeroed)
|
||||
}
|
||||
P_temp_H(i) = P(i, state_index) - K(i) * PH(state_index);
|
||||
}
|
||||
|
||||
// Step 2: stabilized update
|
||||
// P (or "P_temp") is not symmetric so we must take the column
|
||||
PH = P.col(state_index);
|
||||
|
||||
for (unsigned i = 0; i < State::size; i++) {
|
||||
for (unsigned j = 0; j <= i; j++) {
|
||||
P(i, j) = P(i, j) - PH(i) * K(j) + K(i) * R * K(j);
|
||||
// P_temp is not symmetric if K is not optimal (e.g.: some gains have been zeroed)
|
||||
const float P_temp = P(i, j) - K(i) * PH(j);
|
||||
P(i, j) = P_temp - P_temp_H(i) * K(j) + K(i) * R * K(j);
|
||||
P(j, i) = P(i, j);
|
||||
}
|
||||
}
|
||||
@@ -1097,6 +1135,12 @@ void Ekf::fuseDirectStateMeasurement(const float innov, const float innov_var, c
|
||||
fuse(K, innov);
|
||||
}
|
||||
|
||||
Ekf::VectorState Ekf::computeKalmanGain(const VectorState &H, const float innov_var) const
|
||||
{
|
||||
// K = P * H / innov_var
|
||||
return multiplySparse(P, H, NonZeroIndices(H)) / innov_var;
|
||||
}
|
||||
|
||||
bool Ekf::measurementUpdate(VectorState &K, const VectorState &H, const float R, const float innovation)
|
||||
{
|
||||
clearInhibitedStateKalmanGains(K);
|
||||
@@ -1118,23 +1162,31 @@ bool Ekf::measurementUpdate(VectorState &K, const VectorState &H, const float R,
|
||||
// = P_temp * (I - H.T * K.T) + K * R * K.T
|
||||
// = P_temp - P_temp * H.T * K.T + K * R * K.T
|
||||
|
||||
// Step 1: conventional update
|
||||
// Compute P_temp and store it in P to avoid allocating more memory
|
||||
// P is symmetric, so PH == H.T * P.T == H.T * P. Taking the row is faster as matrices are row-major
|
||||
VectorState PH = P * H; // H is stored as a column vector. H is in fact H.T
|
||||
// P_temp = P - K * PH^T is never stored: each entry is evaluated where it is used, first in
|
||||
// P_temp * H (only its columns selected by H), then in the stabilized update of the lower
|
||||
// triangle, which is mirrored to the upper one. This is the same arithmetic as storing P_temp first.
|
||||
const VectorState K_local = K; // stores to P cannot alias the gain
|
||||
const NonZeroIndices H_nz(H);
|
||||
const VectorState PH = multiplySparse(P, H, H_nz); // H is stored as a column vector. H is in fact H.T
|
||||
VectorState P_temp_H;
|
||||
|
||||
for (unsigned i = 0; i < State::size; i++) {
|
||||
for (unsigned j = 0; j < State::size; j++) {
|
||||
P(i, j) -= K(i) * PH(j); // P is now not symmetrical if K is not optimal (e.g.: some gains have been zeroed)
|
||||
}
|
||||
}
|
||||
float sum = 0.f;
|
||||
|
||||
// Step 2: stabilized update
|
||||
PH = P * H; // H is stored as a column vector. H is in fact H.T
|
||||
for (unsigned k = 0; k < H_nz.count; k++) {
|
||||
const unsigned j = H_nz.idx[k];
|
||||
const float P_temp = P(i, j) - K_local(i) * PH(j);
|
||||
sum += P_temp * H(j);
|
||||
}
|
||||
|
||||
P_temp_H(i) = sum;
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < State::size; i++) {
|
||||
for (unsigned j = 0; j <= i; j++) {
|
||||
P(i, j) = P(i, j) - PH(i) * K(j) + K(i) * R * K(j);
|
||||
// P_temp is not symmetrical if K is not optimal (e.g.: some gains have been zeroed)
|
||||
const float P_temp = P(i, j) - K_local(i) * PH(j);
|
||||
P(i, j) = P_temp - P_temp_H(i) * K_local(j) + K_local(i) * R * K_local(j);
|
||||
P(j, i) = P(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ def predict_covariance(
|
||||
gyro: sf.V3,
|
||||
gyro_var: sf.Scalar,
|
||||
dt: sf.Scalar
|
||||
) -> MTangent:
|
||||
) -> sf.Matrix:
|
||||
|
||||
state = vstate_to_state(state)
|
||||
g = sf.Symbol("g") # does not appear in the jacobians
|
||||
@@ -216,7 +216,14 @@ def predict_covariance(
|
||||
if index > j:
|
||||
P_new[index,j] = 0
|
||||
|
||||
return P_new
|
||||
# The states after the kinematic ones have an identity process model and no process
|
||||
# noise here, so their rows of the upper triangle are copies of P: only return the rows
|
||||
# up to the last kinematic state and let the implementation keep the rest of P.
|
||||
n = state.tangent_dim()
|
||||
I = sf.Matrix.eye(n)
|
||||
n_predicted = 1 + max(i for i in range(n) if A[i, :] != I[i, :] or G[i, :] != sf.Matrix.zeros(1, G.shape[1]))
|
||||
|
||||
return P_new[:n_predicted, :]
|
||||
|
||||
def jacobian_chain_rule(expr: sf.Scalar , state: State):
|
||||
# First compute the jacobian in the parameter space
|
||||
|
||||
@@ -25,15 +25,15 @@ namespace sym {
|
||||
* dt: Scalar
|
||||
*
|
||||
* Outputs:
|
||||
* res: Matrix24_24
|
||||
* res: Matrix9_24
|
||||
*/
|
||||
template <typename Scalar>
|
||||
matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25, 1>& state,
|
||||
const matrix::Matrix<Scalar, 24, 24>& P,
|
||||
const matrix::Matrix<Scalar, 3, 1>& accel,
|
||||
const matrix::Matrix<Scalar, 3, 1>& accel_var,
|
||||
const matrix::Matrix<Scalar, 3, 1>& gyro,
|
||||
const Scalar gyro_var, const Scalar dt) {
|
||||
matrix::Matrix<Scalar, 9, 24> PredictCovariance(const matrix::Matrix<Scalar, 25, 1>& state,
|
||||
const matrix::Matrix<Scalar, 24, 24>& P,
|
||||
const matrix::Matrix<Scalar, 3, 1>& accel,
|
||||
const matrix::Matrix<Scalar, 3, 1>& accel_var,
|
||||
const matrix::Matrix<Scalar, 3, 1>& gyro,
|
||||
const Scalar gyro_var, const Scalar dt) {
|
||||
// Total ops: 1810
|
||||
|
||||
// Unused inputs
|
||||
@@ -232,7 +232,7 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
P(13, 5) * _tmp132 - P(14, 5) * _tmp130 + P(5, 5);
|
||||
|
||||
// Output terms (1)
|
||||
matrix::Matrix<Scalar, 24, 24> _res;
|
||||
matrix::Matrix<Scalar, 9, 24> _res;
|
||||
|
||||
_res.setZero();
|
||||
|
||||
@@ -357,7 +357,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 9) = P(3, 9) * dt + P(6, 9);
|
||||
_res(7, 9) = P(4, 9) * dt + P(7, 9);
|
||||
_res(8, 9) = P(5, 9) * dt + P(8, 9);
|
||||
_res(9, 9) = P(9, 9);
|
||||
_res(0, 10) = _tmp25;
|
||||
_res(1, 10) = _tmp40;
|
||||
_res(2, 10) = _tmp49;
|
||||
@@ -370,8 +369,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 10) = P(3, 10) * dt + P(6, 10);
|
||||
_res(7, 10) = P(4, 10) * dt + P(7, 10);
|
||||
_res(8, 10) = P(5, 10) * dt + P(8, 10);
|
||||
_res(9, 10) = P(9, 10);
|
||||
_res(10, 10) = P(10, 10);
|
||||
_res(0, 11) = _tmp24;
|
||||
_res(1, 11) = _tmp41;
|
||||
_res(2, 11) = _tmp50;
|
||||
@@ -384,9 +381,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 11) = P(3, 11) * dt + P(6, 11);
|
||||
_res(7, 11) = P(4, 11) * dt + P(7, 11);
|
||||
_res(8, 11) = P(5, 11) * dt + P(8, 11);
|
||||
_res(9, 11) = P(9, 11);
|
||||
_res(10, 11) = P(10, 11);
|
||||
_res(11, 11) = P(11, 11);
|
||||
_res(0, 12) = _tmp53;
|
||||
_res(1, 12) = _tmp81;
|
||||
_res(2, 12) = _tmp85;
|
||||
@@ -396,10 +390,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 12) = P(3, 12) * dt + P(6, 12);
|
||||
_res(7, 12) = P(4, 12) * dt + P(7, 12);
|
||||
_res(8, 12) = P(5, 12) * dt + P(8, 12);
|
||||
_res(9, 12) = P(9, 12);
|
||||
_res(10, 12) = P(10, 12);
|
||||
_res(11, 12) = P(11, 12);
|
||||
_res(12, 12) = P(12, 12);
|
||||
_res(0, 13) = _tmp58;
|
||||
_res(1, 13) = _tmp82;
|
||||
_res(2, 13) = _tmp87;
|
||||
@@ -409,11 +399,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 13) = P(3, 13) * dt + P(6, 13);
|
||||
_res(7, 13) = P(4, 13) * dt + P(7, 13);
|
||||
_res(8, 13) = P(5, 13) * dt + P(8, 13);
|
||||
_res(9, 13) = P(9, 13);
|
||||
_res(10, 13) = P(10, 13);
|
||||
_res(11, 13) = P(11, 13);
|
||||
_res(12, 13) = P(12, 13);
|
||||
_res(13, 13) = P(13, 13);
|
||||
_res(0, 14) = _tmp62;
|
||||
_res(1, 14) = _tmp83;
|
||||
_res(2, 14) = _tmp88;
|
||||
@@ -423,12 +408,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 14) = P(3, 14) * dt + P(6, 14);
|
||||
_res(7, 14) = P(4, 14) * dt + P(7, 14);
|
||||
_res(8, 14) = P(5, 14) * dt + P(8, 14);
|
||||
_res(9, 14) = P(9, 14);
|
||||
_res(10, 14) = P(10, 14);
|
||||
_res(11, 14) = P(11, 14);
|
||||
_res(12, 14) = P(12, 14);
|
||||
_res(13, 14) = P(13, 14);
|
||||
_res(14, 14) = P(14, 14);
|
||||
_res(0, 15) = P(0, 15) * _tmp18 + P(10, 15) * _tmp23 + P(11, 15) * _tmp6 + P(9, 15) * _tmp15;
|
||||
_res(1, 15) = P(1, 15) * _tmp18 + P(10, 15) * _tmp29 + P(11, 15) * _tmp34 + P(9, 15) * _tmp36;
|
||||
_res(2, 15) = P(10, 15) * _tmp45 + P(11, 15) * _tmp44 + P(2, 15) * _tmp18 + P(9, 15) * _tmp47;
|
||||
@@ -441,13 +420,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 15) = P(3, 15) * dt + P(6, 15);
|
||||
_res(7, 15) = P(4, 15) * dt + P(7, 15);
|
||||
_res(8, 15) = P(5, 15) * dt + P(8, 15);
|
||||
_res(9, 15) = P(9, 15);
|
||||
_res(10, 15) = P(10, 15);
|
||||
_res(11, 15) = P(11, 15);
|
||||
_res(12, 15) = P(12, 15);
|
||||
_res(13, 15) = P(13, 15);
|
||||
_res(14, 15) = P(14, 15);
|
||||
_res(15, 15) = P(15, 15);
|
||||
_res(0, 16) = P(0, 16) * _tmp18 + P(10, 16) * _tmp23 + P(11, 16) * _tmp6 + P(9, 16) * _tmp15;
|
||||
_res(1, 16) = P(1, 16) * _tmp18 + P(10, 16) * _tmp29 + P(11, 16) * _tmp34 + P(9, 16) * _tmp36;
|
||||
_res(2, 16) = P(10, 16) * _tmp45 + P(11, 16) * _tmp44 + P(2, 16) * _tmp18 + P(9, 16) * _tmp47;
|
||||
@@ -460,14 +432,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 16) = P(3, 16) * dt + P(6, 16);
|
||||
_res(7, 16) = P(4, 16) * dt + P(7, 16);
|
||||
_res(8, 16) = P(5, 16) * dt + P(8, 16);
|
||||
_res(9, 16) = P(9, 16);
|
||||
_res(10, 16) = P(10, 16);
|
||||
_res(11, 16) = P(11, 16);
|
||||
_res(12, 16) = P(12, 16);
|
||||
_res(13, 16) = P(13, 16);
|
||||
_res(14, 16) = P(14, 16);
|
||||
_res(15, 16) = P(15, 16);
|
||||
_res(16, 16) = P(16, 16);
|
||||
_res(0, 17) = P(0, 17) * _tmp18 + P(10, 17) * _tmp23 + P(11, 17) * _tmp6 + P(9, 17) * _tmp15;
|
||||
_res(1, 17) = P(1, 17) * _tmp18 + P(10, 17) * _tmp29 + P(11, 17) * _tmp34 + P(9, 17) * _tmp36;
|
||||
_res(2, 17) = P(10, 17) * _tmp45 + P(11, 17) * _tmp44 + P(2, 17) * _tmp18 + P(9, 17) * _tmp47;
|
||||
@@ -480,15 +444,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 17) = P(3, 17) * dt + P(6, 17);
|
||||
_res(7, 17) = P(4, 17) * dt + P(7, 17);
|
||||
_res(8, 17) = P(5, 17) * dt + P(8, 17);
|
||||
_res(9, 17) = P(9, 17);
|
||||
_res(10, 17) = P(10, 17);
|
||||
_res(11, 17) = P(11, 17);
|
||||
_res(12, 17) = P(12, 17);
|
||||
_res(13, 17) = P(13, 17);
|
||||
_res(14, 17) = P(14, 17);
|
||||
_res(15, 17) = P(15, 17);
|
||||
_res(16, 17) = P(16, 17);
|
||||
_res(17, 17) = P(17, 17);
|
||||
_res(0, 18) = P(0, 18) * _tmp18 + P(10, 18) * _tmp23 + P(11, 18) * _tmp6 + P(9, 18) * _tmp15;
|
||||
_res(1, 18) = P(1, 18) * _tmp18 + P(10, 18) * _tmp29 + P(11, 18) * _tmp34 + P(9, 18) * _tmp36;
|
||||
_res(2, 18) = P(10, 18) * _tmp45 + P(11, 18) * _tmp44 + P(2, 18) * _tmp18 + P(9, 18) * _tmp47;
|
||||
@@ -501,16 +456,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 18) = P(3, 18) * dt + P(6, 18);
|
||||
_res(7, 18) = P(4, 18) * dt + P(7, 18);
|
||||
_res(8, 18) = P(5, 18) * dt + P(8, 18);
|
||||
_res(9, 18) = P(9, 18);
|
||||
_res(10, 18) = P(10, 18);
|
||||
_res(11, 18) = P(11, 18);
|
||||
_res(12, 18) = P(12, 18);
|
||||
_res(13, 18) = P(13, 18);
|
||||
_res(14, 18) = P(14, 18);
|
||||
_res(15, 18) = P(15, 18);
|
||||
_res(16, 18) = P(16, 18);
|
||||
_res(17, 18) = P(17, 18);
|
||||
_res(18, 18) = P(18, 18);
|
||||
_res(0, 19) = P(0, 19) * _tmp18 + P(10, 19) * _tmp23 + P(11, 19) * _tmp6 + P(9, 19) * _tmp15;
|
||||
_res(1, 19) = P(1, 19) * _tmp18 + P(10, 19) * _tmp29 + P(11, 19) * _tmp34 + P(9, 19) * _tmp36;
|
||||
_res(2, 19) = P(10, 19) * _tmp45 + P(11, 19) * _tmp44 + P(2, 19) * _tmp18 + P(9, 19) * _tmp47;
|
||||
@@ -523,17 +468,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 19) = P(3, 19) * dt + P(6, 19);
|
||||
_res(7, 19) = P(4, 19) * dt + P(7, 19);
|
||||
_res(8, 19) = P(5, 19) * dt + P(8, 19);
|
||||
_res(9, 19) = P(9, 19);
|
||||
_res(10, 19) = P(10, 19);
|
||||
_res(11, 19) = P(11, 19);
|
||||
_res(12, 19) = P(12, 19);
|
||||
_res(13, 19) = P(13, 19);
|
||||
_res(14, 19) = P(14, 19);
|
||||
_res(15, 19) = P(15, 19);
|
||||
_res(16, 19) = P(16, 19);
|
||||
_res(17, 19) = P(17, 19);
|
||||
_res(18, 19) = P(18, 19);
|
||||
_res(19, 19) = P(19, 19);
|
||||
_res(0, 20) = P(0, 20) * _tmp18 + P(10, 20) * _tmp23 + P(11, 20) * _tmp6 + P(9, 20) * _tmp15;
|
||||
_res(1, 20) = P(1, 20) * _tmp18 + P(10, 20) * _tmp29 + P(11, 20) * _tmp34 + P(9, 20) * _tmp36;
|
||||
_res(2, 20) = P(10, 20) * _tmp45 + P(11, 20) * _tmp44 + P(2, 20) * _tmp18 + P(9, 20) * _tmp47;
|
||||
@@ -546,18 +480,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 20) = P(3, 20) * dt + P(6, 20);
|
||||
_res(7, 20) = P(4, 20) * dt + P(7, 20);
|
||||
_res(8, 20) = P(5, 20) * dt + P(8, 20);
|
||||
_res(9, 20) = P(9, 20);
|
||||
_res(10, 20) = P(10, 20);
|
||||
_res(11, 20) = P(11, 20);
|
||||
_res(12, 20) = P(12, 20);
|
||||
_res(13, 20) = P(13, 20);
|
||||
_res(14, 20) = P(14, 20);
|
||||
_res(15, 20) = P(15, 20);
|
||||
_res(16, 20) = P(16, 20);
|
||||
_res(17, 20) = P(17, 20);
|
||||
_res(18, 20) = P(18, 20);
|
||||
_res(19, 20) = P(19, 20);
|
||||
_res(20, 20) = P(20, 20);
|
||||
_res(0, 21) = P(0, 21) * _tmp18 + P(10, 21) * _tmp23 + P(11, 21) * _tmp6 + P(9, 21) * _tmp15;
|
||||
_res(1, 21) = P(1, 21) * _tmp18 + P(10, 21) * _tmp29 + P(11, 21) * _tmp34 + P(9, 21) * _tmp36;
|
||||
_res(2, 21) = P(10, 21) * _tmp45 + P(11, 21) * _tmp44 + P(2, 21) * _tmp18 + P(9, 21) * _tmp47;
|
||||
@@ -570,19 +492,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 21) = P(3, 21) * dt + P(6, 21);
|
||||
_res(7, 21) = P(4, 21) * dt + P(7, 21);
|
||||
_res(8, 21) = P(5, 21) * dt + P(8, 21);
|
||||
_res(9, 21) = P(9, 21);
|
||||
_res(10, 21) = P(10, 21);
|
||||
_res(11, 21) = P(11, 21);
|
||||
_res(12, 21) = P(12, 21);
|
||||
_res(13, 21) = P(13, 21);
|
||||
_res(14, 21) = P(14, 21);
|
||||
_res(15, 21) = P(15, 21);
|
||||
_res(16, 21) = P(16, 21);
|
||||
_res(17, 21) = P(17, 21);
|
||||
_res(18, 21) = P(18, 21);
|
||||
_res(19, 21) = P(19, 21);
|
||||
_res(20, 21) = P(20, 21);
|
||||
_res(21, 21) = P(21, 21);
|
||||
_res(0, 22) = P(0, 22) * _tmp18 + P(10, 22) * _tmp23 + P(11, 22) * _tmp6 + P(9, 22) * _tmp15;
|
||||
_res(1, 22) = P(1, 22) * _tmp18 + P(10, 22) * _tmp29 + P(11, 22) * _tmp34 + P(9, 22) * _tmp36;
|
||||
_res(2, 22) = P(10, 22) * _tmp45 + P(11, 22) * _tmp44 + P(2, 22) * _tmp18 + P(9, 22) * _tmp47;
|
||||
@@ -595,20 +504,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 22) = P(3, 22) * dt + P(6, 22);
|
||||
_res(7, 22) = P(4, 22) * dt + P(7, 22);
|
||||
_res(8, 22) = P(5, 22) * dt + P(8, 22);
|
||||
_res(9, 22) = P(9, 22);
|
||||
_res(10, 22) = P(10, 22);
|
||||
_res(11, 22) = P(11, 22);
|
||||
_res(12, 22) = P(12, 22);
|
||||
_res(13, 22) = P(13, 22);
|
||||
_res(14, 22) = P(14, 22);
|
||||
_res(15, 22) = P(15, 22);
|
||||
_res(16, 22) = P(16, 22);
|
||||
_res(17, 22) = P(17, 22);
|
||||
_res(18, 22) = P(18, 22);
|
||||
_res(19, 22) = P(19, 22);
|
||||
_res(20, 22) = P(20, 22);
|
||||
_res(21, 22) = P(21, 22);
|
||||
_res(22, 22) = P(22, 22);
|
||||
_res(0, 23) = P(0, 23) * _tmp18 + P(10, 23) * _tmp23 + P(11, 23) * _tmp6 + P(9, 23) * _tmp15;
|
||||
_res(1, 23) = P(1, 23) * _tmp18 + P(10, 23) * _tmp29 + P(11, 23) * _tmp34 + P(9, 23) * _tmp36;
|
||||
_res(2, 23) = P(10, 23) * _tmp45 + P(11, 23) * _tmp44 + P(2, 23) * _tmp18 + P(9, 23) * _tmp47;
|
||||
@@ -621,21 +516,6 @@ matrix::Matrix<Scalar, 24, 24> PredictCovariance(const matrix::Matrix<Scalar, 25
|
||||
_res(6, 23) = P(3, 23) * dt + P(6, 23);
|
||||
_res(7, 23) = P(4, 23) * dt + P(7, 23);
|
||||
_res(8, 23) = P(5, 23) * dt + P(8, 23);
|
||||
_res(9, 23) = P(9, 23);
|
||||
_res(10, 23) = P(10, 23);
|
||||
_res(11, 23) = P(11, 23);
|
||||
_res(12, 23) = P(12, 23);
|
||||
_res(13, 23) = P(13, 23);
|
||||
_res(14, 23) = P(14, 23);
|
||||
_res(15, 23) = P(15, 23);
|
||||
_res(16, 23) = P(16, 23);
|
||||
_res(17, 23) = P(17, 23);
|
||||
_res(18, 23) = P(18, 23);
|
||||
_res(19, 23) = P(19, 23);
|
||||
_res(20, 23) = P(20, 23);
|
||||
_res(21, 23) = P(21, 23);
|
||||
_res(22, 23) = P(22, 23);
|
||||
_res(23, 23) = P(23, 23);
|
||||
|
||||
return _res;
|
||||
} // NOLINT(readability/fn_size)
|
||||
|
||||
Reference in new issue
Block a user