From 5b453a0f272906f70aca8e0d7bf20a30150f34a7 Mon Sep 17 00:00:00 2001 From: Parneel Bhakhri Date: Mon, 7 Jul 2025 18:06:18 -0700 Subject: [PATCH 1/2] add arm subsystem code --- .../frc/robot/subsystems/ArmSubsystem.java | 65 +++++++------------ 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/src/main/java/frc/robot/subsystems/ArmSubsystem.java b/src/main/java/frc/robot/subsystems/ArmSubsystem.java index b48640f..f961672 100644 --- a/src/main/java/frc/robot/subsystems/ArmSubsystem.java +++ b/src/main/java/frc/robot/subsystems/ArmSubsystem.java @@ -1,62 +1,47 @@ -// 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; -import com.ctre.phoenix6.configs.Slot0Configs; -import com.ctre.phoenix6.controls.PositionVoltage; -import com.ctre.phoenix6.hardware.TalonFX; import edu.wpi.first.wpilibj2.command.SubsystemBase; -import frc.robot.Constants; +import com.ctre.phoenix6.hardware.TalonFX; +import com.ctre.phoenix6.controls.PositionVoltage; public class ArmSubsystem extends SubsystemBase { + // 10 rotations of the arm motor = 1 radian of movement + // or about 0.1745 degrees per rotation + private static final int rotationsPerRadian = 10; + private static final double rotationsPerDegree = 0.1745; + private final TalonFX motor; + // Set both so that both radians and degrees can be set private double targetRadians; - + private double targetDegrees; private final PositionVoltage positionRequest; public ArmSubsystem() { - targetRadians = (3.14 / 2d); - motor = new TalonFX(1); - Slot0Configs slot0 = new Slot0Configs(); - slot0.kP = Constants.ArmConstants.kP; - slot0.kI = Constants.ArmConstants.kI; - slot0.kD = Constants.ArmConstants.kD; - motor.getConfigurator().apply(slot0); positionRequest = new PositionVoltage(0).withSlot(0); } - public void setAngle(double armRadians) { - targetRadians = armRadians; + // Allow choice between setting radians or degrees + public void setAngle(double angle, boolean radians) { + if (radians) { + targetRadians = angle; + motor.setControl(positionRequest.withPosition(angle * rotationsPerRadian)); + } else { + targetDegrees = angle; + motor.setControl(positionRequest.withPosition(angle * rotationsPerDegree)); + } } - public double getAngle() { - return motor.getRotorPosition().getValueAsDouble() / Constants.ArmConstants.rotationsPerRadian; - } - - public void zeroEncoder() { - motor.setPosition(0.0); - } - - public boolean isAtTargetAngle(double toleranceRadians) { - return Math.abs(getAngle() - targetRadians) <= toleranceRadians; + public double getAngle(boolean radians) { + if (radians) { + return motor.getRotorPosition().getValueAsDouble() / rotationsPerRadian; + } else { + return motor.getRotorPosition().getValueAsDouble() / rotationsPerDegree; + } } public void stop() { - setAngle(getAngle()); - } - - @Override - public void periodic() { - motor.setControl(positionRequest.withPosition(targetRadians * Constants.ArmConstants.rotationsPerRadian)); - // This method will be called once per scheduler run - } - - @Override - public void simulationPeriodic() { - // This method will be called once per scheduler run during simulation + setAngle(getAngle(true), true); } } From d34b0da512d518e7165d1b42f756d0ef47435a0a Mon Sep 17 00:00:00 2001 From: Parneel Bhakhri Date: Mon, 7 Jul 2025 22:05:33 -0700 Subject: [PATCH 2/2] Add notes --- .../java/frc/robot/subsystems/week3-notes.txt | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/frc/robot/subsystems/week3-notes.txt diff --git a/src/main/java/frc/robot/subsystems/week3-notes.txt b/src/main/java/frc/robot/subsystems/week3-notes.txt new file mode 100644 index 0000000..ae6ef7b --- /dev/null +++ b/src/main/java/frc/robot/subsystems/week3-notes.txt @@ -0,0 +1,22 @@ +CommandScheduler +- Java class in charge of running commands +- Regulates the lifecycle of commands + - Schedules commands for execution based on what buttons were pressed + - Runs scheduled commands + - Terminates commands that have finished execution +- Also in charge of running the periodic() method of each subsystem + +Subsystems +- Organization unit, groups together all related hardware +- The CommandScheduler will either interrupt the current command or ignore the + request when requested to run a command that requires an already-in-use subsystem +- "Default commands" can be configured to run when no other command is currently running for a subsystem +- Subsystems are created by extending WPILib's SubsystemBase class + - This is useful because subsystems tend to have many things in common +- Hardware in a subsystem should only be accessed through its corresponding subsystem +- Hardware is initialized in the constructor of its subsystem with a port number +- Things that need to be called every cycle (~20ms) can be put into the periodic() method, + a method defined by the SubsystemBase class + - This includes logging and other simple tasks like updating values +- The periodic() method should not move motors + - This is because the code in the periodic() method always runs, regardless of current execution state \ No newline at end of file