-
Notifications
You must be signed in to change notification settings - Fork 6
/
MovingAverageTwist2d.java
50 lines (38 loc) · 1.06 KB
/
MovingAverageTwist2d.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.team254.lib.util;
import com.team254.lib.geometry.Twist2d;
import java.util.ArrayList;
/**
* Helper class for storing and calculating a moving average of the Twist2d class
*/
public class MovingAverageTwist2d {
ArrayList<Twist2d> twists = new ArrayList<Twist2d>();
private int maxSize;
public MovingAverageTwist2d(int maxSize) {
this.maxSize = maxSize;
}
public synchronized void add(Twist2d twist) {
twists.add(twist);
if (twists.size() > maxSize) {
twists.remove(0);
}
}
public synchronized Twist2d getAverage() {
double x = 0.0, y = 0.0, t = 0.0;
for (Twist2d twist : twists) {
x += twist.dx;
y += twist.dy;
t += twist.dtheta;
}
double size = getSize();
return new Twist2d(x / size, y / size, t / size);
}
public int getSize() {
return twists.size();
}
public boolean isUnderMaxSize() {
return getSize() < maxSize;
}
public void clear() {
twists.clear();
}
}