Skip to content

Commit 1e70674

Browse files
committed
feat: create arm subsytem
Signed-off-by: Dasun Abeykoon <[email protected]>
1 parent 3bc5710 commit 1e70674

File tree

1 file changed

+28
-121
lines changed
  • src/main/java/org/frc6423/robot/subsystems/arm

1 file changed

+28
-121
lines changed

src/main/java/org/frc6423/robot/subsystems/arm/Arm.java

Lines changed: 28 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -6,158 +6,65 @@
66

77
package org.frc6423.robot.subsystems.arm;
88

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-
159
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;
2810
import edu.wpi.first.wpilibj2.command.Command;
2911
import edu.wpi.first.wpilibj2.command.Commands;
3012
import edu.wpi.first.wpilibj2.command.SubsystemBase;
13+
import org.frc6423.lib.subsystems.RollerIO;
3114

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;
4919

50-
@Logged(name = "Filted Pivot Motor Current (Amps)")
51-
private double filteredPivotCurrent;
20+
@Logged(name = "Roller Subsystem-Component")
21+
private final ArmRoller roller;
5222

53-
@Logged(name = "Filted Roller Motor Current (Amps)")
54-
private double filteredRollerCurrent;
23+
@Logged(name = "Arm Setpoint State")
24+
private ArmState setpointState = ArmState.STOWED;
5525

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);
7229
}
7330

7431
@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() {}
8433

8534
/**
8635
* @return true if arm is within a certain tolerance of the setpoint angle
8736
*/
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();
9239
}
9340

9441
/**
95-
* @return true when coral hits hardstop
42+
* @return true if arm has vectored coral piece
9643
*/
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();
10246
}
10347

10448
/**
105-
* Run arm backwards until it hits hardstop to determine zero
49+
* Run arm to specified {@link ArmState}
10650
*
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}
12952
* @return {@link Command}
13053
*/
13154
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));
13759
}
13860

13961
/**
140-
* Hold Arm at specified {@link ArmState}
62+
* Hold arm at current angle and roller speed
14163
*
142-
* @param state {@link ArmState} representing hardware setpoints
14364
* @return {@link Command}
14465
*/
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());
16269
}
16370
}

0 commit comments

Comments
 (0)