Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/state_estimation/GroundLevelEstimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class GroundLevelEstimator{
* Stops recording ground level measurements and freezes the EGL.
* Should be called once when launch is confirmed.
*/
void launchDeteched();
void launchDetected();

/**
* @brief Gets the estimated ground level.
Expand All @@ -49,6 +49,7 @@ class GroundLevelEstimator{
bool launched = false; //Turned true if launch is detected
float estimatedGroundLevel_m = 0.0F; //EGL in meters
uint32_t sampleCount = 0; //Number of samples used for ground level estimate
float alpha; //Determines how much weight the most recent number added has on the current EGL

};

Expand Down
14 changes: 9 additions & 5 deletions src/state_estimation/GroundLevelEstimator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

// Constructor
GroundLevelEstimator::GroundLevelEstimator()
: launched(false), estimatedGroundLevel_m(0.0F), sampleCount(0)
: launched(false), estimatedGroundLevel_m(0.0F), sampleCount(0), alpha(0.1F)
{}

// Update the ground level estimate or convert ASL to AGL - Altitude ABOVE ground level
float GroundLevelEstimator::update(float currentASL_m) {

// Before launch: accumulate samples to estimate ground level
if (!launched) {
// Running average of ground level samples
estimatedGroundLevel_m = ((estimatedGroundLevel_m * sampleCount) + currentASL_m) / (sampleCount + 1);
if (sampleCount == 0) {
// Initialize with first sample
estimatedGroundLevel_m = currentASL_m;
} else {
// Exponential moving average: EMA = alpha * newValue + (1 - alpha) * oldEMA
estimatedGroundLevel_m = (alpha * currentASL_m) + ((1.0F - alpha) * estimatedGroundLevel_m);
}
sampleCount++;

// Still on ground, so AGL is 0
Expand All @@ -23,7 +27,7 @@ float GroundLevelEstimator::update(float currentASL_m) {
}

// Signal that launch has been detected
void GroundLevelEstimator::launchDeteched() {
void GroundLevelEstimator::launchDetected() {
launched = true;
// Ground level estimate is now frozen at estimatedGroundLevel_m
}
Expand Down