From cb904002ad873f83d248ca01a1882700588a1628 Mon Sep 17 00:00:00 2001 From: "Keenan D. Buckley" Date: Fri, 20 Dec 2019 12:53:02 -0700 Subject: [PATCH] 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()); } }