Skip to content

Commit

Permalink
Chain mech + SVR Shooter Auto
Browse files Browse the repository at this point in the history
* Added code for nextState from FSM

* adjustments to shooter mech

* Fix Checkstyles

Fixed All checkstyle issues with the code.
Please make sure you have the plugin
enabled when writing the code and please follow the checkstyles

* fixed checkstyles again

* Fixed Shooter Code for Testing

* finalized shooter code

* addressing svr sw mech pr adjustments

* chain mech complete

* restoring chain mech files

* added auto functions

* fixed merge conflicts

* fixed checkstyle

* resolving pr comments

---------

Co-authored-by: Araav <nayakaraav@gmail.com>
Co-authored-by: Aaron <143990364+Adbritto@users.noreply.github.com>
Co-authored-by: Daiji <duchino292@student.fuhsd.org>
Co-authored-by: Akshay Anand <95551900+AknA13@users.noreply.github.com>
  • Loading branch information
5 people authored Jan 24, 2024
1 parent f10d043 commit 25387d1
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 48 deletions.
9 changes: 6 additions & 3 deletions src/main/java/frc/robot/HardwareMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
*/
public final class HardwareMap {
// ID numbers for devices on the CAN bus
public static final int CAN_ID_SPARK_SHOOTER_UPPER = 31;
public static final int CAN_ID_SPARK_SHOOTER_LOWER = 30;

public static final int CAN_ID_SPARK_LEFT_CLIMBER_MOTOR = 5;
public static final int CAN_ID_SPARK_RIGHT_CLIMBER_MOTOR = 32;

public static final int FRONT_LEFT_DRIVING_CAN_ID = 8;
public static final int FRONT_RIGHT_DRIVING_CAN_ID = 6;
public static final int REAR_LEFT_DRIVING_CAN_ID = 2;
Expand All @@ -15,7 +21,4 @@ public final class HardwareMap {
public static final int FRONT_RIGHT_TURNING_CAN_ID = 5;
public static final int REAR_LEFT_TURNING_CAN_ID = 1;
public static final int REAR_RIGHT_TURNING_CAN_ID = 3;

public static final int CAN_ID_SPARK_SHOOTER_UPPER = 31;
public static final int CAN_ID_SPARK_SHOOTER_LOWER = 32;
}
16 changes: 14 additions & 2 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import frc.robot.systems.DriveFSMSystem;
import frc.robot.systems.KitBotShooterFSM;
import frc.robot.systems.AutoHandlerSystem;
import frc.robot.systems.ClimberMechFSMLeft;
import frc.robot.systems.ClimberMechFSMRight;
import frc.robot.systems.AutoHandlerSystem.AutoPath;

/**
Expand All @@ -18,8 +20,11 @@
public class Robot extends TimedRobot {
private TeleopInput input;
// Systems
private DriveFSMSystem driveFSMSystem;
private KitBotShooterFSM shooterFSM;
private ClimberMechFSMLeft climberMechLeftFSM;
private ClimberMechFSMRight climberMechRightFSM;
private DriveFSMSystem driveFSMSystem;

private AutoHandlerSystem autoHandler;
private AutoPathChooser autoPathChooser;

Expand All @@ -32,9 +37,12 @@ public void robotInit() {
System.out.println("robotInit");
input = new TeleopInput();
// Instantiate all systems here
shooterFSM = new KitBotShooterFSM();
climberMechLeftFSM = new ClimberMechFSMLeft();
climberMechRightFSM = new ClimberMechFSMRight();

autoPathChooser = new AutoPathChooser();
driveFSMSystem = new DriveFSMSystem();
shooterFSM = new KitBotShooterFSM();
autoHandler = new AutoHandlerSystem(driveFSMSystem, shooterFSM);
}

Expand All @@ -56,12 +64,16 @@ public void autonomousPeriodic() {
@Override
public void teleopInit() {
System.out.println("-------- Teleop Init --------");
climberMechLeftFSM.reset();
climberMechRightFSM.reset();
driveFSMSystem.reset();
shooterFSM.reset();
}

@Override
public void teleopPeriodic() {
climberMechLeftFSM.update(input);
climberMechRightFSM.update(input);
driveFSMSystem.update(input);
shooterFSM.update(input);
}
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/frc/robot/TeleopInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
public class TeleopInput {
/* ======================== Constants ======================== */
private static final int LEFT_JOYSTICK_PORT = 1;
private static final int RETRACT_CLIMBER_BUTTON = 3;
private static final int INTAKE_BUTTON = 2;
private static final int OUTTAKE_BUTTON = 1;
public static final int DRIVER_CONTROLLER_PORT = 0;
Expand Down Expand Up @@ -119,20 +120,15 @@ public double getLeftJoystickX() {
public double getLeftJoystickY() {
return leftJoystick.getY();
}
/**
* Get the value of the shooter button.
* @return True if button is pressed
*/
public boolean isShooterButtonPressed() {
return leftJoystick.getRawButton(1);
}

/**
* Get the value of the intake button.
* @return True if button is pressed
*/
public boolean isIntakeButtonPressed() {
return leftJoystick.getRawButton(INTAKE_BUTTON);
}

/**
* Get the value of the outtake button.
* @return True if button is pressed
Expand All @@ -141,6 +137,13 @@ public boolean isOuttakeButtonPressed() {
return leftJoystick.getRawButton(OUTTAKE_BUTTON);
}

/**
* Get the value of the retract button.
* @return True if button is pressed
*/
public boolean isRetractClimberButtonPressed() {
return leftJoystick.getRawButton(RETRACT_CLIMBER_BUTTON);
}
/* ======================== Private methods ======================== */

}
17 changes: 6 additions & 11 deletions src/main/java/frc/robot/systems/AutoHandlerSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ public enum AutoFSMState {
DRIVE_PATH_4_STATE_1,
DRIVE_PATH_4_STATE_2,
DRIVE_PATH_5,
SHOOTER_STATE_1,
SHOOTER_STATE_2,
SHOOTER_STATE_3,
SHOOTER_STATE,
INTAKE_STATE,
PENDING
}
public enum AutoPath {
Expand Down Expand Up @@ -88,7 +87,6 @@ public AutoFSMState getCurrentState() {
* @param path the auto path to be executed
*/
public void reset(AutoPath path) {

driveSystem.resetAutonomus();
shooterFSM.reset();

Expand Down Expand Up @@ -139,14 +137,11 @@ public void update() {
case DRIVE_PATH_5:
isCurrentStateFinished = driveSystem.updateAutonomous(AutoFSMState.DRIVE_PATH_5);
break;
case SHOOTER_STATE_1:
isCurrentStateFinished = shooterFSM.updateAutonomous(AutoFSMState.SHOOTER_STATE_1);
break;
case SHOOTER_STATE_2:
isCurrentStateFinished = shooterFSM.updateAutonomous(AutoFSMState.SHOOTER_STATE_2);
case SHOOTER_STATE:
isCurrentStateFinished = shooterFSM.updateAutonomous(AutoFSMState.SHOOTER_STATE);
break;
case SHOOTER_STATE_3:
isCurrentStateFinished = shooterFSM.updateAutonomous(AutoFSMState.SHOOTER_STATE_3);
case INTAKE_STATE:
isCurrentStateFinished = shooterFSM.updateAutonomous(AutoFSMState.INTAKE_STATE);
break;
case PENDING:
isCurrentStateFinished = driveSystem.updateAutonomous(AutoFSMState.PENDING);
Expand Down
173 changes: 173 additions & 0 deletions src/main/java/frc/robot/systems/ClimberMechFSMLeft.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package frc.robot.systems;

// WPILib Imports
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

// Third party Hardware Imports
import com.revrobotics.CANSparkMax;
import com.revrobotics.SparkLimitSwitch;

// Robot Imports
import frc.robot.TeleopInput;
import frc.robot.HardwareMap;
import frc.robot.systems.AutoHandlerSystem.AutoFSMState;

public class ClimberMechFSMLeft {
/* ======================== Constants ======================== */
// FSM state definitions
public enum ClimberMechFSMState {
IDLE_STOP,
RETRACTING
}

private static final float MOTOR_RUN_POWER = -0.1f;
private boolean limitPressed = false;


/* ======================== Private variables ======================== */
private ClimberMechFSMState currentState;

// Hardware devices should be owned by one and only one system. They must
// be private to their owner system and may not be used elsewhere.
private CANSparkMax motor;
private SparkLimitSwitch peakLimitSwitch;

/* ======================== Constructor ======================== */
/**
* Create FSMSystem and initialize to starting state. Also perform any
* one-time initialization or configuration of hardware required. Note
* the constructor is called only once when the robot boots.
*/
public ClimberMechFSMLeft() {
// Perform hardware init
motor = new CANSparkMax(HardwareMap.CAN_ID_SPARK_LEFT_CLIMBER_MOTOR,
CANSparkMax.MotorType.kBrushless);
motor.setIdleMode(CANSparkMax.IdleMode.kBrake);

peakLimitSwitch = motor.getReverseLimitSwitch(SparkLimitSwitch.Type.kNormallyClosed);
peakLimitSwitch.enableLimitSwitch(false);

// Reset state machine
reset();
}

/* ======================== Public methods ======================== */
/**
* Return current FSM state.
* @return Current FSM state
*/
public ClimberMechFSMState getCurrentState() {
return currentState;
}
/**
* Reset this system to its start state. This may be called from mode init
* when the robot is enabled.
*
* Note this is distinct from the one-time initialization in the constructor
* as it may be called multiple times in a boot cycle,
* Ex. if the robot is enabled, disabled, then reenabled.
*/
public void reset() {
currentState = ClimberMechFSMState.IDLE_STOP;

// Call one tick of update to ensure outputs reflect start state
update(null);
}

/**
* Update FSM based on new inputs. This function only calls the FSM state
* specific handlers.
* @param input Global TeleopInput if robot in teleop mode or null if
* the robot is in autonomous mode.
*/
public void update(TeleopInput input) {
if (peakLimitSwitch.isPressed()) {
limitPressed = true;
}

if (input == null) {
return;
}

switch (currentState) {
case IDLE_STOP:
handleIdleState(input);
break;
case RETRACTING:
handleRetractingState(input);
break;
default:
throw new IllegalStateException("Invalid state: " + currentState.toString());
}
SmartDashboard.putString("Current State", currentState.toString());
SmartDashboard.putBoolean("Bottom Limit Switch Pressed", peakLimitSwitchHit());
SmartDashboard.putBoolean("Retract Button Pressed", input.isRetractClimberButtonPressed());
currentState = nextState(input);
}

/**
* Performs specific action based on the autoState passed in.
* @param autoState autoState that the subsystem executes.
* @return if the action carried out in this state has finished executing
*/
public boolean updateAutonomous(AutoFSMState autoState) {
switch (autoState) {
default:
return true;
}
}

/* ======================== Private methods ======================== */
/**
* Decide the next state to transition to. This is a function of the inputs
* and the current state of this FSM. This method should not have any side
* effects on outputs. In other words, this method should only read or get
* values to decide what state to go to.
* @param input Global TeleopInput if robot in teleop mode or null if
* the robot is in autonomous mode.
* @return FSM state for the next iteration
*/
private ClimberMechFSMState nextState(TeleopInput input) {
switch (currentState) {
case IDLE_STOP:
if (input.isRetractClimberButtonPressed() && !peakLimitSwitchHit()) {
return ClimberMechFSMState.RETRACTING;
} else {
return ClimberMechFSMState.IDLE_STOP;
}
case RETRACTING:
if (input.isRetractClimberButtonPressed() && !peakLimitSwitchHit()) {
return ClimberMechFSMState.RETRACTING;
} else {
return ClimberMechFSMState.IDLE_STOP;
}
default:
throw new IllegalStateException("Invalid state: " + currentState.toString());
}
}

/* ------------------------ FSM state handlers ------------------------ */
/**
* Handle behavior in START_STATE.
* @param input Global TeleopInput if robot in teleop mode or null if
* the robot is in autonomous mode.
*/
private void handleIdleState(TeleopInput input) {
motor.set(0);
}
/**
* Handle behavior in RETRACTING state.
* @param input Global TeleopInput if robot in teleop mode or null if
* the robot is in autonomous mode.
*/
private void handleRetractingState(TeleopInput input) {
motor.set(-MOTOR_RUN_POWER);
}

private boolean peakLimitSwitchHit() {
return limitPressed;
}



}
Loading

0 comments on commit 25387d1

Please sign in to comment.