Skip to content

Commit c184870

Browse files
GrabberSim
1 parent d6d4219 commit c184870

3 files changed

Lines changed: 61 additions & 14 deletions

File tree

src/main/java/frc/robot/RobotContainer.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import frc.robot.generated.PracticeTunerConstants;
5757
import frc.robot.subsystems.brain.BrainSubsystem;
5858
import frc.robot.subsystems.brain.ExecuteRobotActionCmd;
59-
import frc.robot.subsystems.brain.GamePiece;
6059
import frc.robot.subsystems.brain.QueueRobotActionCmd;
6160
import frc.robot.subsystems.brain.RobotAction;
6261
import frc.robot.subsystems.brain.SetCoralSideCmd;
@@ -76,6 +75,7 @@
7675
import frc.robot.subsystems.funnel.FunnelSubsystem;
7776
import frc.robot.subsystems.grabber.GrabberIO;
7877
import frc.robot.subsystems.grabber.GrabberIOHardware;
78+
import frc.robot.subsystems.grabber.GrabberIOSim;
7979
import frc.robot.subsystems.grabber.GrabberSubsystem;
8080
import frc.robot.subsystems.manipulator.ManipulatorConstants;
8181
import frc.robot.subsystems.manipulator.ManipulatorIO;
@@ -272,7 +272,7 @@ private RobotContainer() {
272272
}
273273

274274
try {
275-
grabber_ = new GrabberSubsystem(new GrabberIOHardware());
275+
grabber_ = new GrabberSubsystem(new GrabberIOSim(drivebase_::getPose));
276276
} catch (Exception ex) {
277277
subsystemCreateException(ex);
278278
}
@@ -390,8 +390,7 @@ private RobotContainer() {
390390
manipulator_::getElevatorPosition,
391391
manipulator_::getArmPosition,
392392
climber_::getClimberPosition,
393-
() -> brain_.gp() == GamePiece.ALGAE_HIGH,
394-
() -> brain_.gp() == GamePiece.CORAL
393+
brain_::gp
395394
);
396395

397396
new Mechanism3d(
@@ -400,8 +399,7 @@ private RobotContainer() {
400399
manipulator_::getElevatorTarget,
401400
manipulator_::getArmTarget,
402401
climber_::getClimberPosition,
403-
() -> brain_.gp() == GamePiece.ALGAE_HIGH,
404-
() -> brain_.gp() == GamePiece.CORAL
402+
brain_::gp
405403
);
406404

407405
// Configure the button bindings
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package frc.robot.subsystems.grabber;
2+
3+
import static edu.wpi.first.units.Units.Meters;
4+
5+
import java.util.List;
6+
import java.util.function.Supplier;
7+
8+
import edu.wpi.first.math.geometry.Pose2d;
9+
import edu.wpi.first.units.measure.Distance;
10+
import frc.robot.Constants;
11+
import frc.robot.Constants.FieldConstants;
12+
import frc.robot.Constants.RobotType;
13+
14+
public class GrabberIOSim extends GrabberIOHardware {
15+
16+
private static final Distance collectTolerance = Meters.one();
17+
18+
private final Supplier<Pose2d> robotPose_;
19+
private final List<Pose2d> stations_;
20+
21+
// 1, 2, 13, 12
22+
23+
public GrabberIOSim(Supplier<Pose2d> robotPose) throws Exception {
24+
robotPose_ = robotPose;
25+
26+
stations_ = List.of(
27+
tagPose(1),
28+
tagPose(2),
29+
tagPose(12),
30+
tagPose(13)
31+
);
32+
}
33+
34+
@Override
35+
public void updateInputs(GrabberIOInputs inputs) {
36+
super.updateInputs(inputs);
37+
38+
if (Constants.getRobot() != RobotType.XEROSIM) {
39+
Pose2d robot = robotPose_.get();
40+
Pose2d nearestStation = robot.nearest(stations_);
41+
42+
inputs.coralSensor = robot.getTranslation().getDistance(nearestStation.getTranslation()) <= collectTolerance.in(Meters);
43+
inputs.algaeSensor1 = true;
44+
inputs.algaeSensor2 = true;
45+
}
46+
}
47+
48+
private Pose2d tagPose(int id) {
49+
return FieldConstants.layout.getTagPose(id).orElseThrow().toPose2d();
50+
}
51+
}

src/main/java/frc/robot/util/Mechanism3d.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import edu.wpi.first.units.measure.Angle;
1616
import edu.wpi.first.units.measure.Distance;
1717
import edu.wpi.first.wpilibj2.command.SubsystemBase;
18+
import frc.robot.subsystems.brain.GamePiece;
1819

1920
public class Mechanism3d extends SubsystemBase {
2021

@@ -40,8 +41,7 @@ public class Mechanism3d extends SubsystemBase {
4041
private final Supplier<Distance> elevatorHeightSupplier_;
4142
private final Supplier<Angle> armAngleSupplier_;
4243
private final Supplier<Angle> climberAngleSupplier_;
43-
private final Supplier<Boolean> hasAlgaeSupplier_;
44-
private final Supplier<Boolean> hasCoralSupplier_;
44+
private final Supplier<GamePiece> gpSupplier_;
4545

4646
private Pose2d robotPose_ = new Pose2d();
4747
private Distance elevatorHeight_ = Meters.zero();
@@ -54,16 +54,14 @@ public Mechanism3d(
5454
Supplier<Distance> elevatorHeight,
5555
Supplier<Angle> armAngle,
5656
Supplier<Angle> climberAngle,
57-
Supplier<Boolean> hasAlgae,
58-
Supplier<Boolean> hasCoral
57+
Supplier<GamePiece> gamePiece
5958
) {
6059
key_ = key;
6160
robotPoseSupplier_ = robotPose;
6261
elevatorHeightSupplier_ = elevatorHeight;
6362
armAngleSupplier_ = armAngle;
6463
climberAngleSupplier_ = climberAngle;
65-
hasAlgaeSupplier_ = hasAlgae;
66-
hasCoralSupplier_ = hasCoral;
64+
gpSupplier_ = gamePiece;
6765
}
6866

6967
@Override
@@ -90,11 +88,11 @@ public void periodic() {
9088
new Rotation3d(climberAngle_.unaryMinus(), Degrees.zero(), Degrees.zero())
9189
);
9290

93-
Logger.recordOutput("Gamepiece3d/Algae/" + key_, hasAlgaeSupplier_.get() ?
91+
Logger.recordOutput("Gamepiece3d/Algae/" + key_, gpSupplier_.get() == GamePiece.ALGAE_HIGH ?
9492
new Pose3d[] { robotToField(arm.plus(armToAlgae)) } : new Pose3d[0]
9593
);
9694

97-
Logger.recordOutput("Gamepiece3d/Coral/" + key_, hasCoralSupplier_.get() ?
95+
Logger.recordOutput("Gamepiece3d/Coral/" + key_, gpSupplier_.get() == GamePiece.CORAL ?
9896
new Pose3d[] { robotToField(arm.transformBy(armToCoral)) } : new Pose3d[0]
9997
);
10098

0 commit comments

Comments
 (0)