Skip to content

Commit

Permalink
Merge branch 'main' into intake-sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
Alenguye582 authored Oct 26, 2024
2 parents 860aed4 + 72d4fa4 commit 168e102
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 21 deletions.
44 changes: 34 additions & 10 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package frc.robot;

import java.lang.annotation.Target;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -14,6 +13,7 @@
import com.ctre.phoenix.led.ColorFlowAnimation;
import com.ctre.phoenix.led.FireAnimation;
import com.ctre.phoenix.led.RainbowAnimation;
import com.ctre.phoenix.led.StrobeAnimation;
import com.ctre.phoenix.led.TwinkleAnimation;
import com.ctre.phoenix.led.ColorFlowAnimation.Direction;
import com.ctre.phoenix.led.TwinkleAnimation.TwinklePercent;
Expand All @@ -22,7 +22,6 @@
import com.ctre.phoenix6.signals.GravityTypeValue;
import com.ctre.phoenix6.signals.InvertedValue;
import com.ctre.phoenix6.signals.NeutralModeValue;
import com.ctre.phoenix6.signals.RobotEnableValue;
import com.ctre.phoenix6.signals.SensorDirectionValue;
import com.ctre.phoenix6.signals.StaticFeedforwardSignValue;
import com.frcteam3255.components.swerve.SN_SwerveConstants;
Expand Down Expand Up @@ -111,7 +110,7 @@ public static class constDrivetrain {

public static final Measure<Angle> AUTO_PRELOAD_TAXI_ROTATION = Units.Degrees.of(119.62);

public static final Measure<Angle> AT_ROTATION_TOLERANCE = Units.Degrees.of(20);
public static final Measure<Angle> AT_ROTATION_TOLERANCE = Units.Degrees.of(5);

public static final boolean DRIVE_ENABLE_CURRENT_LIMITING = true;
public static final double DRIVE_CURRENT_THRESH = 40;
Expand Down Expand Up @@ -259,6 +258,7 @@ public static class constShooter {

public static final double PIVOT_GEAR_RATIO = 58.5;
public static final NeutralModeValue PIVOT_NEUTRAL_MODE = NeutralModeValue.Brake;
public static final boolean PIVOT_SINGS_IN_DISABLE = true;

// - PID -
public static Slot0Configs LEFT_PID_SLOT_0_FAST = new Slot0Configs();
Expand Down Expand Up @@ -358,6 +358,9 @@ public static class constShooter {

public static final Measure<Time> ZEROING_TIMEOUT = Units.Seconds.of(3);

public static final Measure<Velocity<Angle>> MANUAL_ZEROING_START_VELOCITY = Units.RotationsPerSecond.of(7);
public static final Measure<Velocity<Angle>> MANUAL_ZEROING_DELTA_VELOCITY = Units.RotationsPerSecond.of(7);

// -- Current Limiting --
public static final boolean PIVOT_ENABLE_CURRENT_LIMITING = true;
public static final double PIVOT_CURRENT_THRESH = 50;
Expand Down Expand Up @@ -571,6 +574,9 @@ public static class constElevator {

public static final Measure<Time> ZEROING_TIMEOUT = Units.Seconds.of(3);

public static final Measure<Velocity<Angle>> MANUAL_ZEROING_START_VELOCITY = Units.RotationsPerSecond.of(7);
public static final Measure<Velocity<Angle>> MANUAL_ZEROING_DELTA_VELOCITY = Units.RotationsPerSecond.of(7);

// -- Current Limiting --
public static final boolean ELEVATOR_ENABLE_CURRENT_LIMITING = true;
public static final double ELEVATOR_CURRENT_LIMIT = 30;
Expand Down Expand Up @@ -643,21 +649,39 @@ public static class constLimelight {

public static class constLEDs {
public static final double LED_BRIGHTNESS = 1;
public static final int LED_NUMBER = 200;
public static final int LED_NUMBER = 192;
public static final int LED_STRIP_START_INDEX = 8;

public static final int[] CLEAR_LEDS = { 0, 0, 0 };
public static final int[] GAME_PIECE_COLLECTED_COLOR = { 0, 0, 255 };
public static final int[] PREP_AMP_COLOR = { 200, 0, 255 };
public static final int[] PREP_SUB_BACKWARDS_COLOR = { 255, 255, 0 };
public static final int[] PREP_SPEAKER_COLOR = { 255, 130, 0 };

public static final ColorFlowAnimation STORE_FEEDER_COLOR = new ColorFlowAnimation(0, 255, 0, 0, 1, LED_NUMBER,
Direction.Forward);
// public static final ColorFlowAnimation STORE_FEEDER_COLOR = new
// ColorFlowAnimation(0, 255, 0, 0, 1, LED_NUMBER,
// Direction.Forward);

public static final StrobeAnimation STORE_FEEDER_COLOR = new StrobeAnimation(0, 255, 0,
0, 0.05, LED_NUMBER, LED_STRIP_START_INDEX);

public static final RainbowAnimation READY_TO_SHOOT_COLOR = new RainbowAnimation();

public static final TwinkleAnimation DISABLED_COLOR_1 = new TwinkleAnimation(0, 255, 255, 0, 0.5, LED_NUMBER / 2,
TwinklePercent.Percent100);
public static final TwinkleAnimation DISABLED_COLOR_2 = new TwinkleAnimation(255, 100, 0, 0, 0.5, LED_NUMBER / 2,
TwinklePercent.Percent100, LED_NUMBER / 2);
public static final TwinkleAnimation DISABLED_COLOR_1 = new TwinkleAnimation(0, 255, 255, 0, 0.5,
(LED_NUMBER / 2) - 5,
TwinklePercent.Percent100,
8);
public static final TwinkleAnimation DISABLED_COLOR_2 = new TwinkleAnimation(255, 100, 0, 0, 0.5,
(LED_NUMBER / 2),
TwinklePercent.Percent100, (LED_NUMBER / 2) + 3);

public static final int[] ELEVATOR_NOT_ZEROED = { 255, 0, 0 };
public static final int[] ELEVATOR_ATTEMPTING_ZERO = { 0, 0, 255 };
public static final int[] ELEVATOR_ZEROED = { 0, 255, 0 };

public static final int[] SHOOTER_NOT_ZEROED = { 255, 0, 0 };
public static final int[] SHOOTER_ATTEMPTING_ZERO = { 0, 0, 255 };
public static final int[] SHOOTER_ZEROED = { 0, 255, 0 };

}
}
16 changes: 15 additions & 1 deletion src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj2.command.Commands;
import frc.robot.Constants.constField;
import frc.robot.subsystems.Elevator;
import frc.robot.subsystems.Shooter;
import frc.robot.subsystems.StateMachine.RobotState;
import frc.robot.subsystems.StateMachine.TargetState;

Expand All @@ -23,6 +25,7 @@ public class Robot extends TimedRobot {
private RobotContainer m_robotContainer;

private boolean hasAutonomousRun = false;
private boolean bothSubsystemsZeroed = false;

@Override
public void robotInit() {
Expand Down Expand Up @@ -53,13 +56,19 @@ public void robotPeriodic() {
public void disabledInit() {
m_robotContainer.subStateMachine.setRobotState(RobotState.NONE);
m_robotContainer.subStateMachine.setTargetState(TargetState.PREP_NONE);

Shooter.hasZeroed = false;
Elevator.hasZeroed = false;

RobotContainer.checkForManualZeroing().schedule();
m_robotContainer.setDisabledLEDs();
}

@Override
public void disabledPeriodic() {
constField.ALLIANCE = DriverStation.getAlliance();
SmartDashboard.putString("ALLIANCE", constField.ALLIANCE.toString());
m_robotContainer.setZeroedLEDs();
}

@Override
Expand All @@ -70,8 +79,11 @@ public void disabledExit() {
@Override
public void autonomousInit() {
m_autonomousCommand = m_robotContainer.getAutonomousCommand();
bothSubsystemsZeroed = Shooter.hasZeroed && Elevator.hasZeroed;

if (m_autonomousCommand != null) {
if (bothSubsystemsZeroed && m_autonomousCommand != null) {
Commands.deferredProxy(() -> m_autonomousCommand).schedule();
} else if (m_autonomousCommand != null) {
RobotContainer.zeroSubsystems().andThen(Commands.deferredProxy(() -> m_autonomousCommand)).schedule();
} else {
RobotContainer.zeroSubsystems().schedule();
Expand All @@ -90,6 +102,8 @@ public void autonomousExit() {

@Override
public void teleopInit() {
bothSubsystemsZeroed = Shooter.hasZeroed && Elevator.hasZeroed;

if (m_autonomousCommand != null) {
m_autonomousCommand.cancel();
}
Expand Down
33 changes: 31 additions & 2 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.function.BooleanSupplier;
import java.util.function.DoubleSupplier;

import com.ctre.phoenix6.SignalLogger;
import com.frcteam3255.joystick.SN_XboxController;
import com.pathplanner.lib.auto.NamedCommands;

Expand All @@ -19,7 +18,6 @@
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.ParallelCommandGroup;
import edu.wpi.first.wpilibj2.command.button.Trigger;
import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine;
import frc.robot.Constants.constControllers;
import frc.robot.Constants.constElevator;
import frc.robot.Constants.constField;
Expand All @@ -35,6 +33,8 @@
import frc.robot.commands.Autos.PreloadTaxi;
import frc.robot.commands.Autos.WingOnly;
import frc.robot.commands.ManualPivot;
import frc.robot.commands.Zeroing.ManualZeroElevator;
import frc.robot.commands.Zeroing.ManualZeroShooterPivot;
import frc.robot.commands.Zeroing.ZeroElevator;
import frc.robot.commands.Zeroing.ZeroShooterPivot;
import frc.robot.commands.States.IntakeSource;
Expand Down Expand Up @@ -312,6 +312,10 @@ public static Command zeroSubsystems() {
return returnedCommand;
}

public static Command checkForManualZeroing() {
return new ManualZeroShooterPivot(subShooter).alongWith(new ManualZeroElevator(subElevator)).ignoringDisable(true);
}

public static Command AddVisionMeasurement() {
return new AddVisionMeasurement(subDrivetrain, subLimelight)
.withInterruptBehavior(Command.InterruptionBehavior.kCancelIncoming).ignoringDisable(true);
Expand All @@ -322,6 +326,31 @@ public void setDisabledLEDs() {
subLEDs.setLEDAnimation(constLEDs.DISABLED_COLOR_2, 1);
}

public void setZeroedLEDs() {
int[] elevatorRGB = new int[3];
int[] shooterRGB = new int[3];

if (Elevator.hasZeroed) {
elevatorRGB = constLEDs.ELEVATOR_ZEROED;
} else if (Elevator.attemptingZeroing) {
elevatorRGB = constLEDs.ELEVATOR_ATTEMPTING_ZERO;
} else {
elevatorRGB = constLEDs.ELEVATOR_NOT_ZEROED;
}

if (Shooter.hasZeroed) {
shooterRGB = constLEDs.SHOOTER_ZEROED;
} else if (Shooter.attemptingZeroing) {
shooterRGB = constLEDs.SHOOTER_ATTEMPTING_ZERO;
} else {
shooterRGB = constLEDs.SHOOTER_NOT_ZEROED;
}

subLEDs.setLEDMatrix(elevatorRGB, 0, 2);
subLEDs.setLEDMatrix(elevatorRGB, 6, 2);
subLEDs.setLEDMatrix(shooterRGB, 2, 4);
}

public void clearLEDs() {
subLEDs.clearAnimation();
subLEDs.setLEDs(constLEDs.CLEAR_LEDS);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/RobotPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static final class prefDrivetrain {

// Rotational speed (degrees per second) while MANUALLY driving
public static final Measure<Velocity<Angle>> maxManualTurnSpeed = Units.DegreesPerSecond.of(30);
public static final Measure<Velocity<Angle>> maxTurnSpeed = Units.DegreesPerSecond.of(520);
public static final Measure<Velocity<Angle>> maxTurnSpeed = Units.DegreesPerSecond.of(520 * 2);

/**
* <p>
Expand Down
89 changes: 89 additions & 0 deletions src/main/java/frc/robot/commands/Zeroing/ManualZeroElevator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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.commands.Zeroing;

import edu.wpi.first.units.Angle;
import edu.wpi.first.units.Measure;
import edu.wpi.first.units.Time;
import edu.wpi.first.units.Units;
import edu.wpi.first.units.Velocity;
import edu.wpi.first.units.Voltage;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.Constants.constElevator;
import frc.robot.subsystems.Elevator;

public class ManualZeroElevator extends Command {
Elevator subElevator;

boolean zeroingSuccess = false;
Measure<Time> zeroingTimestamp = Units.Seconds.zero();

Measure<Velocity<Angle>> lastRotorVelocity = Units.RotationsPerSecond.of(0);

public ManualZeroElevator(Elevator subElevator) {
this.subElevator = subElevator;

addRequirements(subElevator);
}

@Override
public void initialize() {
}

@Override
public void execute() {
// Check if we have raised the shooter above a certain speed
if (subElevator.getRotorVelocity().gte(constElevator.MANUAL_ZEROING_START_VELOCITY)
|| Elevator.attemptingZeroing) {
// Enter zeroing mode!
if (!Elevator.attemptingZeroing) {
Elevator.attemptingZeroing = true;
zeroingTimestamp = Units.Seconds.of(Timer.getFPGATimestamp());

System.out.println("Elevator Zeroing Started!");
}

// Check if time elapsed since previous zeroing is too high - if true, then exit
// zeroing mode :(
if (Units.Seconds.of(Timer.getFPGATimestamp()).minus(zeroingTimestamp).gte(constElevator.ZEROING_TIMEOUT)) {
Elevator.attemptingZeroing = false;
System.out.println("Elevator Zeroing Failed :(");

} else {
boolean rotorVelocity = subElevator.getRotorVelocity().minus(lastRotorVelocity)
.lte(constElevator.MANUAL_ZEROING_DELTA_VELOCITY);

if (rotorVelocity && lastRotorVelocity.lte(Units.RotationsPerSecond.of(0))) {
zeroingSuccess = true;
} else {
lastRotorVelocity = subElevator.getRotorVelocity();
}
}

}

}

@Override
public void end(boolean interrupted) {
if (!interrupted) {
Elevator.hasZeroed = true;
subElevator.setElevatorSensorPosition(constElevator.ZEROED_POS);
System.out.println("Elevator Zeroing Successful!!!! Yippee and hooray!!! :3");
} else {
System.out.println("Elevator was never zeroed :((( blame eli");
}
}

@Override
public boolean isFinished() {
boolean rotorVelocityIsZero = subElevator.getRotorVelocity().isNear(Units.RotationsPerSecond.zero(), 0.01);
SmartDashboard.putBoolean("Zeroing/Pivot/Is Rotor Velocity Zero", rotorVelocityIsZero);
return zeroingSuccess && rotorVelocityIsZero;

}
}
Loading

0 comments on commit 168e102

Please sign in to comment.