Skip to content

Commit

Permalink
More SwerveWithPathPlanner cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bhall-ctre committed Oct 22, 2024
1 parent 2f46a8b commit e3be887
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
4 changes: 2 additions & 2 deletions java/SwerveWithPathPlanner/src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Robot extends TimedRobot {

private final RobotContainer m_robotContainer;

private final boolean UseLimelight = false;
private final boolean kUseLimelight = false;

public Robot() {
m_robotContainer = new RobotContainer();
Expand All @@ -33,7 +33,7 @@ public void robotPeriodic() {
* This example is sufficient to show that vision integration is possible, though exact implementation
* of how to use vision should be tuned per-robot and to the team's specification.
*/
if (UseLimelight) {
if (kUseLimelight) {
var llMeasurement = LimelightHelpers.getBotPoseEstimate_wpiBlue("limelight");
if (llMeasurement != null) {
m_robotContainer.drivetrain.addVisionMeasurement(llMeasurement.pose, Utils.fpgaToCurrentTime(llMeasurement.timestampSeconds));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class RobotContainer {
.withDriveRequestType(DriveRequestType.OpenLoopVoltage); // Use open-loop control for drive motors
private final SwerveRequest.SwerveDriveBrake brake = new SwerveRequest.SwerveDriveBrake();
private final SwerveRequest.PointWheelsAt point = new SwerveRequest.PointWheelsAt();
private final SwerveRequest.RobotCentric forwardStraight = new SwerveRequest.RobotCentric().withDriveRequestType(DriveRequestType.OpenLoopVoltage);
private final SwerveRequest.RobotCentric forwardStraight = new SwerveRequest.RobotCentric()
.withDriveRequestType(DriveRequestType.OpenLoopVoltage);

/* Path follower */
private Command runAuto = new PathPlannerAuto("Tests");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class CommandSwerveDrivetrain extends SwerveDrivetrain implements Subsyst
private boolean m_hasAppliedOperatorPerspective = false;

/* Swerve request to apply during path following */
private final SwerveRequest.ApplyChassisSpeeds m_applySpeeds = new SwerveRequest.ApplyChassisSpeeds();
private final SwerveRequest.ApplyChassisSpeeds m_applyRobotSpeeds = new SwerveRequest.ApplyChassisSpeeds();

/* Swerve requests to apply during SysId characterization */
private final SwerveRequest.SysIdSwerveTranslation m_translationCharacterization = new SwerveRequest.SysIdSwerveTranslation();
Expand Down Expand Up @@ -126,10 +126,10 @@ public class CommandSwerveDrivetrain extends SwerveDrivetrain implements Subsyst
*/
public CommandSwerveDrivetrain(SwerveDrivetrainConstants drivetrainConstants, SwerveModuleConstants... modules) {
super(drivetrainConstants, modules);
configurePathPlanner();
if (Utils.isSimulation()) {
startSimThread();
}
configureAutoBuilder();
}

/**
Expand All @@ -147,10 +147,10 @@ public CommandSwerveDrivetrain(SwerveDrivetrainConstants drivetrainConstants, Sw
*/
public CommandSwerveDrivetrain(SwerveDrivetrainConstants drivetrainConstants, double OdometryUpdateFrequency, SwerveModuleConstants... modules) {
super(drivetrainConstants, OdometryUpdateFrequency, modules);
configurePathPlanner();
if (Utils.isSimulation()) {
startSimThread();
}
configureAutoBuilder();
}

/**
Expand All @@ -173,13 +173,13 @@ public CommandSwerveDrivetrain(
Matrix<N3, N1> odometryStandardDeviation, Matrix<N3, N1> visionStandardDeviation,
SwerveModuleConstants... modules) {
super(drivetrainConstants, odometryUpdateFrequency, odometryStandardDeviation, visionStandardDeviation, modules);
configurePathPlanner();
if (Utils.isSimulation()) {
startSimThread();
}
configureAutoBuilder();
}

private void configurePathPlanner() {
private void configureAutoBuilder() {
double driveBaseRadius = 0;
for (var moduleLocation : m_moduleLocations) {
driveBaseRadius = Math.max(driveBaseRadius, moduleLocation.getNorm());
Expand All @@ -193,7 +193,7 @@ private void configurePathPlanner() {
() -> this.getState().Speeds, // Supplier of current robot speeds
// Consumer of ChassisSpeeds and feedforwards to drive the robot
(speeds, feedforwards) -> this.setControl(
m_applySpeeds.withSpeeds(speeds)
m_applyRobotSpeeds.withSpeeds(speeds)
.withWheelForceFeedforwardsX(feedforwards.robotRelativeForcesXNewtons())
.withWheelForceFeedforwardsY(feedforwards.robotRelativeForcesYNewtons())
),
Expand Down Expand Up @@ -242,16 +242,20 @@ public Command sysIdDynamic(SysIdRoutine.Direction direction) {

@Override
public void periodic() {
/* Periodically try to apply the operator perspective */
/* If we haven't applied the operator perspective before, then we should apply it regardless of DS state */
/* This allows us to correct the perspective in case the robot code restarts mid-match */
/* Otherwise, only check and apply the operator perspective if the DS is disabled */
/* This ensures driving behavior doesn't change until an explicit disable event occurs during testing*/
/*
* Periodically try to apply the operator perspective.
* If we haven't applied the operator perspective before, then we should apply it regardless of DS state.
* This allows us to correct the perspective in case the robot code restarts mid-match.
* Otherwise, only check and apply the operator perspective if the DS is disabled.
* This ensures driving behavior doesn't change until an explicit disable event occurs during testing.
*/
if (!m_hasAppliedOperatorPerspective || DriverStation.isDisabled()) {
DriverStation.getAlliance().ifPresent((allianceColor) -> {
DriverStation.getAlliance().ifPresent(allianceColor -> {
setOperatorPerspectiveForward(
allianceColor == Alliance.Red ? kRedAlliancePerspectiveRotation
: kBlueAlliancePerspectiveRotation);
allianceColor == Alliance.Red
? kRedAlliancePerspectiveRotation
: kBlueAlliancePerspectiveRotation
);
m_hasAppliedOperatorPerspective = true;
});
}
Expand Down

0 comments on commit e3be887

Please sign in to comment.