From 6ab017be03a86792c082b8c8feb61f53b108d330 Mon Sep 17 00:00:00 2001 From: aarav18 Date: Mon, 6 Feb 2023 19:06:56 -0700 Subject: [PATCH] change (this is the good branch) --- src/main/java/frc4388/robot/Constants.java | 2 + .../controller/DeadbandedNunchuks.java | 29 ++++ .../frc4388/utility/controller/Nunchuks.java | 146 ++++++++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 src/main/java/frc4388/utility/controller/DeadbandedNunchuks.java create mode 100644 src/main/java/frc4388/utility/controller/Nunchuks.java diff --git a/src/main/java/frc4388/robot/Constants.java b/src/main/java/frc4388/robot/Constants.java index 1b94ed1..6569456 100644 --- a/src/main/java/frc4388/robot/Constants.java +++ b/src/main/java/frc4388/robot/Constants.java @@ -137,6 +137,8 @@ public final class Constants { public static final class OIConstants { public static final int XBOX_DRIVER_ID = 0; public static final int XBOX_OPERATOR_ID = 1; + + public static final int NUNCHUKS_ID = 2; public static final double LEFT_AXIS_DEADBAND = 0.1; public static final double RIGHT_AXIS_DEADBAND = 0.6; diff --git a/src/main/java/frc4388/utility/controller/DeadbandedNunchuks.java b/src/main/java/frc4388/utility/controller/DeadbandedNunchuks.java new file mode 100644 index 0000000..6b3a789 --- /dev/null +++ b/src/main/java/frc4388/utility/controller/DeadbandedNunchuks.java @@ -0,0 +1,29 @@ +// 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 frc4388.utility.controller; + +import edu.wpi.first.math.geometry.Translation2d; +import frc4388.robot.Constants.OIConstants; + +/** Add your docs here. */ +public class DeadbandedNunchuks extends Nunchuks { + public DeadbandedNunchuks(int port) { super(port); } + + @Override public double getLeftX() { return getLeft().getX(); } + @Override public double getLeftY() { return getLeft().getY(); } + @Override public double getRightX() { return getRight().getX(); } + @Override public double getRightY() { return getRight().getY(); } + + public Translation2d getLeft() { return skewToDeadzonedCircle(super.getLeftX(), super.getLeftY()); } + public Translation2d getRight() { return skewToDeadzonedCircle(-super.getRightX(), super.getRightY()); } + + public static Translation2d skewToDeadzonedCircle(double x, double y) { + Translation2d translation2d = new Translation2d(x, y); + double magnitude = translation2d.getNorm(); + if (OIConstants.SKEW_STICKS && magnitude >= 1) return translation2d.div(magnitude); + if (magnitude < OIConstants.LEFT_AXIS_DEADBAND) return new Translation2d(0,0); + return translation2d; + } +} diff --git a/src/main/java/frc4388/utility/controller/Nunchuks.java b/src/main/java/frc4388/utility/controller/Nunchuks.java new file mode 100644 index 0000000..25d95c0 --- /dev/null +++ b/src/main/java/frc4388/utility/controller/Nunchuks.java @@ -0,0 +1,146 @@ +// 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 frc4388.utility.controller; + +import edu.wpi.first.hal.HAL; +import edu.wpi.first.hal.FRCNetComm.tResourceType; +import edu.wpi.first.wpilibj.GenericHID; +import edu.wpi.first.wpilibj.Joystick; + +/** Add your docs here. */ +public class Nunchuks extends GenericHID { + + public enum Button { + kLeftZ(2); + + public final int value; + + Button(int value) { + this.value = value; + } + + /** + * Get the human-friendly name of the button, matching the relevant methods. This is done by + * stripping the leading `k`, and if not a Bumper button append `Button`. + * + *

Primarily used for automated unit tests. + * + * @return the human-friendly name of the button. + */ + @Override + public String toString() { + var name = this.name().substring(1); // Remove leading `k` + if (name.endsWith("Bumper")) { + return name; + } + return name + "Button"; + } + } + + /** Represents an axis on an XboxController. */ + public enum Axis { + kLeftX(0), + kLeftY(1), + kRightX(3), + kRightY(4); + + public final int value; + + Axis(int value) { + this.value = value; + } + + /** + * Get the human-friendly name of the axis, matching the relevant methods. This is done by + * stripping the leading `k`, and if a trigger axis append `Axis`. + * + *

Primarily used for automated unit tests. + * + * @return the human-friendly name of the axis. + */ + @Override + public String toString() { + var name = this.name().substring(1); // Remove leading `k` + if (name.endsWith("Trigger")) { + return name + "Axis"; + } + return name; + } + } + + // public static final int LEFT_X_AXIS = 0; + // public static final int LEFT_Y_AXIS = 1; + // public static final int RIGHT_Y_AXIS = 2; + // public static final int RIGHT_X_AXIS = 3; + + // public static final int LEFT_Z_BUTTON = 2; + + // private static final double DEADZONE = 0.1; + + // private Joystick stick; + + /** + * Construct an instance of a controller. + * + * @param port The port index on the Driver Station that the controller is plugged into. + */ + public Nunchuks(final int port) { + super(port); + + HAL.report(tResourceType.kResourceType_XboxController, port + 1); + } + + // /** + // * Add your docs here. + // */ + // public Nunchuks(int portNumber){ + // stick = new Joystick(portNumber); + // } + + /** + * Get the X axis value of left side of the controller. + * + * @return The axis value. + */ + public double getLeftX() { + return getRawAxis(Axis.kLeftX.value); + } + + /** + * Get the X axis value of right side of the controller. + * + * @return The axis value. + */ + public double getRightX() { + return getRawAxis(Axis.kRightX.value); + } + + /** + * Get the Y axis value of left side of the controller. + * + * @return The axis value. + */ + public double getLeftY() { + return getRawAxis(Axis.kLeftY.value); + } + + /** + * Get the Y axis value of right side of the controller. + * + * @return The axis value. + */ + public double getRightY() { + return getRawAxis(Axis.kRightY.value); + } + + /** + * Read the value of the B button on the controller. + * + * @return The state of the button. + */ + public boolean getLeftZButton() { + return getRawButton(Button.kLeftZ.value); + } +}