|
6 | 6 |
|
7 | 7 | package org.frc6423.robot.subsystems.arm; |
8 | 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 | 9 | 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 | 10 | import edu.wpi.first.wpilibj2.command.Command; |
29 | 11 | import edu.wpi.first.wpilibj2.command.Commands; |
30 | 12 | import edu.wpi.first.wpilibj2.command.SubsystemBase; |
| 13 | +import org.frc6423.lib.subsystems.RollerIO; |
31 | 14 |
|
32 | | -public class Arm extends SubsystemBase implements AutoCloseable { |
33 | | - /** CONSTANTS */ |
34 | | - public static final double PIVOT_GEARING = 50; |
35 | | - |
36 | | - public static final double ROLLER_GEARING = 5.8677; // ! this is wrong |
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 Hardware Loggables") |
45 | | - private final ArmIO hardware; |
46 | | - |
47 | | - private final LinearFilter pivotCurrentFilter = LinearFilter.movingAverage(5); |
48 | | - private final LinearFilter rollerCurrentFilter = LinearFilter.movingAverage(3); |
| 15 | +/** Arm Subsystem */ |
| 16 | +public class Arm extends SubsystemBase { |
| 17 | + @Logged(name = "Pivot Subsystem-Component") |
| 18 | + private final ArmPivot pivot; |
49 | 19 |
|
50 | | - @Logged(name = "Filted Pivot Motor Current (Amps)") |
51 | | - private double filteredPivotCurrent; |
| 20 | + @Logged(name = "Roller Subsystem-Component") |
| 21 | + private final ArmRoller roller; |
52 | 22 |
|
53 | | - @Logged(name = "Filted Roller Motor Current (Amps)") |
54 | | - private double filteredRollerCurrent; |
| 23 | + @Logged(name = "Arm Setpoint State") |
| 24 | + private ArmState setpointState = ArmState.STOWED; |
55 | 25 |
|
56 | | - @Logged(name = "Is Zeroed (bool)") |
57 | | - private boolean isZeroed = false; |
58 | | - |
59 | | - private final Mechanism2d canvas = |
60 | | - new Mechanism2d(LENGTH.in(Centimeters), LENGTH.in(Centimeters) * 2); |
61 | | - private final MechanismRoot2d root = |
62 | | - canvas.getRoot("pivot", LENGTH.in(Centimeters), LENGTH.in(Centimeters)); |
63 | | - private final MechanismLigament2d visualizer = |
64 | | - root.append( |
65 | | - new MechanismLigament2d( |
66 | | - "arm", LENGTH.in(Centimeters), 0.0, 10, new Color8Bit(Color.kAliceBlue))); |
67 | | - |
68 | | - public Arm(ArmIO hardware) { |
69 | | - this.hardware = hardware; |
70 | | - |
71 | | - SmartDashboard.putData("ArmVisualizer", canvas); |
| 26 | + public Arm(ArmPivotIO pivotHardware, RollerIO rollerHardware) { |
| 27 | + this.pivot = new ArmPivot(pivotHardware); |
| 28 | + this.roller = new ArmRoller(rollerHardware); |
72 | 29 | } |
73 | 30 |
|
74 | 31 | @Override |
75 | | - public void periodic() { |
76 | | - hardware.periodic(); |
77 | | - |
78 | | - filteredPivotCurrent = pivotCurrentFilter.calculate(hardware.getPivotStatorCurrentAmps()); |
79 | | - filteredRollerCurrent = rollerCurrentFilter.calculate(hardware.getRollerStatorCurrentAmps()); |
80 | | - |
81 | | - visualizer.setAngle( |
82 | | - Rotation2d.fromRadians(hardware.getPivotAngleRads()).plus(Rotation2d.kCCW_90deg)); |
83 | | - } |
| 32 | + public void periodic() {} |
84 | 33 |
|
85 | 34 | /** |
86 | 35 | * @return true if arm is within a certain tolerance of the setpoint angle |
87 | 36 | */ |
88 | | - @Logged(name = "Near Setpoint Angle (bool)") |
89 | | - public boolean nearSetpointAngle() { |
90 | | - return MathUtil.isNear( |
91 | | - hardware.getPivotSetpointAngleRads(), hardware.getPivotAngleRads(), TOLERANCE.in(Radians)); |
| 37 | + public boolean isNearSetpointAngle() { |
| 38 | + return pivot.isNearSetpointAngle(); |
92 | 39 | } |
93 | 40 |
|
94 | 41 | /** |
95 | | - * @return true when coral hits hardstop |
| 42 | + * @return true if arm has vectored coral piece |
96 | 43 | */ |
97 | | - @Logged(name = "Coral Vectored (bool)") |
98 | | - public boolean coralVectored() { |
99 | | - // Checks if roller motors are stalled |
100 | | - // TODO check this value |
101 | | - return Math.abs(filteredRollerCurrent) > 30.0; |
| 44 | + public boolean hasCoralVectored() { |
| 45 | + return roller.isStalling(); |
102 | 46 | } |
103 | 47 |
|
104 | 48 | /** |
105 | | - * Run arm backwards until it hits hardstop to determine zero |
| 49 | + * Run arm to specified {@link ArmState} |
106 | 50 | * |
107 | | - * @return {@link Command} |
108 | | - */ |
109 | | - public Command runCurrentHoming() { |
110 | | - return this.run( |
111 | | - () -> { |
112 | | - hardware.runPivotVolts(-2.5); |
113 | | - // TODO check this value |
114 | | - }) |
115 | | - .until(() -> Math.abs(filteredPivotCurrent) > 50.0) |
116 | | - .finallyDo( |
117 | | - (interupted) -> { |
118 | | - if (!interupted) { |
119 | | - hardware.resetPivotEncoder(0.0); |
120 | | - isZeroed = true; |
121 | | - } |
122 | | - }); |
123 | | - } |
124 | | - |
125 | | - /** |
126 | | - * Run Arm to specified {@link ArmState} |
127 | | - * |
128 | | - * @param state {@link ArmState} representing hardware setpoints |
| 51 | + * @param state desired {@link ArmState} |
129 | 52 | * @return {@link Command} |
130 | 53 | */ |
131 | 54 | public Command runState(ArmState state) { |
132 | | - return this.run( |
133 | | - () -> { |
134 | | - hardware.runPivotAngle(state.angle.getRadians()); |
135 | | - hardware.runRollerSpeed(state.rollerSpeedRpm); |
136 | | - }); |
| 55 | + return Commands.parallel( |
| 56 | + this.run(() -> setpointState = state).until(() -> true), |
| 57 | + pivot.runAngle(state.angle.getMeasure()), |
| 58 | + roller.runSpeed(state.rollerSpeedRpm)); |
137 | 59 | } |
138 | 60 |
|
139 | 61 | /** |
140 | | - * Hold Arm at specified {@link ArmState} |
| 62 | + * Hold arm at current angle and roller speed |
141 | 63 | * |
142 | | - * @param state {@link ArmState} representing hardware setpoints |
143 | 64 | * @return {@link Command} |
144 | 65 | */ |
145 | | - public Command holdState(ArmState state) { |
146 | | - return Commands.sequence( |
147 | | - this.run( |
148 | | - () -> { |
149 | | - var currentAngle = hardware.getPivotAngleRads(); |
150 | | - var currentSpeed = hardware.getRollerSpeedRpm(); |
151 | | - |
152 | | - hardware.runPivotAngle(currentAngle); |
153 | | - hardware.runRollerSpeed(currentSpeed); |
154 | | - }) |
155 | | - .until(() -> true), |
156 | | - this.run(() -> {})); |
157 | | - } |
158 | | - |
159 | | - @Override |
160 | | - public void close() throws Exception { |
161 | | - hardware.close(); |
| 66 | + public Command holdState() { |
| 67 | + return Commands.parallel( |
| 68 | + this.run(() -> {}).until(() -> true), pivot.holdAngle(), roller.holdSpeed()); |
162 | 69 | } |
163 | 70 | } |
0 commit comments