Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command factories and triggers examples #53

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/main/java/frc/robot/commands/auto/ExampleAuto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.commands.auto;

import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
import frc.robot.subsystems.example.ExampleSubsystem;

// NOTE: Consider using this command inline, rather than writing a subclass. For more
// information, see:
// https://docs.wpilib.org/en/stable/docs/software/commandbased/convenience-features.html
public class ExampleAuto extends SequentialCommandGroup {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is what we've been using for autos 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're rewriting this!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11

Copy link
Author

@aridavidson001 aridavidson001 Jan 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk tbh. I created another branch that uses auto factories and it's a lot cleaner. I'm not 100% sure it's done yet but if you want to check it out go for it


/** Creates a new ExampleAuto. */
public ExampleAuto(ExampleSubsystem exampleSubsystem) {
// Add your commands in the addCommands() call, e.g.
// addCommands(new FooCommand(), new BarCommand());
addCommands(exampleSubsystem.setPivotToScore().alongWith(exampleSubsystem.setPivotToScore()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package frc.robot.subsystems.example;

public class ExampleConstants {
public static final double SHOOTER_PIVOT_ANGLE = 0 - 9;
}
103 changes: 103 additions & 0 deletions src/main/java/frc/robot/subsystems/example/ExampleSubsystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.subsystems.example;

import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.FunctionalCommand;
import edu.wpi.first.wpilibj2.command.StartEndCommand;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import edu.wpi.first.wpilibj2.command.button.Trigger;
import java.util.function.DoubleSupplier;

public class ExampleSubsystem extends SubsystemBase {
private DoubleSupplier DOUBLELOL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?


/** Creates a new ExampleSubsystem. */
public ExampleSubsystem() {}

// this is the interface for the subsystem
// code for motors and such would go here

@Override
public void periodic() {
// This method will be called once per scheduler run
}

private void exampleSubsystemFunction() {}

private void setExamplePivotAngle(double angle) {
// normal subsystem stuff
}

private double getExamplePivotAngle() {
return 0.0;
}

private boolean isAngleSet() {
return false;
}

private boolean isGamepieceinRobot() {
return false;
}

// Public Command Factory
// create these for everything you would need to access from a command
// so you don't input values or acces the internals of a subsystem in the commands themselves
public Command exampleFunctionalCommand() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dude yk what this is pretty clean

// the command is written here instead of in it's own seperate file.
// this saves space and makes the code more readable

return new FunctionalCommand(
// does on init
() -> this.setExamplePivotAngle(0),
// does on execute
() -> this.setExamplePivotAngle(1),
// does when command ends
interrupted -> this.setExamplePivotAngle(0),
// ends the command when this is true
() -> getExamplePivotAngle() >= 0,
// requirements for the command
this);
}

// Shared internal implementation
private Command exampleStartEndCommand() {
// the command is written here instead of in it's own seperate file.
// this saves space and makes the code more readable

return new StartEndCommand(
// Sets the pivot to 1 while the command is active
() -> this.setExamplePivotAngle(1),
// Sets the pivot to 0 when the command ends
() -> this.setExamplePivotAngle(0),
// requirements for the command
this);
}

// Shared internal implementation
private Command exampleRunOnceCommand(double angle) {
return this.runOnce(() -> setExamplePivotAngle(angle));
}

// Public Command Factory
public Command setPivotToScore() {
return exampleRunOnceCommand(ExampleConstants.SHOOTER_PIVOT_ANGLE);
}

// Public Command Factory
public Command setPivotToAngle(DoubleSupplier angle) {
// if you need to pass a value in do it this way
return exampleRunOnceCommand(angle.getAsDouble());
}

// this is a trigger
// if you need to give information from the subsystem do it like this
// must be a boolean
public final Trigger isAngleInRightSpot = new Trigger(() -> isAngleSet());

// another trigger example
public final Trigger hasGamepiece = new Trigger(() -> isGamepieceinRobot());
}
Loading