88import static frc .robot .Constants .RobotConstants .*;
99import static frc .robot .subsystems .PoseEstimationSubsystem .*;
1010
11+ import java .util .Arrays ;
1112import java .util .List ;
1213import java .util .Map ;
1314import java .util .function .DoubleSupplier ;
15+ import java .util .function .Supplier ;
1416
1517import org .littletonrobotics .urcl .URCL ;
1618import org .photonvision .PhotonCamera ;
1719
1820import edu .wpi .first .math .geometry .Pose2d ;
19- import edu .wpi .first .math .geometry .Rotation2d ;
2021import edu .wpi .first .math .geometry .Transform2d ;
2122import edu .wpi .first .math .kinematics .ChassisSpeeds ;
2223import edu .wpi .first .math .util .Units ;
3132import edu .wpi .first .wpilibj2 .command .CommandScheduler ;
3233import edu .wpi .first .wpilibj2 .command .button .CommandPS4Controller ;
3334import frc .robot .Constants .ControllerConstants ;
34- import frc .robot .Constants .ControllerConstants .Button ;
3535import frc .robot .commands .DriveCommand ;
36+ import frc .robot .commands .PathDriveCommand ;
3637import frc .robot .subsystems .DriveSubsystem ;
3738import frc .robot .subsystems .PhotonCameraSimulator ;
3839import frc .robot .subsystems .PoseEstimationSubsystem ;
@@ -87,7 +88,8 @@ public Robot() {
8788 CommandComposer .alignToTags (
8889 distanceTolerance , angleToleranceInDegrees , intermediateDistanceTolerance ,
8990 intermediateAngleToleranceInDegrees ,
90- List .of (transform (2.7 , 0 , 180 ), transform (1.4 , 0 , 180 ), transform (.8 , 0 , 180 )), 7 , 6 , 1 ,
91+ List .of (transform (1.5 , 0 , 180 ), transform (1.0 , 0 , 180 ), transform (.5 , 0 , 180 )),
92+ transform (1.5 , 0 , 180 ), 7 , 6 , 1 ,
9193 6 , 7 , 8 , 2 , 8 , 7 ));
9294 m_testSelector .addOption ("Check All Subsystems in Pitt" , CommandComposer .testAllSubsystems ());
9395 m_testSelector .addOption ("Check All Subsystems on Field" , CommandComposer .testAllSubsystems ());
@@ -123,14 +125,19 @@ public void bindDriveControls() {
123125 () -> -m_driverController .getLeftX (),
124126 () -> m_driverController .getL2Axis () - m_driverController .getR2Axis (),
125127 () -> !m_driverController .getHID ().getSquareButton ()));
126-
127- m_driverController .button (Button .kSquare )
128- .whileTrue (
129- driveWithAlignmentCommand (
130- () -> -m_driverController .getLeftY (),
131- () -> -m_driverController .getLeftX (),
132- () -> m_driverController .getL2Axis () - m_driverController .getR2Axis (),
133- new Transform2d (0.5 , 0 , Rotation2d .fromDegrees (180 )), 2 , 0.03 , 3 ));
128+ m_driverController .square ().whileTrue (
129+ driveWithAlignmentCommand (
130+ () -> -m_driverController .getLeftY (),
131+ () -> -m_driverController .getLeftX (),
132+ () -> m_driverController .getL2Axis () - m_driverController .getR2Axis (), 2 , 0.01 , 1 ,
133+ transform (0.5 , 0 , 180 )));
134+ m_driverController .cross ().whileTrue (
135+ driveWithAlignmentCommand (
136+ () -> -m_driverController .getLeftY (),
137+ () -> -m_driverController .getLeftX (),
138+ () -> m_driverController .getL2Axis () - m_driverController .getR2Axis (), 2 , 0.01 , 1 , 0.1 , 5 ,
139+ transform (1.0 , 0 , 180 ),
140+ transform (0.5 , 0 , 180 )));
134141 }
135142
136143 /**
@@ -143,19 +150,19 @@ public void bindDriveControls() {
143150 * go to the left (+Y direction).
144151 * @param rotation rotation speed supplier. Positive values make the
145152 * robot rotate CCW.
146- * @param robotToTag the {@code Tranform2d} representing the pose of the
147- * closest {@code AprilTag} relative to the robot when the robot is
148- * aligned
149153 * @param distanceThresholdInMeters the maximum distance (in meters) within
150154 * which {@code AprilTag}s are considered
151155 * @param distanceTolerance the distance error in meters which is tolerable
152156 * @param angleToleranceInDegrees the angle error in degrees which is tolerable
157+ * @param robotToTag the {@code Tranform2d} representing the pose of the
158+ * closest {@code AprilTag} relative to the robot when the robot is
159+ * aligned
153160 * @return a {@code Command} to automatically align the robot to the closest tag
154161 * while driving the robot with joystick input
155162 */
156- Command driveWithAlignmentCommand (DoubleSupplier forwardSpeed , DoubleSupplier strafeSpeed ,
157- DoubleSupplier rotation , Transform2d robotToTag , double distanceThresholdInMeters , double distanceTolerance ,
158- double angleToleranceInDegrees ) {
163+ Command driveWithAlignmentCommand (DoubleSupplier forwardSpeed , DoubleSupplier strafeSpeed , DoubleSupplier rotation ,
164+ double distanceThresholdInMeters , double distanceTolerance , double angleToleranceInDegrees ,
165+ Transform2d robotToTag ) {
159166 return new DriveCommand (m_driveSubsystem , distanceTolerance , angleToleranceInDegrees , () -> {
160167 Pose2d closestTagPose = m_poseEstimationSubsystem .closestTagPose (180 , distanceThresholdInMeters );
161168 if (closestTagPose == null )
@@ -172,6 +179,48 @@ public ChassisSpeeds chassisSpeeds() {
172179 };
173180 }
174181
182+ /**
183+ * Creates a {@code Command} to automatically align the robot to the closest
184+ * {@code AprilTag} while driving the robot with joystick input.
185+ *
186+ * @param forwardSpeed forward speed supplier. Positive values make the robot
187+ * go forward (+X direction).
188+ * @param strafeSpeed strafe speed supplier. Positive values make the robot
189+ * go to the left (+Y direction).
190+ * @param rotation rotation speed supplier. Positive values make the
191+ * robot rotate CCW.
192+ * @param distanceThresholdInMeters the maximum distance (in meters) within
193+ * which {@code AprilTag}s are considered
194+ * @param distanceTolerance the distance error in meters which is tolerable
195+ * @param angleToleranceInDegrees the angle error in degrees which is tolerable
196+ * @param robotToTags the {@code Tranform2d} representing the pose of the
197+ * closest {@code AprilTag} relative to the robot when the robot is
198+ * aligned
199+ * @return a {@code Command} to automatically align the robot to the closest tag
200+ * while driving the robot with joystick input
201+ */
202+ Command driveWithAlignmentCommand (DoubleSupplier forwardSpeed , DoubleSupplier strafeSpeed , DoubleSupplier rotation ,
203+ double distanceThresholdInMeters , double distanceTolerance , double angleToleranceInDegrees ,
204+ double intermedateDistanceTolerance , double intermediateAngleToleranceInDegrees ,
205+ Transform2d ... robotToTags ) {
206+ var l = Arrays .stream (robotToTags ).map (r -> (Supplier <Pose2d >) (() -> {
207+ Pose2d closestTagPose = m_poseEstimationSubsystem .closestTagPose (180 , distanceThresholdInMeters );
208+ if (closestTagPose == null )
209+ return m_driveSubsystem .getPose ();
210+ return m_poseEstimationSubsystem .odometryCentricPose (closestTagPose .plus (r ));
211+ })).toList ();
212+ return new PathDriveCommand (m_driveSubsystem , distanceTolerance , angleToleranceInDegrees ,
213+ intermedateDistanceTolerance , intermediateAngleToleranceInDegrees , l ) {
214+
215+ @ Override
216+ public ChassisSpeeds chassisSpeeds () {
217+ ChassisSpeeds speeds = DriveSubsystem .chassisSpeeds (forwardSpeed , strafeSpeed , rotation );
218+ return speeds .plus (super .chassisSpeeds ());
219+ }
220+
221+ };
222+ }
223+
175224 @ Override
176225 public void robotPeriodic () {
177226 CommandScheduler .getInstance ().run ();
0 commit comments