Merge pull request #8 from Team4388/inline-commands

Optimize Commands to Reduce Uneeded Verbosity
This commit is contained in:
ryan123rudder
2020-01-07 11:06:51 -07:00
committed by GitHub
7 changed files with 22 additions and 191 deletions
@@ -7,6 +7,8 @@
package frc4388.robot; package frc4388.robot;
import frc4388.utility.LEDPatterns;
/** /**
* The Constants class provides a convenient place for teams to hold robot-wide numerical or boolean * The Constants class provides a convenient place for teams to hold robot-wide numerical or boolean
* constants. This class should not be used for any other purpose. All constants should be * constants. This class should not be used for any other purpose. All constants should be
@@ -25,6 +27,8 @@ public final class Constants {
public static final class LEDConstants { public static final class LEDConstants {
public static final int LED_SPARK_ID = 0; public static final int LED_SPARK_ID = 0;
public static final LEDPatterns DEFAULT_PATTERN = LEDPatterns.FOREST_WAVES;
} }
public static final class OIConstants { public static final class OIConstants {
@@ -10,13 +10,12 @@ package frc4388.robot;
import edu.wpi.first.wpilibj.Joystick; import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.InstantCommand; import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.RunCommand;
import edu.wpi.first.wpilibj2.command.button.JoystickButton; import edu.wpi.first.wpilibj2.command.button.JoystickButton;
import frc4388.robot.Constants.*; import frc4388.robot.Constants.*;
import frc4388.robot.commands.Drive.DriveWithJoystick;
import frc4388.robot.commands.Drive.GamerMove;
import frc4388.robot.commands.LED.UpdateLED;
import frc4388.robot.subsystems.Drive; import frc4388.robot.subsystems.Drive;
import frc4388.robot.subsystems.LED; import frc4388.robot.subsystems.LED;
import frc4388.utility.LEDPatterns;
import frc4388.utility.controller.IHandController; import frc4388.utility.controller.IHandController;
import frc4388.utility.controller.XboxController; import frc4388.utility.controller.XboxController;
@@ -43,8 +42,12 @@ public class RobotContainer {
configureButtonBindings(); configureButtonBindings();
/* Default Commands */ /* Default Commands */
m_robotDrive.setDefaultCommand(new DriveWithJoystick(m_robotDrive, getDriverController())); // drives the robot with a two-axis input from the driver controller
m_robotLED.setDefaultCommand(new UpdateLED(m_robotLED)); m_robotDrive.setDefaultCommand(new RunCommand(() -> m_robotDrive.driveWithInput(
getDriverController().getLeftYAxis(),
getDriverController().getRightXAxis())));
// continually sends updates to the Blinkin LED controller to keep the lights on
m_robotLED.setDefaultCommand(new RunCommand(() -> m_robotLED.updateLED()));
} }
/** /**
@@ -54,9 +57,16 @@ public class RobotContainer {
* {@link edu.wpi.first.wpilibj2.command.button.JoystickButton}. * {@link edu.wpi.first.wpilibj2.command.button.JoystickButton}.
*/ */
private void configureButtonBindings() { private void configureButtonBindings() {
/* Driver Buttons */
// test command to spin the robot while pressing A on the driver controller
new JoystickButton(getDriverJoystick(), XboxController.A_BUTTON) new JoystickButton(getDriverJoystick(), XboxController.A_BUTTON)
.whenPressed(new GamerMove(m_robotDrive)) .whileHeld(() -> m_robotDrive.driveWithInput(0, 1));
.whenReleased(new DriveWithJoystick(m_robotDrive, getDriverController()));
/* Operator Buttons */
// activates "Lit Mode"
new JoystickButton(getOperatorJoystick(), XboxController.A_BUTTON)
.whenPressed(() -> m_robotLED.setPattern(LEDPatterns.LAVA_RAINBOW))
.whenReleased(() -> m_robotLED.setPattern(LEDConstants.DEFAULT_PATTERN));
} }
/** /**
@@ -1,52 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package frc4388.robot.commands.Drive;
import edu.wpi.first.wpilibj2.command.CommandBase;
import frc4388.robot.subsystems.Drive;
import frc4388.utility.controller.IHandController;
public class DriveWithJoystick extends CommandBase {
private final Drive m_drive;
private final IHandController m_driverXbox;
public double m_inputMove, m_inputSteer;
/**
* Creates a new DriveWithJoystick, driving the robot with the given controller
*/
public DriveWithJoystick(Drive subsystem, IHandController controller) {
m_drive = subsystem;
m_driverXbox = controller;
addRequirements(m_drive);
}
// Called when the command is initially scheduled.
@Override
public void initialize() {
}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
m_inputMove = m_driverXbox.getLeftYAxis();
m_inputSteer = m_driverXbox.getRightXAxis();
m_drive.driveWithInput(m_inputMove, m_inputSteer);
}
// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
}
// Returns true when the command should end.
@Override
public boolean isFinished() {
return false;
}
}
@@ -1,47 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package frc4388.robot.commands.Drive;
import edu.wpi.first.wpilibj2.command.CommandBase;
import frc4388.robot.subsystems.Drive;
public class GamerMove extends CommandBase {
private final Drive m_drive;
/**
* Creates a new GamerMove.
*/
public GamerMove(Drive subsystem) {
m_drive = subsystem;
addRequirements(m_drive);
}
// Called when the command is initially scheduled.
@Override
public void initialize() {
}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
m_drive.driveWithInput(0, 1);
}
// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
}
// Returns true when the command should end.
@Override
public boolean isFinished() {
return false;
}
}
@@ -1,37 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package frc4388.robot.commands.LED;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import frc4388.robot.subsystems.LED;
import frc4388.utility.LEDPatterns;
// NOTE: Consider using this command inline, rather than writing a subclass. For more
// information, see:
// https://docs.wpilib.org/en/latest/docs/software/commandbased/convenience-features.html
public class SetLEDPattern extends InstantCommand {
private final LED m_led;
public static LEDPatterns m_pattern;
/**
* Add your docs here.
*/
public SetLEDPattern(LED subsystem, LEDPatterns pattern) {
m_led = subsystem;
m_pattern = pattern;
addRequirements(m_led);
}
// Called when the command is initially scheduled.
@Override
public void initialize() {
m_led.setPattern(m_pattern);
}
}
@@ -1,47 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package frc4388.robot.commands.LED;
import edu.wpi.first.wpilibj2.command.CommandBase;
import frc4388.robot.subsystems.LED;
public class UpdateLED extends CommandBase {
private final LED m_LED;
/**
* Creates a new UpdateLED that continually runs updateLED in the LED subsystem.
*/
public UpdateLED(LED subsystem) {
m_LED = subsystem;
addRequirements(m_LED);
}
// Called when the command is initially scheduled.
@Override
public void initialize() {
}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
m_LED.updateLED();
}
// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
}
// Returns true when the command should end.
@Override
public boolean isFinished() {
return false;
}
}
@@ -28,7 +28,7 @@ public class LED extends SubsystemBase {
*/ */
public LED(){ public LED(){
LEDController = new Spark(LEDConstants.LED_SPARK_ID); LEDController = new Spark(LEDConstants.LED_SPARK_ID);
setPattern(LEDPatterns.FOREST_WAVES); setPattern(LEDConstants.DEFAULT_PATTERN);
LEDController.set(currentLED); LEDController.set(currentLED);
System.err.println("In the Beginning, there was Joe.\nAnd he said, 'Let there be LEDs.'\nAnd it was good."); System.err.println("In the Beginning, there was Joe.\nAnd he said, 'Let there be LEDs.'\nAnd it was good.");
} }