Skip to content

Commit

Permalink
Keep yaw with AutoDriveTo
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelLesirge committed Mar 15, 2024
1 parent 3a9c087 commit 75c2353
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/commands/AimAtTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public AimAtTag(SwerveDrivetrain drivetrain, Vision vision, int tagID, ChassisDr
RobotMovementConstants.ROTATION_PID_P,
RobotMovementConstants.ROTATION_PID_I,
RobotMovementConstants.ROTATION_PID_D);
rotatePID.enableContinuousInput(-1, 1);
rotatePID.enableContinuousInput(-Math.PI, Math.PI);
rotatePID.setTolerance(RobotMovementConstants.ANGLE_TOLERANCE_RADIANS);
rotatePID.setSetpoint(0);

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/frc/robot/commands/AutoDriveTo.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class AutoDriveTo extends Command {
private final SwerveDrivetrain drivetrain;

private final PIDController xMovePID, yMovePID;
private final PIDController xMovePID, yMovePID, rotatePID;
private double initX, initY, goalX, goalY;

/***
Expand All @@ -39,6 +39,13 @@ public AutoDriveTo(SwerveDrivetrain subsystem, Translation2d translation) {
RobotMovementConstants.TRANSLATION_PID_D);
yMovePID.setTolerance(RobotMovementConstants.POSITION_TOLERANCE_METERS);

rotatePID = new PIDController(
RobotMovementConstants.ROTATION_PID_P,
RobotMovementConstants.ROTATION_PID_I,
RobotMovementConstants.ROTATION_PID_D);
rotatePID.enableContinuousInput(-Math.PI, Math.PI);
rotatePID.setTolerance(RobotMovementConstants.ANGLE_TOLERANCE_RADIANS);

addRequirements(this.drivetrain);
}

Expand All @@ -49,6 +56,8 @@ public void initialize() {

final Pose2d position = drivetrain.getPosition();

rotatePID.setSetpoint(drivetrain.getHeading().getRadians());

initX = position.getX();
initY = position.getY();
}
Expand All @@ -59,6 +68,7 @@ public void execute() {

double targetX = position.getX() - initX;
double targetY = position.getY() - initY;
double targetRadians = drivetrain.getHeading().getRadians();

double xSpeed = xMovePID.calculate(targetX, goalX);
double ySpeed = yMovePID.calculate(targetY, goalY);
Expand All @@ -76,7 +86,7 @@ public void execute() {
ySpeed = (ySpeed / maxSpeed) * speedLimit;
}

drivetrain.setDesiredState(new ChassisSpeeds(xSpeed, ySpeed, 0));
drivetrain.setDesiredState(new ChassisSpeeds(xSpeed, ySpeed, rotatePID.calculate(targetRadians)));
drivetrain.updateSmartDashboard();
}

Expand Down

0 comments on commit 75c2353

Please sign in to comment.