Skip to content

Commit fd3f95f

Browse files
Nummun14ShmayaR
andauthored
Ejection Commands (#14)
* Added Ejecting commands * added ejecting coral from arm/gripper * Update CoralEjectionCommands.java * made atTargetAngle and atState functions that check for reversed states * forget to add the logic for the reversed states * added variable to know if the state is reversed or not * fixed logic issues and renamed stuff * switched reversed state functions to get called by regular ones --------- Co-authored-by: ShmayaR <shmaya.rosenblatt@gmail.com>
1 parent faa4410 commit fd3f95f

File tree

4 files changed

+95
-8
lines changed

4 files changed

+95
-8
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package frc.trigon.robot.commands.commandfactories;
2+
3+
import edu.wpi.first.wpilibj2.command.Command;
4+
import edu.wpi.first.wpilibj2.command.ParallelCommandGroup;
5+
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
6+
import frc.trigon.robot.RobotContainer;
7+
import frc.trigon.robot.subsystems.arm.ArmCommands;
8+
import frc.trigon.robot.subsystems.arm.ArmConstants;
9+
import frc.trigon.robot.subsystems.intake.IntakeCommands;
10+
import frc.trigon.robot.subsystems.intake.IntakeConstants;
11+
import frc.trigon.robot.subsystems.transporter.TransporterCommands;
12+
import frc.trigon.robot.subsystems.transporter.TransporterConstants;
13+
14+
public class CoralEjectionCommands {
15+
public static Command getCoralEjectionCommand() {
16+
return GeneralCommands.getContinuousConditionalCommand(
17+
getEjectCoralFromIntakeCommand(),
18+
getEjectCoralFromArmCommand(),
19+
() -> RobotContainer.TRANSPORTER.hasCoral() || RobotContainer.INTAKE.hasCoral()
20+
);
21+
}
22+
23+
private static Command getEjectCoralFromIntakeCommand() {
24+
return new ParallelCommandGroup(
25+
IntakeCommands.getSetTargetStateCommand(IntakeConstants.IntakeState.EJECT),
26+
TransporterCommands.getSetTargetStateCommand(TransporterConstants.TransporterState.EJECT)
27+
);
28+
}
29+
30+
private static Command getEjectCoralFromArmCommand() {
31+
return new SequentialCommandGroup(
32+
ArmCommands.getPrepareForStateCommand(ArmConstants.ArmState.EJECT).until(RobotContainer.ARM::atTargetAngle),
33+
ArmCommands.getSetTargetStateCommand(ArmConstants.ArmState.EJECT)
34+
);
35+
}
36+
}

src/main/java/frc/trigon/robot/subsystems/arm/Arm.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class Arm extends MotorSubsystem {
2525
ArmConstants.ARM_DEFAULT_MAXIMUM_ACCELERATION,
2626
ArmConstants.ARM_DEFAULT_MAXIMUM_JERK
2727
).withEnableFOC(ArmConstants.FOC_ENABLED);
28+
public boolean isStateReversed = false;
2829
private ArmConstants.ArmState targetState = ArmConstants.ArmState.REST;
2930

3031
public Arm() {
@@ -80,10 +81,24 @@ public void sysIDDrive(double targetVoltage) {
8081
armMasterMotor.setControl(voltageRequest.withOutput(targetVoltage));
8182
}
8283

84+
public boolean atState(ArmConstants.ArmState targetState, boolean isStateReversed) {
85+
if (isStateReversed)
86+
return this.targetState == targetState && atTargetAngle(isStateReversed);
87+
return atState(targetState);
88+
}
89+
8390
public boolean atState(ArmConstants.ArmState targetState) {
8491
return this.targetState == targetState && atTargetAngle();
8592
}
8693

94+
public boolean atTargetAngle(boolean isStateReversed) {
95+
if (isStateReversed) {
96+
final double currentToTargetStateDifferenceDegrees = Math.abs(ArmConstants.FULL_ROTATION.minus(targetState.targetAngle).minus(getAngle()).getDegrees());
97+
return currentToTargetStateDifferenceDegrees < ArmConstants.ANGLE_TOLERANCE.getDegrees();
98+
}
99+
return atTargetAngle();
100+
}
101+
87102
public boolean atTargetAngle() {
88103
final double currentToTargetStateDifferenceDegrees = Math.abs(targetState.targetAngle.minus(getAngle()).getDegrees());
89104
return currentToTargetStateDifferenceDegrees < ArmConstants.ANGLE_TOLERANCE.getDegrees();
@@ -93,30 +108,48 @@ public boolean hasGamePiece() {
93108
return ArmConstants.COLLECTION_DETECTION_BOOLEAN_EVENT.getAsBoolean();
94109
}
95110

111+
void setTargetState(ArmConstants.ArmState targetState) {
112+
this.isStateReversed = false;
113+
this.targetState = targetState;
114+
setTargetState(targetState, false);
115+
}
116+
96117
void setTargetState(ArmConstants.ArmState targetState, boolean isStateReversed) {
118+
this.isStateReversed = isStateReversed;
119+
this.targetState = targetState;
120+
97121
if (isStateReversed) {
98122
setTargetState(
99-
Rotation2d.fromDegrees(360 - targetState.targetAngle.getDegrees())
123+
ArmConstants.FULL_ROTATION.minus(targetState.targetAngle)
100124
, targetState.targetEndEffectorVoltage
101125
);
102126
return;
103127
}
104-
setTargetState(targetState);
105-
}
106-
107-
void setTargetState(ArmConstants.ArmState targetState) {
108-
this.targetState = targetState;
109128
setTargetState(
110129
targetState.targetAngle,
111-
targetState.targetEndEffectorVoltage
112-
);
130+
targetState.targetEndEffectorVoltage);
113131
}
114132

115133
void setTargetState(Rotation2d targetAngle, double targetVoltage) {
116134
setTargetAngle(targetAngle);
117135
setTargetVoltage(targetVoltage);
118136
}
119137

138+
void prepareForState(ArmConstants.ArmState targetState) {
139+
prepareForState(targetState, false);
140+
}
141+
142+
void prepareForState(ArmConstants.ArmState targetState, boolean isStateReversed) {
143+
this.isStateReversed = isStateReversed;
144+
this.targetState = targetState;
145+
146+
if (isStateReversed) {
147+
setTargetAngle(ArmConstants.FULL_ROTATION.minus(targetState.targetAngle));
148+
return;
149+
}
150+
setTargetAngle(targetState.targetAngle);
151+
}
152+
120153
private Rotation2d getAngle() {
121154
return Rotation2d.fromRotations(angleEncoder.getSignal(CANcoderSignal.POSITION));
122155
}

src/main/java/frc/trigon/robot/subsystems/arm/ArmCommands.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,20 @@ public static Command getSetTargetStateCommand(ArmConstants.ArmState targetState
4747
RobotContainer.ARM
4848
);
4949
}
50+
51+
public static Command getPrepareForStateCommand(ArmConstants.ArmState targetState, boolean isStateReversed) {
52+
return new StartEndCommand(
53+
() -> RobotContainer.ARM.prepareForState(targetState, isStateReversed),
54+
RobotContainer.ARM::stop,
55+
RobotContainer.ARM
56+
);
57+
}
58+
59+
public static Command getPrepareForStateCommand(ArmConstants.ArmState targetState) {
60+
return new StartEndCommand(
61+
() -> RobotContainer.ARM.prepareForState(targetState),
62+
RobotContainer.ARM::stop,
63+
RobotContainer.ARM
64+
);
65+
}
5066
}

src/main/java/frc/trigon/robot/subsystems/arm/ArmConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public class ArmConstants {
120120
CommandScheduler.getInstance().getActiveButtonLoop(),
121121
DISTANCE_SENSOR::getBinaryValue
122122
).debounce(COLLECTION_DETECTION_DEBOUNCE_TIME_SECONDS);
123+
static final Rotation2d FULL_ROTATION = Rotation2d.fromDegrees(360);
123124

124125
static {
125126
configureArmMasterMotor();
@@ -229,6 +230,7 @@ public enum ArmState {
229230
REST(Rotation2d.fromDegrees(0), 0),
230231
REST_FOR_CLIMB(Rotation2d.fromDegrees(0), 0),
231232
HOLD_ALGAE(Rotation2d.fromDegrees(90), -4),
233+
EJECT(Rotation2d.fromDegrees(60), 4),
232234
PREPARE_SCORE_L1(Rotation2d.fromDegrees(80), 0),
233235
SCORE_L1(Rotation2d.fromDegrees(75), 4),
234236
PREPARE_SCORE_L2(Rotation2d.fromDegrees(95), 0),

0 commit comments

Comments
 (0)