44
55package frc .robot ;
66
7- import static edu .wpi .first .wpilibj2 .command .Commands .*;
87import static frc .robot .Constants .*;
98import static frc .robot .Constants .RobotConstants .*;
109import static frc .robot .subsystems .PoseEstimationSubsystem .*;
1514import org .littletonrobotics .urcl .URCL ;
1615import org .photonvision .PhotonCamera ;
1716
17+ import edu .wpi .first .math .geometry .Pose2d ;
1818import edu .wpi .first .math .geometry .Rotation2d ;
1919import edu .wpi .first .math .geometry .Transform2d ;
2020import edu .wpi .first .math .kinematics .ChassisSpeeds ;
3131import edu .wpi .first .wpilibj2 .command .button .CommandPS4Controller ;
3232import frc .robot .Constants .ControllerConstants ;
3333import frc .robot .Constants .ControllerConstants .Button ;
34+ import frc .robot .commands .drive .DriveCommand2Controllers ;
3435import frc .robot .subsystems .DriveSubsystem ;
3536import frc .robot .subsystems .PhotonCameraSimulator ;
3637import frc .robot .subsystems .PoseEstimationSubsystem ;
@@ -64,6 +65,9 @@ public Robot() {
6465 m_autoSelector .addOption ("Test DriveSubsystem" , m_driveSubsystem .testCommand ());
6566 SmartDashboard .putData ("Auto Selector" , m_autoSelector );
6667
68+ double distanceTolerance = 0.01 ;
69+ double angleToleranceInDegrees = 1.0 ;
70+
6771 m_testSelector .addOption ("Check All Subsystems in Pitt" , CommandComposer .testAllSubsystems ());
6872 m_testSelector .addOption ("Check All Subsystems on Field" , CommandComposer .testAllSubsystems ());
6973 m_testSelector .addOption ("Check DriveSubsystem (Robot-Oriented F/B/L/R/LR/RR)" , m_driveSubsystem .testCommand ());
@@ -74,15 +78,16 @@ public Robot() {
7478 m_testSelector
7579 .addOption (
7680 "Check kDriveGearRatio and kWheelDiameter (F/B 6 feet)" ,
77- CommandComposer .moveForwardBackward2Controllers (6 , 0.01 , 1 ));
81+ CommandComposer .moveForwardBackward2Controllers (6 , distanceTolerance , angleToleranceInDegrees ));
7882 m_testSelector
7983 .addOption (
8084 "Check PID Constants for Driving (5'x5' Square)" ,
81- CommandComposer .moveOnSquare (Units .feetToMeters (5 ), 0.01 , 1 , 16 ));
85+ CommandComposer
86+ .moveOnSquare (Units .feetToMeters (5 ), distanceTolerance , angleToleranceInDegrees , 16 ));
8287 m_testSelector
8388 .addOption (
8489 "Check PID Constants for Driving (Unit Circle)" ,
85- CommandComposer .moveOnCircle (1 , 1 , 3 , 0.01 , 1 , 10 ));
90+ CommandComposer .moveOnCircle (1 , 1 , 3 , distanceTolerance , angleToleranceInDegrees , 10 ));
8691 m_testSelector .addOption ("Test Rotation" , CommandComposer .testRotation ());
8792 m_testSelector .addOption ("Turn toward Tag 1" , CommandComposer .turnTowardTag (1 ));
8893
@@ -114,7 +119,7 @@ public void bindDriveControls() {
114119 () -> -m_driverController .getLeftY (),
115120 () -> -m_driverController .getLeftX (),
116121 () -> m_driverController .getL2Axis () - m_driverController .getR2Axis (),
117- new Transform2d (0.5 , 0 , Rotation2d .fromDegrees (180 )), 2 ));
122+ new Transform2d (0.5 , 0 , Rotation2d .fromDegrees (180 )), 2 , 0.03 , 3 ));
118123 }
119124
120125 /**
@@ -130,20 +135,30 @@ public void bindDriveControls() {
130135 * @param robotToTag the {@code Tranform2d} representing the pose of the
131136 * closest {@code AprilTag} relative to the robot when the robot is
132137 * aligned
133- * @param isFieldRelative {@code Supplier} for determining whether or not
134- * driving should be field relative.
138+ * @param distanceThresholdInMeters the maximum distance (in meters) within
139+ * which {@code AprilTag}s are considered
140+ * @param distanceTolerance the distance error in meters which is tolerable
141+ * @param angleToleranceInDegrees the angle error in degrees which is tolerable
135142 * @return a {@code Command} to automatically align the robot to the closest tag
136143 * while driving the robot with joystick input
137144 */
138145 Command driveWithAlignmentCommand (DoubleSupplier forwardSpeed , DoubleSupplier strafeSpeed ,
139- DoubleSupplier rotation , Transform2d robotToTag , double distanceThresholdInMeters ) {
140- return run (() -> {
141- ChassisSpeeds speeds = DriveSubsystem .chassisSpeeds (forwardSpeed , strafeSpeed , rotation );
142- speeds = speeds .plus (
143- m_poseEstimationSubsystem
144- .chassisSpeedsTowardClosestTag (robotToTag , distanceThresholdInMeters ));
145- m_driveSubsystem .drive (speeds , true );
146- });
146+ DoubleSupplier rotation , Transform2d robotToTag , double distanceThresholdInMeters , double distanceTolerance ,
147+ double angleToleranceInDegrees ) {
148+ return new DriveCommand2Controllers (m_driveSubsystem , () -> {
149+ Pose2d closestTagPose = m_poseEstimationSubsystem .closestTagPose (180 , distanceThresholdInMeters );
150+ if (closestTagPose == null )
151+ return m_driveSubsystem .getPose ();
152+ return m_poseEstimationSubsystem .odometryCentricPose (closestTagPose .plus (robotToTag ));
153+ }, false , distanceTolerance , angleToleranceInDegrees ) {
154+
155+ @ Override
156+ public ChassisSpeeds chassisSpeeds () {
157+ ChassisSpeeds speeds = DriveSubsystem .chassisSpeeds (forwardSpeed , strafeSpeed , rotation );
158+ return speeds .plus (super .chassisSpeeds ());
159+ }
160+
161+ };
147162 }
148163
149164 @ Override
0 commit comments