2020-11-14 01:06:31 -04:00
|
|
|
/*
|
|
|
|
common EKF Buffer class. This handles the storage buffers for EKF data to
|
|
|
|
bring it onto the fusion time horizon
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "EKF_Buffer.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-11-26 18:36:35 -04:00
|
|
|
#include <AP_InternalError/AP_InternalError.h>
|
2020-11-14 01:06:31 -04:00
|
|
|
|
|
|
|
// constructor
|
|
|
|
ekf_ring_buffer::ekf_ring_buffer(uint8_t _elsize) :
|
2022-05-30 19:33:02 -03:00
|
|
|
elsize(_elsize),
|
|
|
|
buffer(nullptr)
|
2020-11-14 01:06:31 -04:00
|
|
|
{}
|
|
|
|
|
2022-05-30 19:33:02 -03:00
|
|
|
bool ekf_ring_buffer::init(uint8_t _size)
|
2020-11-14 01:06:31 -04:00
|
|
|
{
|
2020-11-16 23:27:38 -04:00
|
|
|
if (buffer) {
|
|
|
|
free(buffer);
|
|
|
|
}
|
2022-05-30 19:33:02 -03:00
|
|
|
buffer = calloc(_size, elsize);
|
2020-11-14 01:06:31 -04:00
|
|
|
if (buffer == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-30 19:33:02 -03:00
|
|
|
size = _size;
|
|
|
|
reset();
|
2020-11-14 01:06:31 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
get buffer offset for an index
|
|
|
|
*/
|
|
|
|
void *ekf_ring_buffer::get_offset(uint8_t idx) const
|
|
|
|
{
|
|
|
|
return (void*)(((uint8_t*)buffer)+idx*uint32_t(elsize));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
get a reference to the timestamp for an index
|
|
|
|
*/
|
2022-05-30 19:33:02 -03:00
|
|
|
uint32_t ekf_ring_buffer::time_ms(uint8_t idx) const
|
2020-11-14 01:06:31 -04:00
|
|
|
{
|
|
|
|
EKF_obs_element_t *el = (EKF_obs_element_t *)get_offset(idx);
|
|
|
|
return el->time_ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Search through a ring buffer and return the newest data that is
|
2022-05-30 19:33:02 -03:00
|
|
|
older than the time specified by sample_time_ms
|
|
|
|
Returns false if no data can be found that is less than 100msec old
|
2020-11-14 01:06:31 -04:00
|
|
|
*/
|
2022-05-30 19:33:02 -03:00
|
|
|
bool ekf_ring_buffer::recall(void *element, const uint32_t sample_time_ms)
|
2020-11-14 01:06:31 -04:00
|
|
|
{
|
2022-05-30 19:33:02 -03:00
|
|
|
bool ret = false;
|
2024-07-04 08:00:45 -03:00
|
|
|
uint8_t best_index = 0; // only valid when ret becomes true
|
2022-05-30 19:33:02 -03:00
|
|
|
while (count > 0) {
|
|
|
|
const uint32_t toldest = time_ms(oldest);
|
|
|
|
const int32_t dt = sample_time_ms - toldest;
|
|
|
|
const bool matches = dt >= 0 && dt < 100;
|
|
|
|
if (matches) {
|
2024-07-04 08:00:45 -03:00
|
|
|
best_index = oldest;
|
2022-05-30 19:33:02 -03:00
|
|
|
ret = true;
|
2020-11-14 01:06:31 -04:00
|
|
|
}
|
2022-05-30 19:33:02 -03:00
|
|
|
if (dt < 0) {
|
|
|
|
// the oldest element is younger than we want, stop
|
|
|
|
// searching and don't consume this element
|
|
|
|
break;
|
2020-11-14 01:06:31 -04:00
|
|
|
}
|
2022-05-30 19:33:02 -03:00
|
|
|
// discard the sample
|
|
|
|
count--;
|
|
|
|
oldest = (oldest+1) % size;
|
2020-11-14 01:06:31 -04:00
|
|
|
}
|
2024-07-04 08:00:45 -03:00
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
memcpy(element, get_offset(best_index), elsize);
|
|
|
|
}
|
|
|
|
|
2022-05-30 19:33:02 -03:00
|
|
|
return ret;
|
2020-11-14 01:06:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Writes data and timestamp to a Ring buffer and advances indices that
|
|
|
|
* define the location of the newest and oldest data
|
|
|
|
*/
|
|
|
|
void ekf_ring_buffer::push(const void *element)
|
|
|
|
{
|
|
|
|
if (buffer == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-30 19:33:02 -03:00
|
|
|
|
2020-11-14 01:06:31 -04:00
|
|
|
// Advance head to next available index
|
2022-05-30 19:33:02 -03:00
|
|
|
const uint8_t head = (oldest+count) % size;
|
|
|
|
|
2020-11-14 01:06:31 -04:00
|
|
|
// New data is written at the head
|
2022-05-30 19:33:02 -03:00
|
|
|
memcpy(get_offset(head), element, elsize);
|
|
|
|
|
|
|
|
if (count < size) {
|
|
|
|
count++;
|
|
|
|
} else {
|
|
|
|
oldest = (oldest+1) % size;
|
|
|
|
}
|
2020-11-14 01:06:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// zeroes all data in the ring buffer
|
|
|
|
void ekf_ring_buffer::reset()
|
|
|
|
{
|
2022-05-30 19:33:02 -03:00
|
|
|
count = 0;
|
|
|
|
oldest = 0;
|
2020-11-14 01:06:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
/*
|
|
|
|
IMU buffer operations implemented separately due to different
|
|
|
|
semantics
|
|
|
|
*/
|
|
|
|
|
|
|
|
// constructor
|
|
|
|
ekf_imu_buffer::ekf_imu_buffer(uint8_t _elsize) :
|
|
|
|
elsize(_elsize)
|
|
|
|
{}
|
|
|
|
|
|
|
|
/*
|
|
|
|
get buffer offset for an index
|
|
|
|
*/
|
|
|
|
void *ekf_imu_buffer::get_offset(uint8_t idx) const
|
|
|
|
{
|
|
|
|
return (void*)(((uint8_t*)buffer)+idx*uint32_t(elsize));
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialise buffer, returns false when allocation has failed
|
|
|
|
bool ekf_imu_buffer::init(uint32_t size)
|
|
|
|
{
|
2020-11-16 23:27:38 -04:00
|
|
|
if (buffer != nullptr) {
|
2020-11-26 18:36:35 -04:00
|
|
|
// allow for init twice
|
2020-11-16 23:27:38 -04:00
|
|
|
free(buffer);
|
|
|
|
}
|
2020-11-14 01:06:31 -04:00
|
|
|
buffer = calloc(size, elsize);
|
|
|
|
if (buffer == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_size = size;
|
|
|
|
_youngest = 0;
|
|
|
|
_oldest = 0;
|
2024-04-04 23:30:03 -03:00
|
|
|
_filled = false;
|
2020-11-14 01:06:31 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Writes data to a Ring buffer and advances indices that
|
|
|
|
define the location of the newest and oldest data
|
|
|
|
*/
|
|
|
|
void ekf_imu_buffer::push_youngest_element(const void *element)
|
|
|
|
{
|
|
|
|
if (!buffer) {
|
2020-11-26 18:36:35 -04:00
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
|
2020-11-14 01:06:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// push youngest to the buffer
|
2024-04-04 23:30:03 -03:00
|
|
|
_youngest++;
|
|
|
|
if (_youngest == _size) {
|
|
|
|
_youngest = 0;
|
|
|
|
_filled = true;
|
|
|
|
}
|
2020-11-14 01:06:31 -04:00
|
|
|
memcpy(get_offset(_youngest), element, elsize);
|
|
|
|
// set oldest data index
|
|
|
|
_oldest = (_youngest+1) % _size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve the oldest data from the ring buffer tail
|
|
|
|
void ekf_imu_buffer::get_oldest_element(void *element)
|
|
|
|
{
|
2020-11-26 18:36:35 -04:00
|
|
|
if (buffer == nullptr) {
|
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
|
|
|
|
memset(element, 0, elsize);
|
|
|
|
} else {
|
|
|
|
memcpy(element, get_offset(_oldest), elsize);
|
|
|
|
}
|
2020-11-14 01:06:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// writes the same data to all elements in the ring buffer
|
|
|
|
void ekf_imu_buffer::reset_history(const void *element)
|
|
|
|
{
|
|
|
|
for (uint8_t index=0; index<_size; index++) {
|
|
|
|
memcpy(get_offset(index), element, elsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// zeroes all data in the ring buffer
|
|
|
|
void ekf_imu_buffer::reset()
|
|
|
|
{
|
|
|
|
_youngest = 0;
|
|
|
|
_oldest = 0;
|
|
|
|
memset(buffer, 0, _size*uint32_t(elsize));
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieves data from the ring buffer at a specified index
|
|
|
|
void *ekf_imu_buffer::get(uint8_t index) const
|
|
|
|
{
|
|
|
|
return get_offset(index);
|
|
|
|
}
|