|
4 | 4 | import edu.wpi.first.math.geometry.Pose2d; |
5 | 5 | import edu.wpi.first.math.geometry.Rotation2d; |
6 | 6 | import edu.wpi.first.math.geometry.Translation2d; |
| 7 | +import edu.wpi.first.math.util.Units; |
7 | 8 | import edu.wpi.first.wpilibj2.command.Command; |
| 9 | +import frc.robot.FieldConstants.Barge; |
8 | 10 | import frc.robot.FieldConstants.CoralStation; |
9 | 11 | import frc.robot.FieldConstants.Reef; |
10 | 12 | import frc.robot.commands.controllers.JoystickInputController; |
11 | 13 | import frc.robot.commands.controllers.SpeedLevelController.SpeedLevel; |
12 | 14 | import frc.robot.subsystems.drive.Drive; |
13 | 15 | import frc.robot.utility.AllianceFlipUtil; |
| 16 | +import java.util.List; |
14 | 17 | import java.util.Optional; |
| 18 | +import java.util.stream.Stream; |
15 | 19 |
|
16 | 20 | public class ManualAlignCommands { |
| 21 | + |
17 | 22 | public static Command alignToCage(Drive drivetrain, JoystickInputController input) { |
18 | 23 | return DriveCommands.joystickHeadingDrive( |
19 | 24 | drivetrain, |
20 | 25 | input::getTranslationMetersPerSecond, |
21 | | - () -> Optional.of(AllianceFlipUtil.apply(Rotation2d.kZero)), |
| 26 | + () -> Optional.of(allianceRotation().plus(Rotation2d.k180deg)), |
| 27 | + () -> SpeedLevel.NO_LEVEL, |
| 28 | + () -> true); |
| 29 | + } |
| 30 | + |
| 31 | + public static Command alignToCageAdv(Drive drivetrain, JoystickInputController input) { |
| 32 | + return DriveCommands.joystickHeadingDrive( |
| 33 | + drivetrain, |
| 34 | + input::getTranslationMetersPerSecond, |
| 35 | + () -> { |
| 36 | + Translation2d robotPose = drivetrain.getRobotPose().getTranslation(); |
| 37 | + |
| 38 | + List<Translation2d> cagePose = |
| 39 | + Stream.of(Barge.farCage, Barge.middleCage, Barge.closeCage) |
| 40 | + .map(AllianceFlipUtil::apply) |
| 41 | + .toList(); |
| 42 | + |
| 43 | + return Optional.of( |
| 44 | + robotPose.minus(robotPose.nearest(cagePose)).getAngle().plus(allianceRotation())); |
| 45 | + }, |
22 | 46 | () -> SpeedLevel.NO_LEVEL, |
23 | 47 | () -> true); |
24 | 48 | } |
@@ -49,22 +73,23 @@ public static Command alignToReef(Drive drivetrain, JoystickInputController inpu |
49 | 73 | Translation2d robotPose = drivetrain.getRobotPose().getTranslation(); |
50 | 74 | Translation2d reefPose = AllianceFlipUtil.apply(Reef.center); |
51 | 75 |
|
52 | | - double dx = robotPose.getX() - reefPose.getX(); |
53 | | - double dy = robotPose.getY() - reefPose.getY(); |
54 | | - |
55 | | - double angleRad = Math.atan2(dy, dx); |
| 76 | + double angleRad = robotPose.minus(reefPose).getAngle().getRadians(); |
56 | 77 |
|
57 | 78 | for (Pose2d face : Reef.alignmentFaces) { |
58 | 79 | double faceRad = face.getRotation().getRadians(); |
59 | 80 |
|
60 | | - if (MathUtil.isNear(faceRad, angleRad, 0.5)) { |
| 81 | + if (MathUtil.isNear(faceRad, angleRad, Units.degreesToRadians(25))) { |
61 | 82 | angleRad = faceRad; |
62 | 83 | } |
63 | 84 | } |
64 | 85 |
|
65 | | - return Optional.of(Rotation2d.fromRadians(angleRad)); |
| 86 | + return Optional.of(AllianceFlipUtil.apply(Rotation2d.fromRadians(angleRad))); |
66 | 87 | }, |
67 | 88 | () -> SpeedLevel.NO_LEVEL, |
68 | 89 | () -> true); |
69 | 90 | } |
| 91 | + |
| 92 | + private static Rotation2d allianceRotation() { |
| 93 | + return AllianceFlipUtil.shouldFlip() ? Rotation2d.kZero : Rotation2d.k180deg; |
| 94 | + } |
70 | 95 | } |
0 commit comments