This repository has been archived by the owner on May 19, 2024. 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.
refactor(arm): Use position controller for arm.
- Loading branch information
1 parent
d6124f3
commit a2cd733
Showing
12 changed files
with
222 additions
and
259 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,7 +189,7 @@ | |
0.0, | ||
0.8500000238418579 | ||
], | ||
"height": 0, | ||
"height": 338, | ||
"series": [ | ||
{ | ||
"color": [ | ||
|
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
134 changes: 134 additions & 0 deletions
134
src/main/java/frc/lib/controller/PositionControllerIOTalonFX2.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,134 @@ | ||
package frc.lib.controller; | ||
|
||
import com.ctre.phoenix6.BaseStatusSignal; | ||
import com.ctre.phoenix6.StatusSignal; | ||
import com.ctre.phoenix6.configs.CANcoderConfiguration; | ||
import com.ctre.phoenix6.configs.TalonFXConfiguration; | ||
import com.ctre.phoenix6.controls.Follower; | ||
import com.ctre.phoenix6.controls.VoltageOut; | ||
import com.ctre.phoenix6.hardware.CANcoder; | ||
import com.ctre.phoenix6.hardware.ParentDevice; | ||
import com.ctre.phoenix6.hardware.TalonFX; | ||
import com.ctre.phoenix6.signals.InvertedValue; | ||
import com.ctre.phoenix6.signals.NeutralModeValue; | ||
import com.ctre.phoenix6.signals.SensorDirectionValue; | ||
|
||
import edu.wpi.first.math.controller.ArmFeedforward; | ||
import edu.wpi.first.math.controller.PIDController; | ||
import edu.wpi.first.math.util.Units; | ||
import frc.lib.CAN; | ||
import frc.lib.Configurator; | ||
import frc.lib.PIDFConstants; | ||
|
||
/** Position controller using two TalonFXs and a CANcoder and an external PIDF for an arm. */ | ||
public class PositionControllerIOTalonFX2 implements PositionControllerIO { | ||
|
||
private final TalonFX leaderMotor, followerMotor; | ||
|
||
private final CANcoder encoder; | ||
|
||
private final StatusSignal<Double> position, velocity, acceleration, volts, amps; | ||
|
||
private final ArmFeedforward feedforward; | ||
|
||
private final PIDController feedback; | ||
|
||
private final VoltageOut voltage; | ||
|
||
/** | ||
* Creates a new position controller using two TalonFXs and a CANcoder and an external PIDF for an arm. | ||
* | ||
* @param leaderCAN | ||
* @param followerCAN | ||
* @param encoderCAN | ||
* @param pidf | ||
* @param enableFOC | ||
* @param invertFollower | ||
*/ | ||
public PositionControllerIOTalonFX2(CAN leaderCAN, CAN followerCAN, CAN encoderCAN, PIDFConstants pidf, boolean enableFOC, boolean invertFollower) { | ||
leaderMotor = new TalonFX(leaderCAN.id(), leaderCAN.bus()); | ||
followerMotor = new TalonFX(followerCAN.id(), followerCAN.bus()); | ||
|
||
encoder = new CANcoder(encoderCAN.id(), encoderCAN.bus()); | ||
|
||
position = encoder.getAbsolutePosition(); | ||
|
||
velocity = leaderMotor.getVelocity(); | ||
acceleration = leaderMotor.getAcceleration(); | ||
|
||
volts = leaderMotor.getMotorVoltage(); | ||
amps = leaderMotor.getStatorCurrent(); | ||
|
||
feedforward = new ArmFeedforward(pidf.kS, pidf.kG, pidf.kV, pidf.kA); | ||
|
||
feedback = new PIDController(pidf.kP, pidf.kI, pidf.kD); | ||
|
||
followerMotor.setControl(new Follower(leaderMotor.getDeviceID(), invertFollower)); | ||
|
||
voltage = new VoltageOut(0.0).withEnableFOC(enableFOC); | ||
} | ||
|
||
@Override | ||
public void configure(PositionControllerIOConstants constants) { | ||
BaseStatusSignal.setUpdateFrequencyForAll(100.0, position, velocity, acceleration, volts, amps); | ||
|
||
ParentDevice.optimizeBusUtilizationForAll(leaderMotor, followerMotor, encoder); | ||
|
||
TalonFXConfiguration motorConfig = new TalonFXConfiguration(); | ||
|
||
motorConfig.MotorOutput.Inverted = constants.ccwPositive ? InvertedValue.CounterClockwise_Positive : InvertedValue.Clockwise_Positive; | ||
motorConfig.MotorOutput.NeutralMode = constants.neutralBrake ? NeutralModeValue.Brake : NeutralModeValue.Coast; | ||
|
||
// Stator current is a measure of the current inside of the motor and is typically higher than supply (breaker) current | ||
motorConfig.CurrentLimits.StatorCurrentLimit = constants.currentLimitAmps * 2.0; | ||
motorConfig.CurrentLimits.StatorCurrentLimitEnable = true; | ||
|
||
motorConfig.CurrentLimits.SupplyCurrentLimit = constants.currentLimitAmps; | ||
// Allow higher current spikes (150%) for a brief duration (one second) | ||
// REV 40A auto-resetting breakers typically trip when current exceeds 300% for one second | ||
motorConfig.CurrentLimits.SupplyCurrentThreshold = constants.currentLimitAmps * 1.5; | ||
motorConfig.CurrentLimits.SupplyTimeThreshold = 1; | ||
motorConfig.CurrentLimits.SupplyCurrentLimitEnable = true; | ||
|
||
motorConfig.Feedback.SensorToMechanismRatio = constants.sensorToMechanismRatio; | ||
|
||
Configurator.configureTalonFX(leaderMotor.getConfigurator(), motorConfig); | ||
Configurator.configureTalonFX(followerMotor.getConfigurator(), motorConfig); | ||
|
||
CANcoderConfiguration encoderConfig = new CANcoderConfiguration(); | ||
|
||
encoderConfig.MagnetSensor.MagnetOffset = constants.absoluteEncoderOffsetRotations; | ||
encoderConfig.MagnetSensor.SensorDirection = constants.ccwPositive ? SensorDirectionValue.CounterClockwise_Positive : SensorDirectionValue.Clockwise_Positive; | ||
|
||
Configurator.configureCANcoder(encoder.getConfigurator(), encoderConfig); | ||
} | ||
|
||
@Override | ||
public void update(PositionControllerIOValues values) { | ||
BaseStatusSignal.refreshAll(position, velocity, acceleration, volts, amps); | ||
|
||
values.positionRotations = position.getValue(); | ||
values.velocityRotationsPerSecond = velocity.getValue(); | ||
values.accelerationRotationsPerSecondPerSecond = acceleration.getValue(); | ||
values.motorVolts = volts.getValue(); | ||
values.motorAmps = amps.getValue(); | ||
} | ||
|
||
@Override | ||
public void setPosition(double positionRotations) { | ||
leaderMotor.setPosition(positionRotations); | ||
followerMotor.setPosition(positionRotations); | ||
} | ||
|
||
@Override | ||
public void setSetpoint(double positionRotations, double velocityRotationsPerSecond) { | ||
double measuredPositionRotations = position.getValue(); | ||
|
||
double feedforwardVolts = feedforward.calculate(Units.rotationsToRadians(measuredPositionRotations), velocityRotationsPerSecond); | ||
|
||
double feedbackVolts = feedback.calculate(measuredPositionRotations, positionRotations); | ||
|
||
leaderMotor.setControl(voltage.withOutput(feedforwardVolts + feedbackVolts)); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,4 +1,41 @@ | ||
package frc.robot.arm; | ||
|
||
import edu.wpi.first.math.util.Units; | ||
import frc.lib.CAN; | ||
import frc.lib.PIDFConstants; | ||
import frc.lib.controller.PositionControllerIO.PositionControllerIOConstants; | ||
|
||
/** Constants for the arm subsystem. */ | ||
public class ArmConstants {} | ||
public class ArmConstants { | ||
|
||
/** Constants for the shoulder. */ | ||
public static class ShoulderConstants { | ||
/** Shoulder's leader CAN. */ | ||
public static final CAN LEADER_CAN = new CAN(48); | ||
|
||
/** Shoulder's follower CAN. */ | ||
public static final CAN FOLLOWER_CAN = new CAN(46); | ||
|
||
/** Shoulder's encoder CAN. */ | ||
public static final CAN ENCODER_CAN = new CAN(52); | ||
|
||
/** Shoulder's PIDF constants. */ | ||
public static final PIDFConstants PIDF = new PIDFConstants(); | ||
static { | ||
PIDF.kS = 0.14; | ||
PIDF.kG = 0.45; | ||
PIDF.kV = 4.0; | ||
PIDF.kP = 20.0; | ||
} | ||
|
||
/** Shoulder's controller constants. */ | ||
public static final PositionControllerIOConstants CONTROLLER_CONSTANTS = new PositionControllerIOConstants(); | ||
static { | ||
CONTROLLER_CONSTANTS.ccwPositive = false; | ||
CONTROLLER_CONSTANTS.neutralBrake = true; | ||
CONTROLLER_CONSTANTS.sensorToMechanismRatio = 39.771428571; | ||
CONTROLLER_CONSTANTS.absoluteEncoderOffsetRotations = Units.degreesToRotations(-146.77) + Units.degreesToRotations(-27.07); | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.