|
| 1 | +// Copyright (c) 2025 FRC 6423 - Ward Melville Iron Patriots |
| 2 | +// https://github.com/wmironpatriots |
| 3 | +// |
| 4 | +// Open Source Software; you can modify and/or share it under the terms of |
| 5 | +// MIT license file in the root directory of this project |
| 6 | + |
| 7 | +package org.frc6423.robot.subsystems.arm; |
| 8 | + |
| 9 | +import static edu.wpi.first.units.Units.Centimeters; |
| 10 | +import static edu.wpi.first.units.Units.Degrees; |
| 11 | +import static edu.wpi.first.units.Units.Inches; |
| 12 | +import static edu.wpi.first.units.Units.KilogramSquareMeters; |
| 13 | +import static edu.wpi.first.units.Units.Radians; |
| 14 | + |
| 15 | +import edu.wpi.first.epilogue.Logged; |
| 16 | +import edu.wpi.first.math.MathUtil; |
| 17 | +import edu.wpi.first.math.filter.LinearFilter; |
| 18 | +import edu.wpi.first.math.geometry.Rotation2d; |
| 19 | +import edu.wpi.first.units.measure.Angle; |
| 20 | +import edu.wpi.first.units.measure.Distance; |
| 21 | +import edu.wpi.first.units.measure.MomentOfInertia; |
| 22 | +import edu.wpi.first.wpilibj.smartdashboard.Mechanism2d; |
| 23 | +import edu.wpi.first.wpilibj.smartdashboard.MechanismLigament2d; |
| 24 | +import edu.wpi.first.wpilibj.smartdashboard.MechanismRoot2d; |
| 25 | +import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; |
| 26 | +import edu.wpi.first.wpilibj.util.Color; |
| 27 | +import edu.wpi.first.wpilibj.util.Color8Bit; |
| 28 | +import edu.wpi.first.wpilibj2.command.Command; |
| 29 | +import edu.wpi.first.wpilibj2.command.Commands; |
| 30 | +import edu.wpi.first.wpilibj2.command.SubsystemBase; |
| 31 | +import java.util.function.DoubleSupplier; |
| 32 | + |
| 33 | +/** Pivot subsystem-component of the {@link Arm} Subsystem */ |
| 34 | +public class ArmPivot extends SubsystemBase implements AutoCloseable { |
| 35 | + /** CONSTANTS */ |
| 36 | + public static final double PIVOT_GEARING = 50; |
| 37 | + |
| 38 | + public static final MomentOfInertia MOI = KilogramSquareMeters.of(0.001); |
| 39 | + public static final Distance LENGTH = Inches.of(7.5); |
| 40 | + |
| 41 | + public static final Angle MAX_ANGLE = Degrees.of(180); |
| 42 | + public static final Angle TOLERANCE = Degrees.of(1.5); |
| 43 | + |
| 44 | + @Logged(name = "Arm Pivot Hardware Loggables") |
| 45 | + private final ArmPivotIO hardware; |
| 46 | + |
| 47 | + private final LinearFilter pivotCurrentFilter = LinearFilter.movingAverage(5); |
| 48 | + |
| 49 | + @Logged(name = "Filted Pivot Motor Current (Amps)") |
| 50 | + private double filteredPivotCurrent; |
| 51 | + |
| 52 | + @Logged(name = "Is Zeroed (bool)") |
| 53 | + private boolean isZeroed = false; |
| 54 | + |
| 55 | + private final Mechanism2d canvas = |
| 56 | + new Mechanism2d(LENGTH.in(Centimeters), LENGTH.in(Centimeters) * 2); |
| 57 | + private final MechanismRoot2d root = |
| 58 | + canvas.getRoot("pivot", LENGTH.in(Centimeters), LENGTH.in(Centimeters)); |
| 59 | + private final MechanismLigament2d visualizer = |
| 60 | + root.append( |
| 61 | + new MechanismLigament2d( |
| 62 | + "arm", LENGTH.in(Centimeters), 0.0, 10, new Color8Bit(Color.kAliceBlue))); |
| 63 | + |
| 64 | + public ArmPivot(ArmPivotIO hardware) { |
| 65 | + this.hardware = hardware; |
| 66 | + |
| 67 | + SmartDashboard.putData("ArmVisualizer", canvas); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void periodic() { |
| 72 | + hardware.periodic(); |
| 73 | + |
| 74 | + filteredPivotCurrent = pivotCurrentFilter.calculate(hardware.getStatorCurrentAmps()); |
| 75 | + |
| 76 | + visualizer.setAngle( |
| 77 | + Rotation2d.fromRadians(hardware.getAngleRads()).plus(Rotation2d.kCCW_90deg)); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return true if arm is within a certain tolerance of the setpoint angle |
| 82 | + */ |
| 83 | + @Logged(name = "Near Setpoint Angle (bool)") |
| 84 | + public boolean isNearSetpointAngle() { |
| 85 | + return MathUtil.isNear( |
| 86 | + hardware.getSetpointAngleRads(), hardware.getAngleRads(), TOLERANCE.in(Radians)); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Run arm into hardstop to determine home (aka, zero) |
| 91 | + * |
| 92 | + * @return {@link Command} |
| 93 | + */ |
| 94 | + // TODO CHECK VALUES |
| 95 | + public Command runCurrentHoming() { |
| 96 | + return this.run( |
| 97 | + () -> { |
| 98 | + hardware.setVolts(-2.5); |
| 99 | + }) |
| 100 | + .until(() -> Math.abs(filteredPivotCurrent) > 50.0) |
| 101 | + .finallyDo( |
| 102 | + (interupted) -> { |
| 103 | + if (!interupted) { |
| 104 | + hardware.resetEncoder(0.0); |
| 105 | + isZeroed = true; |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Run arm to specified angle |
| 112 | + * |
| 113 | + * @param angle {@link Angle} representing desired angle |
| 114 | + * @return {@link Command} |
| 115 | + */ |
| 116 | + public Command runAngle(Angle angle) { |
| 117 | + return this.run(() -> hardware.setAngle(angle.in(Radians))); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Run arm to specified angle |
| 122 | + * |
| 123 | + * @param angle desired angle in radians |
| 124 | + * @return {@link Command} |
| 125 | + */ |
| 126 | + public Command runAngle(DoubleSupplier angleRads) { |
| 127 | + return this.run(() -> hardware.setAngle(angleRads.getAsDouble())); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Run arm to specified angle |
| 132 | + * |
| 133 | + * @param angle desired angle in radians |
| 134 | + * @return {@link Command} |
| 135 | + */ |
| 136 | + public Command runAngle(double angleRads) { |
| 137 | + return runAngle(() -> angleRads); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Hold arm at current angle |
| 142 | + * |
| 143 | + * @return {@link Command} |
| 144 | + */ |
| 145 | + public Command holdAngle() { |
| 146 | + return Commands.sequence( |
| 147 | + this.run( |
| 148 | + () -> { |
| 149 | + var currentAngle = hardware.getAngleRads(); |
| 150 | + hardware.setAngle(currentAngle); |
| 151 | + }) |
| 152 | + .until(() -> true), |
| 153 | + this.run(() -> {})); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void close() throws Exception { |
| 158 | + hardware.close(); |
| 159 | + } |
| 160 | +} |
0 commit comments