2024-10-14 13:22:11 -05:00
|
|
|
package swervelib.math;
|
|
|
|
|
|
|
|
|
|
import edu.wpi.first.util.DoubleCircularBuffer;
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-06 00:10:07 +00:00
|
|
|
* A linear filter that does not calculate() each time a value is added to the DoubleCircularBuffer.
|
2024-10-14 13:22:11 -05:00
|
|
|
*/
|
2024-11-06 00:10:07 +00:00
|
|
|
public class IMULinearMovingAverageFilter
|
|
|
|
|
{
|
2024-10-14 13:22:11 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Circular buffer storing the current IMU readings
|
|
|
|
|
*/
|
|
|
|
|
private final DoubleCircularBuffer m_inputs;
|
|
|
|
|
/**
|
|
|
|
|
* Gain on each reading.
|
|
|
|
|
*/
|
2024-12-09 23:26:04 +00:00
|
|
|
private final double m_inputGain;
|
2024-11-06 00:10:07 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct a linear moving average fitler
|
|
|
|
|
*
|
|
|
|
|
* @param bufferLength The number of values to average across
|
|
|
|
|
*/
|
2024-10-14 13:22:11 -05:00
|
|
|
public IMULinearMovingAverageFilter(int bufferLength)
|
|
|
|
|
{
|
|
|
|
|
m_inputs = new DoubleCircularBuffer(bufferLength);
|
|
|
|
|
m_inputGain = 1.0 / bufferLength;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 00:10:07 +00:00
|
|
|
/**
|
|
|
|
|
* Add a value to the DoubleCircularBuffer
|
|
|
|
|
*
|
|
|
|
|
* @param input Value to add
|
|
|
|
|
*/
|
2024-10-14 13:22:11 -05:00
|
|
|
public void addValue(double input)
|
|
|
|
|
{
|
|
|
|
|
m_inputs.addFirst(input);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 00:10:07 +00:00
|
|
|
/**
|
|
|
|
|
* Calculate the average of the samples in the buffer
|
|
|
|
|
*
|
|
|
|
|
* @return The average of the values in the buffer
|
|
|
|
|
*/
|
2024-10-14 13:22:11 -05:00
|
|
|
public double calculate()
|
|
|
|
|
{
|
|
|
|
|
double returnVal = 0.0;
|
|
|
|
|
|
2024-11-06 00:10:07 +00:00
|
|
|
for (int i = 0; i < m_inputs.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
returnVal += m_inputs.get(i) * m_inputGain;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return returnVal;
|
2024-10-14 13:22:11 -05:00
|
|
|
}
|
|
|
|
|
}
|