Repository navigation
Expand file tree
/
Copy pathAimAndDriveCommand.java
More file actions
172 lines (152 loc) · 7.18 KB
/
Copy pathAimAndDriveCommand.java
File metadata and controls
172 lines (152 loc) · 7.18 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package frc.robot.commands;
import static edu.wpi.first.units.Units.Degrees;
import java.util.function.DoubleSupplier;
import com.ctre.phoenix6.swerve.SwerveModule.DriveRequestType;
import com.ctre.phoenix6.swerve.SwerveModule.SteerRequestType;
import com.ctre.phoenix6.swerve.SwerveRequest;
import com.ctre.phoenix6.swerve.SwerveRequest.ForwardPerspectiveValue;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.units.measure.Angle;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.Constants.Driving;
import frc.robot.Landmarks;
import frc.robot.subsystems.CommandSwerveDrivetrain;
import frc.util.DriveInputSmoother;
import frc.util.GeometryUtil;
import frc.util.ManualDriveInput;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.Timer;
import org.littletonrobotics.junction.Logger;
public class AimAndDriveCommand extends Command {
private static final Angle kAimTolerance = Degrees.of(5);
private static final double kDebugPrintIntervalSeconds = 0.5;
private final CommandSwerveDrivetrain swerve;
private final DriveInputSmoother inputSmoother;
private static final double kPoseEdgeMarginMeters = 0.1;
private boolean poseWarningIssued = false;
private double lastDebugPrintTimestamp = 0.0;
private final SwerveRequest.FieldCentricFacingAngle fieldCentricFacingAngleRequest = new SwerveRequest.FieldCentricFacingAngle()
.withRotationalDeadband(Driving.kPIDRotationDeadband)
.withMaxAbsRotationalRate(Driving.kMaxRotationalRate)
.withDriveRequestType(DriveRequestType.OpenLoopVoltage)
.withSteerRequestType(SteerRequestType.MotionMagicExpo)
.withForwardPerspective(ForwardPerspectiveValue.OperatorPerspective)
.withHeadingPID(5, 0, 0);
public AimAndDriveCommand(
CommandSwerveDrivetrain swerve,
DoubleSupplier forwardInput,
DoubleSupplier leftInput
) {
this.swerve = swerve;
this.inputSmoother = new DriveInputSmoother(forwardInput, leftInput);
addRequirements(swerve);
}
public AimAndDriveCommand(CommandSwerveDrivetrain swerve) {
this(swerve, () -> 0, () -> 0);
}
public boolean isAimed() {
if (!currentPoseIsValid()) {
return false;
}
final Rotation2d targetHeading = getTargetHeadingInOperatorPerspective();
final Rotation2d currentHeadingInBlueAlliancePerspective = swerve.getState().Pose.getRotation();
final Rotation2d currentHeadingInOperatorPerspective = currentHeadingInBlueAlliancePerspective.minus(swerve.getOperatorForwardDirection());
return GeometryUtil.isNear(targetHeading, currentHeadingInOperatorPerspective, kAimTolerance);
}
private Rotation2d getTargetHeadingInOperatorPerspective() {
return getTargetHeadingInFieldFrame().minus(swerve.getOperatorForwardDirection());
}
private Rotation2d getTargetHeadingInFieldFrame() {
final Translation2d robotPosition = swerve.getState().Pose.getTranslation();
final Translation2d hubPosition = Landmarks.hubPosition(robotPosition);
return hubPosition.minus(robotPosition).getAngle();
}
private boolean isPoseValid(Pose2d pose) {
if (pose == null) {
return false;
}
final Translation2d translation = pose.getTranslation();
final double x = translation.getX();
final double y = translation.getY();
if (!Double.isFinite(x) || !Double.isFinite(y)) {
return false;
}
return x >= -kPoseEdgeMarginMeters
&& x <= Landmarks.fieldLength + kPoseEdgeMarginMeters
&& y >= -kPoseEdgeMarginMeters
&& y <= Landmarks.fieldWidth + kPoseEdgeMarginMeters;
}
private boolean currentPoseIsValid() {
return isPoseValid(swerve.getState().Pose);
}
@Override
public void initialize() {
poseWarningIssued = false;
ensurePoseValidWithWarning();
}
@Override
public void execute() {
if (!ensurePoseValidWithWarning()) {
swerve.requestIdle();
return;
}
// DEBUG: Print aim diagnostics
final double now = Timer.getFPGATimestamp();
if (now - lastDebugPrintTimestamp >= kDebugPrintIntervalSeconds) {
lastDebugPrintTimestamp = now;
final Pose2d pose = swerve.getState().Pose;
final Translation2d robotPosition = pose.getTranslation();
final Translation2d hubPosition = Landmarks.hubPosition(robotPosition);
final Rotation2d currentHeading = pose.getRotation();
final Rotation2d targetFieldHeading = getTargetHeadingInFieldFrame();
final Rotation2d targetOpHeading = getTargetHeadingInOperatorPerspective();
final Rotation2d operatorForward = swerve.getOperatorForwardDirection();
final double degreesToTurn = targetOpHeading.getDegrees()
- currentHeading.minus(operatorForward).getDegrees();
final String allianceStr = DriverStation.getAlliance()
.map(a -> a.toString())
.orElse("EMPTY (defaulting to Blue!)");
System.out.printf(
"[AimDebug] Alliance=%s | OpForward=%.1f° | Robot=(%.2f, %.2f) heading=%.1f° | Hub=(%.2f, %.2f) | Target(field)=%.1f° Target(op)=%.1f° | Turn=%.1f°%n",
allianceStr, operatorForward.getDegrees(),
pose.getX(), pose.getY(), currentHeading.getDegrees(),
hubPosition.getX(), hubPosition.getY(),
targetFieldHeading.getDegrees(), targetOpHeading.getDegrees(),
degreesToTurn
);
Logger.recordOutput("Aim/Alliance", allianceStr);
Logger.recordOutput("Aim/OpForwardDeg", operatorForward.getDegrees());
Logger.recordOutput("Aim/HubPosition", new Pose2d(hubPosition, new Rotation2d()));
Logger.recordOutput("Aim/TargetFieldHeadingDeg", targetFieldHeading.getDegrees());
Logger.recordOutput("Aim/TargetOpHeadingDeg", targetOpHeading.getDegrees());
Logger.recordOutput("Aim/DegreesToTurn", degreesToTurn);
}
// If you want to use the above for debugging without the robot moving, comment out below
final ManualDriveInput input = inputSmoother.getSmoothedInput();
final Rotation2d targetHeading = getTargetHeadingInOperatorPerspective();
swerve.setControl(
fieldCentricFacingAngleRequest
.withVelocityX(Driving.kMaxSpeed.times(input.forward))
.withVelocityY(Driving.kMaxSpeed.times(input.left))
.withTargetDirection(targetHeading)
);
}
@Override
public boolean isFinished() {
return false;
}
private boolean ensurePoseValidWithWarning() {
final boolean valid = currentPoseIsValid();
if (!valid) {
if (!poseWarningIssued) {
DriverStation.reportWarning("Auto aim blocked: robot pose outside field bounds", false);
poseWarningIssued = true;
}
} else {
poseWarningIssued = false;
}
return valid;
}
}