This repository has been archived by the owner on Jan 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
port autobalance and auto rotate from spike
- Loading branch information
Showing
10 changed files
with
165 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
package frc.robot.managers.managers; | ||
|
||
import edu.wpi.first.math.geometry.Rotation2d; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import frc.robot.fms.FmsSubsystem; | ||
import frc.robot.swerve.SwerveSubsystem; | ||
import frc.robot.util.scheduling.LifecycleSubsystem; | ||
import frc.robot.util.scheduling.SubsystemPriority; | ||
import java.util.function.Supplier; | ||
import org.littletonrobotics.junction.Logger; | ||
|
||
public class AutoRotate extends LifecycleSubsystem { | ||
public static Rotation2d getLeftAngle() { | ||
return FmsSubsystem.isRedAlliance() ? Rotation2d.fromDegrees(270) : Rotation2d.fromDegrees(90); | ||
} | ||
|
||
public static Rotation2d getRightAngle() { | ||
return FmsSubsystem.isRedAlliance() ? Rotation2d.fromDegrees(90) : Rotation2d.fromDegrees(270); | ||
} | ||
|
||
public static Rotation2d getForwardAngle() { | ||
return FmsSubsystem.isRedAlliance() ? Rotation2d.fromDegrees(180) : Rotation2d.fromDegrees(0); | ||
} | ||
|
||
public static Rotation2d getBackwardsAngle() { | ||
return FmsSubsystem.isRedAlliance() ? Rotation2d.fromDegrees(0) : Rotation2d.fromDegrees(180); | ||
} | ||
|
||
private final SwerveSubsystem swerve; | ||
private Rotation2d angle = new Rotation2d(); | ||
private boolean enabled; | ||
|
||
public AutoRotate(SwerveSubsystem swerve) { | ||
super(SubsystemPriority.AUTOROTATE); | ||
this.swerve = swerve; | ||
} | ||
|
||
public void setAngle(Rotation2d angle) { | ||
this.angle = angle; | ||
this.enabled = true; | ||
} | ||
|
||
public void disable() { | ||
this.enabled = false; | ||
swerve.disableSnapToAngle(); | ||
} | ||
|
||
@Override | ||
public void robotPeriodic() { | ||
Logger.getInstance().recordOutput("AutoRotate/GoalAngle", angle.getDegrees()); | ||
} | ||
|
||
@Override | ||
public void enabledPeriodic() { | ||
if (enabled) { | ||
swerve.setSnapToAngle(angle); | ||
} | ||
} | ||
|
||
public Command getCommand(Supplier<Rotation2d> angle) { | ||
return run(() -> setAngle(angle.get())); | ||
} | ||
|
||
public Command getDisableCommand() { | ||
return runOnce(() -> disable()); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/frc/robot/managers/managers/Autobalance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
package frc.robot.managers.managers; | ||
|
||
import edu.wpi.first.math.filter.Debouncer; | ||
import edu.wpi.first.math.filter.LinearFilter; | ||
import edu.wpi.first.math.geometry.Rotation2d; | ||
import edu.wpi.first.math.kinematics.ChassisSpeeds; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import edu.wpi.first.wpilibj2.command.Commands; | ||
import frc.robot.fms.FmsSubsystem; | ||
import frc.robot.imu.ImuSubsystem; | ||
import frc.robot.swerve.SwerveSubsystem; | ||
import frc.robot.util.scheduling.LifecycleSubsystem; | ||
import frc.robot.util.scheduling.SubsystemPriority; | ||
|
||
public class Autobalance extends LifecycleSubsystem { | ||
private final SwerveSubsystem swerve; | ||
private final ImuSubsystem imu; | ||
private boolean enabled = false; | ||
private static final double DRIVE_VELOCITY = -0.475; | ||
private static final double ANGLE_THRESHOLD = 9; | ||
private final LinearFilter autoBalanceFilter = LinearFilter.movingAverage(13); | ||
private Rotation2d averageRoll = new Rotation2d(); | ||
private final Debouncer driveVelocityDebouncer = new Debouncer(9 * 0.02); | ||
|
||
public Autobalance(SwerveSubsystem swerve, ImuSubsystem imu) { | ||
super(SubsystemPriority.AUTOBALANCE); | ||
this.swerve = swerve; | ||
this.imu = imu; | ||
} | ||
|
||
public void setEnabled(boolean mode) { | ||
enabled = mode; | ||
if (!mode) { | ||
swerve.disableSnapToAngle(); | ||
} | ||
} | ||
|
||
@Override | ||
public void robotPeriodic() { | ||
averageRoll = Rotation2d.fromDegrees(autoBalanceFilter.calculate(ANGLE_THRESHOLD)); | ||
} | ||
|
||
@Override | ||
public void enabledPeriodic() { | ||
if (enabled) { | ||
Rotation2d goalAngle = Rotation2d.fromDegrees(FmsSubsystem.isRedAlliance() ? 0 : 180); | ||
|
||
if (driveVelocityDebouncer.calculate(getDriveVelocity() == 0)) { | ||
swerve.setXSwerve(true); | ||
} else { | ||
swerve.setXSwerve(false); | ||
} | ||
|
||
ChassisSpeeds chassisSpeeds = new ChassisSpeeds(getDriveVelocity(), 0, 0); | ||
swerve.setSnapToAngle(goalAngle); | ||
swerve.setChassisSpeeds(chassisSpeeds, false); | ||
} | ||
} | ||
|
||
private double getDriveVelocity() { | ||
if (imu.getRoll().getDegrees() > ANGLE_THRESHOLD) { | ||
return DRIVE_VELOCITY * -1; | ||
} else if (imu.getRoll().getDegrees() < -ANGLE_THRESHOLD) { | ||
return DRIVE_VELOCITY; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
private boolean atGoal() { | ||
return averageRoll.getDegrees() < ANGLE_THRESHOLD | ||
&& averageRoll.getDegrees() > -ANGLE_THRESHOLD; | ||
} | ||
|
||
public Command getCommand() { | ||
return Commands.run(() -> setEnabled(true), swerve) | ||
.until(() -> atGoal()) | ||
.withTimeout(15.0) | ||
.andThen(runOnce(() -> setEnabled(false))) | ||
.handleInterrupt(() -> setEnabled(false)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters