generated from yeti-robotics/yeti-robot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
made flywheel(shooter) subsystem #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
968fde0
made flywheel(shooter) subsystem
Srijan-Murai 5e9c379
removed sim file, renamed motors and used MotionMagicVelocityTorqueCu…
Srijan-Murai 5bbcac7
added physicssim and withOutput
Srijan-Murai 6e30178
controller binding for flywheel
Srijan-Murai 2847609
added physicssim, used voltagerequest, added bindings
Srijan-Murai 612be04
switched from VoltageOut to MotionMagicVelocityVoltage
Srijan-Murai f5d87a9
removed unnecessary comments and renamed power to velocity
Srijan-Murai d195e0c
Merge branch 'main' into flywheel
samperlmutter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,4 +137,3 @@ public void simulationInit() {} | |
| @Override | ||
| public void simulationPeriodic() {} | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package frc.robot.subsystems.flywheel; | ||
|
|
||
| import edu.wpi.first.wpilibj2.command.Command; | ||
| import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
| import org.littletonrobotics.junction.Logger; | ||
|
|
||
| public class Flywheel extends SubsystemBase { | ||
| // Auto logged classes for AKit | ||
| private final FlywheelIO io; | ||
| private final FlywheelIOInputsAutoLogged inputs = new FlywheelIOInputsAutoLogged(); | ||
|
|
||
| @Override | ||
| public void periodic() { | ||
| io.updateInputs(inputs); | ||
| Logger.processInputs("Flywheel", inputs); | ||
| } | ||
|
|
||
| public Flywheel(FlywheelIO io) { | ||
| this.io = io; | ||
| } | ||
|
|
||
| public Command setRoller(double velocity) { | ||
| return runEnd( | ||
| () -> { | ||
| io.setRoller(velocity); | ||
| }, | ||
| () -> { | ||
| io.setRoller(0); | ||
| }); | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/main/java/frc/robot/subsystems/flywheel/FlywheelConfigs.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package frc.robot.subsystems.flywheel; | ||
|
|
||
| import com.ctre.phoenix6.configs.MotorOutputConfigs; | ||
| import com.ctre.phoenix6.configs.TalonFXConfiguration; | ||
| import com.ctre.phoenix6.signals.InvertedValue; | ||
|
|
||
| public class FlywheelConfigs { | ||
| static final int leftMotorID = 41; | ||
| static final int rightMotorID = 67; | ||
|
|
||
| static TalonFXConfiguration rollerConfig = | ||
| new TalonFXConfiguration() | ||
| .withMotorOutput( | ||
| new MotorOutputConfigs() | ||
| .withInverted(InvertedValue.Clockwise_Positive)); | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/frc/robot/subsystems/flywheel/FlywheelIO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package frc.robot.subsystems.flywheel; | ||
|
|
||
| import org.littletonrobotics.junction.AutoLog; | ||
|
|
||
| public interface FlywheelIO { | ||
|
|
||
| @AutoLog | ||
| public static class FlywheelIOInputs { | ||
| public double leftMotorVelocityRPM = 0; | ||
| public double leftMotorSpeed = 0; | ||
|
|
||
| public double rightMotorVelocityRPM = 0; | ||
| public double rightMotorSpeed = 0; | ||
| } | ||
|
|
||
| public default void updateInputs(FlywheelIOInputs inputs) {} | ||
|
|
||
| public default void setRoller(double velocity) {} | ||
| } |
42 changes: 42 additions & 0 deletions
42
src/main/java/frc/robot/subsystems/flywheel/FlywheelIOTalonFX.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package frc.robot.subsystems.flywheel; | ||
|
|
||
| import com.ctre.phoenix6.controls.Follower; | ||
| import com.ctre.phoenix6.controls.MotionMagicVelocityVoltage; | ||
| import com.ctre.phoenix6.controls.VoltageOut; | ||
| import com.ctre.phoenix6.hardware.TalonFX; | ||
| import frc.robot.Robot; | ||
| import frc.robot.util.sim.PhysicsSim; | ||
|
|
||
| public class FlywheelIOTalonFX implements FlywheelIO { | ||
| private final TalonFX leftMotor; | ||
| private final TalonFX rightMotor; | ||
|
|
||
| private final MotionMagicVelocityVoltage motionRequest = new MotionMagicVelocityVoltage(0); | ||
|
|
||
| public FlywheelIOTalonFX() { | ||
| leftMotor = new TalonFX(FlywheelConfigs.leftMotorID); | ||
| rightMotor = new TalonFX(FlywheelConfigs.rightMotorID); | ||
| // make left a follower of right | ||
| leftMotor.setControl(new Follower(rightMotor.getDeviceID(), false)); | ||
| leftMotor.getConfigurator().apply(FlywheelConfigs.rollerConfig); | ||
| rightMotor.getConfigurator().apply(FlywheelConfigs.rollerConfig); | ||
Srijan-Murai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (Robot.isSimulation()) { | ||
| PhysicsSim.getInstance().addTalonFX(leftMotor); | ||
| PhysicsSim.getInstance().addTalonFX(rightMotor); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void updateInputs(FlywheelIO.FlywheelIOInputs inputs) { | ||
| inputs.leftMotorVelocityRPM = leftMotor.getVelocity().getValueAsDouble(); | ||
| inputs.leftMotorSpeed = leftMotor.getDutyCycle().getValueAsDouble(); | ||
|
|
||
| inputs.rightMotorVelocityRPM = rightMotor.getVelocity().getValueAsDouble(); | ||
| inputs.rightMotorSpeed = rightMotor.getDutyCycle().getValueAsDouble(); | ||
| } | ||
|
|
||
| @Override | ||
| public void setRoller(double velocity) { | ||
| leftMotor.setControl(motionRequest.withVelocity(velocity)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package frc.robot.util.sim; | ||
|
|
||
| import com.ctre.phoenix6.Utils; | ||
| import com.ctre.phoenix6.hardware.CANcoder; | ||
| import com.ctre.phoenix6.hardware.TalonFX; | ||
| import java.util.ArrayList; | ||
|
|
||
| /** Manages physics simulation for CTRE products. */ | ||
| public class PhysicsSim { | ||
| private static final PhysicsSim sim = new PhysicsSim(); | ||
| private final ArrayList<SimProfile> simProfiles = new ArrayList<>(); | ||
|
|
||
| /** Gets the robot simulator instance. */ | ||
| public static PhysicsSim getInstance() { | ||
| return sim; | ||
| } | ||
|
|
||
| /** | ||
| * Adds a TalonFX controller to the simulator. | ||
| * | ||
| * @param talonFX The TalonFX device | ||
| */ | ||
| public void addTalonFX(TalonFX talonFX) { | ||
| if (talonFX != null) { | ||
| TalonFXSimProfile simTalonFX = new TalonFXSimProfile(talonFX, 0.001); | ||
| simProfiles.add(simTalonFX); | ||
| } | ||
| } | ||
|
|
||
| public void addTalonFX(TalonFX talonFX, CANcoder cancoder) { | ||
| if (talonFX != null && cancoder != null) { | ||
| TalonFXSimProfile simTalonFX = new TalonFXSimProfile(talonFX, 0.001, cancoder); | ||
| simProfiles.add(simTalonFX); | ||
| } | ||
| } | ||
|
|
||
| /** Runs the simulator: - enable the robot - simulate sensors */ | ||
| public void run() { | ||
| // Simulate devices | ||
| for (SimProfile simProfile : simProfiles) { | ||
| simProfile.run(); | ||
| } | ||
| } | ||
|
|
||
| /** Holds information about a simulated device. */ | ||
| static class SimProfile { | ||
| private double _lastTime; | ||
| private boolean _running = false; | ||
|
|
||
| /** Runs the simulation profile. Implemented by device-specific profiles. */ | ||
| public void run() {} | ||
|
|
||
| /** Returns the time since last call, in seconds. */ | ||
| protected double getPeriod() { | ||
| // set the start time if not yet running | ||
| if (!_running) { | ||
| _lastTime = Utils.getCurrentTimeSeconds(); | ||
| _running = true; | ||
| } | ||
|
|
||
| double now = Utils.getCurrentTimeSeconds(); | ||
| final double period = now - _lastTime; | ||
| _lastTime = now; | ||
|
|
||
| return period; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package frc.robot.util.sim; | ||
|
|
||
| import com.ctre.phoenix6.hardware.CANcoder; | ||
| import com.ctre.phoenix6.hardware.TalonFX; | ||
| import com.ctre.phoenix6.sim.CANcoderSimState; | ||
| import com.ctre.phoenix6.sim.TalonFXSimState; | ||
| import edu.wpi.first.math.system.plant.DCMotor; | ||
| import edu.wpi.first.math.system.plant.LinearSystemId; | ||
| import edu.wpi.first.math.util.Units; | ||
| import edu.wpi.first.wpilibj.simulation.DCMotorSim; | ||
|
|
||
| /** Holds information about a simulated TalonFX. */ | ||
| class TalonFXSimProfile extends PhysicsSim.SimProfile { | ||
| private static final double MOTOR_RESISTANCE = | ||
| 0.002; // Assume 2mOhm resistance for voltage drop calculation | ||
|
|
||
| private final DCMotorSim motorSim; | ||
| private final TalonFXSimState talonFXSim; | ||
| private CANcoderSimState cancoderSimState; | ||
|
|
||
| /** | ||
| * Creates a new simulation profile for a TalonFX device. | ||
| * | ||
| * @param talonFX The TalonFX device | ||
| * @param rotorInertia Rotational Inertia of the mechanism at the rotor | ||
| */ | ||
| public TalonFXSimProfile(final TalonFX talonFX, final double rotorInertia) { | ||
| var gearbox = DCMotor.getKrakenX60Foc(1); | ||
| this.motorSim = | ||
| new DCMotorSim( | ||
| LinearSystemId.createDCMotorSystem(gearbox, rotorInertia, 1.0), gearbox); | ||
| this.talonFXSim = talonFX.getSimState(); | ||
| } | ||
|
|
||
| public TalonFXSimProfile(final TalonFX talonFX, final double rotorInertia, CANcoder cancoder) { | ||
| this(talonFX, rotorInertia); | ||
| cancoderSimState = cancoder.getSimState(); | ||
| } | ||
|
|
||
| /** | ||
| * Runs the simulation profile. | ||
| * | ||
| * <p>This uses very rudimentary physics simulation and exists to allow users to test features | ||
| * of our products in simulation using our examples out of the box. Users may modify this to | ||
| * utilize more accurate physics simulation. | ||
| */ | ||
| public void run() { | ||
| /// DEVICE SPEED SIMULATION | ||
|
|
||
| motorSim.setInputVoltage(talonFXSim.getMotorVoltage()); | ||
|
|
||
| motorSim.update(getPeriod()); | ||
|
|
||
| /// SET SIM PHYSICS INPUTS | ||
| final double position_rot = motorSim.getAngularPositionRotations(); | ||
| final double velocity_rps = Units.radiansToRotations(motorSim.getAngularVelocityRPM()); | ||
|
|
||
| if (cancoderSimState != null) { | ||
| cancoderSimState.setRawPosition(position_rot); | ||
| cancoderSimState.setVelocity(velocity_rps); | ||
| } | ||
| talonFXSim.setRawRotorPosition(position_rot); | ||
| talonFXSim.setRotorVelocity(velocity_rps); | ||
|
|
||
| talonFXSim.setSupplyVoltage(12 - talonFXSim.getSupplyCurrent() * MOTOR_RESISTANCE); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.