|
1 | 1 | package frc.robot.commands; |
2 | 2 |
|
| 3 | +import com.pathplanner.lib.auto.AutoBuilder; |
| 4 | +import com.pathplanner.lib.path.PathConstraints; |
3 | 5 | import edu.wpi.first.math.controller.PIDController; |
4 | 6 | import edu.wpi.first.math.filter.SlewRateLimiter; |
5 | 7 | import edu.wpi.first.math.geometry.Pose2d; |
6 | 8 | import edu.wpi.first.math.geometry.Rotation2d; |
7 | 9 | import edu.wpi.first.math.kinematics.ChassisSpeeds; |
8 | 10 | import edu.wpi.first.math.util.Units; |
| 11 | +import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; |
9 | 12 | import edu.wpi.first.wpilibj2.command.Command; |
| 13 | +import frc.robot.Robot; |
10 | 14 | import frc.robot.RobotContainer; |
| 15 | +import frc.robot.subsystems.AllianceFlipUtil; |
11 | 16 | import frc.robot.subsystems.VisionSubsystem; |
12 | 17 |
|
13 | 18 | public class AlignWithNearest extends Command { |
14 | 19 |
|
| 20 | + public Pose2d getSelectedPose() { |
| 21 | + return AllianceFlipUtil.apply(Robot.desiredScoreSendableChooser.getSelected().scorePosition); |
| 22 | + } |
| 23 | + |
15 | 24 | public static String name = frc.robot.Constants.VisionSubsystemConstants.limelightName; |
16 | 25 | private final SlewRateLimiter xLimiter, yLimiter, giroLimiter; |
17 | 26 | private final PIDController drivePID, strafePID, rotationPID; |
18 | 27 | private final double driveOffset, strafeOffset, rotationOffset; |
| 28 | + private Command pathCommand; |
| 29 | + |
| 30 | + private Pose2d targetPose; |
| 31 | + private Pose2d robotPose; |
| 32 | + private double distanceAway = -0.55; |
19 | 33 |
|
20 | 34 | public static Pose2d[] TagPos = { |
21 | 35 | new Pose2d(16.408, 1.048, new Rotation2d(-0.9075712)), |
@@ -90,65 +104,80 @@ public AlignWithNearest() { |
90 | 104 |
|
91 | 105 | @Override |
92 | 106 | public void initialize() { |
93 | | - /* |
94 | | - if (DriverStation.getAlliance().get().equals(Alliance.Red)) { |
95 | | - targetCommand = RobotContainer.drivebase.driveToPose(new Pose2d(13, 4, new Rotation2d(0))); |
96 | | - } else { |
97 | | - targetCommand = RobotContainer.drivebase.driveToPose(new Pose2d(4, 4, new Rotation2d(0))); |
| 107 | + if (Robot.assistSendableChooser.getSelected().equals("B")) { |
| 108 | + Pose2d selectedPosition = getSelectedPose(); |
| 109 | + |
| 110 | + targetPose = |
| 111 | + new Pose2d( |
| 112 | + Math.cos(selectedPosition.getRotation().getRadians()) * distanceAway |
| 113 | + - Math.sin(selectedPosition.getRotation().getRadians()) |
| 114 | + * SmartDashboard.getNumber("getAutoAlignOffsetX", 0) |
| 115 | + + selectedPosition.getTranslation().getX(), |
| 116 | + Math.sin(selectedPosition.getRotation().getRadians()) * distanceAway |
| 117 | + + Math.cos(selectedPosition.getRotation().getRadians()) |
| 118 | + * SmartDashboard.getNumber("getAutoAlignOffsetX", 0) |
| 119 | + + selectedPosition.getTranslation().getY(), |
| 120 | + selectedPosition.getRotation()); |
| 121 | + |
| 122 | + pathCommand = AutoBuilder.pathfindToPose(targetPose, new PathConstraints(1, 1, 180, 180)); |
98 | 123 | } |
99 | | - targetCommand.schedule(); |
100 | | - */ |
101 | 124 | } |
102 | 125 |
|
103 | 126 | public void execute() { |
104 | | - double velForward = 0; |
105 | | - double velStrafe = 0; |
106 | | - double velGiro = 0; |
107 | | - |
108 | | - /** |
109 | | - * If there is a seen target, calculate the PIDs velocities, otherwise, rotate so the robot can |
110 | | - * search the target |
111 | | - */ |
112 | | - if (VisionSubsystem.getTags().length != 0) { |
113 | | - |
114 | | - velForward = drivePID.calculate(VisionSubsystem.getTagArea(), driveOffset); |
115 | | - velStrafe = strafePID.calculate(VisionSubsystem.getXDistance(), strafeOffset); |
116 | | - velGiro = |
117 | | - -rotationPID.calculate( |
118 | | - VisionSubsystem.getTagPose2d().getRotation().getDegrees(), rotationOffset); |
119 | | - } else if (VisionSubsystem.getTags().length == 0) { |
120 | | - velForward = 0; |
121 | | - velStrafe = 0; |
122 | | - velGiro = 0.4; |
123 | | - } else { |
124 | | - velForward = 0; |
125 | | - velStrafe = 0; |
126 | | - velGiro = 0; |
| 127 | + if (Robot.assistSendableChooser.getSelected().equals("A")) { |
| 128 | + double velForward = 0; |
| 129 | + double velStrafe = 0; |
| 130 | + double velGiro = 0; |
| 131 | + |
| 132 | + /* |
| 133 | + * If there is a seen target, calculate the PIDs velocities, otherwise, rotate so the robot can |
| 134 | + * search the target |
| 135 | + */ |
| 136 | + if (VisionSubsystem.getTags().length != 0) { |
| 137 | + |
| 138 | + velForward = drivePID.calculate(VisionSubsystem.getTagArea(), driveOffset); |
| 139 | + velStrafe = strafePID.calculate(VisionSubsystem.getXDistance(), strafeOffset); |
| 140 | + velGiro = |
| 141 | + -rotationPID.calculate( |
| 142 | + VisionSubsystem.getTagPose2d().getRotation().getDegrees(), rotationOffset); |
| 143 | + } else if (VisionSubsystem.getTags().length == 0) { |
| 144 | + velForward = 0; |
| 145 | + velStrafe = 0; |
| 146 | + velGiro = 0.4; |
| 147 | + } else { |
| 148 | + velForward = 0; |
| 149 | + velStrafe = 0; |
| 150 | + velGiro = 0; |
| 151 | + } |
| 152 | + |
| 153 | + // 3. Make the driving smoother |
| 154 | + velForward = xLimiter.calculate(velForward) * 3; |
| 155 | + velStrafe = yLimiter.calculate(velStrafe) * 3; |
| 156 | + velGiro = giroLimiter.calculate(velGiro) * 5; |
| 157 | + |
| 158 | + // 4. Construct desired chassis speeds |
| 159 | + ChassisSpeeds chassisSpeeds; |
| 160 | + |
| 161 | + // Relative to robot |
| 162 | + chassisSpeeds = new ChassisSpeeds(velForward, velStrafe, velGiro); |
| 163 | + |
| 164 | + RobotContainer.drivebase.drive(chassisSpeeds); |
| 165 | + } else if (Robot.assistSendableChooser.getSelected().equals("B")) { |
| 166 | + robotPose = VisionSubsystem.getRobotPoseInFieldSpace().toPose2d(); |
| 167 | + |
| 168 | + if (!robotPose.equals(new Pose2d())) RobotContainer.drivebase.driveToPose(robotPose); |
| 169 | + |
| 170 | + pathCommand.schedule(); |
127 | 171 | } |
128 | | - |
129 | | - // 3. Make the driving smoother |
130 | | - velForward = xLimiter.calculate(velForward) * 3; |
131 | | - velStrafe = yLimiter.calculate(velStrafe) * 3; |
132 | | - velGiro = giroLimiter.calculate(velGiro) * 5; |
133 | | - |
134 | | - // 4. Construct desired chassis speeds |
135 | | - ChassisSpeeds chassisSpeeds; |
136 | | - |
137 | | - // Relative to robot |
138 | | - chassisSpeeds = new ChassisSpeeds(velForward, velStrafe, velGiro); |
139 | 172 | } |
140 | 173 |
|
141 | 174 | @Override |
142 | 175 | public void end(boolean inter) { |
143 | | - targetCommand.cancel(); |
144 | | - RobotContainer.drivebase.drive(new ChassisSpeeds(0, 0, 0)); |
| 176 | + pathCommand.end(inter); |
145 | 177 | } |
146 | 178 |
|
147 | 179 | @Override |
148 | 180 | public boolean isFinished() { |
149 | | - return RobotContainer.driverXbox.getLeftX() >= 0.3 |
150 | | - || RobotContainer.driverXbox.getLeftY() >= 0.3 |
151 | | - || RobotContainer.driverXbox.getLeftX() <= -0.3 |
152 | | - || RobotContainer.driverXbox.getLeftY() <= -0.3; |
| 181 | + return pathCommand.isFinished(); |
153 | 182 | } |
154 | 183 | } |
0 commit comments