This repository has been archived by the owner on Jan 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
there's also regen braking for velocity which I havent refactored
- Loading branch information
1 parent
81105fa
commit 60183de
Showing
8 changed files
with
295 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2024 FRC 3256 | ||
// https://github.com/Team3256 | ||
// | ||
// Use of this source code is governed by a | ||
// license that can be found in the LICENSE file at | ||
// the root directory of this project. | ||
|
||
package frc.robot.utils.generics; | ||
|
||
import com.ctre.phoenix6.controls.NeutralOut; | ||
import com.ctre.phoenix6.controls.VoltageOut; | ||
import com.ctre.phoenix6.hardware.TalonFX; | ||
|
||
public interface PositionIO { | ||
|
||
public default void updateInputs(SingleMotorSubsystemInputs inputs) {} | ||
|
||
public default void setVoltage(double voltage) {} | ||
|
||
public default void setPosition(double position) {} | ||
|
||
public default TalonFX getMotor() { | ||
return new TalonFX(0); | ||
} | ||
|
||
public default VoltageOut getVoltageRequest() { | ||
return new VoltageOut(0); | ||
} | ||
|
||
public default void off() { | ||
this.getMotor().setControl(new NeutralOut()); | ||
} | ||
|
||
public default void zero() { | ||
this.getMotor().setPosition(0); | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
src/main/java/frc/robot/utils/generics/PositionIOTalonFX.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright (c) 2024 FRC 3256 | ||
// https://github.com/Team3256 | ||
// | ||
// Use of this source code is governed by a | ||
// license that can be found in the LICENSE file at | ||
// the root directory of this project. | ||
|
||
package frc.robot.utils.generics; | ||
|
||
import com.ctre.phoenix6.BaseStatusSignal; | ||
import com.ctre.phoenix6.StatusSignal; | ||
import com.ctre.phoenix6.controls.MotionMagicVoltage; | ||
import com.ctre.phoenix6.controls.NeutralOut; | ||
import com.ctre.phoenix6.controls.PositionVoltage; | ||
import com.ctre.phoenix6.controls.VoltageOut; | ||
import com.ctre.phoenix6.hardware.TalonFX; | ||
import frc.robot.utils.PhoenixUtil; | ||
|
||
public class PositionIOTalonFX<T extends SingleMotorConstants> implements PositionIO { | ||
|
||
private final TalonFX motor; | ||
private final PositionVoltage positionRequest = new PositionVoltage(0).withSlot(0); | ||
private final MotionMagicVoltage motionMagicRequest = new MotionMagicVoltage(0).withSlot(0); | ||
private final VoltageOut voltageReq = new VoltageOut(0); | ||
|
||
private final StatusSignal<Double> motorVoltage; | ||
private final StatusSignal<Double> motorPosition; | ||
private final StatusSignal<Double> motorStatorCurrent; | ||
private final StatusSignal<Double> motorSupplyCurrent; | ||
// private final StatusSignal<Double> motorTemperature; | ||
private final StatusSignal<Double> motorReferenceSlope; | ||
|
||
public PositionIOTalonFX() { | ||
motor = new TalonFX(T.kMotorID); | ||
motorVoltage = motor.getMotorVoltage(); | ||
motorPosition = motor.getPosition(); | ||
motorStatorCurrent = motor.getStatorCurrent(); | ||
motorSupplyCurrent = motor.getSupplyCurrent(); | ||
// motorTemperature = motor.getDeviceTemp(); | ||
motorReferenceSlope = motor.getClosedLoopReferenceSlope(); | ||
|
||
PhoenixUtil.applyMotorConfigs(motor, T.kMotorConfig, T.kFlashConfigRetries); | ||
|
||
BaseStatusSignal.setUpdateFrequencyForAll( | ||
T.kStatusSignalUpdateFrequency, | ||
motorVoltage, | ||
motorPosition, | ||
motorStatorCurrent, | ||
motorSupplyCurrent, | ||
// motorTemperature, | ||
motorReferenceSlope); | ||
motor.optimizeBusUtilization(); | ||
} | ||
|
||
@Override | ||
public void updateInputs(SingleMotorSubsystemInputs inputs) { | ||
BaseStatusSignal.refreshAll( | ||
motorVoltage, | ||
motorPosition, | ||
motorStatorCurrent, | ||
motorSupplyCurrent, | ||
// motorTemperature, | ||
motorReferenceSlope); | ||
inputs.motorVoltage = motorVoltage.getValueAsDouble(); | ||
inputs.motorPosition = motorPosition.getValueAsDouble(); | ||
inputs.motorStatorCurrent = motorStatorCurrent.getValueAsDouble(); | ||
inputs.motorSupplyCurrent = motorSupplyCurrent.getValueAsDouble(); | ||
// inputs.motorTemperature = motorTemperature.getValueAsDouble(); | ||
inputs.motorReferenceSlope = motorReferenceSlope.getValueAsDouble(); | ||
} | ||
|
||
@Override | ||
public void setPosition(double position) { | ||
if (T.kUseMotionMagic) { | ||
motor.setControl(motionMagicRequest.withPosition(position)); | ||
} else { | ||
motor.setControl(positionRequest.withPosition(position)); | ||
} | ||
} | ||
|
||
@Override | ||
public void setVoltage(double voltage) { | ||
motor.setVoltage(voltage); | ||
} | ||
|
||
@Override | ||
public void off() { | ||
motor.setControl(new NeutralOut()); | ||
} | ||
|
||
@Override | ||
public void zero() { | ||
motor.setPosition(0); | ||
} | ||
|
||
@Override | ||
public TalonFX getMotor() { | ||
return motor; | ||
} | ||
|
||
@Override | ||
public VoltageOut getVoltageRequest() { | ||
return voltageReq; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/frc/robot/utils/generics/SingleMotorSubsystemInputs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2024 FRC 3256 | ||
// https://github.com/Team3256 | ||
// | ||
// Use of this source code is governed by a | ||
// license that can be found in the LICENSE file at | ||
// the root directory of this project. | ||
|
||
package frc.robot.utils.generics; | ||
|
||
import org.littletonrobotics.junction.AutoLog; | ||
|
||
@AutoLog | ||
public class SingleMotorSubsystemInputs { | ||
public double motorVoltage = 0.0; | ||
public double motorVelocity = 0.0; | ||
public double motorPosition = 0.0; | ||
public double motorStatorCurrent = 0.0; | ||
public double motorSupplyCurrent = 0.0; | ||
public double motorTemperature = 0.0; | ||
public double motorReferenceSlope = 0.0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2024 FRC 3256 | ||
// https://github.com/Team3256 | ||
// | ||
// Use of this source code is governed by a | ||
// license that can be found in the LICENSE file at | ||
// the root directory of this project. | ||
|
||
package frc.robot.utils.generics; | ||
|
||
import com.ctre.phoenix6.controls.NeutralOut; | ||
import com.ctre.phoenix6.controls.VoltageOut; | ||
import com.ctre.phoenix6.hardware.TalonFX; | ||
|
||
public interface VelocityIO { | ||
|
||
public default void updateInputs(SingleMotorSubsystemInputs inputs) {} | ||
|
||
public default void setVoltage(double voltage) {} | ||
|
||
public default void setVelocity(double velocity) {} | ||
|
||
public default TalonFX getMotor() { | ||
return new TalonFX(0); | ||
} | ||
|
||
public default VoltageOut getVoltageRequest() { | ||
return new VoltageOut(0); | ||
} | ||
|
||
public default void off() { | ||
this.getMotor().setControl(new NeutralOut()); | ||
} | ||
|
||
public default void zero() { | ||
this.getMotor().setPosition(0); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/frc/robot/utils/generics/VelocityIOTalonFX.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) 2024 FRC 3256 | ||
// https://github.com/Team3256 | ||
// | ||
// Use of this source code is governed by a | ||
// license that can be found in the LICENSE file at | ||
// the root directory of this project. | ||
|
||
package frc.robot.utils.generics; | ||
|
||
import com.ctre.phoenix6.BaseStatusSignal; | ||
import com.ctre.phoenix6.StatusSignal; | ||
import com.ctre.phoenix6.controls.NeutralOut; | ||
import com.ctre.phoenix6.controls.VoltageOut; | ||
import com.ctre.phoenix6.hardware.TalonFX; | ||
import frc.robot.utils.PhoenixUtil; | ||
|
||
public class VelocityIOTalonFX<T extends SingleMotorConstants> implements VelocityIO { | ||
|
||
private final TalonFX motor; | ||
private final VoltageOut voltageReq = new VoltageOut(0); | ||
|
||
private final StatusSignal<Double> motorVoltage; | ||
private final StatusSignal<Double> motorVelocity; | ||
private final StatusSignal<Double> motorStatorCurrent; | ||
private final StatusSignal<Double> motorSupplyCurrent; | ||
private final StatusSignal<Double> motorTemperature; | ||
private final StatusSignal<Double> motorReferenceSlope; | ||
|
||
public VelocityIOTalonFX() { | ||
motor = new TalonFX(T.kMotorID); | ||
motorVoltage = motor.getMotorVoltage(); | ||
motorVelocity = motor.getVelocity(); | ||
motorStatorCurrent = motor.getStatorCurrent(); | ||
motorSupplyCurrent = motor.getSupplyCurrent(); | ||
motorTemperature = motor.getDeviceTemp(); | ||
motorReferenceSlope = motor.getClosedLoopReferenceSlope(); | ||
|
||
PhoenixUtil.applyMotorConfigs(motor, T.kMotorConfig, T.kFlashConfigRetries); | ||
|
||
BaseStatusSignal.setUpdateFrequencyForAll( | ||
T.kStatusSignalUpdateFrequency, | ||
motorVoltage, | ||
motorVelocity, | ||
motorStatorCurrent, | ||
motorSupplyCurrent, | ||
motorTemperature, | ||
motorReferenceSlope); | ||
motor.optimizeBusUtilization(); | ||
} | ||
|
||
@Override | ||
public void updateInputs(SingleMotorSubsystemInputs inputs) { | ||
BaseStatusSignal.refreshAll( | ||
motorVoltage, | ||
motorVelocity, | ||
motorStatorCurrent, | ||
motorSupplyCurrent, | ||
motorTemperature, | ||
motorReferenceSlope); | ||
inputs.motorVoltage = motorVoltage.getValueAsDouble(); | ||
inputs.motorVelocity = motorVelocity.getValueAsDouble(); | ||
inputs.motorStatorCurrent = motorStatorCurrent.getValueAsDouble(); | ||
inputs.motorSupplyCurrent = motorSupplyCurrent.getValueAsDouble(); | ||
inputs.motorTemperature = motorTemperature.getValueAsDouble(); | ||
inputs.motorReferenceSlope = motorReferenceSlope.getValueAsDouble(); | ||
} | ||
|
||
@Override | ||
public void setVoltage(double voltage) { | ||
motor.setVoltage(voltage); | ||
} | ||
|
||
@Override | ||
public void off() { | ||
// Or VelocityTorqueCurrentFOC for regen braking | ||
motor.setControl(new NeutralOut()); | ||
} | ||
|
||
@Override | ||
public TalonFX getMotor() { | ||
return motor; | ||
} | ||
|
||
@Override | ||
public VoltageOut getVoltageRequest() { | ||
return voltageReq; | ||
} | ||
} |