Skip to content

Commit 8a956e3

Browse files
authored
Merge branch 'main' into port-forwarding-insert-question-mark-here
2 parents 6bd9683 + cc8c56e commit 8a956e3

File tree

11 files changed

+740
-30
lines changed

11 files changed

+740
-30
lines changed

src/main/java/frc/robot/Constants.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,18 @@ or the reef closer to the robots (Red side) and goes clockwise. Now thinking abo
337337
Units.inchesToMeters(642.903),
338338
Units.inchesToMeters(86.50),
339339
new Rotation2d(Units.degreesToRadians(180)));
340+
341+
public static final double RED_REEF_PLACE_X = 2;
342+
public static final double RED_REEF_PLACE_Y = 2;
343+
public static final Rotation2d RED_REEF_ROTATION = Rotation2d.fromDegrees(-90);
344+
345+
public static final double RED_PROCESSOR_PLACE_X = 0;
346+
public static final double RED_PROCESSOR_PLACE_Y = 0;
347+
public static final Rotation2d RED_PROCESSOR_ROTATION = Rotation2d.fromDegrees(-90);
348+
349+
public static final double RED_FEEDER_STATION_PLACE_X = 0;
350+
public static final double RED_FEEDER_STATION_PLACE_Y = 0;
351+
public static final Rotation2d RED_FEEDER_STATION_ROTATION = Rotation2d.fromDegrees(-90);
340352
}
341353

342354
public static final class JoystickConstants {

src/main/java/frc/robot/RobotContainer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import edu.wpi.first.wpilibj2.command.InstantCommand;
1010
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
1111
import edu.wpi.first.wpilibj2.command.button.Trigger;
12+
import frc.robot.Constants.FieldConstants;
1213
import frc.robot.Constants.SimulationConstants;
14+
import frc.robot.commands.autodrive.AutoAlign;
1315
import frc.robot.commands.drive.DriveCommand;
1416
import frc.robot.extras.simulation.field.SimulatedField;
1517
import frc.robot.extras.simulation.mechanismSim.swerve.GyroSimulation;
@@ -205,6 +207,11 @@ private void configureButtonBindings() {
205207
driverLeftDirectionPad.onTrue(
206208
new InstantCommand(
207209
() -> swerveDrive.resetEstimatedPose(visionSubsystem.getLastSeenPose())));
210+
211+
// FieldConstants has all reef poses
212+
driverController
213+
.a()
214+
.whileTrue(new AutoAlign(swerveDrive, visionSubsystem, FieldConstants.RED_REEF_ONE));
208215
}
209216

210217
public Command getAutonomousCommand() {
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package frc.robot.commands.autodrive;
6+
7+
import edu.wpi.first.math.MathUtil;
8+
import edu.wpi.first.math.controller.ProfiledPIDController;
9+
import edu.wpi.first.math.geometry.Pose2d;
10+
import edu.wpi.first.math.kinematics.ChassisSpeeds;
11+
import frc.robot.commands.drive.DriveCommandBase;
12+
import frc.robot.subsystems.swerve.SwerveConstants;
13+
import frc.robot.subsystems.swerve.SwerveConstants.TrajectoryConstants;
14+
import frc.robot.subsystems.swerve.SwerveDrive;
15+
import frc.robot.subsystems.vision.VisionSubsystem;
16+
17+
/* Auto Align takes in Pose2d and moves robot to it */
18+
public class AutoAlign extends DriveCommandBase {
19+
20+
private final SwerveDrive swerveDrive;
21+
private final VisionSubsystem visionSubsystem;
22+
23+
private Pose2d targetPose;
24+
25+
private final ProfiledPIDController rotationController =
26+
new ProfiledPIDController(
27+
SwerveConstants.TrajectoryConstants.AUTO_LINEUP_ROTATION_P,
28+
SwerveConstants.TrajectoryConstants.AUTO_LINEUP_ROTATION_I,
29+
SwerveConstants.TrajectoryConstants.AUTO_LINEUP_ROTATION_D,
30+
SwerveConstants.TrajectoryConstants.AUTO_LINEUP_ROTATION_CONSTRAINTS);
31+
32+
private final ProfiledPIDController xTranslationController =
33+
new ProfiledPIDController(
34+
SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_P,
35+
SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_I,
36+
SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_D,
37+
SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_CONSTRAINTS);
38+
39+
private final ProfiledPIDController yTranslationController =
40+
new ProfiledPIDController(
41+
SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_P,
42+
SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_I,
43+
SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_D,
44+
SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_CONSTRAINTS);
45+
46+
/**
47+
* Creates a new AutoAlign.
48+
*
49+
* @param visionSubsystem The subsystem for vision
50+
* @param swerveDrive The subsystem for the swerve drive
51+
* @param targetPose The target pose for the robot to align to
52+
*/
53+
public AutoAlign(SwerveDrive swerveDrive, VisionSubsystem visionSubsystem, Pose2d targetPose) {
54+
super(swerveDrive, visionSubsystem);
55+
this.targetPose = targetPose;
56+
this.swerveDrive = swerveDrive;
57+
this.visionSubsystem = visionSubsystem;
58+
// Use addRequirements() here to declare subsystem dependencies.
59+
addRequirements(swerveDrive, visionSubsystem);
60+
// Enables continuous input for the rotation controller
61+
rotationController.enableContinuousInput(-Math.PI, Math.PI);
62+
}
63+
64+
// Called every time the scheduler runs while the command is scheduled.
65+
@Override
66+
public void execute() {
67+
super.execute();
68+
// Gets the error between the desired pos (the target) and the current pos of the robot
69+
Pose2d drivePose = swerveDrive.getEstimatedPose();
70+
double xPoseError = targetPose.getX() - drivePose.getX();
71+
double yPoseError = targetPose.getY() - drivePose.getY();
72+
double thetaPoseError =
73+
targetPose.getRotation().getRadians() - drivePose.getRotation().getRadians();
74+
75+
// Uses the PID controllers to calculate the drive output
76+
double xOutput =
77+
MathUtil.applyDeadband(
78+
xTranslationController.calculate(xPoseError, 0), TrajectoryConstants.DEADBAND_AMOUNT);
79+
double yOutput =
80+
MathUtil.applyDeadband(
81+
yTranslationController.calculate(yPoseError, 0), TrajectoryConstants.DEADBAND_AMOUNT);
82+
double turnOutput =
83+
MathUtil.applyDeadband(
84+
rotationController.calculate(thetaPoseError, 0), TrajectoryConstants.DEADBAND_AMOUNT);
85+
86+
// Gets the chassis speeds for the robot using the odometry rotation (not alliance relative)
87+
ChassisSpeeds chassisSpeeds =
88+
ChassisSpeeds.fromFieldRelativeSpeeds(
89+
xOutput, yOutput, turnOutput, swerveDrive.getOdometryRotation2d());
90+
91+
// Drives the robot towards the target pose
92+
swerveDrive.drive(
93+
chassisSpeeds.vxMetersPerSecond,
94+
chassisSpeeds.vyMetersPerSecond,
95+
chassisSpeeds.omegaRadiansPerSecond,
96+
false);
97+
}
98+
99+
// Called once the command ends or is interrupted.
100+
@Override
101+
public void end(boolean interrupted) {
102+
swerveDrive.drive(0, 0, 0, false);
103+
}
104+
105+
// Returns true when the command should end.
106+
@Override
107+
public boolean isFinished() {
108+
return false;
109+
}
110+
}

0 commit comments

Comments
 (0)