Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct off-by-one problem in EKF IMU ringbuffer #26696

Merged
merged 3 commits into from
Apr 10, 2024
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
10 changes: 6 additions & 4 deletions libraries/AP_NavEKF/EKF_Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ bool ekf_imu_buffer::init(uint32_t size)
_size = size;
_youngest = 0;
_oldest = 0;
_filled = false;
return true;
}

Expand All @@ -151,13 +152,14 @@ void ekf_imu_buffer::push_youngest_element(const void *element)
return;
}
// push youngest to the buffer
_youngest = (_youngest+1) % _size;
_youngest++;
if (_youngest == _size) {
_youngest = 0;
_filled = true;
}
memcpy(get_offset(_youngest), element, elsize);
// set oldest data index
_oldest = (_youngest+1) % _size;
if (_oldest == 0) {
_filled = true;
}
}

// retrieve the oldest data from the ring buffer tail
Expand Down
57 changes: 57 additions & 0 deletions libraries/AP_NavEKF/tests/test_ring_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <AP_NavEKF/EKF_Buffer.h>
#include <stdlib.h>

#include <AP_HAL/AP_HAL.h>
const AP_HAL::HAL& hal = AP_HAL::get_HAL();

#if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX

TEST(EKF_Buffer, EKF_Buffer)
Expand Down Expand Up @@ -119,6 +122,60 @@ TEST(EKF_Buffer, EKF_Buffer)
EXPECT_FALSE(buf.recall(d2, 103));
}

TEST(ekf_imu_buffer, one_element_case)
{
// test degenerate 1-element case:
struct element {
uint8_t value;
};
ekf_imu_buffer *b = new ekf_imu_buffer(sizeof(element));
b->init(1); // 1 element
EXPECT_EQ(b->is_filled(), false);
EXPECT_EQ(b->get_oldest_index(), b->get_youngest_index());
const element e { 34 };
b->push_youngest_element((void*)&e);
EXPECT_EQ(b->is_filled(), true);
EXPECT_EQ(b->get_oldest_index(), b->get_youngest_index());
element returned_element {};
b->get_oldest_element((void*)&returned_element);
EXPECT_EQ(e.value, returned_element.value);
element *another_returned_element = (element*)b->get(0);
EXPECT_EQ(e.value, another_returned_element->value);
// we don't do bounds checking, so get here returns non-nullptr:
// another_returned_element = (element*)b.get(17);
// EXPECT_EQ(another_returned_element, nullptr);
b->reset();
EXPECT_EQ(b->get_oldest_index(), b->get_youngest_index());
// we don't do bounds checking, so get here returns non-nullptr:
// another_returned_element = (element*)b.get(0);
// EXPECT_EQ(another_returned_element, nullptr);
}

TEST(ekf_imu_buffer, is_filled)
{
// https://github.com/ArduPilot/ardupilot/issues/25316
struct element {
uint8_t value;
};
ekf_imu_buffer *b = new ekf_imu_buffer(sizeof(element));
b->init(4); // 4 elements
const element e { 34 };

EXPECT_EQ(b->is_filled(), false);

b->push_youngest_element((void*)&e);
EXPECT_EQ(b->is_filled(), false);

b->push_youngest_element((void*)&e);
EXPECT_EQ(b->is_filled(), false);

b->push_youngest_element((void*)&e);
EXPECT_EQ(b->is_filled(), false);

b->push_youngest_element((void*)&e);
EXPECT_EQ(b->is_filled(), true);
}

AP_GTEST_MAIN()

#endif // HAL_SITL or HAL_LINUX
Loading