Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 25 additions & 40 deletions src/main/java/frc/robot/subsystems/ArmSubsystem.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
22 changes: 22 additions & 0 deletions src/main/java/frc/robot/subsystems/week3-notes.txt
Original file line number Diff line number Diff line change
@@ -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