Skip to content

Commit 268efe7

Browse files
committed
Created basic talonfx interface
1 parent 0f5d026 commit 268efe7

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-2
lines changed

src/main/java/org/frc6423/frc2025/subsystems/swerve/module/ModuleIO.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,19 @@ public class ModuleIOInputs {
3636
/** Update module hardware */
3737
public void periodic();
3838

39+
public void setPivotVolts(double volts, boolean focEnabled);
40+
41+
public void setDriveVolts(double volts, boolean focEnabled);
42+
3943
/** Set Pivot motor voltage */
40-
public void setPivotVolts(double volts);
44+
public default void setPivotVolts(double volts) {
45+
setPivotVolts(volts, true);
46+
}
4147

4248
/** Set Drive motor voltage */
43-
public void setDriveVolts(double volts);
49+
public default void setDriveVolts(double volts) {
50+
setDriveVolts(volts, true);
51+
}
4452

4553
/** Set Module angle goal */
4654
public void setPivotAngle(Rotation2d angle);
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package org.frc6423.frc2025.subsystems.swerve.module;
2+
3+
import org.frc6423.frc2025.util.swerveUtil.ModuleConfig;
4+
5+
import com.ctre.phoenix6.BaseStatusSignal;
6+
import com.ctre.phoenix6.controls.MotionMagicVoltage;
7+
import com.ctre.phoenix6.controls.PositionTorqueCurrentFOC;
8+
import com.ctre.phoenix6.controls.TorqueCurrentFOC;
9+
import com.ctre.phoenix6.controls.VelocityTorqueCurrentFOC;
10+
import com.ctre.phoenix6.controls.VoltageOut;
11+
import com.ctre.phoenix6.hardware.CANcoder;
12+
import com.ctre.phoenix6.hardware.TalonFX;
13+
14+
import edu.wpi.first.math.geometry.Rotation2d;
15+
16+
public class ModuleIOTalonFX implements ModuleIO {
17+
18+
private final TalonFX m_pivotMotor, m_driveMotor;
19+
private final CANcoder m_pivotCANcoder;
20+
21+
// Control Requests
22+
private final VoltageOut m_voltageOutRequest;
23+
private final PositionTorqueCurrentFOC m_positionTorqueRequest;
24+
private final VelocityTorqueCurrentFOC m_velocityTorqueRequest;
25+
26+
// Signals
27+
private final BaseStatusSignal pivotABSPose, pivotPose, pivotVelRadsPerSec, pivotAppliedVolts, pivotSupplyCurrent, pivotTorqueCurrent;
28+
private final BaseStatusSignal drivePoseRads, driveVelRadsPerSec, driveAppliedVolts, driveSupplyCurrent, driveTorqueCurrent;
29+
30+
public ModuleIOTalonFX(ModuleConfig config) {
31+
m_pivotMotor = new TalonFX(config.kPivotID);
32+
m_driveMotor = new TalonFX(config.kDriveID);
33+
34+
m_pivotCANcoder = new CANcoder(config.kPivotABSID);
35+
36+
m_voltageOutRequest = new VoltageOut(0.0).withEnableFOC(true);
37+
m_positionTorqueRequest = new PositionTorqueCurrentFOC(0.0).withUpdateFreqHz(0);
38+
m_velocityTorqueRequest = new VelocityTorqueCurrentFOC(0.0).withUpdateFreqHz(0);
39+
40+
pivotABSPose = m_pivotCANcoder.getAbsolutePosition();
41+
pivotPose = m_pivotMotor.getPosition();
42+
pivotVelRadsPerSec = m_pivotMotor.getVelocity();
43+
pivotAppliedVolts = m_pivotMotor.getMotorVoltage();
44+
pivotSupplyCurrent = m_pivotMotor.getStatorCurrent();
45+
pivotTorqueCurrent = m_pivotMotor.getTorqueCurrent();
46+
47+
drivePoseRads = m_driveMotor.getPosition();
48+
driveVelRadsPerSec = m_driveMotor.getVelocity();
49+
driveAppliedVolts = m_driveMotor.getMotorVoltage();
50+
driveSupplyCurrent = m_driveMotor.getStatorCurrent();
51+
driveTorqueCurrent = m_driveMotor.getTorqueCurrent();
52+
53+
// ! Register to odo thread
54+
55+
BaseStatusSignal.setUpdateFrequencyForAll(
56+
50.0,
57+
pivotABSPose,
58+
pivotPose,
59+
pivotVelRadsPerSec,
60+
pivotAppliedVolts,
61+
pivotSupplyCurrent,
62+
pivotTorqueCurrent,
63+
drivePoseRads,
64+
driveVelRadsPerSec,
65+
driveAppliedVolts,
66+
driveSupplyCurrent,
67+
driveTorqueCurrent);
68+
69+
m_pivotCANcoder.optimizeBusUtilization();
70+
m_pivotMotor.optimizeBusUtilization();
71+
m_driveMotor.optimizeBusUtilization();
72+
}
73+
74+
@Override
75+
public void updateInputs(ModuleIOInputs inputs) {
76+
BaseStatusSignal.refreshAll(
77+
pivotABSPose,
78+
pivotPose,
79+
pivotVelRadsPerSec,
80+
pivotAppliedVolts,
81+
pivotSupplyCurrent,
82+
pivotTorqueCurrent,
83+
drivePoseRads,
84+
driveVelRadsPerSec,
85+
driveAppliedVolts,
86+
driveSupplyCurrent,
87+
driveTorqueCurrent);
88+
89+
inputs.pivotEnabled = true;
90+
}
91+
92+
@Override
93+
public void periodic() {
94+
// TODO Auto-generated method stub
95+
throw new UnsupportedOperationException("Unimplemented method 'periodic'");
96+
}
97+
98+
@Override
99+
public void setPivotVolts(double volts, boolean focEnabled) {
100+
m_pivotMotor.setControl(m_voltageOutRequest.withOutput(volts).withEnableFOC(focEnabled));
101+
}
102+
103+
@Override
104+
public void setDriveVolts(double volts, boolean focEnabled) {
105+
m_driveMotor.setControl(m_voltageOutRequest.withOutput(volts).withEnableFOC(focEnabled));
106+
}
107+
108+
@Override
109+
public void setPivotAngle(Rotation2d angle) {
110+
m_pivotMotor.setControl(m_positionTorqueRequest.withPosition(angle.getRotations()));
111+
}
112+
113+
@Override
114+
public void setDriveVelocity(double velMetersPerSec, double ff) {
115+
m_driveMotor.setControl(
116+
m_velocityTorqueRequest
117+
.withVelocity(velMetersPerSec)
118+
.withFeedForward(ff)
119+
);
120+
}
121+
122+
}

0 commit comments

Comments
 (0)