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(swerve): Use generic position controller for azimuth.
- Loading branch information
1 parent
650405e
commit 1c26dac
Showing
15 changed files
with
172 additions
and
470 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
106 changes: 106 additions & 0 deletions
106
src/main/java/frc/lib/controller/PositionControllerIOTalonFXSteer.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,106 @@ | ||
package frc.lib.controller; | ||
|
||
import com.ctre.phoenix6.BaseStatusSignal; | ||
import com.ctre.phoenix6.StatusSignal; | ||
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 edu.wpi.first.math.controller.PIDController; | ||
import edu.wpi.first.math.controller.SimpleMotorFeedforward; | ||
import frc.lib.CAN; | ||
import frc.lib.config.Configurator; | ||
import frc.lib.config.MechanismConfig; | ||
|
||
/** Creates a new position controller using a steer TalonFX and azimuth CANcoder. */ | ||
public class PositionControllerIOTalonFXSteer implements PositionControllerIO { | ||
|
||
private final MechanismConfig config; | ||
|
||
private final TalonFX steer; | ||
|
||
private final CANcoder azimuth; | ||
|
||
private final StatusSignal<Double> position, velocity, acceleration, volts, amps; | ||
|
||
private final SimpleMotorFeedforward feedforward; | ||
|
||
private final PIDController feedback; | ||
|
||
private final VoltageOut voltage; | ||
|
||
public PositionControllerIOTalonFXSteer( | ||
CAN steerCAN, CAN encoderCAN, MechanismConfig config, boolean enableFOC) { | ||
this.config = config; | ||
|
||
steer = new TalonFX(steerCAN.id(), steerCAN.bus()); | ||
|
||
azimuth = new CANcoder(encoderCAN.id(), encoderCAN.bus()); | ||
|
||
// TODO Use steer position after seeding with azimuth | ||
position = azimuth.getAbsolutePosition(); | ||
|
||
velocity = steer.getVelocity(); | ||
acceleration = steer.getAcceleration(); | ||
|
||
volts = steer.getMotorVoltage(); | ||
amps = steer.getStatorCurrent(); | ||
|
||
feedforward = config.feedforwardControllerConfig().createSimpleMotorFeedforward(); | ||
|
||
feedback = config.feedbackControllerConfig().createPIDController(); | ||
|
||
voltage = new VoltageOut(0.0).withEnableFOC(enableFOC); | ||
} | ||
|
||
@Override | ||
public void configure() { | ||
BaseStatusSignal.setUpdateFrequencyForAll(100.0, position, velocity, acceleration, volts, amps); | ||
|
||
ParentDevice.optimizeBusUtilizationForAll(steer, azimuth); | ||
|
||
Configurator.configureTalonFX( | ||
steer.getConfigurator(), config.motorConfig().createTalonFXConfig()); | ||
|
||
Configurator.configureCANcoder( | ||
azimuth.getConfigurator(), config.absoluteEncoderConfig().createCANcoderConfig()); | ||
} | ||
|
||
@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) {} | ||
|
||
@Override | ||
public void setSetpoint(double positionRotations, double velocityRotationsPerSecond) { | ||
double measuredPositionRotations = position.getValue(); | ||
|
||
// double feedforwardVolts = feedforward.calculate(velocityRotationsPerSecond); | ||
double feedforwardVolts = calculateFeedforward(measuredPositionRotations, positionRotations); | ||
|
||
double feedbackVolts = feedback.calculate(measuredPositionRotations, positionRotations); | ||
|
||
steer.setControl(voltage.withOutput(feedforwardVolts + feedbackVolts)); | ||
} | ||
|
||
private double calculateFeedforward(double measurementRotations, double setpointRotations) { | ||
if (feedback.atSetpoint() == false) { | ||
if (measurementRotations > setpointRotations) { | ||
return feedforward.ks; | ||
} else { | ||
return -feedforward.ks; | ||
} | ||
} | ||
|
||
return 0.0; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
51 changes: 0 additions & 51 deletions
51
src/main/java/frc/robot/swerve/AzimuthEncoderIOCANcoder.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.