Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
port autobalance and auto rotate from spike
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen756 committed Aug 30, 2023
1 parent ad0cba3 commit dca9357
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/controller/DriveController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import frc.robot.autoscore.NodeKind;
import frc.robot.managers.superstructure.NodeHeight;
import frc.robot.managers.managers.NodeHeight;

public class DriveController extends CommandXboxController {
private boolean slowModeEnabled;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/lights/LightsSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import frc.robot.intake.HeldGamePiece;
import frc.robot.intake.IntakeState;
import frc.robot.intake.IntakeSubsystem;
import frc.robot.managers.superstructure.SuperstructureManager;
import frc.robot.managers.managers.SuperstructureManager;
import frc.robot.util.scheduling.LifecycleSubsystem;
import frc.robot.util.scheduling.SubsystemPriority;

Expand Down
71 changes: 71 additions & 0 deletions src/main/java/frc/robot/managers/managers/AutoRotate.java
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 src/main/java/frc/robot/managers/managers/Autobalance.java
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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.superstructure;
package frc.robot.managers.managers;

public enum NodeHeight {
LOW,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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.superstructure;
package frc.robot.managers.managers;

import edu.wpi.first.math.geometry.Rotation2d;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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.superstructure;
package frc.robot.managers.managers;

import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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.superstructure;
package frc.robot.managers.managers;

import frc.robot.util.scheduling.LifecycleSubsystem;
import frc.robot.util.scheduling.SubsystemPriority;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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.superstructure;
package frc.robot.managers.managers;

import edu.wpi.first.math.geometry.Rotation2d;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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.superstructure;
package frc.robot.managers.managers;

import frc.robot.intake.IntakeState;

Expand Down

0 comments on commit dca9357

Please sign in to comment.