Skip to content

Commit

Permalink
Use delegation pattern for registering with the AutoDrive subsystem t…
Browse files Browse the repository at this point in the history
…o negotiate joystick values
  • Loading branch information
ZachOrr committed Jan 30, 2022
1 parent bda096b commit 7dea323
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
5 changes: 0 additions & 5 deletions src/main/java/frc/robot/commands/auto/Top3Ball.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,4 @@ public Top3Ball(Drivetrain drivetrain, Heading heading, AutoDrive autoDrive) {
);
}

@Override
public State calculate(double forward, double strafe, boolean isFieldOriented) {
return m_currentCommand.calculate(forward, strafe, isFieldOriented);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public PointToPointCommand(PolarCoordinate target, Supplier<Pose2d> poseSupplier
@Override
public void initialize() {
heading.enableMaintainHeading();
autoDrive.registerAutoDrivableCommand(this);
}

@Override
Expand Down
40 changes: 24 additions & 16 deletions src/main/java/frc/robot/subsystems/AutoDrive.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package frc.robot.subsystems;

import java.lang.ref.WeakReference;

import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandBase;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.commands.AutoDrivableCommand;

public class AutoDrive extends SubsystemBase {

private WeakReference<AutoDrivableCommand> commandReference;

public static class State {
public double forward;
public double strafe;
Expand All @@ -20,6 +22,19 @@ public State(double forward, double strafe, boolean isFieldOriented) {
}
}

/**
* Registered an AutoDrivableCommand to be called when an output is needed for
* the SwerveDriveCommand. Note: Only one AutoDrivableCommand can be registered
* at a time. Commands calling registerAutoDrivableCommand should require
* the AutoDrive subsystem in order to prevent other commands from
* registering for callbacks.
*
* @param command
*/
public void registerAutoDrivableCommand(AutoDrivableCommand command) {
this.commandReference = new WeakReference<AutoDrivableCommand>(command);
}

/**
* Calculate forward/strafe values using the current command. May return null if
* there is no auto drive command scheduled or the currently auto drive command
Expand All @@ -31,28 +46,21 @@ public State(double forward, double strafe, boolean isFieldOriented) {
* @return - A negotiated forward/strafe from the auto driveable command
*/
public State calculate(double forward, double strafe, boolean isFieldOriented) {
AutoDrivableCommand command = getAutoDrivableCommand();
AutoDrivableCommand command = commandReference.get();
if (command == null) {
return null;
}
return command.calculate(forward, strafe, isFieldOriented);
}

/**
* Gets the current Command for the subsystem if it's an AutoDrivableCommand.
* May return null if the subsystem has no Command or the Command does
* not conform to AutoDrivableCommand.
*/
private AutoDrivableCommand getAutoDrivableCommand() {
Command command = getCurrentCommand();
@Override
public void periodic() {
AutoDrivableCommand command = commandReference.get();
if (command != null) {
SmartDashboard.putData("AutoDrive Current Command", (CommandBase) command);
SmartDashboard.putBoolean("Is AutoDrivableCommand", command instanceof AutoDrivableCommand);
}
if (command != null && command instanceof AutoDrivableCommand) {
return (AutoDrivableCommand) command;
SmartDashboard.putString("AutoDrivable Command", command.toString());
} else {
SmartDashboard.putString("AutoDrivable Command", "N/A");
}
return null;
}

}

0 comments on commit 7dea323

Please sign in to comment.