public class Hood extends SubsystemBase implements HoodIO{
private TalonFX motor = new TalonFX(IdConstants.HOOD_ID, Constants.SUBSYSTEM_CANIVORE_CAN);
- private double MIN_ANGLE_RAD = Units.degreesToRadians(HoodConstants.MIN_ANGLE);
- private double MAX_ANGLE_RAD = Units.degreesToRadians(HoodConstants.MAX_ANGLE);
-
- private double MAX_VEL_RAD_PER_SEC = HoodConstants.MAX_VELOCITY;
- private double MAX_ACCEL_RAD_PER_SEC2 = HoodConstants.MAX_ACCELERATION;
-
- private double GEAR_RATIO = HoodConstants.HOOD_GEAR_RATIO;
-
private final LinearFilter setpointFilter = LinearFilter.singlePoleIIR(0.02, 0.02);
private Rotation2d goalAngle = new Rotation2d(Units.degreesToRadians(HoodConstants.MAX_ANGLE));
config.Slot0.kD = 0.02; // The "Braking" term to stop overshoot
var mm = config.MotionMagic;
- mm.MotionMagicCruiseVelocity = Units.radiansToRotations(MAX_VEL_RAD_PER_SEC) * GEAR_RATIO;
- mm.MotionMagicAcceleration = Units.radiansToRotations(MAX_ACCEL_RAD_PER_SEC2) * GEAR_RATIO; // Lowered for belt safety
+ mm.MotionMagicCruiseVelocity = Units.radiansToRotations(HoodConstants.MAX_VELOCITY) * HoodConstants.HOOD_GEAR_RATIO;
+ mm.MotionMagicAcceleration = Units.radiansToRotations(HoodConstants.MAX_ACCELERATION) * HoodConstants.HOOD_GEAR_RATIO; // Lowered for belt safety
mm.MotionMagicJerk = 0; // Set to > 0 for "S-Curve" smoothing if needed
motor.getConfigurator().apply(config);
- motor.setPosition(Units.degreesToRotations(HoodConstants.MAX_ANGLE) * GEAR_RATIO);
+ motor.setPosition(Units.degreesToRotations(HoodConstants.MAX_ANGLE) * HoodConstants.HOOD_GEAR_RATIO);
SmartDashboard.putData("max", new InstantCommand(() -> setFieldRelativeTarget(new Rotation2d(Units.degreesToRadians(HoodConstants.MAX_ANGLE)), 0)));
SmartDashboard.putData("medium", new InstantCommand(() -> setFieldRelativeTarget(new Rotation2d(Units.degreesToRadians((HoodConstants.MAX_ANGLE + HoodConstants.MIN_ANGLE) / 2)), 0)));
* @return Position of turret in degrees
*/
public double getPositionDeg(){
- return Units.rotationsToDegrees(motor.getPosition().getValueAsDouble()) / GEAR_RATIO;
+ return Units.rotationsToDegrees(motor.getPosition().getValueAsDouble()) / HoodConstants.HOOD_GEAR_RATIO;
}
@Override
setpointRad = lastFilteredRad;
// Tells the Kraken to get to this position using 1000Hz profile
- double motorGoalRotations = Units.radiansToRotations(setpointRad) * GEAR_RATIO;
+ double motorGoalRotations = Units.radiansToRotations(setpointRad) * HoodConstants.HOOD_GEAR_RATIO;
//Clamp the setpoint rotations
- motorGoalRotations = MathUtil.clamp(motorGoalRotations, Units.radiansToRotations(MIN_ANGLE_RAD) * GEAR_RATIO, Units.radiansToRotations(MAX_ANGLE_RAD) * GEAR_RATIO);
+ motorGoalRotations = MathUtil.clamp(motorGoalRotations, Units.radiansToRotations(Units.degreesToRadians(HoodConstants.MIN_ANGLE)) * HoodConstants.HOOD_GEAR_RATIO, Units.radiansToRotations(Units.degreesToRadians(HoodConstants.MAX_ANGLE)) * HoodConstants.HOOD_GEAR_RATIO);
// Multiply goal velocity by kV
double velocityCompensation = goalVelocityRadPerSec * HoodConstants.FEEDFORWARD_KV;
.withFeedForward(velocityCompensation));
Logger.recordOutput("Hood/Voltage", motor.getMotorVoltage().getValue());
- Logger.recordOutput("Hood/velocitySetpoint", goalVelocityRadPerSec / GEAR_RATIO);
- Logger.recordOutput("Hood/SetpointDeg", Units.radiansToDegrees(goalAngle.getRadians()) / GEAR_RATIO);
+ Logger.recordOutput("Hood/velocitySetpoint", goalVelocityRadPerSec / HoodConstants.HOOD_GEAR_RATIO);
+ Logger.recordOutput("Hood/SetpointDeg", Units.radiansToDegrees(goalAngle.getRadians()) / HoodConstants.HOOD_GEAR_RATIO);
}
@Override
public void updateInputs() {
- inputs.positionDeg = Units.rotationsToDegrees(motor.getPosition().getValueAsDouble()) / GEAR_RATIO;
- inputs.velocityRadPerSec = Units.rotationsToRadians(motor.getVelocity().getValueAsDouble()) / GEAR_RATIO;
+ inputs.positionDeg = Units.rotationsToDegrees(motor.getPosition().getValueAsDouble()) / HoodConstants.HOOD_GEAR_RATIO;
+ inputs.velocityRadPerSec = Units.rotationsToRadians(motor.getVelocity().getValueAsDouble()) / HoodConstants.HOOD_GEAR_RATIO;
inputs.motorCurrent = motor.getStatorCurrent().getValueAsDouble();
}
}
import frc.robot.constants.IdConstants;
public class Turret extends SubsystemBase implements TurretIO{
-
- /* ---------------- Constants ---------------- */
-
- private static final double MIN_ANGLE_RAD = Units.degreesToRadians(TurretConstants.MIN_ANGLE);
- private static final double MAX_ANGLE_RAD = Units.degreesToRadians(TurretConstants.MAX_ANGLE);
-
- private static final double MAX_VEL_RAD_PER_SEC = TurretConstants.MAX_VELOCITY;
- private static final double MAX_ACCEL_RAD_PER_SEC2 = TurretConstants.MAX_ACCELERATION;
-
- private static final double EXTRAPOLATION_TIME_CONSTANT = 0.06;
-
- private static final double GEAR_RATIO = TurretConstants.TURRET_GEAR_RATIO;
-
- private double FEEDFORWARD_KV = 0.185;
-
// Super low magnitude filter for the position to make it less jittery
private final LinearFilter setpointFilter = LinearFilter.singlePoleIIR(0.02, 0.02);
config.Slot0.kD = 0.15; // The "Braking" term to stop overshoot
var mm = config.MotionMagic;
- mm.MotionMagicCruiseVelocity = Units.radiansToRotations(MAX_VEL_RAD_PER_SEC) * GEAR_RATIO;
- mm.MotionMagicAcceleration = Units.radiansToRotations(MAX_ACCEL_RAD_PER_SEC2) * GEAR_RATIO; // Lowered for belt safety
+ mm.MotionMagicCruiseVelocity = Units.radiansToRotations(TurretConstants.MAX_VELOCITY) * TurretConstants.GEAR_RATIO;
+ mm.MotionMagicAcceleration = Units.radiansToRotations(TurretConstants.MAX_ACCELERATION) * TurretConstants.GEAR_RATIO; // Lowered for belt safety
mm.MotionMagicJerk = 0; // Set to > 0 for "S-Curve" smoothing if needed
motor.getConfigurator().apply(config);
simState = motor.getSimState();
turretSim = new SingleJointedArmSim(
edu.wpi.first.math.system.plant.DCMotor.getKrakenX60(1),
- GEAR_RATIO,
+ TurretConstants.GEAR_RATIO,
0.01,
0.15,
- MIN_ANGLE_RAD,
- MAX_ANGLE_RAD,
+ Units.degreesToRadians(TurretConstants.MIN_ANGLE),
+ Units.degreesToRadians(TurretConstants.MAX_ANGLE),
false,
0.0);
}
// double turretRotations = turretIndex / (double) totalTeeth;
- // double motorRotations = turretRotations * TurretConstants.TURRET_GEAR_RATIO;
+ // double motorRotations = turretRotations * TurretConstants.TURRET_TurretConstant.GEAR_RATIO;
// motor.setPosition(motorRotations);
motor.setPosition(0.0); //TODO: remove after chinese remainder theorem works
* @return Posiiton of the turret in radians
*/
public double getPositionRad() {
- return Units.rotationsToRadians(motor.getPosition().getValueAsDouble()) / GEAR_RATIO;
+ return Units.rotationsToRadians(motor.getPosition().getValueAsDouble()) / TurretConstants.GEAR_RATIO;
}
/**
* @return Posiiton of the turret in degrees
*/
public double getPositionDeg() {
- return Units.rotationsToDegrees(motor.getPosition().getValueAsDouble()) / GEAR_RATIO;
+ return Units.rotationsToDegrees(motor.getPosition().getValueAsDouble()) / TurretConstants.GEAR_RATIO;
}
/* ---------------- Periodic ---------------- */
Logger.processInputs("Turret", inputs);
// Position extrapolation
- double lookAheadSeconds = EXTRAPOLATION_TIME_CONSTANT;
+ double lookAheadSeconds = TurretConstants.EXTRAPOLATION_TIME_CONSTANT;
double futureRobotAngle = goalAngle.getRadians() + (goalVelocityRadPerSec * lookAheadSeconds);
//Continuous wrap selection
for (int i = -2; i <= 2; i++) {
double candidate = futureRobotAngle + 2.0 * Math.PI * i;
- if (candidate < MIN_ANGLE_RAD || candidate > MAX_ANGLE_RAD)
+ if (candidate < Units.degreesToRadians(TurretConstants.MIN_ANGLE) || candidate > Units.degreesToRadians(TurretConstants.MAX_ANGLE))
continue;
if (!found || Math.abs(candidate - lastGoalRad) < Math.abs(best - lastGoalRad)) {
best = lastFilteredRad;
// Tells the Kraken to get to this position using 1000Hz profile
- double motorGoalRotations = Units.radiansToRotations(best) * GEAR_RATIO;
+ double motorGoalRotations = Units.radiansToRotations(best) * TurretConstants.GEAR_RATIO;
// Clamp position setpoint to min and max angles
- motorGoalRotations = MathUtil.clamp(motorGoalRotations, Units.degreesToRotations(-180) * GEAR_RATIO, Units.degreesToRotations(180) * GEAR_RATIO);
+ motorGoalRotations = MathUtil.clamp(motorGoalRotations, Units.degreesToRotations(-180) * TurretConstants.GEAR_RATIO, Units.degreesToRotations(180) * TurretConstants.GEAR_RATIO);
// Multiply goal velocity by kV
- double robotTurnCompensation = goalVelocityRadPerSec * FEEDFORWARD_KV;
+ double robotTurnCompensation = goalVelocityRadPerSec * TurretConstants.FEEDFORWARD_KV;
// Sets motor control with feedforward
motor.setControl(mmVoltageRequest
turretSim.update(Constants.LOOP_TIME);
simState.setRawRotorPosition(
- Units.radiansToRotations(turretSim.getAngleRads()) * GEAR_RATIO);
+ Units.radiansToRotations(turretSim.getAngleRads()) * TurretConstants.GEAR_RATIO);
simState.setRotorVelocity(
- Units.radiansToRotations(turretSim.getVelocityRadPerSec()) * GEAR_RATIO);
+ Units.radiansToRotations(turretSim.getVelocityRadPerSec()) * TurretConstants.GEAR_RATIO);
}
@Override
public void updateInputs() {
- inputs.positionDeg = Units.rotationsToDegrees(motor.getPosition().getValueAsDouble()) / GEAR_RATIO;
- inputs.velocityRadPerSec = Units.rotationsToRadians(motor.getVelocity().getValueAsDouble()) / GEAR_RATIO;
+ inputs.positionDeg = Units.rotationsToDegrees(motor.getPosition().getValueAsDouble()) / TurretConstants.GEAR_RATIO;
+ inputs.velocityRadPerSec = Units.rotationsToRadians(motor.getVelocity().getValueAsDouble()) / TurretConstants.GEAR_RATIO;
inputs.motorCurrent = motor.getStatorCurrent().getValueAsDouble();
inputs.encoderLeftRot = encoderLeft.getAbsolutePosition().getValueAsDouble();
inputs.encoderRightRot = encoderRight.getAbsolutePosition().getValueAsDouble();