Skip to content

Commit

Permalink
Updates filters to match WPILib.
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Jan 3, 2016
1 parent f9bb8f4 commit 2180fe5
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 238 deletions.
8 changes: 3 additions & 5 deletions WPILib.Tests/TestCircularBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ public class TestCircularBuffer
private double[] pushBackOut = {342.657, 234.252, 716.126, 132.344, 445.697,
22.727, 421.125, 799.913};

private CircularBuffer<double> queue = new CircularBuffer<double>(8);

[Test]
public void PushFrontTest()
{
queue.Reset();
CircularBuffer<double> queue = new CircularBuffer<double>(8);

foreach(double value in values)
{
Expand All @@ -38,7 +36,7 @@ public void PushFrontTest()
[Test]
public void PushBackTest()
{
queue.Reset();
CircularBuffer<double> queue = new CircularBuffer<double>(8);

foreach (double value in values)
{
Expand All @@ -54,7 +52,7 @@ public void PushBackTest()
[Test]
public void TestPushPop()
{
queue = new CircularBuffer<double>(3);
CircularBuffer<double> queue = new CircularBuffer<double>(3);

queue.PushBack(1.0);
queue.PushBack(2.0);
Expand Down
148 changes: 77 additions & 71 deletions WPILib/CircularBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,138 +1,144 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPILib
namespace WPILib
{
/// <summary>
/// This is a simple circular buffer so we don't need to "bucket brigade"
/// copy old values.
/// </summary>
public class CircularBuffer<T>
{
private T[] m_data;
private readonly T[] m_data;
private int m_front = 0;
private int m_back = 0;
private int m_size = 0;
private int m_length = 0;

public CircularBuffer(int size)
{
m_data = new T[size];
for(int i = 0; i < size; i++)
for (int i = 0; i < size; i++)
{
m_data[i] = default(T);
}
}

/// <summary>
/// Push new value onto front of the buffer. The value at the back
/// is overriden if the buffer is full.
/// </summary>
/// <param name="value">The value to push to the front.</param>
public void PushFront(T value)
{
if (m_data.Length == 0) return;

// If buffer is full, decrement back index so front doesn't underrun it.
if (m_front == m_back && m_size > 0)
{
if (m_front == 0)
{
m_front = m_data.Length - 1;
}
else
{
m_front--;
}

m_back = m_front;
}
else
{
if (m_front == 0)
{
m_front = m_data.Length - 1;
}
else
{
m_front--;
}

m_size++;
}
m_front = ModuloDec(m_front);

m_data[m_front] = value;

if (m_length < m_data.Length)
{
m_length++;
}
}

/// <summary>
/// Push new value onto the back of the buffer. The value at the front is
/// overriden if the buffer is full.
/// </summary>
/// <param name="value"></param>
public void PushBack(T value)
{
if (m_data.Length == 0) return;

m_data[m_back] = value;
m_data[(m_front + m_length) % m_data.Length] = value;

//If buffer is full, advance front index so back doesn't overrun it.
if (m_front == m_back && m_size > 0)
if (m_length < m_data.Length)
{
m_front = (m_front + 1) % m_data.Length;
m_back = m_front;
m_length++;
}
else
{
m_back = (m_back + 1) % m_data.Length;
m_size++;
//Increment front if buffer is full to maintain size.
m_front = ModuloInc(m_front);
}
}

/// <summary>
/// Pop value at front of buffer.
/// </summary>
/// <returns>Value at front of buffer</returns>
public T PopFront()
{
if (m_size == 0) return default(T);
// If there are no elements in the buffer, do nothing
if (m_length == 0)
{

return default(T);
}

m_size--;

T temp = m_data[m_front];
m_front = (m_front + 1) & m_data.Length;
m_front = ModuloInc(m_front);
m_length--;
return temp;
}

/// <summary>
/// Pop value at back of buffer.
/// </summary>
/// <returns>The value at back of buffer.</returns>
public T PopBack()
{
if (m_size == 0) return default(T);

m_size--;

if (m_back == 0)
// If there are no elements in the buffer, do nothing
if (m_length == 0)
{
m_back = m_data.Length - 1;
}
else
{
m_back--;
return default(T);
}

return m_data[m_back];
}

public int Size()
{
return m_size;
m_length--;
return m_data[(m_front + m_length) % m_data.Length];
}

/// <summary>
/// Resets the buffer back to default values.
/// </summary>
public void Reset()
{
for (int i = 0; i < m_data.Length; i++)
{
m_data[i] = default(T);
}

m_front = 0;
m_length = 0;
}

public T this[int i]
/// <summary>
/// Gets the element at index starting from front of buffer.
/// </summary>
/// <param name="i">The index to get</param>
/// <returns>The value at index.</returns>
public T this[int i] => m_data[(m_front + i) % m_data.Length];

/// <summary>
/// Gets the element at index starting from front of buffer.
/// </summary>
/// <param name="index">The index to get</param>
/// <returns>The value at index.</returns>
public T Get(int index) => this[index];

private int ModuloInc(int index)
{
get
{
return m_data[(m_front + i) % m_data.Length];
}
return (index + 1) % m_data.Length;
}

public T Get(int index)
private int ModuloDec(int index)
{
return m_data[(m_front + index) % m_data.Length];
if (index == 0)
{
return m_data.Length - 1;
}
else
{
return index - 1;
}
}
}
}
83 changes: 0 additions & 83 deletions WPILib/Filters/KalmanFilter.cs

This file was deleted.

Loading

0 comments on commit 2180fe5

Please sign in to comment.