Skip to content

Commit

Permalink
Using Talon Factory.
Browse files Browse the repository at this point in the history
Switched out code for the talonFX factory
  • Loading branch information
Grandcircuit424 committed Mar 8, 2024
1 parent 1ab227e commit 5e92b96
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
14 changes: 5 additions & 9 deletions src/main/java/frc/robot/subsystems/arm/ArmIOKraken.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.gos.lib.properties.GosDoubleProperty;
import com.gos.lib.properties.HeavyDoubleProperty;
import edu.wpi.first.math.util.Units;
import lib.factories.TalonFXFactory;
import lib.properties.phoenix6.Phoenix6PidPropertyBuilder;
import lib.properties.phoenix6.PidPropertyPublic;
import frc.robot.Constants.ArmConstants;
Expand Down Expand Up @@ -63,12 +64,7 @@ public class ArmIOKraken implements ArmIO {

public ArmIOKraken() {
final String CANBUS = "canivore";
m_armMaster = new TalonFX(ArmConstants.ARM_MASTER_ID, CANBUS);
m_armFollower = new TalonFX(ArmConstants.ARM_FOLLOWER_ID, CANBUS);
m_armEncoder = new CANcoder(ArmConstants.ARM_ENCODER_ID, CANBUS);

m_wristMaster = new TalonFX(ArmConstants.WRIST_MASTER_ID, CANBUS);
m_wristFollower = new TalonFX(ArmConstants.WRIST_FOLLOWER_ID, CANBUS);
m_wristEncoder = new CANcoder(ArmConstants.WRIST_ENCODER_ID, CANBUS);

// Arm Configuration
Expand All @@ -80,8 +76,8 @@ public ArmIOKraken() {
armConfig.Feedback.FeedbackSensorSource = FeedbackSensorSourceValue.RotorSensor;
armConfig.Feedback.SensorToMechanismRatio = ArmConstants.ARM_SENSOR_MECHANISM_RATIO;

m_armMaster.getConfigurator().apply(armConfig);
m_armFollower.getConfigurator().apply(armConfig);
m_armMaster = TalonFXFactory.createTalon(ArmConstants.ARM_MASTER_ID, armConfig);
m_armFollower = TalonFXFactory.createTalon(ArmConstants.ARM_MASTER_ID, armConfig);

// Wrist Configuration
TalonFXConfiguration wristConfig = new TalonFXConfiguration();
Expand All @@ -91,8 +87,8 @@ public ArmIOKraken() {
wristConfig.MotorOutput.NeutralMode = NeutralModeValue.Brake;
wristConfig.Feedback.SensorToMechanismRatio = ArmConstants.WRIST_SENSOR_MECHANISM_RATIO;

m_wristMaster.getConfigurator().apply(wristConfig);
m_wristFollower.getConfigurator().apply(wristConfig);
m_wristMaster = TalonFXFactory.createTalon(ArmConstants.WRIST_MASTER_ID, wristConfig);
m_wristFollower = TalonFXFactory.createTalon(ArmConstants.WRIST_FOLLOWER_ID, wristConfig);

// Encoder Configuration
CANcoderConfiguration armEncoderConfig = new CANcoderConfiguration();
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/frc/robot/subsystems/climber/ClimberIOKraken.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.ctre.phoenix6.signals.InvertedValue;
import com.ctre.phoenix6.signals.NeutralModeValue;
import frc.robot.Constants.ClimberConstants;
import lib.factories.TalonFXFactory;
import lib.properties.phoenix6.Phoenix6PidPropertyBuilder;
import lib.properties.phoenix6.PidPropertyPublic;

Expand All @@ -34,16 +35,15 @@ public class ClimberIOKraken implements ClimberIO {
private final StatusSignal<Double> m_rightAppliedOutputSignal;

public ClimberIOKraken() {
m_leftClimber = new TalonFX(ClimberConstants.LEFT_CLIMBER_ID, "canivore");
m_rightClimber = new TalonFX(ClimberConstants.RIGHT_CLIMBER_ID, "canivore");

TalonFXConfiguration climberConfig = new TalonFXConfiguration();
climberConfig.MotorOutput.NeutralMode = NeutralModeValue.Brake;
climberConfig.MotorOutput.Inverted = InvertedValue.CounterClockwise_Positive;
m_leftClimber.getConfigurator().apply(climberConfig);

m_leftClimber = TalonFXFactory.createTalon(ClimberConstants.LEFT_CLIMBER_ID, climberConfig);

climberConfig.MotorOutput.Inverted = InvertedValue.Clockwise_Positive;
m_rightClimber.getConfigurator().apply(climberConfig);

m_rightClimber = TalonFXFactory.createTalon(ClimberConstants.RIGHT_CLIMBER_ID, climberConfig);

m_leftProperty = new Phoenix6PidPropertyBuilder("Climber/Left Climber", false, m_leftClimber, 0)
.addP(ClimberConstants.CLIMBER_KP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import frc.robot.Constants.ClimberConstants;
import lib.properties.phoenix6.Phoenix6PidPropertyBuilder;
import lib.properties.phoenix6.PidPropertyPublic;
import lib.factories.TalonFXFactory;

public class ClimberIOPrototype implements ClimberIO {
private final TalonFX m_leftTalon;
Expand All @@ -15,15 +16,16 @@ public class ClimberIOPrototype implements ClimberIO {
private final PidPropertyPublic m_rightClimberPid;

public ClimberIOPrototype() {
m_leftTalon = new TalonFX(ClimberConstants.LEFT_CLIMBER_ID);
m_rightTalon = new TalonFX(ClimberConstants.RIGHT_CLIMBER_ID);


var rightTalonConfig = new TalonFXConfiguration();
rightTalonConfig.MotorOutput.Inverted = InvertedValue.Clockwise_Positive;
m_rightTalon.getConfigurator().apply(rightTalonConfig);

m_rightTalon = TalonFXFactory.createTalon(ClimberConstants.RIGHT_CLIMBER_ID, "canivore", rightTalonConfig);

var leftTalonConfig = new TalonFXConfiguration();
m_leftTalon.getConfigurator().apply(leftTalonConfig);

m_leftTalon = TalonFXFactory.createTalon(ClimberConstants.LEFT_CLIMBER_ID, "canivore", leftTalonConfig);

m_leftClimberPid = new Phoenix6PidPropertyBuilder(
"Climber/Left PID",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package frc.robot.subsystems.drive.module;

import com.ctre.phoenix6.BaseStatusSignal;
import com.ctre.phoenix6.CANBus;
import com.ctre.phoenix6.StatusSignal;
import com.ctre.phoenix6.configs.CANcoderConfiguration;
import com.ctre.phoenix6.configs.MotorOutputConfigs;
Expand All @@ -28,6 +29,7 @@
import com.ctre.phoenix6.signals.SensorDirectionValue;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.util.Units;
import lib.factories.TalonFXFactory;
import lib.properties.phoenix6.Phoenix6PidPropertyBuilder;

import java.util.Queue;
Expand Down Expand Up @@ -75,8 +77,6 @@ public class ModuleIOTalonFX implements ModuleIO {

public ModuleIOTalonFX(ModuleConstants moduleConstants) {
String canbus = "canivore";
m_driveTalon = new TalonFX(moduleConstants.DRIVE_MOTOR_ID(), canbus);
m_turnTalon = new TalonFX(moduleConstants.TURN_MOTOR_ID(), canbus);

/*
* this is technically the proper way of using any class that
Expand Down Expand Up @@ -104,7 +104,8 @@ public ModuleIOTalonFX(ModuleConstants moduleConstants) {
moduleConstants.DRIVE_MOTOR_INVERTED() ? InvertedValue.Clockwise_Positive
: InvertedValue.CounterClockwise_Positive;
driveConfig.Feedback.SensorToMechanismRatio = m_moduleConstants.DRIVE_GEAR_RATIO();
m_driveTalon.getConfigurator().apply(driveConfig);

m_driveTalon = TalonFXFactory.createTalon(moduleConstants.DRIVE_MOTOR_ID(), canbus, driveConfig);
setDriveBrakeMode(true);

// setup pid gains for drive motor
Expand All @@ -126,7 +127,8 @@ public ModuleIOTalonFX(ModuleConstants moduleConstants) {
: InvertedValue.CounterClockwise_Positive;
turnConfig.ClosedLoopGeneral.ContinuousWrap = true;
turnConfig.Feedback.SensorToMechanismRatio = m_moduleConstants.TURNING_GEAR_RATIO();
m_turnTalon.getConfigurator().apply(turnConfig);

m_turnTalon = TalonFXFactory.createTalon(moduleConstants.TURN_MOTOR_ID(), turnConfig);
setTurnBrakeMode(true);

// setup pid gains for turn motor
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/lib/factories/TalonFXFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ public static TalonFX createTalon(int id, String canbusName, TalonFXConfiguratio
return talonFX;
}

//METHOD: This allows for the creation of a new Talon Brushless Motor with a preset Config
public static TalonFX createTalon(int id, TalonFXConfiguration config) {
TalonFX talonFX = new TalonFX(id, "canivore");

talonFX.getConfigurator().apply(config);

TalonFxMotors.add(new Pair<>(talonFX, config));
return talonFX;
}

// METHOD: Checks each motor and handles sticky faults
public static void handleFaults() {
TalonFxMotors.forEach((Pair<TalonFX, TalonFXConfiguration> pair) -> {
Expand Down

0 comments on commit 5e92b96

Please sign in to comment.