Skip to content

Commit

Permalink
add glass Mechanism2d integration
Browse files Browse the repository at this point in the history
We added the mechanism2d, and made the simulation function so that on the display, the arm revolves around the center.

Co-Authored-By: lydiahays <[email protected]>
  • Loading branch information
Keegan-Welch and lydiahays committed Jan 27, 2024
1 parent 3d018d1 commit c221d62
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/org/team340/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,7 @@ public void testPeriodic() {}
public void simulationInit() {}

@Override
public void simulationPeriodic() {}
public void simulationPeriodic() {
RobotContainer.simulationPeriodic();
}
}
4 changes: 4 additions & 0 deletions src/main/java/org/team340/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,8 @@ private static double getDriveY() {
private static double getDriveRotate() {
return driver.getTriggerDifference(ControllerConstants.DRIVE_ROT_MULTIPLIER, ControllerConstants.DRIVE_ROT_EXP);
}

public static void simulationPeriodic() {
shooter.simulationPeriodic();
}
}
24 changes: 24 additions & 0 deletions src/main/java/org/team340/robot/subsystems/Shooter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.smartdashboard.Mechanism2d;
import edu.wpi.first.wpilibj.smartdashboard.MechanismLigament2d;
import edu.wpi.first.wpilibj.smartdashboard.MechanismRoot2d;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.util.Color;
import edu.wpi.first.wpilibj.util.Color8Bit;
import edu.wpi.first.wpilibj2.command.Command;
import java.util.function.Supplier;
import org.team340.lib.GRRSubsystem;
Expand Down Expand Up @@ -49,6 +55,15 @@ public class Shooter extends GRRSubsystem {
private final RelativeEncoder pivotRelativeEncoder = pivotMotor.getEncoder();

private final DigitalInput noteDetector = createDigitalInput("Note Detector", RobotMap.SHOOTER_NOTE_DETECTOR);

// Create a Mechanism2d display of an Arm with a fixed ArmTower and moving Arm.
int simulationAngle = 0;
private static final Mechanism2d m_mech2d = new Mechanism2d(60, 60);
private static final MechanismRoot2d m_armPivot = m_mech2d.getRoot("ArmPivot", 30, 30);
private final MechanismLigament2d m_armTower = m_armPivot.append(new MechanismLigament2d("ArmTower", 30, -90));
private static final MechanismLigament2d m_arm = m_armPivot.append(
new MechanismLigament2d("Arm", 30, 30, 6, new Color8Bit(Color.kYellow))
);

public Shooter() {
super("Shooter");
Expand Down Expand Up @@ -88,6 +103,9 @@ public Shooter() {
)
.setIZone(ShooterConstants.SHOOTER_RIGHT_SHOOT_PID.iZone())
.apply(rightShootMotor, rightShootPID);

SmartDashboard.putData("Mech2d", m_mech2d);
m_armTower.setColor(new Color8Bit(Color.kBlue));
}

/**
Expand Down Expand Up @@ -257,4 +275,10 @@ public Command spit() {
//TODO: write this
return null;
}

public void simulationPeriodic() {
// Update the Mechanism Arm angle based on the simulated arm angle
simulationAngle += 1;
m_arm.setAngle(simulationAngle);
}
}

0 comments on commit c221d62

Please sign in to comment.