2015-11-19 13:08:04 -04:00
|
|
|
/****************************************************************************
|
|
|
|
*
|
2015-12-06 07:55:11 -04:00
|
|
|
* Copyright (C) 2015 PX4 Development Team. All rights reserved.
|
2015-11-19 13:08:04 -04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name PX4 nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file RingBuffer.h
|
|
|
|
* @author Roman Bapst <bapstroman@gmail.com>
|
|
|
|
* Template RingBuffer.
|
|
|
|
*/
|
|
|
|
|
2016-01-19 02:19:10 -04:00
|
|
|
#include <inttypes.h>
|
2015-12-06 07:55:11 -04:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
|
2015-11-19 13:08:04 -04:00
|
|
|
template <typename data_type>
|
|
|
|
class RingBuffer
|
|
|
|
{
|
|
|
|
public:
|
2018-05-03 15:14:12 -03:00
|
|
|
RingBuffer()
|
|
|
|
{
|
2017-11-16 14:09:17 -04:00
|
|
|
if (allocate(1)) {
|
|
|
|
// initialize with one empty sample
|
|
|
|
data_type d = {};
|
|
|
|
push(d);
|
|
|
|
}
|
|
|
|
}
|
2016-01-31 03:48:41 -04:00
|
|
|
~RingBuffer() { delete[] _buffer; }
|
2015-11-19 13:08:04 -04:00
|
|
|
|
2017-11-14 21:14:33 -04:00
|
|
|
// no copy, assignment, move, move assignment
|
|
|
|
RingBuffer(const RingBuffer &) = delete;
|
|
|
|
RingBuffer &operator=(const RingBuffer &) = delete;
|
|
|
|
RingBuffer(RingBuffer &&) = delete;
|
|
|
|
RingBuffer &operator=(RingBuffer &&) = delete;
|
2015-11-19 13:08:04 -04:00
|
|
|
|
2017-11-14 21:14:33 -04:00
|
|
|
bool allocate(uint8_t size)
|
|
|
|
{
|
|
|
|
if (_buffer != nullptr) {
|
2016-01-31 03:48:41 -04:00
|
|
|
delete[] _buffer;
|
2015-12-06 07:55:11 -04:00
|
|
|
}
|
|
|
|
|
2015-11-19 13:08:04 -04:00
|
|
|
_buffer = new data_type[size];
|
|
|
|
|
2017-11-14 21:14:33 -04:00
|
|
|
if (_buffer == nullptr) {
|
2015-11-19 13:08:04 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_size = size;
|
2017-11-14 21:14:33 -04:00
|
|
|
|
|
|
|
_head = 0;
|
|
|
|
_tail = 0;
|
|
|
|
|
2016-04-05 22:53:14 -03:00
|
|
|
// set the time elements to zero so that bad data is not retrieved from the buffers
|
2017-11-16 14:09:17 -04:00
|
|
|
for (uint8_t index = 0; index < _size; index++) {
|
|
|
|
_buffer[index] = {};
|
2016-04-05 22:53:14 -03:00
|
|
|
}
|
2018-05-03 15:14:12 -03:00
|
|
|
|
2015-12-07 17:40:20 -04:00
|
|
|
_first_write = true;
|
2017-11-14 21:14:33 -04:00
|
|
|
|
2015-11-19 13:08:04 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-03 15:14:12 -03:00
|
|
|
void unallocate()
|
|
|
|
{
|
2017-11-14 21:14:33 -04:00
|
|
|
delete[] _buffer;
|
|
|
|
_buffer = nullptr;
|
2016-01-31 03:48:41 -04:00
|
|
|
}
|
|
|
|
|
2018-05-03 15:14:12 -03:00
|
|
|
void push(const data_type &sample)
|
2015-11-19 13:08:04 -04:00
|
|
|
{
|
2017-11-14 21:14:33 -04:00
|
|
|
uint8_t head_new = _head;
|
2015-12-07 17:40:20 -04:00
|
|
|
|
2017-11-14 21:14:33 -04:00
|
|
|
if (!_first_write) {
|
2015-12-07 17:40:20 -04:00
|
|
|
head_new = (_head + 1) % _size;
|
|
|
|
}
|
|
|
|
|
2015-12-06 07:55:11 -04:00
|
|
|
_buffer[head_new] = sample;
|
|
|
|
_head = head_new;
|
2015-11-19 13:08:04 -04:00
|
|
|
|
|
|
|
// move tail if we overwrite it
|
2015-12-07 17:40:20 -04:00
|
|
|
if (_head == _tail && !_first_write) {
|
2015-11-19 13:08:04 -04:00
|
|
|
_tail = (_tail + 1) % _size;
|
2015-12-07 17:40:20 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
_first_write = false;
|
2015-11-19 13:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:14:33 -04:00
|
|
|
uint8_t get_length() const { return _size; }
|
2015-11-19 13:08:04 -04:00
|
|
|
|
2017-11-14 21:14:33 -04:00
|
|
|
data_type &operator[](const uint8_t index) { return _buffer[index]; }
|
2017-04-22 20:04:00 -03:00
|
|
|
|
2018-05-03 15:14:12 -03:00
|
|
|
const data_type &get_newest() { return _buffer[_head]; }
|
|
|
|
const data_type &get_oldest() { return _buffer[_tail]; }
|
2015-12-06 07:55:11 -04:00
|
|
|
|
2017-11-14 21:14:33 -04:00
|
|
|
uint8_t get_oldest_index() const { return _tail; }
|
|
|
|
|
2018-05-03 15:14:12 -03:00
|
|
|
bool pop_first_older_than(const uint64_t ×tamp, data_type *sample)
|
2015-11-19 13:08:04 -04:00
|
|
|
{
|
2015-12-06 07:55:11 -04:00
|
|
|
// start looking from newest observation data
|
2017-11-14 21:14:33 -04:00
|
|
|
for (uint8_t i = 0; i < _size; i++) {
|
2015-12-06 07:55:11 -04:00
|
|
|
int index = (_head - i);
|
|
|
|
index = index < 0 ? _size + index : index;
|
|
|
|
|
2017-11-08 06:40:10 -04:00
|
|
|
if (timestamp >= _buffer[index].time_us && timestamp - _buffer[index].time_us < (uint64_t)1e5) {
|
2015-12-06 07:55:11 -04:00
|
|
|
|
2017-11-14 21:14:33 -04:00
|
|
|
*sample = _buffer[index];
|
2015-12-06 07:55:11 -04:00
|
|
|
|
|
|
|
// Now we can set the tail to the item which comes after the one we removed
|
|
|
|
// since we don't want to have any older data in the buffer
|
2017-11-14 21:14:33 -04:00
|
|
|
if (index == _head) {
|
2015-12-06 07:55:11 -04:00
|
|
|
_tail = _head;
|
2015-12-07 17:40:20 -04:00
|
|
|
_first_write = true;
|
2015-12-06 07:55:11 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
_tail = (index + 1) % _size;
|
|
|
|
}
|
|
|
|
|
|
|
|
_buffer[index].time_us = 0;
|
2015-11-19 13:08:04 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:14:33 -04:00
|
|
|
if (index == _tail) {
|
2015-12-06 07:55:11 -04:00
|
|
|
// we have reached the tail and haven't got a match
|
2015-11-19 13:08:04 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-16 14:36:03 -04:00
|
|
|
int get_total_size() { return sizeof(*this) + sizeof(data_type) * _size; }
|
|
|
|
|
2015-11-19 13:08:04 -04:00
|
|
|
private:
|
2017-11-14 21:14:33 -04:00
|
|
|
data_type *_buffer{nullptr};
|
|
|
|
|
|
|
|
uint8_t _head{0};
|
|
|
|
uint8_t _tail{0};
|
|
|
|
uint8_t _size{0};
|
2015-12-06 07:55:11 -04:00
|
|
|
|
2017-11-14 21:14:33 -04:00
|
|
|
bool _first_write{true};
|
2015-12-06 07:55:11 -04:00
|
|
|
};
|