-
Notifications
You must be signed in to change notification settings - Fork 0
Subsystem/comp turret #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d087f2f
cce0e0c
a2ca5ee
bfad163
a693e61
a64a601
1104b2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // Copyright (c) FIRST and other WPILib contributors. | ||
| // Open Source Software; you can modify and/or share it under the terms of | ||
| // the WPILib BSD license file in the root directory of this project. | ||
|
|
||
| package frc.robot.subsystems.shooter; | ||
|
|
||
| import com.ctre.phoenix6.BaseStatusSignal; | ||
| import com.ctre.phoenix6.CANBus; | ||
| import com.ctre.phoenix6.StatusSignal; | ||
| import com.ctre.phoenix6.configs.TalonFXConfiguration; | ||
| import com.ctre.phoenix6.controls.PositionVoltage; | ||
| import com.ctre.phoenix6.controls.VelocityVoltage; | ||
| import com.ctre.phoenix6.controls.VoltageOut; | ||
| import com.ctre.phoenix6.hardware.TalonFX; | ||
| import com.ctre.phoenix6.signals.InvertedValue; | ||
| import com.ctre.phoenix6.signals.NeutralModeValue; | ||
| import edu.wpi.first.math.geometry.Rotation2d; | ||
| import edu.wpi.first.units.measure.AngularVelocity; | ||
| import edu.wpi.first.units.measure.Current; | ||
| import edu.wpi.first.units.measure.Temperature; | ||
| import edu.wpi.first.units.measure.Voltage; | ||
| import org.littletonrobotics.junction.AutoLog; | ||
|
|
||
| /** Add your docs here. */ | ||
| public class TurretIO { | ||
| @AutoLog | ||
| public static class TurretIOInputs { | ||
| public Rotation2d turretPositionRotations = new Rotation2d(); | ||
| public double turretStatorCurrentAmps = 0.0; | ||
| public double velocityRotationsPerSec = 0.0; | ||
| public double turretSupplyCurrentAmps = 0.0; | ||
| public double turretVoltage = 0.0; | ||
| public double turretTempC = 0.0; | ||
| } | ||
|
|
||
| protected TalonFX turretMotor; | ||
| private final BaseStatusSignal turretPositionRotations; | ||
| private final StatusSignal<Voltage> turretVoltage; | ||
| private final StatusSignal<Current> turretStatorCurrent; | ||
| private final StatusSignal<Current> turretSupplyCurrent; | ||
| private final StatusSignal<Temperature> turretTemp; | ||
| private final StatusSignal<AngularVelocity> turretAngularVelocity; | ||
| private VoltageOut voltageOut = new VoltageOut(0.0).withEnableFOC(true); | ||
| private PositionVoltage positionVoltage = new PositionVoltage(0.0).withEnableFOC(true); | ||
| private VelocityVoltage velocityVoltage = new VelocityVoltage(0.0).withEnableFOC(true); | ||
|
|
||
| public TurretIO(TalonFXConfiguration talonFXConfiguration, CANBus canbus) { | ||
| turretMotor = new TalonFX(11, canbus); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The shooter hood is already ID 11. Comp CAN ids haven't been decided yet so for now give it a large random number (like 40 or smth) for now
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw this is so that the ids don't clash during sim
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ig its not on this branch so maybe it doesn't matter but better avoid the problem now then fix it later |
||
| turretMotor.getConfigurator().apply(TurretIO.getTurretConfiguration()); | ||
| turretPositionRotations = turretMotor.getPosition(); | ||
| turretAngularVelocity = turretMotor.getVelocity(); | ||
| turretVoltage = turretMotor.getMotorVoltage(); | ||
| turretStatorCurrent = turretMotor.getStatorCurrent(); | ||
| turretSupplyCurrent = turretMotor.getSupplyCurrent(); | ||
| turretTemp = turretMotor.getDeviceTemp(); | ||
|
|
||
| BaseStatusSignal.setUpdateFrequencyForAll( | ||
| 50.0, | ||
| turretPositionRotations, | ||
| turretAngularVelocity, | ||
| turretVoltage, | ||
| turretStatorCurrent, | ||
| turretSupplyCurrent, | ||
| turretTemp); | ||
| turretMotor.optimizeBusUtilization(); | ||
| } | ||
|
|
||
| public static TalonFXConfiguration getTurretConfiguration() { | ||
| TalonFXConfiguration config = new TalonFXConfiguration(); | ||
|
|
||
| config.MotorOutput.NeutralMode = NeutralModeValue.Brake; | ||
|
|
||
| config.MotorOutput.Inverted = InvertedValue.Clockwise_Positive; | ||
|
|
||
| config.MotorOutput.NeutralMode = NeutralModeValue.Brake; | ||
|
|
||
| config.MotorOutput.Inverted = InvertedValue.Clockwise_Positive; | ||
|
|
||
| config.Feedback.SensorToMechanismRatio = TurretSubsystem.TURRET_GEAR_RATIO; | ||
|
|
||
| // config.Slot0.GravityType = GravityTypeValue.Arm_Cosine; Potentially need, maybe not tho. | ||
|
|
||
| config.Slot0.kS = 0.0; | ||
| config.Slot0.kG = 0.0; | ||
| config.Slot0.kV = 1.1; | ||
| config.Slot0.kP = 5.0; | ||
| config.Slot0.kD = 0.0; | ||
|
|
||
| config.CurrentLimits.StatorCurrentLimit = 80.0; | ||
| config.CurrentLimits.StatorCurrentLimitEnable = true; | ||
| config.CurrentLimits.SupplyCurrentLimit = 60.0; | ||
|
|
||
| return config; | ||
| } | ||
|
|
||
| public void setTurretVoltage(double turretVoltage) { | ||
| turretMotor.setControl(voltageOut.withOutput(turretVoltage)); | ||
| } | ||
|
|
||
| public void setTurretPosition(Rotation2d turretPosition) { | ||
| turretMotor.setControl(positionVoltage.withPosition(turretPosition.getRotations())); | ||
| } | ||
|
|
||
| public void setTurretVelocity(double turretVelocity) { | ||
| turretMotor.setControl(velocityVoltage.withVelocity(turretVelocity)); | ||
| } | ||
|
|
||
| public void updateInputs(TurretIOInputs inputs) { | ||
| BaseStatusSignal.refreshAll( | ||
| turretPositionRotations, | ||
| turretVoltage, | ||
| turretStatorCurrent, | ||
| turretSupplyCurrent, | ||
| turretTemp); | ||
|
|
||
| inputs.turretPositionRotations = | ||
| Rotation2d.fromRadians(turretPositionRotations.getValueAsDouble()); | ||
| inputs.turretVoltage = turretVoltage.getValueAsDouble(); | ||
| inputs.turretStatorCurrentAmps = turretStatorCurrent.getValueAsDouble(); | ||
| inputs.turretSupplyCurrentAmps = turretSupplyCurrent.getValueAsDouble(); | ||
| inputs.turretTempC = turretTemp.getValueAsDouble(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package frc.robot.subsystems.shooter; | ||
|
|
||
| import com.ctre.phoenix6.CANBus; | ||
| import com.ctre.phoenix6.Utils; | ||
| import com.ctre.phoenix6.configs.TalonFXConfiguration; | ||
| import com.ctre.phoenix6.sim.ChassisReference; | ||
| import com.ctre.phoenix6.sim.TalonFXSimState; | ||
| import com.ctre.phoenix6.sim.TalonFXSimState.MotorType; | ||
|
|
||
| import edu.wpi.first.math.system.plant.DCMotor; | ||
| import edu.wpi.first.math.system.plant.LinearSystemId; | ||
| import edu.wpi.first.wpilibj.Notifier; | ||
| import edu.wpi.first.wpilibj.RobotController; | ||
| import edu.wpi.first.wpilibj.simulation.DCMotorSim; | ||
|
|
||
| public class TurretIOSim extends TurretIO{ | ||
| DCMotorSim turretphysicssSim = | ||
| new DCMotorSim( | ||
| LinearSystemId.createDCMotorSystem(DCMotor.getKrakenX44Foc(1), 0.001, TurretSubsystem.TURRET_GEAR_RATIO), | ||
| DCMotor.getKrakenX44Foc(1)); | ||
|
|
||
| TalonFXSimState motorSim; | ||
| final double simLoopPeriod = 0.002; | ||
| Notifier simNotifier; | ||
| double lastSimTime = 0.0; | ||
|
|
||
| public TurretIOSim(TalonFXConfiguration configuration, CANBus canBus){ | ||
| super(configuration, canBus); | ||
| motorSim = turretMotor.getSimState(); | ||
| motorSim.setMotorType(MotorType.KrakenX44); | ||
| motorSim.Orientation = ChassisReference.Clockwise_Positive; | ||
| simNotifier = new Notifier(() -> { | ||
| double currentTime = Utils.getCurrentTimeSeconds(); | ||
| double deltaTime = currentTime - lastSimTime; | ||
| lastSimTime = currentTime; | ||
| motorSim.setSupplyVoltage(RobotController.getBatteryVoltage()); | ||
| turretphysicssSim.setInputVoltage(motorSim.getMotorVoltage()); | ||
| turretphysicssSim.update(deltaTime); | ||
| motorSim.setRawRotorPosition(turretphysicssSim.getAngularPositionRotations() * turretphysicssSim.getGearing()); | ||
| motorSim.setRotorVelocity(turretphysicssSim.getAngularVelocityRPM()/60 * turretphysicssSim.getGearing()); | ||
|
|
||
| }); | ||
| simNotifier.startPeriodic(simLoopPeriod); | ||
|
|
||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also log velocity here