Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Maintain robot heading during teleop using PID #55

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/frc/robot/controller/DriveController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class DriveController extends ButtonController {
private final LogitechF310DirectInputController controller;
private final SlewRateLimiter xLimiter = new SlewRateLimiter(7);
private final SlewRateLimiter yLimiter = new SlewRateLimiter(7);
private final SlewRateLimiter thetaLimiter = new SlewRateLimiter(7);

public DriveController(LogitechF310DirectInputController controller) {
super(controller);
Expand All @@ -35,6 +34,7 @@ public double getForwardPercentage() {

/** The rotation about the robot's z-axis as a percentage (<code>-1 <= x <= 1</code>) */
public double getThetaPercentage() {
return joystickScale(thetaLimiter.calculate(controller.getRightX()));
// This is not ratelimited since heading control is already motion profiled
return joystickScale(controller.getRightX());
}
}
17 changes: 16 additions & 1 deletion src/main/java/frc/robot/drive/DriveSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,28 @@ private void driveTeleop(
double forwardPercentage,
double thetaPercentage,
Rotation2d robotHeading) {
final var goalHeadingDifferential =
TeleopDriveCommand.MAX_TELEOP_TURN_RATE
.times(Constants.PERIOD_SECONDS)
.times(-thetaPercentage);
final var newGoalHeading =
thetaController.getGoal().position + goalHeadingDifferential.getRadians();

final var thetaControllerVelocity =
thetaController.calculate(imuSubsystem.getRotation().getRadians(), newGoalHeading);
final var chassisSpeeds =
ChassisSpeeds.fromFieldRelativeSpeeds(
forwardPercentage * MAX_VELOCITY,
-sidewaysPercentage * MAX_VELOCITY,
thetaPercentage * MAX_ANGULAR_VELOCITY,
thetaControllerVelocity,
robotHeading);

// TODO: Stop logging this after debugging is finished
Logger.getInstance()
.recordOutput("Drive/ThetaControllerVelocityRadiansPerSecond", thetaControllerVelocity);
Logger.getInstance()
.recordOutput("Drive/GoalHeadingRadians", thetaController.getGoal().position);

setChassisSpeeds(chassisSpeeds);
}

Expand Down