From cb904002ad873f83d248ca01a1882700588a1628 Mon Sep 17 00:00:00 2001 From: "Keenan D. Buckley" Date: Fri, 20 Dec 2019 12:53:02 -0700 Subject: [PATCH 1/3] Change Drive to operate on a command based model --- build.gradle | 2 +- src/main/java/frc4388/robot/OI.java | 14 ++++- .../commands/Drive/DriveWithJoystick.java | 53 +++++++++++++++++++ .../robot/commands/Drive/GamerMove.java | 47 ++++++++++++++++ .../java/frc4388/robot/subsystems/Drive.java | 13 +++-- 5 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 src/main/java/frc4388/robot/commands/Drive/DriveWithJoystick.java create mode 100644 src/main/java/frc4388/robot/commands/Drive/GamerMove.java diff --git a/build.gradle b/build.gradle index 3e0fc59..91e9ef0 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { id "java" - id "edu.wpi.first.GradleRIO" version "2020.1.1-beta-2" + id "edu.wpi.first.GradleRIO" version "2020.1.1-beta-4" } def ROBOT_MAIN_CLASS = "frc4388.robot.Main" diff --git a/src/main/java/frc4388/robot/OI.java b/src/main/java/frc4388/robot/OI.java index 828c8a2..e97b4bb 100644 --- a/src/main/java/frc4388/robot/OI.java +++ b/src/main/java/frc4388/robot/OI.java @@ -8,6 +8,9 @@ package frc4388.robot; import edu.wpi.first.wpilibj.Joystick; +import edu.wpi.first.wpilibj.buttons.JoystickButton; +import frc4388.robot.commands.Drive.DriveWithJoystick; +import frc4388.robot.commands.Drive.GamerMove; import frc4388.utility.controller.IHandController; import frc4388.utility.controller.XboxController; @@ -46,12 +49,16 @@ public class OI { private static OI instance; - private static XboxController m_driverXbox; + public static XboxController m_driverXbox; private static XboxController m_operatorXbox; public OI() { m_driverXbox = new XboxController(RobotMap.XBOX_DRIVER_ID); m_operatorXbox = new XboxController(RobotMap.XBOX_OPERATOR_ID); + + JoystickButton GamerMove = new JoystickButton(getDriverJoystick(), XboxController.A_BUTTON); + GamerMove.whenPressed(new GamerMove()); + GamerMove.whenReleased(new DriveWithJoystick()); } public static OI getInstance() { @@ -74,4 +81,9 @@ public class OI { { return m_operatorXbox.getJoyStick(); } + + public Joystick getDriverJoystick() + { + return m_driverXbox.getJoyStick(); + } } diff --git a/src/main/java/frc4388/robot/commands/Drive/DriveWithJoystick.java b/src/main/java/frc4388/robot/commands/Drive/DriveWithJoystick.java new file mode 100644 index 0000000..572b875 --- /dev/null +++ b/src/main/java/frc4388/robot/commands/Drive/DriveWithJoystick.java @@ -0,0 +1,53 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2018 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.wpilibj.command.Command; +import frc4388.robot.OI; +import frc4388.robot.Robot; + +public class DriveWithJoystick extends Command { + + public double m_inputMove, m_inputSteer; + + public DriveWithJoystick() { + // Use requires() here to declare subsystem dependencies + // eg. requires(chassis); + requires(Robot.m_Drive); + } + + // Called just before this Command runs the first time + @Override + protected void initialize() { + } + + // Called repeatedly when this Command is scheduled to run + @Override + protected void execute() { + m_inputMove = OI.getInstance().getDriverController().getLeftYAxis(); + m_inputSteer = -(OI.getInstance().getDriverController().getRightXAxis()); + Robot.m_Drive.driveWithInput(m_inputMove, m_inputSteer); + } + + // Make this return true when this Command no longer needs to run execute() + @Override + protected boolean isFinished() { + return false; + } + + // Called once after isFinished returns true + @Override + protected void end() { + } + + // Called when another command which requires one or more of the same + // subsystems is scheduled to run + @Override + protected void interrupted() { + } +} diff --git a/src/main/java/frc4388/robot/commands/Drive/GamerMove.java b/src/main/java/frc4388/robot/commands/Drive/GamerMove.java new file mode 100644 index 0000000..fbc171c --- /dev/null +++ b/src/main/java/frc4388/robot/commands/Drive/GamerMove.java @@ -0,0 +1,47 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2018 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.wpilibj.command.Command; +import frc4388.robot.Robot; + +public class GamerMove extends Command { + public GamerMove() { + // Use requires() here to declare subsystem dependencies + // eg. requires(chassis); + requires(Robot.m_Drive); + } + + // Called just before this Command runs the first time + @Override + protected void initialize() { + } + + // Called repeatedly when this Command is scheduled to run + @Override + protected void execute() { + Robot.m_Drive.gamerMove(); + } + + // Make this return true when this Command no longer needs to run execute() + @Override + protected boolean isFinished() { + return false; + } + + // Called once after isFinished returns true + @Override + protected void end() { + } + + // Called when another command which requires one or more of the same + // subsystems is scheduled to run + @Override + protected void interrupted() { + } +} diff --git a/src/main/java/frc4388/robot/subsystems/Drive.java b/src/main/java/frc4388/robot/subsystems/Drive.java index 080c5eb..85cc1f1 100644 --- a/src/main/java/frc4388/robot/subsystems/Drive.java +++ b/src/main/java/frc4388/robot/subsystems/Drive.java @@ -17,6 +17,8 @@ import edu.wpi.first.wpilibj.Talon; import edu.wpi.first.wpilibj.command.Subsystem; import edu.wpi.first.wpilibj.drive.DifferentialDrive; import frc4388.robot.RobotMap; +import frc4388.robot.commands.Drive.DriveWithJoystick; +import frc4388.robot.commands.Drive.GamerMove; import frc4388.robot.OI; import frc4388.robot.Robot; import frc4388.utility.controller.XboxController; @@ -61,17 +63,18 @@ public class Drive extends Subsystem { m_rightBackMotor.setInverted(InvertType.FollowMaster); } - @Override - public void periodic() { - m_inputMove = OI.getInstance().getDriverController().getLeftYAxis(); - m_inputSteer = -(OI.getInstance().getDriverController().getRightXAxis()); + public void driveWithInput(double move, double steer){ + m_driveTrain.arcadeDrive(move, steer); + } - m_driveTrain.arcadeDrive(m_inputMove, m_inputSteer); + public void gamerMove(){ + m_driveTrain.arcadeDrive(0, 1); } @Override public void initDefaultCommand() { // Set the default command for a subsystem here. // setDefaultCommand(new MySpecialCommand()); + setDefaultCommand(new DriveWithJoystick()); } } From c6904663562d225bdfa83860a15987ada5acc498 Mon Sep 17 00:00:00 2001 From: "Keenan D. Buckley" Date: Fri, 20 Dec 2019 13:23:51 -0700 Subject: [PATCH 2/3] Update build.gradle Fixes the version so that the code will compile. No idea how to use 2020 version. --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 91e9ef0..590560a 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { id "java" - id "edu.wpi.first.GradleRIO" version "2020.1.1-beta-4" + id "edu.wpi.first.GradleRIO" version "2019.4.1" } def ROBOT_MAIN_CLASS = "frc4388.robot.Main" From ec873a2835137c85b59d1abcacf268736fb4fb87 Mon Sep 17 00:00:00 2001 From: "Keenan D. Buckley" Date: Sat, 21 Dec 2019 20:41:34 -0700 Subject: [PATCH 3/3] LED Command Based --- build.gradle | 2 +- .../robot/commands/Drive/GamerMove.java | 2 +- .../SetLEDPattern.java} | 6 +-- .../frc4388/robot/commands/LED/UpdateLED.java | 46 +++++++++++++++++++ .../java/frc4388/robot/subsystems/Drive.java | 6 +-- .../java/frc4388/robot/subsystems/LED.java | 12 ++++- 6 files changed, 62 insertions(+), 12 deletions(-) rename src/main/java/frc4388/robot/commands/{setLEDPattern.java => LED/SetLEDPattern.java} (91%) create mode 100644 src/main/java/frc4388/robot/commands/LED/UpdateLED.java diff --git a/build.gradle b/build.gradle index 590560a..9216a46 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { id "java" - id "edu.wpi.first.GradleRIO" version "2019.4.1" + id "edu.wpi.first.GradleRIO" version "2019.1.1" } def ROBOT_MAIN_CLASS = "frc4388.robot.Main" diff --git a/src/main/java/frc4388/robot/commands/Drive/GamerMove.java b/src/main/java/frc4388/robot/commands/Drive/GamerMove.java index fbc171c..b890aa5 100644 --- a/src/main/java/frc4388/robot/commands/Drive/GamerMove.java +++ b/src/main/java/frc4388/robot/commands/Drive/GamerMove.java @@ -25,7 +25,7 @@ public class GamerMove extends Command { // Called repeatedly when this Command is scheduled to run @Override protected void execute() { - Robot.m_Drive.gamerMove(); + Robot.m_Drive.driveWithInput(0, 1); } // Make this return true when this Command no longer needs to run execute() diff --git a/src/main/java/frc4388/robot/commands/setLEDPattern.java b/src/main/java/frc4388/robot/commands/LED/SetLEDPattern.java similarity index 91% rename from src/main/java/frc4388/robot/commands/setLEDPattern.java rename to src/main/java/frc4388/robot/commands/LED/SetLEDPattern.java index 83885cd..11c4447 100644 --- a/src/main/java/frc4388/robot/commands/setLEDPattern.java +++ b/src/main/java/frc4388/robot/commands/LED/SetLEDPattern.java @@ -5,18 +5,18 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -package frc4388.robot.commands; +package frc4388.robot.commands.LED; import frc4388.robot.Robot; import frc4388.robot.constants.LEDPatterns; import edu.wpi.first.wpilibj.command.Command; -public class setLEDPattern extends Command { +public class SetLEDPattern extends Command { public static LEDPatterns m_pattern; - public setLEDPattern(LEDPatterns pattern) { + public SetLEDPattern(LEDPatterns pattern) { requires(Robot.m_led); m_pattern = pattern; } diff --git a/src/main/java/frc4388/robot/commands/LED/UpdateLED.java b/src/main/java/frc4388/robot/commands/LED/UpdateLED.java new file mode 100644 index 0000000..3d6c374 --- /dev/null +++ b/src/main/java/frc4388/robot/commands/LED/UpdateLED.java @@ -0,0 +1,46 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2018 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.wpilibj.command.Command; +import frc4388.robot.Robot; + +public class UpdateLED extends Command { + public UpdateLED() { + // Use requires() here to declare subsystem dependencies + requires(Robot.m_led); + } + + // Called just before this Command runs the first time + @Override + protected void initialize() { + } + + // Called repeatedly when this Command is scheduled to run + @Override + protected void execute() { + Robot.m_led.updateLED(); + } + + // Make this return true when this Command no longer needs to run execute() + @Override + protected boolean isFinished() { + return false; + } + + // Called once after isFinished returns true + @Override + protected void end() { + } + + // Called when another command which requires one or more of the same + // subsystems is scheduled to run + @Override + protected void interrupted() { + } +} diff --git a/src/main/java/frc4388/robot/subsystems/Drive.java b/src/main/java/frc4388/robot/subsystems/Drive.java index 85cc1f1..8090480 100644 --- a/src/main/java/frc4388/robot/subsystems/Drive.java +++ b/src/main/java/frc4388/robot/subsystems/Drive.java @@ -67,12 +67,8 @@ public class Drive extends Subsystem { m_driveTrain.arcadeDrive(move, steer); } - public void gamerMove(){ - m_driveTrain.arcadeDrive(0, 1); - } - @Override - public void initDefaultCommand() { + public void initDefaultCommand(){ // Set the default command for a subsystem here. // setDefaultCommand(new MySpecialCommand()); setDefaultCommand(new DriveWithJoystick()); diff --git a/src/main/java/frc4388/robot/subsystems/LED.java b/src/main/java/frc4388/robot/subsystems/LED.java index 28fad8c..3c66878 100644 --- a/src/main/java/frc4388/robot/subsystems/LED.java +++ b/src/main/java/frc4388/robot/subsystems/LED.java @@ -8,6 +8,7 @@ package frc4388.robot.subsystems; import frc4388.robot.RobotMap; +import frc4388.robot.commands.LED.UpdateLED; import frc4388.robot.constants.LEDPatterns; import edu.wpi.first.wpilibj.Spark; import edu.wpi.first.wpilibj.command.Subsystem; @@ -27,9 +28,8 @@ import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; LEDController.set(currentLED); } - public void periodic() { + public void updateLED(){ LEDController.set(currentLED); - SmartDashboard.putNumber("LED", currentLED); } public void setPattern(LEDPatterns pattern){ @@ -37,7 +37,15 @@ import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; LEDController.set(currentLED); } + @Override + public void periodic(){ + SmartDashboard.putNumber("LED", currentLED); + } + @Override public void initDefaultCommand() { + // Set the default command for a subsystem here. + // setDefaultCommand(new MySpecialCommand()); + setDefaultCommand(new UpdateLED()); } } \ No newline at end of file