Skip to content

Commit d3101f8

Browse files
Merge pull request #53 from 4201VitruvianBots/field-constants
Field constants
2 parents dd53e55 + c8788c8 commit d3101f8

24 files changed

Lines changed: 848 additions & 247 deletions

src/main/java/frc/robot/Constants.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import static edu.wpi.first.units.Units.RPM;
1515
import static edu.wpi.first.units.Units.derive;
1616

17+
import com.ctre.phoenix6.CANBus;
1718
import com.ctre.phoenix6.signals.GravityTypeValue;
1819
import com.ctre.phoenix6.signals.SensorDirectionValue;
1920
import com.pathplanner.lib.config.PIDConstants;
@@ -121,8 +122,8 @@ public Angle getAngle() {
121122
}
122123

123124
public class CAN {
124-
public static final String rioCanbus = "rio";
125-
public static final String driveBaseCanbus = "drivebase";
125+
public static final CANBus roboRIO = new CANBus("rio");
126+
public static final CANBus driveBase = new CANBus("drivebase");
126127

127128
public static final int pigeon = 9;
128129

src/main/java/frc/robot/Robot.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public void robotPeriodic() {
6363
// and running subsystem periodic() methods. This must be called from the robot's periodic
6464
// block in order for anything in the Command-based framework to work.
6565
CommandScheduler.getInstance().run();
66+
67+
m_robotContainer.robotPeriodic();
6668
}
6769

6870
/** This function is called once each time the robot enters Disabled mode. */

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,15 @@
3030
import frc.robot.commands.intake.RunIntake;
3131
import frc.robot.commands.shooter.Shoot;
3232
import frc.robot.commands.shooter.ShootManualFlywheel;
33+
import frc.robot.constants.FIELD;
3334
import frc.robot.generated.WoodBotConstants;
3435
import frc.robot.simulation.Robot2d;
3536
import frc.robot.subsystems.*;
3637
import frc.robot.subsystems.Climber;
38+
import frc.robot.subsystems.CommandSwerveDrivetrain;
39+
import frc.robot.subsystems.Indexer;
40+
import frc.robot.subsystems.Intake;
41+
import frc.robot.subsystems.Uptake;
3742
import frc.team4201.lib.simulation.FieldSim;
3843
import frc.team4201.lib.utils.HubTracker;
3944
import frc.team4201.lib.utils.Telemetry;
@@ -88,13 +93,13 @@ public boolean isHubActive() {
8893
}
8994

9095
@NotLogged
91-
private double MaxSpeed =
92-
WoodBotConstants.kSpeedAt12Volts.in(MetersPerSecond); // Kspeed at 12 volts desired top speed
96+
private final double MaxSpeed =
97+
WoodBotConstants.kSpeedAt12Volts.in(MetersPerSecond); // kSpeed at 12 volts desired top speed
9398

9499
private Boolean m_flipToRight = false;
95100

96101
@NotLogged
97-
private double MaxAngularRate =
102+
private final double MaxAngularRate =
98103
RotationsPerSecond.of(SWERVE.kMaxRotationRadiansPerSecond)
99104
.in(RadiansPerSecond); // 3/4 of a rotation per second max angular velocity
100105

@@ -106,7 +111,7 @@ public boolean isHubActive() {
106111

107112
private Robot2d m_robotSim;
108113
private final Telemetry m_telemetry = new Telemetry(MaxSpeed, SWERVE.kModuleTranslations);
109-
private final FieldSim m_fieldSim = new FieldSim();
114+
private FieldSim m_fieldSim = new FieldSim();
110115

111116
@Logged(name = "AutoChooser")
112117
private final SendableChooser<Command> m_autoChooser = new SendableChooser<>();
@@ -116,6 +121,8 @@ public boolean isHubActive() {
116121
/** The container for the robot. Contains subsystems, OI devices, and commands. */
117122
public RobotContainer() {
118123
// Configure the trigger bindings
124+
FIELD.initializeConstants();
125+
FIELD.updateConstants();
119126
initializeSubSystems();
120127
configureBindings();
121128
initSmartDashboard();
@@ -145,6 +152,7 @@ private void initializeSubSystems() {
145152
rotationRate); // Drive counterclockwise with negative X (left)
146153
return drive;
147154
}));
155+
m_fieldSim = new FieldSim();
148156
m_flywheel = new Flywheel();
149157
m_controls = new Controls();
150158
m_vision = new Vision(m_controls);
@@ -162,6 +170,8 @@ private void initializeSubSystems() {
162170
m_led.setDefaultCommand(new UpdateLEDs(m_led, m_swerveDrive, m_intake, m_climber, m_uptake));
163171

164172
if (Robot.isSimulation()) {
173+
FIELD.plotAllPositions(m_fieldSim);
174+
165175
m_robotSim = new Robot2d();
166176
m_robotSim.registerSubsystems(m_flywheel, m_hood, m_indexer, m_intake, m_uptake);
167177
}
@@ -177,8 +187,8 @@ private void configureBindings() {
177187
new AutoAlignDrive(
178188
m_swerveDrive,
179189
m_vision,
180-
() -> m_driverController.getLeftY(),
181-
() -> m_driverController.getLeftX()),
190+
m_driverController::getLeftY,
191+
m_driverController::getLeftX),
182192
new Shoot(m_flywheel, m_vision)));
183193
}
184194

@@ -189,8 +199,8 @@ private void configureBindings() {
189199
new AutoAlignDrive(
190200
m_swerveDrive,
191201
m_vision,
192-
() -> m_driverController.getLeftY(),
193-
() -> m_driverController.getLeftX()));
202+
m_driverController::getLeftY,
203+
m_driverController::getLeftX));
194204
}
195205

196206
if (m_swerveDrive != null && m_flywheel != null && m_vision != null) {
@@ -201,7 +211,7 @@ private void configureBindings() {
201211
m_driverController.y().whileTrue(new ShootManualFlywheel(m_flywheel));
202212
}
203213

204-
// I forsee a state machine in the future...
214+
// I foresee a state machine in the future...
205215
if (m_uptake != null && m_indexer != null && m_intake != null) {
206216
m_driverController
207217
.a()
@@ -244,10 +254,7 @@ private void initSideChooser() {
244254

245255
m_autoSide.addOption("Depot", false);
246256
m_autoSide.addOption("Outpost", true);
247-
m_autoSide.onChange(
248-
(Boolean selected) -> {
249-
m_flipToRight = selected;
250-
});
257+
m_autoSide.onChange((Boolean selected) -> m_flipToRight = selected);
251258
}
252259

253260
private void initSmartDashboard() {
@@ -278,4 +285,8 @@ public Command getAutonomousCommand() {
278285
// An example command will be run in autonomous
279286
return m_autoChooser.getSelected();
280287
}
288+
289+
public void robotPeriodic() {
290+
FIELD.updateCurrentSector(m_swerveDrive.getState().Pose);
291+
}
281292
}

src/main/java/frc/robot/commands/AutoAlignDrive.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
package frc.robot.commands;
22

33
import edu.wpi.first.math.controller.PIDController;
4-
import edu.wpi.first.math.geometry.Rectangle2d;
54
import edu.wpi.first.math.geometry.Translation2d;
65
import edu.wpi.first.math.kinematics.ChassisSpeeds;
76
import edu.wpi.first.math.util.Units;
87
import edu.wpi.first.wpilibj2.command.Command;
98
import frc.robot.Constants.SWERVE;
109
import frc.robot.constants.FIELD;
1110
import frc.robot.subsystems.CommandSwerveDrivetrain;
12-
import frc.robot.subsystems.Controls;
1311
import frc.robot.subsystems.Vision;
1412
import java.util.function.DoubleSupplier;
1513

1614
public class AutoAlignDrive extends Command {
1715
private final CommandSwerveDrivetrain m_swerveDrivetrain;
1816
private final Vision m_vision;
19-
Translation2d m_goal = new Translation2d();
17+
private Translation2d m_goal = Translation2d.kZero;
2018

2119
private Double kTeleP_Theta = 7.4;
2220
private Double kTeleD_Theta = 0.3;
2321
public static final double kTeleI_Theta = 0.0;
2422

25-
private PIDController m_PidController =
23+
private final PIDController m_PidController =
2624
new PIDController(kTeleP_Theta, kTeleI_Theta, kTeleD_Theta);
2725

2826
private final DoubleSupplier m_throttleInput;
@@ -49,18 +47,14 @@ public void initialize() {
4947
kTeleP_Theta = m_vision.m_kPAutoAlignSubscriber.getAsDouble();
5048
kTeleD_Theta = m_vision.m_kDAutoAlignSubscriber.getAsDouble();
5149
m_PidController.reset();
52-
Rectangle2d m_goalZone;
5350

54-
// If we're outside of our own zone, then we align to pass.
55-
if (m_vision.isInOpposingAllianceZone() || m_vision.isInNeutralZone()) {
56-
m_goalZone =
57-
Controls.isBlueAlliance()
58-
? (m_vision.isInLeftHalf() ? FIELD.blueZoneLeft : FIELD.blueZoneRight)
59-
: (m_vision.isInLeftHalf() ? FIELD.redZoneLeft : FIELD.redZoneRight);
60-
m_goal = m_goalZone.getCenter().getTranslation();
51+
// If we're outside our own zone, then we align to pass.
52+
if (m_vision.isInOpposingAllianceSector() || m_vision.isInNeutralSector()) {
53+
// TODO: Add pass position as goal
54+
m_goal = Translation2d.kZero;
6155
// If we're in our own zone, then we align to the hub
6256
} else {
63-
m_goal = Controls.isBlueAlliance() ? FIELD.blueAutoHub : FIELD.redAutoHub;
57+
m_goal = FIELD.HUB.GOAL.getTargetPosition().toTranslation2d();
6458
}
6559
}
6660

src/main/java/frc/robot/commands/shooter/Shoot.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import edu.wpi.first.wpilibj2.command.Command;
1212
import frc.robot.Constants.FLYWHEEL.Shot;
1313
import frc.robot.constants.FIELD;
14-
import frc.robot.subsystems.Controls;
1514
import frc.robot.subsystems.Flywheel;
1615
import frc.robot.subsystems.Vision;
1716

@@ -54,17 +53,13 @@ public Shoot(Flywheel flywheel, Vision vision) {
5453
// Called when the command is initially scheduled.
5554
@Override
5655
public void initialize() {
57-
if (Controls.isBlueAlliance()) {
58-
m_goal = FIELD.blueHub;
59-
} else {
60-
m_goal = FIELD.redHub;
61-
}
56+
m_goal = FIELD.HUB.GOAL.getTargetPosition().toTranslation2d();
6257
}
6358

6459
// Called every time the scheduler runs while the command is scheduled.
6560
@Override
6661
public void execute() {
67-
Shot shot = distanceToShotMap.get(m_vision.getDistancetoHub());
62+
Shot shot = distanceToShotMap.get(m_vision.getDistanceToHub());
6863
m_flywheel.setRPMOutputFOC(shot.shooterRPM);
6964
// m_shooterHood.setPosition(shot.hoodPosition);
7065
}

0 commit comments

Comments
 (0)