From b52e38eb9dfe0af35caefe4c1528699b51ee515e Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 7 Dec 2015 22:40:20 +0100 Subject: [PATCH] fix --- EKF/RingBuffer.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/EKF/RingBuffer.h b/EKF/RingBuffer.h index b2a6e83350..04a7908846 100644 --- a/EKF/RingBuffer.h +++ b/EKF/RingBuffer.h @@ -48,6 +48,7 @@ public: { _buffer = NULL; _head = _tail = _size = 0; + _first_write = true; } ~RingBuffer() {delete _buffer;} @@ -68,6 +69,7 @@ public: } _size = size; + _first_write = true; return true; } @@ -78,13 +80,24 @@ public: _time_last = sample.time_us; } - int head_new = (_head + 1) % _size; + int head_new = _head; + + if (_first_write) { + head_new = _head; + + } else { + head_new = (_head + 1) % _size; + } + _buffer[head_new] = sample; _head = head_new; // move tail if we overwrite it - if (_head == _tail && _size > 1) { + if (_head == _tail && !_first_write) { _tail = (_tail + 1) % _size; + + } else { + _first_write = false; } } @@ -113,6 +126,7 @@ public: // since we don't want to have any older data in the buffer if (index == _head) { _tail = _head; + _first_write = true; } else { _tail = (index + 1) % _size; @@ -140,6 +154,7 @@ public: private: data_type *_buffer; unsigned _head, _tail, _size; + bool _first_write; // debug uint64_t _time_last;