Skip to content

Commit

Permalink
Merge branch 'main' into swerve
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchen20 committed Feb 19, 2024
2 parents b3d1fe9 + 5eca808 commit f4f9e6e
Show file tree
Hide file tree
Showing 13 changed files with 363 additions and 17 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Main",
"request": "launch",
"mainClass": "frc.robot.Main",
"projectName": "RobotCode2024"
},
{
"type": "wpilib",
"name": "WPILib Desktop Debug",
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
12 changes: 6 additions & 6 deletions src/main/java/frc/robot/BuildConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ public final class BuildConstants {
public static final String MAVEN_GROUP = "";
public static final String MAVEN_NAME = "2024RobotCode";
public static final String VERSION = "unspecified";
public static final int GIT_REVISION = 8;
public static final String GIT_SHA = "71b8574ac4f6d4c376624c57919d7cfb2822c53e";
public static final String GIT_DATE = "2024-02-03 14:28:25 EST";
public static final String GIT_BRANCH = "swerve";
public static final String BUILD_DATE = "2024-02-08 19:59:29 EST";
public static final long BUILD_UNIX_TIME = 1707440369951L;
public static final int GIT_REVISION = 12;
public static final String GIT_SHA = "65f9e459903602ab0738d226c636f60ff3e90899";
public static final String GIT_DATE = "2024-02-17 16:34:27 EST";
public static final String GIT_BRANCH = "CSnair7/2024RobotCode-Intake";
public static final String BUILD_DATE = "2024-02-18 21:07:06 EST";
public static final long BUILD_UNIX_TIME = 1708308426616L;
public static final int DIRTY = 1;

private BuildConstants() {}
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* constants are needed, to reduce verbosity.
*/
public final class Constants {

public static class SwerveConstants {
public static final double MAX_LINEAR_SPEED = 5.56;
public static final double TRACK_WIDTH_X = Units.inchesToMeters(26.0);
Expand All @@ -41,20 +42,21 @@ public static class ModuleConstants {
public static final double TURN_STATOR_CURRENT_LIMIT = 30.0;
public static final boolean TURN_STATOR_CURRENT_LIMIT_ENABLED = true;
}

public static final Mode currentMode = Mode.REAL;

public static final boolean tuningMode = true;

public static final String CANBUS = "CAN Bus 2";

public static enum Mode {
/** Running on a real robot. */
REAL,

/** Running a physics simulator. */
SIM,

/** Replaying from a log file. */
REPLAY
}

public static final Mode currentMode = Mode.SIM;

public static class IntakeConstants {
public static final int ROLLER_CURRENT_LIMIT = 30;
public static final boolean ROLLER_TALON_FX_CURRENT_LIMIT_ENABLED = true;
}
}
2 changes: 2 additions & 0 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Robot extends LoggedRobot {
private Command autonomousCommand;
private RobotContainer robotContainer;


/**
* This function is run when the robot is first started up and should be used for any
* initialization code.
Expand Down Expand Up @@ -87,6 +88,7 @@ public void robotInit() {
// Instantiate our RobotContainer. This will perform all our button bindings,
// and put our autonomous chooser on the dashboard.
robotContainer = new RobotContainer();

}

/** This function is called periodically during all modes. */
Expand Down
38 changes: 34 additions & 4 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
import frc.robot.subsystems.drive.ModuleIOTalonFX;
import org.littletonrobotics.junction.networktables.LoggedDashboardChooser;

import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.button.Trigger;
import frc.robot.commands.Autos;
import frc.robot.commands.ExampleCommand;
import frc.robot.subsystems.ExampleSubsystem;
import frc.robot.subsystems.intake.Intake;
import frc.robot.subsystems.intake.IntakeRollerIOSim;
import frc.robot.subsystems.intake.IntakeRollerIOSparkFlex;


/**
* This class is where the bulk of the robot should be declared. Since Command-based is a
* "declarative" paradigm, very little robot logic should actually be handled in the {@link Robot}
Expand All @@ -41,12 +51,12 @@ public class RobotContainer {
// Subsystems
private final Drive drive;

// Controller
private final CommandXboxController controller = new CommandXboxController(0);

// Dashboard inputs
private final CommandXboxController controller = new CommandXboxController(0);
private final LoggedDashboardChooser<Command> autoChooser;

private final Intake intake;

/** The container for the robot. Contains subsystems, OI devices, and commands. */
public RobotContainer() {
switch (Constants.currentMode) {
Expand All @@ -66,9 +76,19 @@ public RobotContainer() {
new ModuleIOTalonFX(1),
new ModuleIOTalonFX(2),
new ModuleIOTalonFX(3));
intake = new Intake(new IntakeRollerIOSparkFlex(RobotMap.IntakeIDs.ROLLERS));
// flywheel = new Flywheel(new FlywheelIOTalonFX());
break;

case REPLAY:
drive =
new Drive(
new GyroIO() {},
new ModuleIOSim(),
new ModuleIOSim(),
new ModuleIOSim(),
new ModuleIOSim());
intake = new Intake(new IntakeRollerIOSim());
break;
case SIM:
// Sim robot, instantiate physics sim IO implementations
drive =
Expand All @@ -78,10 +98,12 @@ public RobotContainer() {
new ModuleIOSim(),
new ModuleIOSim(),
new ModuleIOSim());
intake = new Intake(new IntakeRollerIOSim());
break;

default:
// Replayed robot, disable IO implementations
intake = new Intake(new IntakeRollerIOSparkFlex(RobotMap.IntakeIDs.ROLLERS));
drive =
new Drive(
new GyroIO() {},
Expand All @@ -102,6 +124,7 @@ public RobotContainer() {
drive, drive::runCharacterizationVolts, drive::getCharacterizationVelocity));
// Configure the button bindings
configureButtonBindings();

}

/**
Expand All @@ -110,6 +133,7 @@ public RobotContainer() {
* edu.wpi.first.wpilibj.Joystick} or {@link XboxController}), and then passing it to a {@link
* edu.wpi.first.wpilibj2.command.button.JoystickButton}.
*/

private void configureButtonBindings() {
drive.setDefaultCommand(
DriveCommands.joystickDrive(
Expand All @@ -127,6 +151,12 @@ private void configureButtonBindings() {
new Pose2d(drive.getPose().getTranslation(), new Rotation2d())),
drive)
.ignoringDisable(true));


// Schedule `exampleMethodCommand` when the Xbox controller's B button is pressed,
// cancelling on release.
m_driverController.b().onTrue(new InstantCommand(() -> intake.runRollers(3)));
m_driverController.b().onFalse(new InstantCommand(() -> intake.stopRollers()));
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/frc/robot/RobotMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package frc.robot;

public class RobotMap {
public static class IntakeIDs {
public static final int ROLLERS = 10;
}
}
57 changes: 57 additions & 0 deletions src/main/java/frc/robot/subsystems/intake/Intake.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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.subsystems.intake;

import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants;
import org.littletonrobotics.junction.Logger;

public class Intake extends SubsystemBase {
/** Creates a new Intake. */
private final IntakeRollerIO roller;

private final IntakeRollerIOInputsAutoLogged rInputs = new IntakeRollerIOInputsAutoLogged();

private final SimpleMotorFeedforward ffModel;

public Intake(IntakeRollerIO roller) {
switch (Constants.currentMode) {
case REAL:
ffModel = new SimpleMotorFeedforward(0, 0);
break;
case REPLAY:
ffModel = new SimpleMotorFeedforward(0, 0);
break;
case SIM:
ffModel = new SimpleMotorFeedforward(0, 0.8);
break;
default:
ffModel = new SimpleMotorFeedforward(0, 0);
break;
}

this.roller = roller;

// make this a constant
roller.configurePID(0.5, 0, 0);
}

public void runRollers(double velocity) {
roller.setVelocity(velocity, ffModel.calculate(velocity));
}

public void stopRollers() {
roller.stop();
}

@Override
public void periodic() {
// This method will be called once per scheduler run
roller.updateInputs(rInputs);

Logger.processInputs("Intake", rInputs);
}
}
26 changes: 26 additions & 0 deletions src/main/java/frc/robot/subsystems/intake/IntakeRollerIO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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.subsystems.intake;

import org.littletonrobotics.junction.AutoLog;

/** Add your docs here. */
public interface IntakeRollerIO {
@AutoLog
public static class IntakeRollerIOInputs {
public double rollerVelocity = 0.0;

public double appliedVolts = 0.0;
public double currentAmps = 0.0;
}

public default void updateInputs(IntakeRollerIOInputs inputs) {}

public default void setVelocity(double velocity, double ffVolts) {}

public default void stop() {}

public default void configurePID(double kP, double kI, double kD) {}
}
49 changes: 49 additions & 0 deletions src/main/java/frc/robot/subsystems/intake/IntakeRollerIOSim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package frc.robot.subsystems.intake;

import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.wpilibj.simulation.DCMotorSim;

public class IntakeRollerIOSim implements IntakeRollerIO {
private final DCMotor simGearbox = DCMotor.getNeoVortex(1);
private DCMotorSim sim = new DCMotorSim(simGearbox, 1, 0.7);
private PIDController pid = new PIDController(0.2, 0, 0);

private boolean closedLoop = false;
private double ffVolts = 0.0;
private double appliedVolts = 0.0;

@Override
public void updateInputs(IntakeRollerIOInputs inputs) {
if (closedLoop) {
appliedVolts =
MathUtil.clamp(pid.calculate(sim.getAngularVelocityRPM()) + ffVolts, -12.0, 12.0);
sim.setInputVoltage(appliedVolts);
}

sim.update(0.02);
inputs.rollerVelocity = sim.getAngularVelocityRPM();
inputs.rollerVelocity = sim.getAngularVelocityRPM();
inputs.appliedVolts = appliedVolts;
inputs.currentAmps = sim.getCurrentDrawAmps();
}

@Override
public void setVelocity(double velocity, double ffVolts) {
closedLoop = true;
pid.setSetpoint(velocity);
this.ffVolts = ffVolts;
}

@Override
public void stop() {
closedLoop = false;
setVelocity(0, 0);
}

@Override
public void configurePID(double kP, double kI, double kD) {
pid.setPID(kP, kI, kD);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package frc.robot.subsystems.intake;

import com.revrobotics.CANSparkBase.ControlType;
import com.revrobotics.CANSparkBase.IdleMode;
import com.revrobotics.CANSparkFlex;
import com.revrobotics.CANSparkLowLevel.MotorType;
import com.revrobotics.SparkPIDController;
import com.revrobotics.SparkPIDController.ArbFFUnits;
import frc.robot.Constants;

public class IntakeRollerIOSparkFlex implements IntakeRollerIO {
private final CANSparkFlex rollers;
private final SparkPIDController pid;

public IntakeRollerIOSparkFlex(int id) {
rollers = new CANSparkFlex(id, MotorType.kBrushless);
rollers.restoreFactoryDefaults();
rollers.setIdleMode(IdleMode.kCoast);

rollers.setSmartCurrentLimit(Constants.IntakeConstants.ROLLER_CURRENT_LIMIT);
rollers.setCANTimeout(250);
rollers.burnFlash();

pid = rollers.getPIDController();
}

@Override
public void updateInputs(IntakeRollerIOInputs inputs) {
inputs.rollerVelocity = rollers.getEncoder().getVelocity();

inputs.appliedVolts = rollers.getBusVoltage();
inputs.currentAmps = rollers.getOutputCurrent();
}

@Override
public void setVelocity(double velocity, double ffVolts) {
pid.setReference(velocity, ControlType.kVelocity, 0, ffVolts, ArbFFUnits.kVoltage);
}

@Override
public void stop() {
rollers.stopMotor();
}

@Override
public void configurePID(double kP, double kI, double kD) {
pid.setP(kP, 0);
pid.setI(kI, 0);
pid.setD(kD, 0);
}
}
Loading

0 comments on commit f4f9e6e

Please sign in to comment.