From a103b2d5c19e1e9f5cc841554d87d419aa9bf740 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 5 May 2021 01:34:02 -0700 Subject: [PATCH 1/4] 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 c15fa3e762b1fba67960eaa8d11174c69ac40e2c Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 5 May 2021 01:48:38 -0700 Subject: [PATCH 2/4] driveW/Joysticks --- src/main/java/frc/robot/Commands/DriveWithJoySticks.java | 4 ++-- src/main/java/frc/robot/RobotContainer.java | 4 ++-- src/main/java/frc/robot/Subsystems/Drivetrain.java | 2 +- 3 files changed, 5 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..725613d 100644 --- a/src/main/java/frc/robot/Commands/DriveWithJoySticks.java +++ b/src/main/java/frc/robot/Commands/DriveWithJoySticks.java @@ -1,7 +1,7 @@ -package frc.robot.commands; +package frc.robot.Commands; import edu.wpi.first.wpilibj2.command.CommandBase; -import frc.robot.subsystems.Drivetrain; +import frc.robot.Subsystems.Drivetrain; public class DriveWithJoysticks extends CommandBase{ private Drivetrain driveTrain; diff --git a/src/main/java/frc/robot/RobotContainer.java b/src/main/java/frc/robot/RobotContainer.java index 57639bd..d027fc7 100644 --- a/src/main/java/frc/robot/RobotContainer.java +++ b/src/main/java/frc/robot/RobotContainer.java @@ -8,8 +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; +import frc.robot.Commands.DriveWithJoysticks; +import frc.robot.Subsystems.Drivetrain; /** * This class is where the bulk of the robot should be declared. Since diff --git a/src/main/java/frc/robot/Subsystems/Drivetrain.java b/src/main/java/frc/robot/Subsystems/Drivetrain.java index 244a100..b6b220f 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; From 7a7ffde506bb5c22f1f61a8634f384593005d778 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 5 May 2021 01:58:49 -0700 Subject: [PATCH 3/4] Drivetrain --- src/main/java/frc/robot/Constants.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/frc/robot/Constants.java b/src/main/java/frc/robot/Constants.java index 0c0ef1e..038cc6d 100644 --- a/src/main/java/frc/robot/Constants.java +++ b/src/main/java/frc/robot/Constants.java @@ -1,3 +1,6 @@ +// 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 c07c9cd6a6deaf7dc5baf835a4ddef5ab3aba2d4 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 6 May 2021 14:08:52 -0700 Subject: [PATCH 4/4] hi --- .../frc/robot/Commands/DriveWithJoySticks.java | 5 +++-- src/main/java/frc/robot/Constants.java | 5 ++++- src/main/java/frc/robot/RobotContainer.java | 3 +++ src/main/java/frc/robot/Subsystems/Drivetrain.java | 14 ++++++++------ 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main/java/frc/robot/Commands/DriveWithJoySticks.java b/src/main/java/frc/robot/Commands/DriveWithJoySticks.java index 725613d..e4d579b 100644 --- a/src/main/java/frc/robot/Commands/DriveWithJoySticks.java +++ b/src/main/java/frc/robot/Commands/DriveWithJoySticks.java @@ -1,6 +1,8 @@ package frc.robot.Commands; import edu.wpi.first.wpilibj2.command.CommandBase; +import frc.robot.Constants; +import frc.robot.RobotContainer; import frc.robot.Subsystems.Drivetrain; public class DriveWithJoysticks extends CommandBase{ @@ -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){ diff --git a/src/main/java/frc/robot/Constants.java b/src/main/java/frc/robot/Constants.java index 038cc6d..ded601f 100644 --- a/src/main/java/frc/robot/Constants.java +++ b/src/main/java/frc/robot/Constants.java @@ -27,5 +27,8 @@ public final class Constants { 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; + public static final int RIGHT_JOYSTICK_CAN_ID = 1; + public static final int LEFT_JOYSTICK_CAN_ID = 1; + public static final int LEFT_JOYSTICK_AXIS = 1; + public static final int RIGHT_JOYSTICK_AXIS = 5; } diff --git a/src/main/java/frc/robot/RobotContainer.java b/src/main/java/frc/robot/RobotContainer.java index d027fc7..c6e7fee 100644 --- a/src/main/java/frc/robot/RobotContainer.java +++ b/src/main/java/frc/robot/RobotContainer.java @@ -6,6 +6,7 @@ import java.util.Set; +import edu.wpi.first.wpilibj.Joystick; import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.Subsystem; import frc.robot.Commands.DriveWithJoysticks; @@ -24,6 +25,8 @@ public class RobotContainer { /** * The container for the robot. Contains subsystems, OI devices, and commands. */ + public static Joystick rightJoystick = new Joystick(Constants.RIGHT_JOYSTICK_CAN_ID); + public static Joystick leftJoystick = new Joystick(Constants.LEFT_JOYSTICK_CAN_ID); private Drivetrain driveTrain; private DriveWithJoysticks driveWithJoysticks; diff --git a/src/main/java/frc/robot/Subsystems/Drivetrain.java b/src/main/java/frc/robot/Subsystems/Drivetrain.java index b6b220f..8cad343 100644 --- a/src/main/java/frc/robot/Subsystems/Drivetrain.java +++ b/src/main/java/frc/robot/Subsystems/Drivetrain.java @@ -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); }