-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZInertialNavigation.java
66 lines (52 loc) · 1.55 KB
/
ZInertialNavigation.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package frc.libzodiac;
import edu.wpi.first.wpilibj.Timer;
import frc.libzodiac.util.Vec2;
public class ZInertialNavigation {
private final Gyro gyro;
private final Timer timer = new Timer();
private boolean started = false;
private double zero = 0;
private Vec2 pos = new Vec2(0, 0);
private Vec2 speed = new Vec2(0, 0);
public ZInertialNavigation(Gyro gyro) {
this.gyro = gyro;
}
public ZInertialNavigation update() {
if (!this.started) {
this.set_zero();
}
final var loopTime = this.timer.get();
this.timer.reset();
final var acc = this.gyro.getAccelerationNoGravity().rot(-this.getYaw());
this.speed = this.speed.add(acc.mul(loopTime));
Vec2 dis = this.speed.mul(loopTime);
this.pos = this.pos.add(dis);
//debug
ZDashboard.add("posinav", "" + this.getPosition());
ZDashboard.add("yawinav", this.getYaw());
ZDashboard.add("acc", "" + acc);
return this;
}
public ZInertialNavigation set_zero() {
this.set_zero(this.gyro.getYaw());
return this;
}
public double getYaw() {
return this.gyro.getYaw() - this.zero;
}
public Vec2 getPosition() {
return pos;
}
public ZInertialNavigation set_zero(double zero) {
this.zero = zero;
this.started = true;
return this;
}
public Vec2 getSpeed() {
return this.speed;
}
public interface Gyro {
double getYaw();
Vec2 getAccelerationNoGravity();
}
}