Browse Source

AP_NavEKF3: use union to alias array and struct access to states

This avoids creating two pointers of different types to the same memory.

Having two pointers to the same memory can lead to the compiler
optimising code such that a write to one pointer is rearranged to be
either before or after a read from the other pointer depending on
which is deemed faster - not a good outcome.
mission-4.1.18
Peter Barker 6 years ago committed by Peter Barker
parent
commit
560f17a570
  1. 2
      libraries/AP_NavEKF3/AP_NavEKF3_core.cpp
  2. 8
      libraries/AP_NavEKF3/AP_NavEKF3_core.h

2
libraries/AP_NavEKF3/AP_NavEKF3_core.cpp

@ -12,8 +12,6 @@ extern const AP_HAL::HAL& hal; @@ -12,8 +12,6 @@ extern const AP_HAL::HAL& hal;
// constructor
NavEKF3_core::NavEKF3_core(void) :
stateStruct(*reinterpret_cast<struct state_elements *>(&statesArray)),
_perf_UpdateFilter(hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "EK3_UpdateFilter")),
_perf_CovariancePrediction(hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "EK3_CovariancePrediction")),
_perf_FuseVelPosNED(hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "EK3_FuseVelPosNED")),

8
libraries/AP_NavEKF3/AP_NavEKF3_core.h

@ -415,7 +415,6 @@ private: @@ -415,7 +415,6 @@ private:
// the states are available in two forms, either as a Vector24, or
// broken down as individual elements. Both are equivalent (same
// memory)
Vector24 statesArray;
struct state_elements {
Quaternion quat; // quaternion defining rotation from local NED earth frame to body frame
Vector3f velocity; // velocity of IMU in local NED earth frame (m/sec)
@ -425,7 +424,12 @@ private: @@ -425,7 +424,12 @@ private:
Vector3f earth_magfield; // earth frame magnetic field vector (Gauss)
Vector3f body_magfield; // body frame magnetic field vector (Gauss)
Vector2f wind_vel; // horizontal North East wind velocity vector in local NED earth frame (m/sec)
} &stateStruct;
};
union {
Vector24 statesArray;
struct state_elements stateStruct;
};
struct output_elements {
Quaternion quat; // quaternion defining rotation from local NED earth frame to body frame

Loading…
Cancel
Save