57cec006 |
1 | package org.usfirst.frc.team3501.robot.subsystems; |
2 | |
a01ebc25 |
3 | /** |
4 | * This command calculates the average of a set of rpm from the HallEffectSensor |
5 | * class |
6 | * |
7 | * @author shaina |
8 | * |
9 | */ |
57cec006 |
10 | public class HallEffectBuffer { |
11 | int length; |
a24695fb |
12 | int index; |
57cec006 |
13 | double[] bufferVals; |
14 | |
15 | public HallEffectBuffer(int length) { |
16 | this.length = length; |
17 | bufferVals = new double[length]; |
a24695fb |
18 | index = 0; |
57cec006 |
19 | } |
20 | |
21 | public void add(double val) { |
22 | bufferVals[index++] = val; |
23 | if (index >= bufferVals.length) { |
24 | index = 0; |
25 | } |
26 | } |
27 | |
6d1b404a |
28 | /** |
29 | * Gets average buffer value from array of existing buffer vals |
30 | * |
31 | * @return average buffered speed |
32 | */ |
57cec006 |
33 | public double getAverage() { |
34 | double total = 0; |
6d1b404a |
35 | for (int i = 0; i < index; i++) { |
57cec006 |
36 | total += bufferVals[i]; |
37 | } |
a24695fb |
38 | return total / bufferVals.length; |
57cec006 |
39 | } |
40 | } |