generated from StuyPulse/Phil
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSwerveDriveDrive.java
83 lines (64 loc) · 2.5 KB
/
SwerveDriveDrive.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.stuypulse.robot.commands.swerve;
import java.util.Optional;
import com.stuypulse.robot.constants.Settings;
import com.stuypulse.robot.constants.Settings.Driver.Drive;
import com.stuypulse.robot.constants.Settings.Driver.Turn;
import com.stuypulse.robot.subsystems.swerve.SwerveDrive;
import com.stuypulse.stuylib.input.Gamepad;
import com.stuypulse.stuylib.math.Angle;
import com.stuypulse.stuylib.math.SLMath;
import com.stuypulse.stuylib.streams.numbers.IStream;
import com.stuypulse.stuylib.streams.numbers.filters.RateLimit;
import com.stuypulse.stuylib.streams.vectors.VStream;
import com.stuypulse.stuylib.streams.vectors.filters.VDeadZone;
import com.stuypulse.stuylib.streams.vectors.filters.VLowPassFilter;
import com.stuypulse.stuylib.streams.vectors.filters.VRateLimit;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.wpilibj2.command.Command;
public class SwerveDriveDrive extends Command {
private SwerveDrive swerve;
private VStream speed;
private IStream turn;
private final Gamepad driver;
private Optional<Rotation2d> holdAngle;
public SwerveDriveDrive(Gamepad driver) {
this.driver = driver;
swerve = SwerveDrive.getInstance();
speed = VStream.create(driver::getLeftStick)
.filtered(
new VDeadZone(Drive.DEADBAND),
x -> x.clamp(1),
x -> Settings.vpow(x, Drive.POWER.get()),
x -> x.mul(Drive.MAX_TELEOP_SPEED.get()),
new VRateLimit(Drive.MAX_TELEOP_ACCEL.get()),
new VLowPassFilter(Drive.RC.get())
);
turn = IStream.create(driver::getRightX)
.filtered(
// x -> SLMath.deadband(x, Turn.DEADBAND.get()),
// x -> SLMath.spow(x, Turn.POWER.get()),
x -> x * Turn.MAX_TELEOP_TURNING.get()
// new RateLimit(Turn.RC.get())
);
holdAngle = Optional.empty();
addRequirements(swerve);
}
@Override
public void initialize() {
holdAngle = Optional.empty();
}
private boolean isWithinTurnDeadband() {
return Math.abs(turn.get()) < Turn.DEADBAND.get();
}
@Override
public void execute() {
double angularVel = turn.get();
if(isWithinTurnDeadband() && holdAngle.isEmpty()){
holdAngle = Optional.of(swerve.getGyroAngle());
}
else {
holdAngle = Optional.empty();
}
swerve.drive(speed.get(), angularVel);
}
}