mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-02-23 00:04:02 -04:00
AP_NavEKF3: moved intermediate variables to common memory
this moves intermediate variables from being per-core to being common between cores. This saves memory on systems with more than one core by avoiding allocating this memory on every core. This is an alternative to #11717 which moves memory onto the stack. It doesn't save as much memory as #11717, but avoids creating large stack frames
This commit is contained in:
parent
a48d97c307
commit
6ffffff8c6
@ -662,7 +662,7 @@ bool NavEKF2::InitialiseFilter(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// allocate common intermediate variable space
|
// allocate common intermediate variable space
|
||||||
core_common = (struct CoreCommon *)hal.util->malloc_type(NavEKF2_core::get_core_common_size(), AP_HAL::Util::MEM_FAST);
|
core_common = (void *)hal.util->malloc_type(NavEKF2_core::get_core_common_size(), AP_HAL::Util::MEM_FAST);
|
||||||
if (core_common == nullptr) {
|
if (core_common == nullptr) {
|
||||||
_enable.set(0);
|
_enable.set(0);
|
||||||
hal.util->free_type(core, sizeof(NavEKF2_core)*num_cores, AP_HAL::Util::MEM_FAST);
|
hal.util->free_type(core, sizeof(NavEKF2_core)*num_cores, AP_HAL::Util::MEM_FAST);
|
||||||
|
@ -683,6 +683,16 @@ bool NavEKF3::InitialiseFilter(void)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allocate common intermediate variable space
|
||||||
|
core_common = (void *)hal.util->malloc_type(NavEKF3_core::get_core_common_size(), AP_HAL::Util::MEM_FAST);
|
||||||
|
if (core_common == nullptr) {
|
||||||
|
_enable.set(0);
|
||||||
|
hal.util->free_type(core, sizeof(NavEKF3_core)*num_cores, AP_HAL::Util::MEM_FAST);
|
||||||
|
core = nullptr;
|
||||||
|
gcs().send_text(MAV_SEVERITY_CRITICAL, "NavEKF3: common allocation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Call constructors on all cores
|
// Call constructors on all cores
|
||||||
for (uint8_t i = 0; i < num_cores; i++) {
|
for (uint8_t i = 0; i < num_cores; i++) {
|
||||||
new (&core[i]) NavEKF3_core(this);
|
new (&core[i]) NavEKF3_core(this);
|
||||||
|
@ -496,6 +496,11 @@ private:
|
|||||||
// time of last lane switch
|
// time of last lane switch
|
||||||
uint32_t lastLaneSwitch_ms;
|
uint32_t lastLaneSwitch_ms;
|
||||||
|
|
||||||
|
/*
|
||||||
|
common intermediate variables used by all cores
|
||||||
|
*/
|
||||||
|
void *core_common;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
uint32_t last_function_call; // last time getLastYawYawResetAngle was called
|
uint32_t last_function_call; // last time getLastYawYawResetAngle was called
|
||||||
bool core_changed; // true when a core change happened and hasn't been consumed, false otherwise
|
bool core_changed; // true when a core change happened and hasn't been consumed, false otherwise
|
||||||
|
@ -20,7 +20,13 @@ NavEKF3_core::NavEKF3_core(NavEKF3 *_frontend) :
|
|||||||
_perf_TerrainOffset(hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "EK3_TerrainOffset")),
|
_perf_TerrainOffset(hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "EK3_TerrainOffset")),
|
||||||
_perf_FuseOptFlow(hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "EK3_FuseOptFlow")),
|
_perf_FuseOptFlow(hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "EK3_FuseOptFlow")),
|
||||||
_perf_FuseBodyOdom(hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "EK3_FuseBodyOdom")),
|
_perf_FuseBodyOdom(hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "EK3_FuseBodyOdom")),
|
||||||
frontend(_frontend)
|
frontend(_frontend),
|
||||||
|
// setup the intermediate variables shared by all cores (to save memory)
|
||||||
|
common((struct core_common *)_frontend->core_common),
|
||||||
|
Kfusion(common->Kfusion),
|
||||||
|
KH(common->KH),
|
||||||
|
KHP(common->KHP),
|
||||||
|
nextP(common->nextP)
|
||||||
{
|
{
|
||||||
_perf_test[0] = hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "EK3_Test0");
|
_perf_test[0] = hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "EK3_Test0");
|
||||||
_perf_test[1] = hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "EK3_Test1");
|
_perf_test[1] = hal.util->perf_alloc(AP_HAL::Util::PC_ELAPSED, "EK3_Test1");
|
||||||
@ -211,9 +217,7 @@ void NavEKF3_core::InitialiseVariables()
|
|||||||
lastKnownPositionNE.zero();
|
lastKnownPositionNE.zero();
|
||||||
prevTnb.zero();
|
prevTnb.zero();
|
||||||
memset(&P[0][0], 0, sizeof(P));
|
memset(&P[0][0], 0, sizeof(P));
|
||||||
memset(&KH[0][0], 0, sizeof(KH));
|
memset(common, 0, sizeof(*common));
|
||||||
memset(&KHP[0][0], 0, sizeof(KHP));
|
|
||||||
memset(&nextP[0][0], 0, sizeof(nextP));
|
|
||||||
flowDataValid = false;
|
flowDataValid = false;
|
||||||
rangeDataToFuse = false;
|
rangeDataToFuse = false;
|
||||||
Popt = 0.0f;
|
Popt = 0.0f;
|
||||||
@ -569,6 +573,12 @@ void NavEKF3_core::CovarianceInit()
|
|||||||
// Update Filter States - this should be called whenever new IMU data is available
|
// Update Filter States - this should be called whenever new IMU data is available
|
||||||
void NavEKF3_core::UpdateFilter(bool predict)
|
void NavEKF3_core::UpdateFilter(bool predict)
|
||||||
{
|
{
|
||||||
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
||||||
|
// fill the common variables with NaN, so we catch any cases in
|
||||||
|
// SITL where they are used without initialisation
|
||||||
|
fill_nanf((float *)common, sizeof(*common)/sizeof(float));
|
||||||
|
#endif
|
||||||
|
|
||||||
// Set the flag to indicate to the filter that the front-end has given permission for a new state prediction cycle to be started
|
// Set the flag to indicate to the filter that the front-end has given permission for a new state prediction cycle to be started
|
||||||
startPredictEnabled = predict;
|
startPredictEnabled = predict;
|
||||||
|
|
||||||
|
@ -367,6 +367,9 @@ public:
|
|||||||
// get timing statistics structure
|
// get timing statistics structure
|
||||||
void getTimingStatistics(struct ekf_timing &timing);
|
void getTimingStatistics(struct ekf_timing &timing);
|
||||||
|
|
||||||
|
// return size of core_common for use in frontend allocation
|
||||||
|
static uint32_t get_core_common_size(void) { return sizeof(core_common); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Reference to the global EKF frontend for parameters
|
// Reference to the global EKF frontend for parameters
|
||||||
NavEKF3 *frontend;
|
NavEKF3 *frontend;
|
||||||
@ -532,6 +535,21 @@ private:
|
|||||||
uint8_t type; // type specifiying Euler rotation order used, 1 = 312 (ZXY), 2 = 321 (ZYX)
|
uint8_t type; // type specifiying Euler rotation order used, 1 = 312 (ZXY), 2 = 321 (ZYX)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
common intermediate variables used by all cores. These save a
|
||||||
|
lot memory by avoiding allocating these arrays on every core
|
||||||
|
Having these as stack variables would save even more memory, but
|
||||||
|
would give us very high stack usage in some functions, which
|
||||||
|
poses a risk of stack overflow until we have infrastructure in
|
||||||
|
place to calculate maximum stack usage using static analysis.
|
||||||
|
*/
|
||||||
|
struct core_common {
|
||||||
|
Vector28 Kfusion;
|
||||||
|
Matrix24 KH;
|
||||||
|
Matrix24 KHP;
|
||||||
|
Matrix24 nextP;
|
||||||
|
} *common;
|
||||||
|
|
||||||
// bias estimates for the IMUs that are enabled but not being used
|
// bias estimates for the IMUs that are enabled but not being used
|
||||||
// by this core.
|
// by this core.
|
||||||
struct {
|
struct {
|
||||||
@ -854,6 +872,9 @@ private:
|
|||||||
bool badIMUdata; // boolean true if the bad IMU data is detected
|
bool badIMUdata; // boolean true if the bad IMU data is detected
|
||||||
|
|
||||||
float gpsNoiseScaler; // Used to scale the GPS measurement noise and consistency gates to compensate for operation with small satellite counts
|
float gpsNoiseScaler; // Used to scale the GPS measurement noise and consistency gates to compensate for operation with small satellite counts
|
||||||
|
Vector28 &Kfusion; // Kalman gain vector
|
||||||
|
Matrix24 &KH; // intermediate result used for covariance updates
|
||||||
|
Matrix24 &KHP; // intermediate result used for covariance updates
|
||||||
Matrix24 P; // covariance matrix
|
Matrix24 P; // covariance matrix
|
||||||
imu_ring_buffer_t<imu_elements> storedIMU; // IMU data buffer
|
imu_ring_buffer_t<imu_elements> storedIMU; // IMU data buffer
|
||||||
obs_ring_buffer_t<gps_elements> storedGPS; // GPS data buffer
|
obs_ring_buffer_t<gps_elements> storedGPS; // GPS data buffer
|
||||||
@ -907,6 +928,7 @@ private:
|
|||||||
bool allMagSensorsFailed; // true if all magnetometer sensors have timed out on this flight and we are no longer using magnetometer data
|
bool allMagSensorsFailed; // true if all magnetometer sensors have timed out on this flight and we are no longer using magnetometer data
|
||||||
uint32_t lastSynthYawTime_ms; // time stamp when synthetic yaw measurement was last fused to maintain covariance health (msec)
|
uint32_t lastSynthYawTime_ms; // time stamp when synthetic yaw measurement was last fused to maintain covariance health (msec)
|
||||||
uint32_t ekfStartTime_ms; // time the EKF was started (msec)
|
uint32_t ekfStartTime_ms; // time the EKF was started (msec)
|
||||||
|
Matrix24 &nextP; // Predicted covariance matrix before addition of process noise to diagonals
|
||||||
Vector2f lastKnownPositionNE; // last known position
|
Vector2f lastKnownPositionNE; // last known position
|
||||||
uint32_t lastDecayTime_ms; // time of last decay of GPS position offset
|
uint32_t lastDecayTime_ms; // time of last decay of GPS position offset
|
||||||
float velTestRatio; // sum of squares of GPS velocity innovation divided by fail threshold
|
float velTestRatio; // sum of squares of GPS velocity innovation divided by fail threshold
|
||||||
|
Loading…
Reference in New Issue
Block a user