[wpilib] Add PS4Controller, remove Hand from GenericHID/XboxController (#3345)

- GenericHID is now concrete, and has only getRawAxis/Button(int) functionality
- getXxx() has been moved into Joystick as that's the only place where it makes sense
- Hand (and therefore getXxx(Hand)) has been removed, replaced by specific getLeft/RightXxx() methods in XboxController and the new PS4Controller class
- C++ ::Button:: and ::Axis:: enums have been converted to identically-namespaced static constexpr ints
This commit is contained in:
Starlight220
2021-08-14 20:00:46 +03:00
committed by GitHub
parent 25f6f478a5
commit 031962608b
82 changed files with 2548 additions and 934 deletions

View File

@@ -8,8 +8,14 @@ import edu.wpi.first.hal.HAL;
import java.util.HashMap;
import java.util.Map;
/** GenericHID Interface. */
public abstract class GenericHID {
/**
* Handle input from standard HID devices connected to the Driver Station.
*
* <p>This class handles standard input that comes from the Driver Station. Each time a value is
* requested the most recent value is returned. There is a single class instance for each device and
* the mapping of ports to hardware buttons depends on the code in the Driver Station.
*/
public class GenericHID {
/** Represents a rumble output on the JoyStick. */
public enum RumbleType {
kLeftRumble,
@@ -55,61 +61,20 @@ public abstract class GenericHID {
}
}
/** Which hand the Human Interface Device is associated with. */
public enum Hand {
kLeft(0),
kRight(1);
public final int value;
Hand(int value) {
this.value = value;
}
}
private final int m_port;
private int m_outputs;
private short m_leftRumble;
private short m_rightRumble;
/**
* Construct an instance of a device.
*
* @param port The port index on the Driver Station that the device is plugged into.
*/
public GenericHID(int port) {
m_port = port;
}
/**
* Get the x position of the HID.
*
* @return the x position of the HID
*/
public final double getX() {
return getX(Hand.kRight);
}
/**
* Get the x position of HID.
*
* @param hand which hand, left or right
* @return the x position
*/
public abstract double getX(Hand hand);
/**
* Get the y position of the HID.
*
* @return the y position
*/
public final double getY() {
return getY(Hand.kRight);
}
/**
* Get the y position of the HID.
*
* @param hand which hand, left or right
* @return the y position
*/
public abstract double getY(Hand hand);
/**
* Get the button value (starting at button 1).
*
@@ -170,13 +135,21 @@ public abstract class GenericHID {
* <p>The POV angles start at 0 in the up direction, and increase clockwise (eg right is 90,
* upper-left is 315).
*
* @param pov The index of the POV to read (starting at 0)
* @param pov The index of the POV to read (starting at 0). Defaults to 0.
* @return the angle of the POV in degrees, or -1 if the POV is not pressed.
*/
public int getPOV(int pov) {
return DriverStation.getStickPOV(m_port, pov);
}
/**
* Get the angle in degrees of the default POV (index 0) on the HID.
*
* <p>The POV angles start at 0 in the up direction, and increase clockwise (eg right is 90,
* upper-left is 315).
*
* @return the angle of the POV in degrees, or -1 if the POV is not pressed.
*/
public int getPOV() {
return getPOV(0);
}

View File

@@ -8,7 +8,7 @@ import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
/**
* Handle input from standard Joysticks connected to the Driver Station.
* Handle input from Flight Joysticks connected to the Driver Station.
*
* <p>This class handles standard input that comes from the Driver Station. Each time a value is
* requested the most recent value is returned. There is a single class instance for each joystick
@@ -48,49 +48,21 @@ public class Joystick extends GenericHID {
}
}
/** Represents a digital button on a joystick. */
private enum Button {
kTrigger(1),
kTop(2);
public final int value;
Button(int value) {
this.value = value;
}
}
/** Represents an analog axis on a joystick. */
private enum Axis {
kX(0),
kY(1),
kZ(2),
kTwist(3),
kThrottle(4),
kNumAxes(5);
public final int value;
Axis(int value) {
this.value = value;
}
}
private final byte[] m_axes = new byte[Axis.kNumAxes.value];
private final byte[] m_axes = new byte[AxisType.values().length];
/**
* Construct an instance of a joystick. The joystick index is the USB port on the drivers station.
* Construct an instance of a joystick.
*
* @param port The port on the Driver Station that the joystick is plugged into.
* @param port The port index on the Driver Station that the joystick is plugged into.
*/
public Joystick(final int port) {
super(port);
m_axes[Axis.kX.value] = kDefaultXChannel;
m_axes[Axis.kY.value] = kDefaultYChannel;
m_axes[Axis.kZ.value] = kDefaultZChannel;
m_axes[Axis.kTwist.value] = kDefaultTwistChannel;
m_axes[Axis.kThrottle.value] = kDefaultThrottleChannel;
m_axes[AxisType.kX.value] = kDefaultXChannel;
m_axes[AxisType.kY.value] = kDefaultYChannel;
m_axes[AxisType.kZ.value] = kDefaultZChannel;
m_axes[AxisType.kTwist.value] = kDefaultTwistChannel;
m_axes[AxisType.kThrottle.value] = kDefaultThrottleChannel;
HAL.report(tResourceType.kResourceType_Joystick, port + 1);
}
@@ -101,7 +73,7 @@ public class Joystick extends GenericHID {
* @param channel The channel to set the axis to.
*/
public void setXChannel(int channel) {
m_axes[Axis.kX.value] = (byte) channel;
m_axes[AxisType.kX.value] = (byte) channel;
}
/**
@@ -110,7 +82,7 @@ public class Joystick extends GenericHID {
* @param channel The channel to set the axis to.
*/
public void setYChannel(int channel) {
m_axes[Axis.kY.value] = (byte) channel;
m_axes[AxisType.kY.value] = (byte) channel;
}
/**
@@ -119,7 +91,7 @@ public class Joystick extends GenericHID {
* @param channel The channel to set the axis to.
*/
public void setZChannel(int channel) {
m_axes[Axis.kZ.value] = (byte) channel;
m_axes[AxisType.kZ.value] = (byte) channel;
}
/**
@@ -128,7 +100,7 @@ public class Joystick extends GenericHID {
* @param channel The channel to set the axis to.
*/
public void setThrottleChannel(int channel) {
m_axes[Axis.kThrottle.value] = (byte) channel;
m_axes[AxisType.kThrottle.value] = (byte) channel;
}
/**
@@ -137,7 +109,7 @@ public class Joystick extends GenericHID {
* @param channel The channel to set the axis to.
*/
public void setTwistChannel(int channel) {
m_axes[Axis.kTwist.value] = (byte) channel;
m_axes[AxisType.kTwist.value] = (byte) channel;
}
/**
@@ -146,7 +118,7 @@ public class Joystick extends GenericHID {
* @return The channel for the axis.
*/
public int getXChannel() {
return m_axes[Axis.kX.value];
return m_axes[AxisType.kX.value];
}
/**
@@ -155,7 +127,7 @@ public class Joystick extends GenericHID {
* @return The channel for the axis.
*/
public int getYChannel() {
return m_axes[Axis.kY.value];
return m_axes[AxisType.kY.value];
}
/**
@@ -164,7 +136,7 @@ public class Joystick extends GenericHID {
* @return The channel for the axis.
*/
public int getZChannel() {
return m_axes[Axis.kZ.value];
return m_axes[AxisType.kZ.value];
}
/**
@@ -173,7 +145,7 @@ public class Joystick extends GenericHID {
* @return The channel for the axis.
*/
public int getTwistChannel() {
return m_axes[Axis.kTwist.value];
return m_axes[AxisType.kTwist.value];
}
/**
@@ -182,31 +154,27 @@ public class Joystick extends GenericHID {
* @return The channel for the axis.
*/
public int getThrottleChannel() {
return m_axes[Axis.kThrottle.value];
return m_axes[AxisType.kThrottle.value];
}
/**
* Get the X value of the joystick. This depends on the mapping of the joystick connected to the
* current port.
*
* @param hand Unused
* @return The X value of the joystick.
*/
@Override
public final double getX(Hand hand) {
return getRawAxis(m_axes[Axis.kX.value]);
public final double getX() {
return getRawAxis(m_axes[AxisType.kX.value]);
}
/**
* Get the Y value of the joystick. This depends on the mapping of the joystick connected to the
* current port.
*
* @param hand Unused
* @return The Y value of the joystick.
*/
@Override
public final double getY(Hand hand) {
return getRawAxis(m_axes[Axis.kY.value]);
public final double getY() {
return getRawAxis(m_axes[AxisType.kY.value]);
}
/**
@@ -215,7 +183,7 @@ public class Joystick extends GenericHID {
* @return the z position
*/
public double getZ() {
return getRawAxis(m_axes[Axis.kZ.value]);
return getRawAxis(m_axes[AxisType.kZ.value]);
}
/**
@@ -225,7 +193,7 @@ public class Joystick extends GenericHID {
* @return The Twist value of the joystick.
*/
public double getTwist() {
return getRawAxis(m_axes[Axis.kTwist.value]);
return getRawAxis(m_axes[AxisType.kTwist.value]);
}
/**
@@ -235,7 +203,7 @@ public class Joystick extends GenericHID {
* @return The Throttle value of the joystick.
*/
public double getThrottle() {
return getRawAxis(m_axes[Axis.kThrottle.value]);
return getRawAxis(m_axes[AxisType.kThrottle.value]);
}
/**
@@ -244,7 +212,7 @@ public class Joystick extends GenericHID {
* @return The state of the trigger.
*/
public boolean getTrigger() {
return getRawButton(Button.kTrigger.value);
return getRawButton(ButtonType.kTrigger.value);
}
/**
@@ -253,7 +221,7 @@ public class Joystick extends GenericHID {
* @return Whether the button was pressed since the last check.
*/
public boolean getTriggerPressed() {
return getRawButtonPressed(Button.kTrigger.value);
return getRawButtonPressed(ButtonType.kTrigger.value);
}
/**
@@ -262,7 +230,7 @@ public class Joystick extends GenericHID {
* @return Whether the button was released since the last check.
*/
public boolean getTriggerReleased() {
return getRawButtonReleased(Button.kTrigger.value);
return getRawButtonReleased(ButtonType.kTrigger.value);
}
/**
@@ -271,7 +239,7 @@ public class Joystick extends GenericHID {
* @return The state of the top button.
*/
public boolean getTop() {
return getRawButton(Button.kTop.value);
return getRawButton(ButtonType.kTop.value);
}
/**
@@ -280,7 +248,7 @@ public class Joystick extends GenericHID {
* @return Whether the button was pressed since the last check.
*/
public boolean getTopPressed() {
return getRawButtonPressed(Button.kTop.value);
return getRawButtonPressed(ButtonType.kTop.value);
}
/**
@@ -289,7 +257,7 @@ public class Joystick extends GenericHID {
* @return Whether the button was released since the last check.
*/
public boolean getTopReleased() {
return getRawButtonReleased(Button.kTop.value);
return getRawButtonReleased(ButtonType.kTop.value);
}
/**

View File

@@ -0,0 +1,535 @@
// 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 edu.wpi.first.wpilibj;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
/**
* Handle input from PS4 controllers connected to the Driver Station.
*
* <p>This class handles PS4 input that comes from the Driver Station. Each time a value is
* requested the most recent value is returned. There is a single class instance for each controller
* and the mapping of ports to hardware buttons depends on the code in the Driver Station.
*/
public class PS4Controller extends GenericHID {
/**
* Construct an instance of a device.
*
* @param port The port index on the Driver Station that the device is plugged into.
*/
public PS4Controller(int port) {
super(port);
HAL.report(tResourceType.kResourceType_PS4Controller, port + 1);
}
/** Represents a digital button on a PS4Controller. */
public enum Button {
kSquare(1),
kCross(2),
kCircle(3),
kTriangle(4),
kL1(5),
kR1(6),
kL2(7),
kR2(8),
kShare(9),
kOptions(10),
kL3(11),
kR3(12),
kPS(13),
kTouchpad(14);
public final int value;
Button(int index) {
this.value = index;
}
/**
* Get the human-friendly name of the button, matching the relevant methods. This is done by
* stripping the leading `k`, and if not the touchpad append `Button`.
*
* <p>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 (this == kTouchpad) {
return name;
}
return name + "Button";
}
}
/** Represents an axis on a PS4Controller. */
public enum Axis {
kLeftX(0),
kLeftY(1),
kRightX(2),
kRightY(5),
kL2(3),
kR2(4);
public final int value;
Axis(int index) {
value = index;
}
/**
* Get the human-friendly name of the axis, matching the relevant methods. This is done by
* stripping the leading `k`, and if one of L2/R2 append `Axis`.
*
* <p>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("2")) {
return name + "Axis";
}
return name;
}
}
/**
* 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);
}
/**
* Get the L2 axis value of the controller. Note that this axis is bound to the range of [0, 1] as
* opposed to the usual [-1, 1].
*
* @return the axis value.
*/
public double getL2Axis() {
return getRawAxis(Axis.kL2.value);
}
/**
* Get the R2 axis value of the controller. Note that this axis is bound to the range of [0, 1] as
* opposed to the usual [-1, 1].
*
* @return the axis value.
*/
public double getR2Axis() {
return getRawAxis(Axis.kR2.value);
}
/**
* Read the value of the left trigger button on the controller.
*
* @return The state of the button.
*/
public boolean getL2Button() {
return getRawButton(Button.kL2.value);
}
/**
* Read the value of the right trigger button on the controller.
*
* @return The state of the button.
*/
public boolean getR2Button() {
return getRawButton(Button.kR2.value);
}
/**
* Whether the L2 button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getL2ButtonPressed() {
return getRawButtonPressed(Button.kL2.value);
}
/**
* Whether the R2 button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getR2ButtonPressed() {
return getRawButtonPressed(Button.kR2.value);
}
/**
* Whether the L2 button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getL2ButtonReleased() {
return getRawButtonReleased(Button.kL2.value);
}
/**
* Whether the R2 button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getR2ButtonReleased() {
return getRawButtonReleased(Button.kR2.value);
}
/**
* Read the value of the L1 button on the controller.
*
* @return The state of the button.
*/
public boolean getL1Button() {
return getRawButton(Button.kL1.value);
}
/**
* Read the value of the R1 button on the controller.
*
* @return The state of the button.
*/
public boolean getR1Button() {
return getRawButton(Button.kR1.value);
}
/**
* Whether the L1 button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getL1ButtonPressed() {
return getRawButtonPressed(Button.kL1.value);
}
/**
* Whether the R1 button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getR1ButtonPressed() {
return getRawButtonPressed(Button.kR1.value);
}
/**
* Whether the L1 button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getL1ButtonReleased() {
return getRawButtonReleased(Button.kL1.value);
}
/**
* Whether the R1 button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getR1ButtonReleased() {
return getRawButtonReleased(Button.kR1.value);
}
/**
* Read the value of the L3 button (pressing the left analog stick) on the controller.
*
* @return The state of the button.
*/
public boolean getL3Button() {
return getRawButton(Button.kL3.value);
}
/**
* Read the value of the R3 button (pressing the right analog stick) on the controller.
*
* @return The state of the button.
*/
public boolean getR3Button() {
return getRawButton(Button.kR3.value);
}
/**
* Whether the L3 (left stick) button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getL3ButtonPressed() {
return getRawButtonPressed(Button.kL3.value);
}
/**
* Whether the R3 (right stick) button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getR3ButtonPressed() {
return getRawButtonPressed(Button.kR3.value);
}
/**
* Whether the L3 (left stick) button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getL3ButtonReleased() {
return getRawButtonReleased(Button.kL3.value);
}
/**
* Whether the R3 (right stick) button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getR3ButtonReleased() {
return getRawButtonReleased(Button.kR3.value);
}
/**
* Read the value of the Square button on the controller.
*
* @return The state of the button.
*/
public boolean getSquareButton() {
return getRawButton(Button.kSquare.value);
}
/**
* Whether the Square button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getSquareButtonPressed() {
return getRawButtonPressed(Button.kSquare.value);
}
/**
* Whether the Square button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getSquareButtonReleased() {
return getRawButtonReleased(Button.kSquare.value);
}
/**
* Read the value of the Cross button on the controller.
*
* @return The state of the button.
*/
public boolean getCrossButton() {
return getRawButton(Button.kCross.value);
}
/**
* Whether the Cross button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getCrossButtonPressed() {
return getRawButtonPressed(Button.kCross.value);
}
/**
* Whether the Cross button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getCrossButtonReleased() {
return getRawButtonReleased(Button.kCross.value);
}
/**
* Read the value of the Triangle button on the controller.
*
* @return The state of the button.
*/
public boolean getTriangleButton() {
return getRawButton(Button.kTriangle.value);
}
/**
* Whether the Triangle button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getTriangleButtonPressed() {
return getRawButtonPressed(Button.kTriangle.value);
}
/**
* Whether the Triangle button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getTriangleButtonReleased() {
return getRawButtonReleased(Button.kTriangle.value);
}
/**
* Read the value of the Circle button on the controller.
*
* @return The state of the button.
*/
public boolean getCircleButton() {
return getRawButton(Button.kCircle.value);
}
/**
* Whether the Circle button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getCircleButtonPressed() {
return getRawButtonPressed(Button.kCircle.value);
}
/**
* Whether the Circle button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getCircleButtonReleased() {
return getRawButtonReleased(Button.kCircle.value);
}
/**
* Read the value of the share button on the controller.
*
* @return The state of the button.
*/
public boolean getShareButton() {
return getRawButton(Button.kShare.value);
}
/**
* Whether the share button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getShareButtonPressed() {
return getRawButtonPressed(Button.kShare.value);
}
/**
* Whether the share button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getShareButtonReleased() {
return getRawButtonReleased(Button.kShare.value);
}
/**
* Read the value of the PS button on the controller.
*
* @return The state of the button.
*/
public boolean getPSButton() {
return getRawButton(Button.kPS.value);
}
/**
* Whether the PS button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getPSButtonPressed() {
return getRawButtonPressed(Button.kPS.value);
}
/**
* Whether the PS button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getPSButtonReleased() {
return getRawButtonReleased(Button.kPS.value);
}
/**
* Read the value of the options button on the controller.
*
* @return The state of the button.
*/
public boolean getOptionsButton() {
return getRawButton(Button.kOptions.value);
}
/**
* Whether the options button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getOptionsButtonPressed() {
return getRawButtonPressed(Button.kOptions.value);
}
/**
* Whether the options button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getOptionsButtonReleased() {
return getRawButtonReleased(Button.kOptions.value);
}
/**
* Read the value of the touchpad on the controller.
*
* @return The state of the touchpad.
*/
public boolean getTouchpad() {
return getRawButton(Button.kTouchpad.value);
}
/**
* Whether the touchpad was pressed since the last check.
*
* @return Whether the touchpad was pressed since the last check.
*/
public boolean getTouchpadPressed() {
return getRawButtonPressed(Button.kTouchpad.value);
}
/**
* Whether the touchpad was released since the last check.
*
* @return Whether the touchpad was released since the last check.
*/
public boolean getTouchpadReleased() {
return getRawButtonReleased(Button.kTouchpad.value);
}
}

View File

@@ -17,10 +17,10 @@ import edu.wpi.first.hal.HAL;
public class XboxController extends GenericHID {
/** Represents a digital button on an XboxController. */
public enum Button {
kBumperLeft(5),
kBumperRight(6),
kStickLeft(9),
kStickRight(10),
kLeftBumper(5),
kRightBumper(6),
kLeftStick(9),
kRightStick(10),
kA(1),
kB(2),
kX(3),
@@ -34,6 +34,23 @@ public class XboxController extends GenericHID {
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`.
*
* <p>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. */
@@ -51,12 +68,29 @@ public class XboxController extends GenericHID {
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`.
*
* <p>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;
}
}
/**
* Construct an instance of a joystick. The joystick index is the USB port on the drivers station.
* Construct an instance of a controller.
*
* @param port The port on the Driver Station that the joystick is plugged into.
* @param port The port index on the Driver Station that the controller is plugged into.
*/
public XboxController(final int port) {
super(port);
@@ -65,131 +99,167 @@ public class XboxController extends GenericHID {
}
/**
* Get the X axis value of the controller.
* Get the X axis value of left side of the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The X axis value of the controller.
* @return The axis value.
*/
@Override
public double getX(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawAxis(Axis.kLeftX.value);
} else {
return getRawAxis(Axis.kRightX.value);
}
public double getLeftX() {
return getRawAxis(Axis.kLeftX.value);
}
/**
* Get the Y axis value of the controller.
* Get the X axis value of right side of the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The Y axis value of the controller.
* @return The axis value.
*/
@Override
public double getY(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawAxis(Axis.kLeftY.value);
} else {
return getRawAxis(Axis.kRightY.value);
}
public double getRightX() {
return getRawAxis(Axis.kRightX.value);
}
/**
* Get the trigger axis value of the controller.
* Get the Y axis value of left side of the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The trigger axis value of the controller.
* @return The axis value.
*/
public double getTriggerAxis(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawAxis(Axis.kLeftTrigger.value);
} else {
return getRawAxis(Axis.kRightTrigger.value);
}
public double getLeftY() {
return getRawAxis(Axis.kLeftY.value);
}
/**
* Read the value of the bumper button on the controller.
* Get the Y axis value of right side of the controller.
*
* @return The axis value.
*/
public double getRightY() {
return getRawAxis(Axis.kRightY.value);
}
/**
* Get the left trigger (LT) axis value of the controller. Note that this axis is bound to the
* range of [0, 1] as opposed to the usual [-1, 1].
*
* @return The axis value.
*/
public double getLeftTriggerAxis() {
return getRawAxis(Axis.kLeftTrigger.value);
}
/**
* Get the right trigger (RT) axis value of the controller. Note that this axis is bound to the
* range of [0, 1] as opposed to the usual [-1, 1].
*
* @return The axis value.
*/
public double getRightTriggerAxis() {
return getRawAxis(Axis.kRightTrigger.value);
}
/**
* Read the value of the left bumper (LB) button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
public boolean getBumper(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawButton(Button.kBumperLeft.value);
} else {
return getRawButton(Button.kBumperRight.value);
}
public boolean getLeftBumper() {
return getRawButton(Button.kLeftBumper.value);
}
/**
* Whether the bumper was pressed since the last check.
* Read the value of the right bumper (RB) button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return Whether the button was pressed since the last check.
*/
public boolean getBumperPressed(Hand hand) {
if (hand == Hand.kLeft) {
return getRawButtonPressed(Button.kBumperLeft.value);
} else {
return getRawButtonPressed(Button.kBumperRight.value);
}
}
/**
* Whether the bumper was released since the last check.
*
* @param hand Side of controller whose value should be returned.
* @return Whether the button was released since the last check.
*/
public boolean getBumperReleased(Hand hand) {
if (hand == Hand.kLeft) {
return getRawButtonReleased(Button.kBumperLeft.value);
} else {
return getRawButtonReleased(Button.kBumperRight.value);
}
}
/**
* Read the value of the stick button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
public boolean getStickButton(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawButton(Button.kStickLeft.value);
} else {
return getRawButton(Button.kStickRight.value);
}
public boolean getRightBumper() {
return getRawButton(Button.kRightBumper.value);
}
/**
* Whether the stick button was pressed since the last check.
* Whether the left bumper (LB) was pressed since the last check.
*
* @param hand Side of controller whose value should be returned.
* @return Whether the button was pressed since the last check.
*/
public boolean getStickButtonPressed(Hand hand) {
if (hand == Hand.kLeft) {
return getRawButtonPressed(Button.kStickLeft.value);
} else {
return getRawButtonPressed(Button.kStickRight.value);
}
public boolean getLeftBumperPressed() {
return getRawButtonPressed(Button.kLeftBumper.value);
}
/**
* Whether the stick button was released since the last check.
* Whether the right bumper (RB) was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getRightBumperPressed() {
return getRawButtonPressed(Button.kRightBumper.value);
}
/**
* Whether the left bumper (LB) was released since the last check.
*
* @param hand Side of controller whose value should be returned.
* @return Whether the button was released since the last check.
*/
public boolean getStickButtonReleased(Hand hand) {
if (hand == Hand.kLeft) {
return getRawButtonReleased(Button.kStickLeft.value);
} else {
return getRawButtonReleased(Button.kStickRight.value);
}
public boolean getLeftBumperReleased() {
return getRawButtonReleased(Button.kLeftBumper.value);
}
/**
* Whether the right bumper (RB) was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getRightBumperReleased() {
return getRawButtonReleased(Button.kRightBumper.value);
}
/**
* Read the value of the left stick button (LSB) on the controller.
*
* @return The state of the button.
*/
public boolean getLeftStickButton() {
return getRawButton(Button.kLeftStick.value);
}
/**
* Read the value of the right stick button (RSB) on the controller.
*
* @return The state of the button.
*/
public boolean getRightStickButton() {
return getRawButton(Button.kRightStick.value);
}
/**
* Whether the left stick button (LSB) was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getLeftStickButtonPressed() {
return getRawButtonPressed(Button.kLeftStick.value);
}
/**
* Whether the right stick button (RSB) was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getRightStickButtonPressed() {
return getRawButtonPressed(Button.kRightStick.value);
}
/**
* Whether the left stick button (LSB) was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getLeftStickButtonReleased() {
return getRawButtonReleased(Button.kLeftStick.value);
}
/**
* Whether the right stick (RSB) button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getRightStickButtonReleased() {
return getRawButtonReleased(Button.kRightStick.value);
}
/**

View File

@@ -0,0 +1,212 @@
// 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 edu.wpi.first.wpilibj.simulation;
import edu.wpi.first.wpilibj.PS4Controller;
/** Class to control a simulated PS4 controller. */
public class PS4ControllerSim extends GenericHIDSim {
/**
* Constructs from a PS4Controller object.
*
* @param joystick controller to simulate
*/
public PS4ControllerSim(PS4Controller joystick) {
super(joystick);
setAxisCount(6);
setButtonCount(14);
}
/**
* Constructs from a joystick port number.
*
* @param port port number
*/
public PS4ControllerSim(int port) {
super(port);
setAxisCount(6);
setButtonCount(14);
}
/**
* Change the X axis value of the controller's left stick.
*
* @param value the new value
*/
public void setLeftX(double value) {
setRawAxis(PS4Controller.Axis.kLeftX.value, value);
}
/**
* Change the X axis value of the controller's right stick.
*
* @param value the new value
*/
public void setRightX(double value) {
setRawAxis(PS4Controller.Axis.kRightX.value, value);
}
/**
* Change the Y axis value of the controller's left stick.
*
* @param value the new value
*/
public void setLeftY(double value) {
setRawAxis(PS4Controller.Axis.kLeftY.value, value);
}
/**
* Change the Y axis value of the controller's right stick.
*
* @param value the new value
*/
public void setRightY(double value) {
setRawAxis(PS4Controller.Axis.kRightY.value, value);
}
/**
* Change the L2 axis axis value of the controller.
*
* @param value the new value
*/
public void setL2Axis(double value) {
setRawAxis(PS4Controller.Axis.kL2.value, value);
}
/**
* Change the R2 axis value of the controller.
*
* @param value the new value
*/
public void setR2Axis(double value) {
setRawAxis(PS4Controller.Axis.kR2.value, value);
}
/**
* Change the value of the Square button on the controller.
*
* @param value the new value
*/
public void setSquareButton(boolean value) {
setRawButton(PS4Controller.Button.kSquare.value, value);
}
/**
* Change the value of the Cross button on the controller.
*
* @param value the new value
*/
public void setCrossButton(boolean value) {
setRawButton(PS4Controller.Button.kCross.value, value);
}
/**
* Change the value of the Circle button on the controller.
*
* @param value the new value
*/
public void setCircleButton(boolean value) {
setRawButton(PS4Controller.Button.kCircle.value, value);
}
/**
* Change the value of the Triangle button on the controller.
*
* @param value the new value
*/
public void setTriangleButton(boolean value) {
setRawButton(PS4Controller.Button.kTriangle.value, value);
}
/**
* Change the value of the L1 button on the controller.
*
* @param value the new value
*/
public void setL1Button(boolean value) {
setRawButton(PS4Controller.Button.kL1.value, value);
}
/**
* Change the value of the R1 button on the controller.
*
* @param value the new value
*/
public void setR1Button(boolean value) {
setRawButton(PS4Controller.Button.kR1.value, value);
}
/**
* Change the value of the L2 button on the controller.
*
* @param value the new value
*/
public void setL2Button(boolean value) {
setRawButton(PS4Controller.Button.kL2.value, value);
}
/**
* Change the value of the R2 button on the controller.
*
* @param value the new value
*/
public void setR2Button(boolean value) {
setRawButton(PS4Controller.Button.kR2.value, value);
}
/**
* Change the value of the Share button on the controller.
*
* @param value the new value
*/
public void setShareButton(boolean value) {
setRawButton(PS4Controller.Button.kShare.value, value);
}
/**
* Change the value of the Options button on the controller.
*
* @param value the new value
*/
public void setOptionsButton(boolean value) {
setRawButton(PS4Controller.Button.kOptions.value, value);
}
/**
* Change the value of the L3 (left stick) button on the controller.
*
* @param value the new value
*/
public void setL3Button(boolean value) {
setRawButton(PS4Controller.Button.kL3.value, value);
}
/**
* Change the value of the R3 (right stick) button on the controller.
*
* @param value the new value
*/
public void setR3Button(boolean value) {
setRawButton(PS4Controller.Button.kR3.value, value);
}
/**
* Change the value of the PS button on the controller.
*
* @param value the new value
*/
public void setPSButton(boolean value) {
setRawButton(PS4Controller.Button.kPS.value, value);
}
/**
* Change the value of the touchpad button on the controller.
*
* @param value the new value
*/
public void setTouchpad(boolean value) {
setRawButton(PS4Controller.Button.kTouchpad.value, value);
}
}

View File

@@ -4,7 +4,6 @@
package edu.wpi.first.wpilibj.simulation;
import edu.wpi.first.wpilibj.GenericHID;
import edu.wpi.first.wpilibj.XboxController;
/** Class to control a simulated Xbox 360 or Xbox One controller. */
@@ -32,73 +31,93 @@ public class XboxControllerSim extends GenericHIDSim {
}
/**
* Change the X value of the joystick.
* Change the left X value of the joystick.
*
* @param hand the joystick hand
* @param value the new value
*/
public void setX(GenericHID.Hand hand, double value) {
if (hand.equals(GenericHID.Hand.kLeft)) {
setRawAxis(XboxController.Axis.kLeftX.value, value);
} else {
setRawAxis(XboxController.Axis.kRightX.value, value);
}
public void setLeftX(double value) {
setRawAxis(XboxController.Axis.kLeftX.value, value);
}
/**
* Change the Y value of the joystick.
* Change the right X value of the joystick.
*
* @param hand the joystick hand
* @param value the new value
*/
public void setY(GenericHID.Hand hand, double value) {
if (hand.equals(GenericHID.Hand.kLeft)) {
setRawAxis(XboxController.Axis.kLeftY.value, value);
} else {
setRawAxis(XboxController.Axis.kRightY.value, value);
}
public void setRightX(double value) {
setRawAxis(XboxController.Axis.kRightX.value, value);
}
/**
* Change the value of a trigger axis on the joystick.
* Change the left Y value of the joystick.
*
* @param hand the joystick hand
* @param value the new value
*/
public void setTriggerAxis(GenericHID.Hand hand, double value) {
if (hand.equals(GenericHID.Hand.kLeft)) {
setRawAxis(XboxController.Axis.kLeftTrigger.value, value);
} else {
setRawAxis(XboxController.Axis.kRightTrigger.value, value);
}
public void setLeftY(double value) {
setRawAxis(XboxController.Axis.kLeftY.value, value);
}
/**
* Change the value of a bumper on the joystick.
* Change the right Y value of the joystick.
*
* @param value the new value
*/
public void setRightY(double value) {
setRawAxis(XboxController.Axis.kRightY.value, value);
}
/**
* Change the value of the left trigger axis on the joystick.
*
* @param value the new value
*/
public void setLeftTriggerAxis(double value) {
setRawAxis(XboxController.Axis.kLeftTrigger.value, value);
}
/**
* Change the value of the right trigger axis on the joystick.
*
* @param value the new value
*/
public void setRightTriggerAxis(double value) {
setRawAxis(XboxController.Axis.kRightTrigger.value, value);
}
/**
* Change the value of the left bumper on the joystick.
*
* @param hand the joystick hand
* @param state the new value
*/
public void setBumper(GenericHID.Hand hand, boolean state) {
if (hand.equals(GenericHID.Hand.kLeft)) {
setRawButton(XboxController.Button.kBumperLeft.value, state);
} else {
setRawButton(XboxController.Button.kBumperRight.value, state);
}
public void setLeftBumper(boolean state) {
setRawButton(XboxController.Button.kLeftBumper.value, state);
}
/**
* Change the value of a button on the joystick.
* Change the value of the right bumper on the joystick.
*
* @param hand the joystick hand
* @param state the new value
*/
public void setStickButton(GenericHID.Hand hand, boolean state) {
if (hand.equals(GenericHID.Hand.kLeft)) {
setRawButton(XboxController.Button.kStickLeft.value, state);
} else {
setRawButton(XboxController.Button.kStickRight.value, state);
}
public void setRightBumper(boolean state) {
setRawButton(XboxController.Button.kRightBumper.value, state);
}
/**
* Change the value of the left stick button on the joystick.
*
* @param state the new value
*/
public void setLeftStickButton(boolean state) {
setRawButton(XboxController.Button.kLeftStick.value, state);
}
/**
* Change the value of the right stick button on the joystick.
*
* @param state the new value
*/
public void setRightStickButton(boolean state) {
setRawButton(XboxController.Button.kRightStick.value, state);
}
/**