Skip to content

Commit

Permalink
Merge branch 'main' into zhangal/phoenixpro
Browse files Browse the repository at this point in the history
  • Loading branch information
mimizh2418 committed Jan 27, 2024
2 parents a1b2215 + 14bbadf commit d71c193
Show file tree
Hide file tree
Showing 13 changed files with 832 additions and 14 deletions.
57 changes: 56 additions & 1 deletion src/main/java/org/team1540/robot2024/Constants.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.team1540.robot2024;

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

/**
Expand All @@ -12,9 +13,10 @@
*/
public final class Constants {
public static final boolean IS_COMPETITION_ROBOT = true;
// Whether to pull PID constants from SmartDashboard
public static final boolean tuningMode = true; // TODO: DO NOT SET TO TRUE FOR COMP
private static final Mode simMode = Mode.SIM; // Can also be Mode.REPLAY


public static final Mode currentMode = Robot.isReal() ? Mode.REAL : simMode;

public enum Mode {
Expand Down Expand Up @@ -55,4 +57,57 @@ public static class Drivetrain {
public static final double DRIVE_BASE_RADIUS = Math.hypot(TRACK_WIDTH_X / 2.0, TRACK_WIDTH_Y / 2.0);
public static final double MAX_ANGULAR_SPEED = MAX_LINEAR_SPEED / DRIVE_BASE_RADIUS;
}

public static class Shooter {
public static class Flywheels {
// TODO: determine ids
public static final int LEFT_ID = 0;
public static final int RIGHT_ID = 0;

public static final double GEAR_RATIO = 24.0 / 36.0;
public static final double SIM_MOI = 4.08232288e-4;

// TODO: if it's tuned in simulation, it's tuned in real life
public static final double KP = 0.4;
public static final double KI = 0.0;
public static final double KD = 0.0;
public static final double KS = 0.01146;
public static final double KV = 0.07485; // TODO: this is what recalc says, may have to tune

public static final double ERROR_TOLERANCE_RPM = 50;
}

public static class Pivot {
// TODO: determine ids
public static final int MOTOR_ID = 0;
public static final int CANCODER_ID = 0;

// TODO: figure this out
public static final double CANCODER_OFFSET_ROTS = 0;
// TODO: determine ratios
public static final double CANCODER_TO_PIVOT = 60.0 / 20.0;
public static final double MOTOR_TO_CANCODER = 33.0;
public static final double TOTAL_GEAR_RATIO = MOTOR_TO_CANCODER * CANCODER_TO_PIVOT;
public static final double SIM_LENGTH_METERS = Units.inchesToMeters(12.910);
// TODO: find the moi
public static final double SIM_MOI = 0.22552744227754662;

public static final Rotation2d MAX_ANGLE = Rotation2d.fromDegrees(60.0);
public static final Rotation2d MIN_ANGLE = Rotation2d.fromDegrees(8.0);

// TODO: tune pid
public static final double KP = 0.1;
public static final double KI = 0.0;
public static final double KD = 0.0;
public static final double KS = 0.0;
public static final double KG = 0.1;
public static final double KV = 0.1;

public static final double CRUISE_VELOCITY_RPS = 4.0;
public static final double MAX_ACCEL_RPS2 = 40.0;
public static final double JERK_RPS3 = 2000;

public static final Rotation2d ERROR_TOLERANCE = Rotation2d.fromDegrees(0.2);
}
}
}
33 changes: 22 additions & 11 deletions src/main/java/org/team1540/robot2024/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;


import org.littletonrobotics.junction.networktables.LoggedDashboardNumber;
import org.team1540.robot2024.commands.FeedForwardCharacterization;
import org.team1540.robot2024.commands.SwerveDriveCommand;
import org.team1540.robot2024.subsystems.drive.*;
import org.team1540.robot2024.subsystems.shooter.*;
import org.team1540.robot2024.util.swerve.SwerveFactory;
import org.littletonrobotics.junction.networktables.LoggedDashboardChooser;

Expand All @@ -27,6 +28,7 @@
public class RobotContainer {
// Subsystems
public final Drivetrain drivetrain;
public final Shooter shooter;

// Controller
public final CommandXboxController driver = new CommandXboxController(0);
Expand All @@ -35,6 +37,10 @@ public class RobotContainer {
// Dashboard inputs
public final LoggedDashboardChooser<Command> autoChooser;

// TODO: testing dashboard inputs, remove for comp
public final LoggedDashboardNumber leftFlywheelSetpoint = new LoggedDashboardNumber("Shooter/Flywheels/leftSetpoint", 6000);
public final LoggedDashboardNumber rightFlywheelSetpoint = new LoggedDashboardNumber("Shooter/Flywheels/rightSetpoint", 6000);

/**
* The container for the robot. Contains subsystems, OI devices, and commands.
*/
Expand All @@ -49,6 +55,7 @@ public RobotContainer() {
new ModuleIOTalonFX(SwerveFactory.getModuleMotors(SwerveConfig.FRONT_RIGHT, SwerveFactory.SwerveCorner.FRONT_RIGHT)),
new ModuleIOTalonFX(SwerveFactory.getModuleMotors(SwerveConfig.BACK_LEFT, SwerveFactory.SwerveCorner.BACK_LEFT)),
new ModuleIOTalonFX(SwerveFactory.getModuleMotors(SwerveConfig.BACK_RIGHT, SwerveFactory.SwerveCorner.BACK_RIGHT)));
shooter = new Shooter(new ShooterPivotIOTalonFX(), new FlywheelsIOTalonFX());
break;

case SIM:
Expand All @@ -60,22 +67,19 @@ public RobotContainer() {
new ModuleIOSim(),
new ModuleIOSim(),
new ModuleIOSim());
shooter = new Shooter(new ShooterPivotIOSim(), new FlywheelsIOSim());
break;

default:
// Replayed robot, disable IO implementations
drivetrain =
new Drivetrain(
new GyroIO() {
},
new ModuleIO() {
},
new ModuleIO() {
},
new ModuleIO() {
},
new ModuleIO() {
});
new GyroIO() {},
new ModuleIO() {},
new ModuleIO() {},
new ModuleIO() {},
new ModuleIO() {});
shooter = new Shooter(new ShooterPivotIO() {}, new FlywheelsIO() {});
break;
}

Expand All @@ -88,6 +92,10 @@ public RobotContainer() {
"Drive FF Characterization",
new FeedForwardCharacterization(
drivetrain, drivetrain::runCharacterizationVolts, drivetrain::getCharacterizationVelocity));
autoChooser.addOption(
"Flywheels FF Characterization",
new FeedForwardCharacterization(
shooter, volts -> shooter.setFlywheelVolts(volts, volts), () -> shooter.getLeftFlywheelSpeed() / 60));

// Configure the button bindings
configureButtonBindings();
Expand All @@ -108,6 +116,9 @@ private void configureButtonBindings() {
drivetrain
).ignoringDisable(true)
);

copilot.a().onTrue(shooter.spinUpCommand(leftFlywheelSetpoint.get(), rightFlywheelSetpoint.get()))
.onFalse(Commands.runOnce(shooter::stopFlywheels, shooter));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Subsystem;
import org.team1540.robot2024.util.PolynomialRegression;
import org.team1540.robot2024.util.math.PolynomialRegression;

import java.util.LinkedList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.team1540.robot2024.subsystems.shooter;

import org.littletonrobotics.junction.AutoLog;

public interface FlywheelsIO {
@AutoLog
class FlywheelsIOInputs {
public double leftAppliedVolts = 0.0;
public double leftCurrentAmps = 0.0;
public double leftVelocityRPM = 0.0;

public double rightAppliedVolts = 0.0;
public double rightCurrentAmps = 0.0;
public double rightVelocityRPM = 0.0;
}

/**
* Updates the set of loggable inputs
*/
default void updateInputs(FlywheelsIOInputs inputs) {}

/**
* Runs open loop at the specified voltages
*/
default void setVoltage(double leftVolts, double rightVolts) {}


/**
* Runs closed loop at the specified RPMs
*/
default void setSpeeds(double leftRPM, double rightRPM) {}

/**
* Configures the PID controller
*/
default void configPID(double kP, double kI, double kD) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.team1540.robot2024.subsystems.shooter;

import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.wpilibj.simulation.FlywheelSim;
import org.team1540.robot2024.Constants;

import static org.team1540.robot2024.Constants.Shooter.Flywheels.*;

public class FlywheelsIOSim implements FlywheelsIO{
private final FlywheelSim leftSim =
new FlywheelSim(DCMotor.getFalcon500(1), GEAR_RATIO, SIM_MOI);
private final FlywheelSim rightSim =
new FlywheelSim(DCMotor.getFalcon500(1), GEAR_RATIO, SIM_MOI);

private final PIDController rightController = new PIDController(KP, KI, KD);
private final PIDController leftController = new PIDController(KP, KI, KD);
private final SimpleMotorFeedforward feedforward = new SimpleMotorFeedforward(KS, KV);

private boolean isClosedLoop;
private double leftSetpointRPS;
private double rightSetpointRPS;

private double leftVolts = 0.0;
private double rightVolts = 0.0;

@Override
public void updateInputs(FlywheelsIOInputs inputs) {
if (isClosedLoop) {
leftVolts =
leftController.calculate(leftSim.getAngularVelocityRPM() / 60, leftSetpointRPS)
+ feedforward.calculate(leftSetpointRPS);
rightVolts =
rightController.calculate(rightSim.getAngularVelocityRPM() / 60, rightSetpointRPS)
+ feedforward.calculate(rightSetpointRPS);
}

leftSim.setInputVoltage(leftVolts);
rightSim.setInputVoltage(rightVolts);
leftSim.update(Constants.LOOP_PERIOD_SECS);
rightSim.update(Constants.LOOP_PERIOD_SECS);

inputs.leftVelocityRPM = leftSim.getAngularVelocityRPM();
inputs.leftAppliedVolts = leftVolts;
inputs.leftCurrentAmps = leftSim.getCurrentDrawAmps();

inputs.rightVelocityRPM = rightSim.getAngularVelocityRPM();
inputs.rightAppliedVolts = rightVolts;
inputs.rightCurrentAmps = rightSim.getCurrentDrawAmps();
}

@Override
public void setVoltage(double leftVolts, double rightVolts) {
isClosedLoop = false;
this.leftVolts = leftVolts;
this.rightVolts = rightVolts;
}

@Override
public void setSpeeds(double leftRPM, double rightRPM) {
isClosedLoop = true;
leftController.reset();
rightController.reset();
leftSetpointRPS = leftRPM / 60;
rightSetpointRPS = rightRPM / 60;
}

@Override
public void configPID(double kP, double kI, double kD) {
leftController.setPID(kP, kI, kD);
rightController.setPID(kP, kI, kD);
}
}
Loading

0 comments on commit d71c193

Please sign in to comment.