generated from Patribots4738/Swerve-Command-Based
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDrive.java
101 lines (80 loc) · 3.06 KB
/
Drive.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package frc.robot.commands.drive;
import java.util.function.BooleanSupplier;
import java.util.function.DoubleSupplier;
import java.util.function.Supplier;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.Robot;
import frc.robot.Robot.GameMode;
import frc.robot.subsystems.Swerve;
import frc.robot.util.Constants.DriveConstants;
public class Drive extends Command {
private final Swerve swerve;
private final DoubleSupplier xSupplier;
private final DoubleSupplier ySupplier;
private final DoubleSupplier rotationSupplier;
private final BooleanSupplier fieldRelativeSupplier;
private final BooleanSupplier shouldMirror;
private double driveMultiplier = 1;
public Drive(
Swerve swerve,
DoubleSupplier xSupplier,
DoubleSupplier ySupplier,
DoubleSupplier rotationsSupplier,
BooleanSupplier fieldRelativeSupplier,
BooleanSupplier shouldMirror) {
this.swerve = swerve;
this.xSupplier = xSupplier;
this.ySupplier = ySupplier;
this.rotationSupplier = rotationsSupplier;
this.fieldRelativeSupplier = fieldRelativeSupplier;
this.shouldMirror = shouldMirror;
addRequirements(swerve);
}
public Drive(Swerve swerve, Supplier<ChassisSpeeds> speeds, BooleanSupplier fieldRelativeSupplier, BooleanSupplier shouldMirror) {
this.swerve = swerve;
this.xSupplier = () -> speeds.get().vyMetersPerSecond;
this.ySupplier = () -> speeds.get().vxMetersPerSecond;
this.rotationSupplier = () -> speeds.get().omegaRadiansPerSecond;
this.fieldRelativeSupplier = fieldRelativeSupplier;
this.shouldMirror = shouldMirror;
addRequirements(swerve);
}
@Override
public void initialize() {
}
@Override
public void execute() {
double x = xSupplier.getAsDouble();
// The driver's right is negative
// on the field's axis
double y = -ySupplier.getAsDouble();
double rotation = rotationSupplier.getAsDouble();
if (shouldMirror.getAsBoolean() || !fieldRelativeSupplier.getAsBoolean()) {
x *= -1;
y *= -1;
}
if (x + y + rotation == 0 && Robot.gameMode == GameMode.TELEOP) {
swerve.setWheelsX();
}
else {
Swerve.desiredChassisSpeeds = new ChassisSpeeds(x, y, rotation);
swerve.drive(
x * DriveConstants.MAX_SPEED_METERS_PER_SECOND * driveMultiplier,
y * DriveConstants.MAX_SPEED_METERS_PER_SECOND * driveMultiplier,
rotation * DriveConstants.MAX_ANGULAR_SPEED_RADS_PER_SECOND * driveMultiplier,
fieldRelativeSupplier.getAsBoolean());
}
}
@Override
public void end(boolean interrupted) {
swerve.drive(0, 0, 0, false);
}
public void toggleDriveMultiplier() {
driveMultiplier = (driveMultiplier == 1) ? 0.5 : 1;
}
@Override
public boolean isFinished() {
return false;
}
}