2e56ed00684f9d2b875d6a80f2e67994cdd46a3b
[3501/2017steamworks] / src / org / usfirst / frc / team3501 / robot / subsystems / HallEffectBuffer.java
1 package org.usfirst.frc.team3501.robot.subsystems;
2
3 public class HallEffectBuffer {
4 int length;
5 int index;
6 double[] bufferVals;
7
8 public HallEffectBuffer(int length) {
9 this.length = length;
10 bufferVals = new double[length];
11 index = 0;
12 }
13
14 public void add(double val) {
15 bufferVals[index++] = val;
16 if (index >= bufferVals.length) {
17 index = 0;
18 }
19 }
20
21 /**
22 * Gets average buffer value from array of existing buffer vals
23 *
24 * @return average buffered speed
25 */
26 public double getAverage() {
27 double total = 0;
28 for (int i = 0; i < index; i++) {
29 total += bufferVals[i];
30 }
31 return total / bufferVals.length;
32 }
33 }