From a103b2d5c19e1e9f5cc841554d87d419aa9bf740 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 5 May 2021 01:34:02 -0700 Subject: [PATCH 1/5] DriveW/Joysticks Command --- .../robot/Commands/DriveWithJoySticks.java | 30 ++++++ src/main/java/frc/robot/Constants.java | 23 ++++- src/main/java/frc/robot/RobotContainer.java | 11 +++ .../java/frc/robot/Subsystems/Drivetrain.java | 91 +++++++++++++++++++ 4 files changed, 150 insertions(+), 5 deletions(-) create mode 100644 src/main/java/frc/robot/Commands/DriveWithJoySticks.java create mode 100644 src/main/java/frc/robot/Subsystems/Drivetrain.java diff --git a/src/main/java/frc/robot/Commands/DriveWithJoySticks.java b/src/main/java/frc/robot/Commands/DriveWithJoySticks.java new file mode 100644 index 0000000..de52c5f --- /dev/null +++ b/src/main/java/frc/robot/Commands/DriveWithJoySticks.java @@ -0,0 +1,30 @@ +package frc.robot.commands; + +import edu.wpi.first.wpilibj2.command.CommandBase; +import frc.robot.subsystems.Drivetrain; + +public class DriveWithJoysticks extends CommandBase{ + private Drivetrain driveTrain; + + public DriveWithJoysticks(Drivetrain driveTrain){ + this.driveTrain = driveTrain; + addRequirements(driveTrain); + } + @Override + public void initialize(){ + + } + @Override + public void execute(){ + driveTrain.driveWithJoysticks(); + + } + @Override + public void end(boolean interrupted){ + driveTrain.stop(); + } + @Override + public boolean isFinished(){ + return false; + } +} \ No newline at end of file diff --git a/src/main/java/frc/robot/Constants.java b/src/main/java/frc/robot/Constants.java index 033c72d..0c0ef1e 100644 --- a/src/main/java/frc/robot/Constants.java +++ b/src/main/java/frc/robot/Constants.java @@ -1,9 +1,8 @@ -// 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; +import edu.wpi.first.wpilibj.GenericHID; +import edu.wpi.first.wpilibj.GenericHID.Hand; + /** * 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 declared @@ -12,4 +11,18 @@ *

It is advised to statically import this class (or one of its inner classes) wherever the * constants are needed, to reduce verbosity. */ -public final class Constants {} +public final class Constants { + //Make global variables all uppercase + public static final int LEFT_FRONT_MOTOR_CAN_ID = 0; + public static final int RIGHT_FRONT_MOTOR_CAN_ID = 1; + public static final int LEFT_REAR_MOTOR_CAN_ID = 2; + public static final int RIGHT_REAR_MOTOR_CAN_ID = 3; + + public static final GenericHID.Hand LEFT_JOY_AXIS = Hand.kLeft; + public static final GenericHID.Hand RIGHT_JOY_AXIS = Hand.kRight; + + public static final int GYRO_ID = 0; + public static final int DISTANCE_SENSORE_PING_CHANNEL = 0; + public static final int DISTANCE_SENSORE_ECHO_CHANNEL = 1; + public static final int JOYSTICK_CAN_ID = 1; +} diff --git a/src/main/java/frc/robot/RobotContainer.java b/src/main/java/frc/robot/RobotContainer.java index 5133735..57639bd 100644 --- a/src/main/java/frc/robot/RobotContainer.java +++ b/src/main/java/frc/robot/RobotContainer.java @@ -8,6 +8,8 @@ import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.Subsystem; +import frc.robot.commands.DriveWithJoysticks; +import frc.robot.subsystems.Drivetrain; /** * This class is where the bulk of the robot should be declared. Since @@ -22,9 +24,17 @@ public class RobotContainer { /** * The container for the robot. Contains subsystems, OI devices, and commands. */ + private Drivetrain driveTrain; + private DriveWithJoysticks driveWithJoysticks; + public RobotContainer() { // Configure the button bindings + + driveTrain = new Drivetrain(); + driveWithJoysticks = new DriveWithJoysticks(driveTrain); + driveTrain.setDefaultCommand(driveWithJoysticks); configureButtonBindings(); + } /** @@ -34,6 +44,7 @@ public RobotContainer() { * passing it to a {@link edu.wpi.first.wpilibj2.command.button.JoystickButton}. */ private void configureButtonBindings() { + } /** diff --git a/src/main/java/frc/robot/Subsystems/Drivetrain.java b/src/main/java/frc/robot/Subsystems/Drivetrain.java new file mode 100644 index 0000000..244a100 --- /dev/null +++ b/src/main/java/frc/robot/Subsystems/Drivetrain.java @@ -0,0 +1,91 @@ +package frc.robot.subsystems; + +import edu.wpi.first.wpilibj.AnalogGyro; +import edu.wpi.first.wpilibj.SpeedControllerGroup; +import edu.wpi.first.wpilibj.Ultrasonic; +import edu.wpi.first.wpilibj.VictorSP; +import edu.wpi.first.wpilibj.drive.DifferentialDrive; +import edu.wpi.first.wpilibj2.command.SubsystemBase; +import frc.robot.Constants; +import edu.wpi.first.wpilibj.Joystick; + +public class Drivetrain extends SubsystemBase { + private VictorSP leftFront, rightFront, leftRear, rightRear; + private SpeedControllerGroup leftMotors, rightMotors; + private DifferentialDrive drive; + private AnalogGyro gyro; + private Ultrasonic distanceSensor; + private Joystick joystick; + + public Drivetrain(){ + leftFront = new VictorSP(Constants.LEFT_FRONT_MOTOR_CAN_ID); + rightFront = new VictorSP(Constants.RIGHT_REAR_MOTOR_CAN_ID); + leftRear = new VictorSP(Constants.LEFT_REAR_MOTOR_CAN_ID); + rightRear = new VictorSP(Constants.LEFT_REAR_MOTOR_CAN_ID); + + leftMotors = new SpeedControllerGroup(leftFront,leftRear); + rightMotors = new SpeedControllerGroup(rightFront,rightRear); + drive = new DifferentialDrive (leftMotors,rightMotors); + + leftMotors.setInverted(true); + rightMotors.setInverted(false); + + gyro = new AnalogGyro(Constants.GYRO_ID); + distanceSensor = new Ultrasonic(Constants.DISTANCE_SENSORE_PING_CHANNEL, Constants.DISTANCE_SENSORE_ECHO_CHANNEL); + + joystick = new Joystick(Constants.JOYSTICK_CAN_ID); + + } + @Override + public void periodic() { + } + //Driving + public void driveWithJoysticks(){ + drive.tankDrive(joystick.getX(Constants.LEFT_JOY_AXIS),joystick.getY(Constants.RIGHT_JOY_AXIS)); + } + + public void stop(){ + drive.stopMotor(); + } + + //Gyro Sensor + public void initializeGyro(){ + gyro.initGyro(); + } + + public double getGyroAngle(){ + return gyro.getAngle(); + } + + public double getGyroRate(){ + return gyro.getRate(); + } + + public double getGyroCenter(){ + return gyro.getCenter(); + } + + public double getGyroOffset(){ + return gyro.getOffset(); + } + public void stopGyro(){ + gyro.reset(); + } + + //Distance Sensor + public void enableDistanceSensor(boolean enable){ + distanceSensor.setEnabled(enable); + } + + public void setDistanceUnits(Ultrasonic.Unit units){ + distanceSensor.setDistanceUnits(units); + } + + public Ultrasonic.Unit getDistanceUnit(){ + return distanceSensor.getDistanceUnits(); + } + + public void stopDistanceSensor(){ + distanceSensor.close(); + } +} \ No newline at end of file From 50d46e622267bcbf16ac7593bbb54482a40de9d7 Mon Sep 17 00:00:00 2001 From: Justin-pyth <73043715+Justin-pyth@users.noreply.github.com> Date: Wed, 5 May 2021 02:09:07 -0700 Subject: [PATCH 2/5] Update Constants.java --- src/main/java/frc/robot/Constants.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/frc/robot/Constants.java b/src/main/java/frc/robot/Constants.java index 0c0ef1e..656123b 100644 --- a/src/main/java/frc/robot/Constants.java +++ b/src/main/java/frc/robot/Constants.java @@ -1,3 +1,7 @@ +// 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; import edu.wpi.first.wpilibj.GenericHID; From 3a57230de005ea9223ecd4310d06fa409be6772c Mon Sep 17 00:00:00 2001 From: Justin-pyth <73043715+Justin-pyth@users.noreply.github.com> Date: Thu, 6 May 2021 14:25:39 -0700 Subject: [PATCH 3/5] Update Drivetrain.java --- .../java/frc/robot/Subsystems/Drivetrain.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/frc/robot/Subsystems/Drivetrain.java b/src/main/java/frc/robot/Subsystems/Drivetrain.java index 244a100..a3f89ad 100644 --- a/src/main/java/frc/robot/Subsystems/Drivetrain.java +++ b/src/main/java/frc/robot/Subsystems/Drivetrain.java @@ -1,4 +1,4 @@ -package frc.robot.subsystems; +package frc.robot.Subsystems; import edu.wpi.first.wpilibj.AnalogGyro; import edu.wpi.first.wpilibj.SpeedControllerGroup; @@ -7,7 +7,7 @@ import edu.wpi.first.wpilibj.drive.DifferentialDrive; import edu.wpi.first.wpilibj2.command.SubsystemBase; import frc.robot.Constants; -import edu.wpi.first.wpilibj.Joystick; + public class Drivetrain extends SubsystemBase { private VictorSP leftFront, rightFront, leftRear, rightRear; @@ -15,7 +15,6 @@ public class Drivetrain extends SubsystemBase { private DifferentialDrive drive; private AnalogGyro gyro; private Ultrasonic distanceSensor; - private Joystick joystick; public Drivetrain(){ leftFront = new VictorSP(Constants.LEFT_FRONT_MOTOR_CAN_ID); @@ -32,16 +31,15 @@ public Drivetrain(){ gyro = new AnalogGyro(Constants.GYRO_ID); distanceSensor = new Ultrasonic(Constants.DISTANCE_SENSORE_PING_CHANNEL, Constants.DISTANCE_SENSORE_ECHO_CHANNEL); - - joystick = new Joystick(Constants.JOYSTICK_CAN_ID); + Ultrasonic.setAutomaticMode(true); } @Override public void periodic() { } //Driving - public void driveWithJoysticks(){ - drive.tankDrive(joystick.getX(Constants.LEFT_JOY_AXIS),joystick.getY(Constants.RIGHT_JOY_AXIS)); + public void driveWithJoysticks(double leftSpeed, double rightSpeed){ + drive.tankDrive(leftSpeed,rightSpeed); } public void stop(){ @@ -73,6 +71,10 @@ public void stopGyro(){ } //Distance Sensor + public double getRangeInches(){ + return distanceSensor.getRangeInches(); + } + public void enableDistanceSensor(boolean enable){ distanceSensor.setEnabled(enable); } @@ -88,4 +90,4 @@ public Ultrasonic.Unit getDistanceUnit(){ public void stopDistanceSensor(){ distanceSensor.close(); } -} \ No newline at end of file +} From af1687d68a73e11a647328c1ec5c77be675b371a Mon Sep 17 00:00:00 2001 From: Justin-pyth <73043715+Justin-pyth@users.noreply.github.com> Date: Thu, 6 May 2021 14:25:55 -0700 Subject: [PATCH 4/5] Create Drivetrain.java From d0997625e1ffa5d128cdcad38ec01bbc9f39f89f Mon Sep 17 00:00:00 2001 From: Justin-pyth <73043715+Justin-pyth@users.noreply.github.com> Date: Thu, 6 May 2021 14:26:23 -0700 Subject: [PATCH 5/5] Update DriveWithJoySticks.java --- .../java/frc/robot/Commands/DriveWithJoySticks.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/frc/robot/Commands/DriveWithJoySticks.java b/src/main/java/frc/robot/Commands/DriveWithJoySticks.java index de52c5f..6cdaa4f 100644 --- a/src/main/java/frc/robot/Commands/DriveWithJoySticks.java +++ b/src/main/java/frc/robot/Commands/DriveWithJoySticks.java @@ -1,7 +1,9 @@ -package frc.robot.commands; +package frc.robot.Commands; import edu.wpi.first.wpilibj2.command.CommandBase; -import frc.robot.subsystems.Drivetrain; +import frc.robot.Constants; +import frc.robot.RobotContainer; +import frc.robot.Subsystems.Drivetrain; public class DriveWithJoysticks extends CommandBase{ private Drivetrain driveTrain; @@ -16,8 +18,7 @@ public void initialize(){ } @Override public void execute(){ - driveTrain.driveWithJoysticks(); - + driveTrain.driveWithJoysticks(RobotContainer.leftJoystick.getRawAxis(Constants.LEFT_JOYSTICK_AXIS),RobotContainer.rightJoystick.getRawAxis(Constants.RIGHT_JOYSTICK_AXIS)); } @Override public void end(boolean interrupted){ @@ -27,4 +28,4 @@ public void end(boolean interrupted){ public boolean isFinished(){ return false; } -} \ No newline at end of file +}