|
7 | 7 | import static edu.wpi.first.units.Units.*; |
8 | 8 |
|
9 | 9 | import com.ctre.phoenix6.configs.TalonFXConfiguration; |
| 10 | +import com.ctre.phoenix6.controls.DutyCycleOut; |
10 | 11 | import com.ctre.phoenix6.controls.MotionMagicVelocityTorqueCurrentFOC; |
11 | 12 | import com.ctre.phoenix6.controls.TorqueCurrentFOC; |
12 | 13 | import com.ctre.phoenix6.hardware.TalonFX; |
|
20 | 21 | import edu.wpi.first.networktables.NetworkTableInstance; |
21 | 22 | import edu.wpi.first.units.measure.AngularVelocity; |
22 | 23 | import edu.wpi.first.units.measure.Voltage; |
| 24 | +import edu.wpi.first.wpilibj.DutyCycle; |
23 | 25 | import edu.wpi.first.wpilibj.RobotController; |
24 | 26 | import edu.wpi.first.wpilibj.simulation.FlywheelSim; |
25 | 27 | import edu.wpi.first.wpilibj.sysid.SysIdRoutineLog; |
@@ -48,7 +50,8 @@ public class Flywheel extends SubsystemBase { |
48 | 50 |
|
49 | 51 | private final MotionMagicVelocityTorqueCurrentFOC m_request = |
50 | 52 | new MotionMagicVelocityTorqueCurrentFOC(0); |
51 | | - private final TorqueCurrentFOC m_TorqueCurrentFOC = new TorqueCurrentFOC(0); |
| 53 | + private final DutyCycleOut m_DutyCycleOut = new DutyCycleOut(0); |
| 54 | + private final TorqueCurrentFOC m_TorqueCurrentFOC = new TorqueCurrentFOC(0.0); |
52 | 55 | private static AngularVelocity m_rpmSetpoint = MANUAL_RPM.IDLE.getRPM(); |
53 | 56 |
|
54 | 57 | public final DoubleSubscriber m_rpmSubscriber; |
@@ -79,7 +82,7 @@ public Flywheel() { |
79 | 82 | config.MotorOutput.NeutralMode = m_neutralMode; |
80 | 83 | config.Feedback.SensorToMechanismRatio = FLYWHEEL.gearRatio; |
81 | 84 | config.CurrentLimits.StatorCurrentLimit = FLYWHEEL.kStatorCurrentLimit; |
82 | | - config.CurrentLimits.StatorCurrentLimitEnable = true; |
| 85 | + config.CurrentLimits.StatorCurrentLimitEnable = false; |
83 | 86 |
|
84 | 87 | config.MotionMagic.MotionMagicCruiseVelocity = FLYWHEEL.motionMagicCruiseVelocity; |
85 | 88 | config.MotionMagic.MotionMagicAcceleration = FLYWHEEL.motionMagicAcceleration; |
@@ -107,9 +110,8 @@ public void changeNeutralMode(NeutralModeValue neutralmode) { |
107 | 110 | m_neutralMode = neutralmode; |
108 | 111 | } |
109 | 112 |
|
110 | | - public void setRPMOutputFOC(double rpm) { |
111 | | - m_rpmSetpoint = RPM.of(rpm); |
112 | | - m_motor1.setControl(m_request.withVelocity(m_rpmSetpoint.abs(RotationsPerSecond))); |
| 113 | + public void setRPMOutputFOC(AngularVelocity rpm) { |
| 114 | + m_rpmSetpoint = rpm; |
113 | 115 | } |
114 | 116 |
|
115 | 117 | public void setTorqueCurrentOutputFOC(Voltage voltage) { |
@@ -168,8 +170,18 @@ public void testPeriodic() { |
168 | 170 | m_rpmSetpoint = RPM.of(m_rpmSubscriber.get()); |
169 | 171 | } |
170 | 172 |
|
| 173 | + public double getRPMerror(){ |
| 174 | + return getRPMSetpoint() - getMotorSpeedRPM(); |
| 175 | + } |
| 176 | + |
171 | 177 | @Override |
172 | | - public void periodic() {} |
| 178 | + public void periodic() { |
| 179 | + if (Math.abs(getRPMerror()) > FLYWHEEL.kVelocityErrorThreshold){ |
| 180 | + m_motor1.setControl(m_DutyCycleOut.withOutput(Math.signum(getRPMerror()))); |
| 181 | + } else { |
| 182 | + m_motor1.setControl(m_request.withVelocity(m_rpmSetpoint.abs(RPM))); |
| 183 | + } |
| 184 | + } |
173 | 185 |
|
174 | 186 | @Override |
175 | 187 | public void simulationPeriodic() { |
|
0 commit comments