Added XboxController class (#140)

Joystick and Gamepad functionality was separated into cleaner interfaces.
This commit is contained in:
Tyler Veness
2016-11-18 23:05:37 -08:00
committed by Peter Johnson
parent 8c93ceb728
commit 140c365e4b
22 changed files with 1499 additions and 646 deletions

View File

@@ -1,122 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2016. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <stdint.h>
#include <memory>
#include <string>
#include <vector>
#include "ErrorBase.h"
#include "GenericHID.h"
namespace frc {
class DriverStation;
/**
* Handle input from standard Joysticks connected to the Driver Station.
* 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 and the mapping of ports to hardware
* buttons depends on the code in the driver station.
*/
class Joystick : public GenericHID, public ErrorBase {
public:
static const int kDefaultXAxis = 0;
static const int kDefaultYAxis = 1;
static const int kDefaultZAxis = 2;
static const int kDefaultTwistAxis = 2;
static const int kDefaultThrottleAxis = 3;
typedef enum {
kXAxis,
kYAxis,
kZAxis,
kTwistAxis,
kThrottleAxis,
kNumAxisTypes
} AxisType;
static const int kDefaultTriggerButton = 1;
static const int kDefaultTopButton = 2;
typedef enum { kTriggerButton, kTopButton, kNumButtonTypes } ButtonType;
typedef enum { kLeftRumble, kRightRumble } RumbleType;
typedef enum {
kUnknown = -1,
kXInputUnknown = 0,
kXInputGamepad = 1,
kXInputWheel = 2,
kXInputArcadeStick = 3,
kXInputFlightStick = 4,
kXInputDancePad = 5,
kXInputGuitar = 6,
kXInputGuitar2 = 7,
kXInputDrumKit = 8,
kXInputGuitar3 = 11,
kXInputArcadePad = 19,
kHIDJoystick = 20,
kHIDGamepad = 21,
kHIDDriving = 22,
kHIDFlight = 23,
kHID1stPerson = 24
} HIDType;
explicit Joystick(int port);
Joystick(int port, int numAxisTypes, int numButtonTypes);
virtual ~Joystick() = default;
Joystick(const Joystick&) = delete;
Joystick& operator=(const Joystick&) = delete;
int GetAxisChannel(AxisType axis) const;
void SetAxisChannel(AxisType axis, int channel);
float GetX(JoystickHand hand = kRightHand) const override;
float GetY(JoystickHand hand = kRightHand) const override;
float GetZ() const override;
float GetTwist() const override;
float GetThrottle() const override;
virtual float GetAxis(AxisType axis) const;
float GetRawAxis(int axis) const override;
bool GetTrigger(JoystickHand hand = kRightHand) const override;
bool GetTop(JoystickHand hand = kRightHand) const override;
bool GetBumper(JoystickHand hand = kRightHand) const override;
bool GetRawButton(int button) const override;
int GetPOV(int pov = 0) const override;
bool GetButton(ButtonType button) const;
static Joystick* GetStickForPort(int port);
virtual float GetMagnitude() const;
virtual float GetDirectionRadians() const;
virtual float GetDirectionDegrees() const;
bool GetIsXbox() const;
Joystick::HIDType GetType() const;
std::string GetName() const;
int GetPort() const;
int GetAxisType(int axis) const;
int GetAxisCount() const;
int GetButtonCount() const;
int GetPOVCount() const;
void SetRumble(RumbleType type, float value);
void SetOutput(int outputNumber, bool value);
void SetOutputs(int value);
private:
DriverStation& m_ds;
int m_port;
std::vector<int> m_axes;
std::vector<int> m_buttons;
int m_outputs = 0;
uint16_t m_leftRumble = 0;
uint16_t m_rightRumble = 0;
};
} // namespace frc

View File

@@ -82,6 +82,7 @@
#include "Victor.h"
#include "VictorSP.h"
#include "WPIErrors.h"
#include "XboxController.h"
#include "interfaces/Accelerometer.h"
#include "interfaces/Gyro.h"
#include "interfaces/Potentiometer.h"

View File

@@ -0,0 +1,129 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. 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. */
/*----------------------------------------------------------------------------*/
#include "GenericHID.h"
#include "DriverStation.h"
#include "HAL/HAL.h"
using namespace frc;
GenericHID::GenericHID(int port) : m_ds(DriverStation::GetInstance()) {
m_port = port;
}
/**
* Get the value of the axis.
*
* @param axis The axis to read, starting at 0.
* @return The value of the axis.
*/
float GenericHID::GetRawAxis(int axis) const {
return m_ds.GetStickAxis(m_port, axis);
}
/**
* Get the button value (starting at button 1)
*
* The buttons are returned in a single 16 bit value with one bit representing
* the state of each button. The appropriate button is returned as a boolean
* value.
*
* @param button The button number to be read (starting at 1)
* @return The state of the button.
**/
bool GenericHID::GetRawButton(int button) const {
return m_ds.GetStickButton(m_port, button);
}
/**
* Get the angle in degrees of a POV on the HID.
*
* The POV angles start at 0 in the up direction, and increase clockwise
* (e.g. right is 90, upper-left is 315).
*
* @param pov The index of the POV to read (starting at 0)
* @return the angle of the POV in degrees, or -1 if the POV is not pressed.
*/
int GenericHID::GetPOV(int pov) const {
return m_ds.GetStickPOV(GetPort(), pov);
}
/**
* Get the number of POVs for the HID.
*
* @return the number of POVs for the current HID
*/
int GenericHID::GetPOVCount() const { return m_ds.GetStickPOVCount(GetPort()); }
/**
* Get the port number of the HID.
*
* @return The port number of the HID.
*/
int GenericHID::GetPort() const { return m_port; }
/**
* Get the type of the HID.
*
* @return the type of the HID.
*/
GenericHID::HIDType GenericHID::GetType() const {
return static_cast<HIDType>(m_ds.GetJoystickType(m_port));
}
/**
* Get the name of the HID.
*
* @return the name of the HID.
*/
std::string GenericHID::GetName() const { return m_ds.GetJoystickName(m_port); }
/**
* Set a single HID output value for the HID.
*
* @param outputNumber The index of the output to set (1-32)
* @param value The value to set the output to
*/
void GenericHID::SetOutput(int outputNumber, bool value) {
m_outputs =
(m_outputs & ~(1 << (outputNumber - 1))) | (value << (outputNumber - 1));
HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
}
/**
* Set all output values for the HID.
*
* @param value The 32 bit output value (1 bit for each output)
*/
void GenericHID::SetOutputs(int value) {
m_outputs = value;
HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
}
/**
* Set the rumble output for the HID.
*
* The DS currently supports 2 rumble values, left rumble and right rumble.
*
* @param type Which rumble value to set
* @param value The normalized value (0 to 1) to set the rumble to
*/
void GenericHID::SetRumble(RumbleType type, double value) {
if (value < 0)
value = 0;
else if (value > 1)
value = 1;
if (type == kLeftRumble) {
m_leftRumble = value * 65535;
} else {
m_rightRumble = value * 65535;
}
HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
}

View File

@@ -28,9 +28,9 @@ static bool joySticksInitialized = false;
/**
* Construct an instance of a joystick.
*
* The joystick index is the usb port on the drivers station.
* The joystick index is the USB port on the Driver Station.
*
* @param port The port on the driver station that the joystick is plugged into
* @param port The port on the Driver Station that the joystick is plugged into
* (0-5).
*/
Joystick::Joystick(int port) : Joystick(port, kNumAxisTypes, kNumButtonTypes) {
@@ -52,24 +52,24 @@ Joystick::Joystick(int port) : Joystick(port, kNumAxisTypes, kNumButtonTypes) {
* This constructor allows the subclass to configure the number of constants
* for axes and buttons.
*
* @param port The port on the driver station that the joystick is
* @param port The port on the Driver Station that the joystick is
* plugged into.
* @param numAxisTypes The number of axis types in the enum.
* @param numButtonTypes The number of button types in the enum.
*/
Joystick::Joystick(int port, int numAxisTypes, int numButtonTypes)
: m_ds(DriverStation::GetInstance()),
m_port(port),
: JoystickBase(port),
m_ds(DriverStation::GetInstance()),
m_axes(numAxisTypes),
m_buttons(numButtonTypes) {
if (!joySticksInitialized) {
for (auto& joystick : joysticks) joystick = nullptr;
joySticksInitialized = true;
}
if (m_port >= DriverStation::kJoystickPorts) {
if (GetPort() >= DriverStation::kJoystickPorts) {
wpi_setWPIError(BadJoystickIndex);
} else {
joysticks[m_port] = this;
joysticks[GetPort()] = this;
}
}
@@ -111,7 +111,9 @@ float Joystick::GetY(JoystickHand hand) const {
*
* This depends on the mapping of the joystick connected to the current port.
*/
float Joystick::GetZ() const { return GetRawAxis(m_axes[kZAxis]); }
float Joystick::GetZ(JoystickHand hand) const {
return GetRawAxis(m_axes[kZAxis]);
}
/**
* Get the twist value of the current joystick.
@@ -129,16 +131,6 @@ float Joystick::GetThrottle() const {
return GetRawAxis(m_axes[kThrottleAxis]);
}
/**
* Get the value of the axis.
*
* @param axis The axis to read, starting at 0.
* @return The value of the axis.
*/
float Joystick::GetRawAxis(int axis) const {
return m_ds.GetStickAxis(m_port, axis);
}
/**
* For the current joystick, return the axis determined by the argument.
*
@@ -193,41 +185,6 @@ bool Joystick::GetTop(JoystickHand hand) const {
return GetRawButton(m_buttons[kTopButton]);
}
/**
* This is not supported for the Joystick.
*
* This method is only here to complete the GenericHID interface.
*/
bool Joystick::GetBumper(JoystickHand hand) const {
// Joysticks don't have bumpers.
return false;
}
/**
* Get the button value (starting at button 1)
*
* The buttons are returned in a single 16 bit value with one bit representing
* the state of each button. The appropriate button is returned as a boolean
* value.
*
* @param button The button number to be read (starting at 1)
* @return The state of the button.
**/
bool Joystick::GetRawButton(int button) const {
return m_ds.GetStickButton(m_port, button);
}
/**
* Get the angle in degrees of a POV on the joystick.
*
* The POV angles start at 0 in the up direction, and increase clockwise
* (e.g. right is 90, upper-left is 315).
*
* @param pov The index of the POV to read (starting at 0)
* @return the angle of the POV in degrees, or -1 if the POV is not pressed.
*/
int Joystick::GetPOV(int pov) const { return m_ds.GetStickPOV(m_port, pov); }
/**
* Get buttons based on an enumerated type.
*
@@ -252,37 +209,7 @@ bool Joystick::GetButton(ButtonType button) const {
*
* @return the number of axis for the current joystick
*/
int Joystick::GetAxisCount() const { return m_ds.GetStickAxisCount(m_port); }
/**
* Get the value of isXbox for the joystick.
*
* @return A boolean that is true if the joystick is an xbox controller.
*/
bool Joystick::GetIsXbox() const { return m_ds.GetJoystickIsXbox(m_port); }
/**
* Get the HID type of the controller.
*
* @return the HID type of the controller.
*/
Joystick::HIDType Joystick::GetType() const {
return static_cast<HIDType>(m_ds.GetJoystickType(m_port));
}
/**
* Get the name of the joystick.
*
* @return the name of the controller.
*/
std::string Joystick::GetName() const { return m_ds.GetJoystickName(m_port); }
/**
* Get the port number of the joystick.
*
* @return The port number of the joystick.
*/
int Joystick::GetPort() const { return m_port; }
int Joystick::GetAxisCount() const { return m_ds.GetStickAxisCount(GetPort()); }
/**
* Get the axis type of a joystick axis.
@@ -290,25 +217,18 @@ int Joystick::GetPort() const { return m_port; }
* @return the axis type of a joystick axis.
*/
int Joystick::GetAxisType(int axis) const {
return m_ds.GetJoystickAxisType(m_port, axis);
return m_ds.GetJoystickAxisType(GetPort(), axis);
}
/**
* Get the number of axis for a joystick.
* Get the number of buttons for a joystick.
*
* @return the number of buttons on the current joystick
*/
int Joystick::GetButtonCount() const {
return m_ds.GetStickButtonCount(m_port);
return m_ds.GetStickButtonCount(GetPort());
}
/**
* Get the number of axis for a joystick.
*
* @return the number of POVs for the current joystick
*/
int Joystick::GetPOVCount() const { return m_ds.GetStickPOVCount(m_port); }
/**
* Get the channel currently associated with the specified axis.
*
@@ -359,47 +279,3 @@ float Joystick::GetDirectionRadians() const {
float Joystick::GetDirectionDegrees() const {
return (180 / std::acos(-1)) * GetDirectionRadians();
}
/**
* Set the rumble output for the joystick.
*
* The DS currently supports 2 rumble values, left rumble and right rumble.
*
* @param type Which rumble value to set
* @param value The normalized value (0 to 1) to set the rumble to
*/
void Joystick::SetRumble(RumbleType type, float value) {
if (value < 0)
value = 0;
else if (value > 1)
value = 1;
if (type == kLeftRumble)
m_leftRumble = value * 65535;
else
m_rightRumble = value * 65535;
HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
}
/**
* Set a single HID output value for the joystick.
*
* @param outputNumber The index of the output to set (1-32)
* @param value The value to set the output to
*/
void Joystick::SetOutput(int outputNumber, bool value) {
m_outputs =
(m_outputs & ~(1 << (outputNumber - 1))) | (value << (outputNumber - 1));
HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
}
/**
* Set all HID output values for the joystick.
*
* @param value The 32 bit output value (1 bit for each output)
*/
void Joystick::SetOutputs(int value) {
m_outputs = value;
HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
}

View File

@@ -0,0 +1,141 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. 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. */
/*----------------------------------------------------------------------------*/
#include "XboxController.h"
#include "DriverStation.h"
#include "HAL/HAL.h"
using namespace frc;
/**
* Construct an instance of an Xbox controller.
*
* The joystick index is the USB port on the Driver Station.
*
* @param port The port on the Driver Station that the joystick is plugged into
* (0-5).
*/
XboxController::XboxController(int port)
: GamepadBase(port), m_ds(DriverStation::GetInstance()) {
// HAL_Report(HALUsageReporting::kResourceType_XboxController, port);
HAL_Report(HALUsageReporting::kResourceType_Joystick, port);
}
/**
* Get the X axis value of the controller.
*
* @param hand Side of controller whose value should be returned.
*/
float XboxController::GetX(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawAxis(0);
} else {
return GetRawAxis(4);
}
}
/**
* Get the Y axis value of the controller.
*
* @param hand Side of controller whose value should be returned.
*/
float XboxController::GetY(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawAxis(1);
} else {
return GetRawAxis(5);
}
}
/**
* Read the value of the bumper button on the controller.
*
* @param hand Side of controller whose value should be returned.
*/
bool XboxController::GetBumper(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawButton(5);
} else {
return GetRawButton(6);
}
}
/**
* 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.
*/
bool XboxController::GetStickButton(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawButton(9);
} else {
return GetRawButton(10);
}
}
/**
* Get the trigger axis value of the controller.
*
* @param hand Side of controller whose value should be returned.
*/
float XboxController::GetTriggerAxis(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawAxis(2);
} else {
return GetRawAxis(3);
}
}
/**
* Read the value of the A button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetAButton() const { return GetRawButton(1); }
/**
* Read the value of the B button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetBButton() const { return GetRawButton(2); }
/**
* Read the value of the X button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetXButton() const { return GetRawButton(3); }
/**
* Read the value of the Y button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetYButton() const { return GetRawButton(4); }
/**
* Read the value of the back button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetBackButton() const { return GetRawButton(7); }
/**
* Read the value of the start button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetStartButton() const { return GetRawButton(8); }

View File

@@ -0,0 +1,26 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include "GenericHID.h"
namespace frc {
/**
* Gamepad Interface.
*/
class GamepadBase : public GenericHID {
public:
explicit GamepadBase(int port);
virtual ~GamepadBase() = default;
virtual bool GetBumper(JoystickHand hand = kRightHand) const = 0;
virtual bool GetStickButton(JoystickHand hand) const = 0;
};
} // namespace frc

View File

@@ -9,30 +9,67 @@
#include <stdint.h>
#include <string>
namespace frc {
class DriverStation;
/**
* GenericHID Interface.
*/
class GenericHID {
public:
typedef enum { kLeftRumble, kRightRumble } RumbleType;
typedef enum {
kUnknown = -1,
kXInputUnknown = 0,
kXInputGamepad = 1,
kXInputWheel = 2,
kXInputArcadeStick = 3,
kXInputFlightStick = 4,
kXInputDancePad = 5,
kXInputGuitar = 6,
kXInputGuitar2 = 7,
kXInputDrumKit = 8,
kXInputGuitar3 = 11,
kXInputArcadePad = 19,
kHIDJoystick = 20,
kHIDGamepad = 21,
kHIDDriving = 22,
kHIDFlight = 23,
kHID1stPerson = 24
} HIDType;
enum JoystickHand { kLeftHand = 0, kRightHand = 1 };
explicit GenericHID(int port);
virtual ~GenericHID() = default;
virtual float GetX(JoystickHand hand = kRightHand) const = 0;
virtual float GetY(JoystickHand hand = kRightHand) const = 0;
virtual float GetZ() const = 0;
virtual float GetTwist() const = 0;
virtual float GetThrottle() const = 0;
virtual float GetRawAxis(int axis) const = 0;
virtual float GetRawAxis(int axis) const;
virtual bool GetTrigger(JoystickHand hand = kRightHand) const = 0;
virtual bool GetTop(JoystickHand hand = kRightHand) const = 0;
virtual bool GetBumper(JoystickHand hand = kRightHand) const = 0;
virtual bool GetRawButton(int button) const = 0;
bool GetRawButton(int button) const;
virtual int GetPOV(int pov = 0) const = 0;
int GetPOV(int pov = 0) const;
int GetPOVCount() const;
int GetPort() const;
GenericHID::HIDType GetType() const;
std::string GetName() const;
void SetOutput(int outputNumber, bool value);
void SetOutputs(int value);
void SetRumble(RumbleType type, double value);
private:
DriverStation& m_ds;
int m_port;
int m_outputs = 0;
uint16_t m_leftRumble = 0;
uint16_t m_rightRumble = 0;
};
} // namespace frc

View File

@@ -7,10 +7,13 @@
#pragma once
#include <stdint.h>
#include <memory>
#include <vector>
#include "ErrorBase.h"
#include "GenericHID.h"
#include "JoystickBase.h"
namespace frc {
@@ -18,19 +21,19 @@ class DriverStation;
/**
* Handle input from standard Joysticks connected to the Driver Station.
*
* 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 and the mapping of ports to hardware
* buttons depends on the code in the driver station.
* buttons depends on the code in the Driver Station.
*/
class Joystick : public GenericHID, public ErrorBase {
class Joystick : public JoystickBase, public ErrorBase {
public:
static const int kDefaultXAxis = 1;
static const int kDefaultYAxis = 2;
static const int kDefaultZAxis = 3;
static const int kDefaultTwistAxis = 4;
static const int kDefaultXAxis = 0;
static const int kDefaultYAxis = 1;
static const int kDefaultZAxis = 2;
static const int kDefaultTwistAxis = 2;
static const int kDefaultThrottleAxis = 3;
typedef enum {
kXAxis,
kYAxis,
@@ -39,8 +42,10 @@ class Joystick : public GenericHID, public ErrorBase {
kThrottleAxis,
kNumAxisTypes
} AxisType;
static const int kDefaultTriggerButton = 1;
static const int kDefaultTopButton = 2;
typedef enum { kTriggerButton, kTopButton, kNumButtonTypes } ButtonType;
explicit Joystick(int port);
@@ -50,22 +55,18 @@ class Joystick : public GenericHID, public ErrorBase {
Joystick(const Joystick&) = delete;
Joystick& operator=(const Joystick&) = delete;
int GetAxisChannel(AxisType axis);
int GetAxisChannel(AxisType axis) const;
void SetAxisChannel(AxisType axis, int channel);
float GetX(JoystickHand hand = kRightHand) const override;
float GetY(JoystickHand hand = kRightHand) const override;
float GetZ() const override;
float GetZ(JoystickHand hand = kRightHand) const override;
float GetTwist() const override;
float GetThrottle() const override;
virtual float GetAxis(AxisType axis) const;
float GetRawAxis(int axis) const override;
bool GetTrigger(JoystickHand hand = kRightHand) const override;
bool GetTop(JoystickHand hand = kRightHand) const override;
bool GetBumper(JoystickHand hand = kRightHand) const override;
bool GetRawButton(int button) const override;
int GetPOV(int pov = 1) const override;
bool GetButton(ButtonType button) const;
static Joystick* GetStickForPort(int port);
@@ -73,11 +74,15 @@ class Joystick : public GenericHID, public ErrorBase {
virtual float GetDirectionRadians() const;
virtual float GetDirectionDegrees() const;
int GetAxisType(int axis) const;
int GetAxisCount() const;
int GetButtonCount() const;
private:
DriverStation& m_ds;
int m_port;
std::unique_ptr<int[]> m_axes;
std::unique_ptr<int[]> m_buttons;
std::vector<int> m_axes;
std::vector<int> m_buttons;
};
} // namespace frc

View File

@@ -0,0 +1,30 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include "GenericHID.h"
namespace frc {
/**
* Joystick Interface.
*/
class JoystickBase : public GenericHID {
public:
explicit JoystickBase(int port);
virtual ~JoystickBase() = default;
virtual float GetZ(JoystickHand hand = kRightHand) const = 0;
virtual float GetTwist() const = 0;
virtual float GetThrottle() const = 0;
virtual bool GetTrigger(JoystickHand hand = kRightHand) const = 0;
virtual bool GetTop(JoystickHand hand = kRightHand) const = 0;
};
} // namespace frc

View File

@@ -0,0 +1,53 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include "ErrorBase.h"
#include "GamepadBase.h"
namespace frc {
class DriverStation;
/**
* Handle input from Xbox 360 or Xbox One controllers connected to the Driver
* Station.
*
* This class handles Xbox input that comes from the Driver Station. Each time a
* value is requested the most recent value is returend. 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.
*/
class XboxController : public GamepadBase, public ErrorBase {
public:
explicit XboxController(int port);
virtual ~XboxController() = default;
XboxController(const XboxController&) = delete;
XboxController& operator=(const XboxController&) = delete;
float GetX(JoystickHand hand) const override;
float GetY(JoystickHand hand) const override;
bool GetBumper(JoystickHand hand) const override;
bool GetStickButton(JoystickHand hand) const override;
virtual float GetTriggerAxis(JoystickHand hand) const;
bool GetAButton() const;
bool GetBButton() const;
bool GetXButton() const;
bool GetYButton() const;
bool GetBackButton() const;
bool GetStartButton() const;
private:
DriverStation& m_ds;
};
} // namespace frc

View File

@@ -0,0 +1,12 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. 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. */
/*----------------------------------------------------------------------------*/
#include "GamepadBase.h"
using namespace frc;
GamepadBase::GamepadBase(int port) : GenericHID(port) {}

View File

@@ -0,0 +1,12 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. 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. */
/*----------------------------------------------------------------------------*/
#include "JoystickBase.h"
using namespace frc;
JoystickBase::JoystickBase(int port) : GenericHID(port) {}

View File

@@ -50,4 +50,5 @@
#include "SpeedController.h"
#include "Talon.h"
#include "Victor.h"
#include "XboxController.h"
#include "interfaces/Potentiometer.h"

View File

@@ -0,0 +1,118 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. 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. */
/*----------------------------------------------------------------------------*/
#include "GenericHID.h"
#include "DriverStation.h"
using namespace frc;
GenericHID::GenericHID(int port) : m_ds(DriverStation::GetInstance()) {
m_port = port;
}
/**
* Get the value of the axis.
*
* @param axis The axis to read, starting at 0.
* @return The value of the axis.
*/
float GenericHID::GetRawAxis(int axis) const {
return m_ds.GetStickAxis(m_port, axis);
}
/**
* Get the button value (starting at button 1)
*
* The buttons are returned in a single 16 bit value with one bit representing
* the state of each button. The appropriate button is returned as a boolean
* value.
*
* @param button The button number to be read (starting at 1)
* @return The state of the button.
**/
bool GenericHID::GetRawButton(int button) const {
return m_ds.GetStickButton(m_port, button);
}
/**
* Get the angle in degrees of a POV on the HID.
*
* The POV angles start at 0 in the up direction, and increase clockwise
* (e.g. right is 90, upper-left is 315).
*
* @param pov The index of the POV to read (starting at 0)
* @return the angle of the POV in degrees, or -1 if the POV is not pressed.
*/
int GenericHID::GetPOV(int pov) const { return 0; }
/**
* Get the number of POVs for the HID.
*
* @return the number of POVs for the current HID
*/
int GenericHID::GetPOVCount() const { return 0; }
/**
* Get the port number of the HID.
*
* @return The port number of the HID.
*/
int GenericHID::GetPort() const { return m_port; }
/**
* Get the type of the HID.
*
* @return the type of the HID.
*/
GenericHID::HIDType GenericHID::GetType() const { return HIDType::kUnknown; }
/**
* Get the name of the HID.
*
* @return the name of the HID.
*/
std::string GenericHID::GetName() const { return ""; }
/**
* Set a single HID output value for the HID.
*
* @param outputNumber The index of the output to set (1-32)
* @param value The value to set the output to
*/
void GenericHID::SetOutput(int outputNumber, bool value) {
m_outputs =
(m_outputs & ~(1 << (outputNumber - 1))) | (value << (outputNumber - 1));
}
/**
* Set all output values for the HID.
*
* @param value The 32 bit output value (1 bit for each output)
*/
void GenericHID::SetOutputs(int value) { m_outputs = value; }
/**
* Set the rumble output for the HID.
*
* The DS currently supports 2 rumble values, left rumble and right rumble.
*
* @param type Which rumble value to set
* @param value The normalized value (0 to 1) to set the rumble to
*/
void GenericHID::SetRumble(RumbleType type, double value) {
if (value < 0)
value = 0;
else if (value > 1)
value = 1;
if (type == kLeftRumble) {
m_leftRumble = value * 65535;
} else {
m_rightRumble = value * 65535;
}
}

View File

@@ -8,7 +8,6 @@
#include "Joystick.h"
#include <cmath>
#include <memory>
#include "DriverStation.h"
#include "WPIErrors.h"
@@ -28,9 +27,10 @@ static bool joySticksInitialized = false;
/**
* Construct an instance of a joystick.
*
* The joystick index is the usb port on the drivers station.
* The joystick index is the USB port on the Driver Station.
*
* @param port The port on the driver station that the joystick is plugged into.
* @param port The port on the Driver Station that the joystick is plugged into
* (0-5).
*/
Joystick::Joystick(int port) : Joystick(port, kNumAxisTypes, kNumButtonTypes) {
m_axes[kXAxis] = kDefaultXAxis;
@@ -49,22 +49,25 @@ Joystick::Joystick(int port) : Joystick(port, kNumAxisTypes, kNumButtonTypes) {
* This constructor allows the subclass to configure the number of constants
* for axes and buttons.
*
* @param port The port on the driver station that the joystick is
* @param port The port on the Driver Station that the joystick is
* plugged into.
* @param numAxisTypes The number of axis types in the enum.
* @param numButtonTypes The number of button types in the enum.
*/
Joystick::Joystick(int port, int numAxisTypes, int numButtonTypes)
: m_port(port), m_ds(DriverStation::GetInstance()) {
: JoystickBase(port),
m_ds(DriverStation::GetInstance()),
m_axes(numAxisTypes),
m_buttons(numButtonTypes) {
if (!joySticksInitialized) {
for (int i = 0; i < DriverStation::kJoystickPorts; i++)
joysticks[i] = nullptr;
for (auto& joystick : joysticks) joystick = nullptr;
joySticksInitialized = true;
}
joysticks[m_port] = this;
m_axes = std::make_unique<int[]>(numAxisTypes);
m_buttons = std::make_unique<int[]>(numButtonTypes);
if (GetPort() >= DriverStation::kJoystickPorts) {
wpi_setWPIError(BadJoystickIndex);
} else {
joysticks[GetPort()] = this;
}
}
Joystick* Joystick::GetStickForPort(int port) {
@@ -80,6 +83,9 @@ Joystick* Joystick::GetStickForPort(int port) {
* Get the X value of the joystick.
*
* This depends on the mapping of the joystick connected to the current port.
*
* @param hand This parameter is ignored for the Joystick class and is only
* here to complete the GenericHID interface.
*/
float Joystick::GetX(JoystickHand hand) const {
return GetRawAxis(m_axes[kXAxis]);
@@ -89,6 +95,9 @@ float Joystick::GetX(JoystickHand hand) const {
* Get the Y value of the joystick.
*
* This depends on the mapping of the joystick connected to the current port.
*
* @param hand This parameter is ignored for the Joystick class and is only
* here to complete the GenericHID interface.
*/
float Joystick::GetY(JoystickHand hand) const {
return GetRawAxis(m_axes[kYAxis]);
@@ -99,7 +108,9 @@ float Joystick::GetY(JoystickHand hand) const {
*
* This depends on the mapping of the joystick connected to the current port.
*/
float Joystick::GetZ() const { return GetRawAxis(m_axes[kZAxis]); }
float Joystick::GetZ(JoystickHand hand) const {
return GetRawAxis(m_axes[kZAxis]);
}
/**
* Get the twist value of the current joystick.
@@ -117,16 +128,6 @@ float Joystick::GetThrottle() const {
return GetRawAxis(m_axes[kThrottleAxis]);
}
/**
* Get the value of the axis.
*
* @param axis The axis to read [1-6].
* @return The value of the axis.
*/
float Joystick::GetRawAxis(int axis) const {
return m_ds.GetStickAxis(m_port, axis);
}
/**
* For the current joystick, return the axis determined by the argument.
*
@@ -160,8 +161,8 @@ float Joystick::GetAxis(AxisType axis) const {
*
* Look up which button has been assigned to the trigger and read its state.
*
* @param hand This parameter is ignored for the Joystick class and is only here
* to complete the GenericHID interface.
* @param hand This parameter is ignored for the Joystick class and is only
* here to complete the GenericHID interface.
* @return The state of the trigger.
*/
bool Joystick::GetTrigger(JoystickHand hand) const {
@@ -173,47 +174,14 @@ bool Joystick::GetTrigger(JoystickHand hand) const {
*
* Look up which button has been assigned to the top and read its state.
*
* @param hand This parameter is ignored for the Joystick class and is only here
* to complete the GenericHID interface.
* @param hand This parameter is ignored for the Joystick class and is only
* here to complete the GenericHID interface.
* @return The state of the top button.
*/
bool Joystick::GetTop(JoystickHand hand) const {
return GetRawButton(m_buttons[kTopButton]);
}
/**
* This is not supported for the Joystick.
*
* This method is only here to complete the GenericHID interface.
*/
bool Joystick::GetBumper(JoystickHand hand) const {
// Joysticks don't have bumpers.
return false;
}
/**
* Get the button value for buttons 1 through 12.
*
* The buttons are returned in a single 16 bit value with one bit representing
* the state of each button. The appropriate button is returned as a boolean
* value.
*
* @param button The button number to be read.
* @return The state of the button.
*/
bool Joystick::GetRawButton(int button) const {
return m_ds.GetStickButton(m_port, button);
}
/**
* Get the state of a POV on the joystick.
*
* @return the angle of the POV in degrees, or -1 if the POV is not pressed.
*/
int Joystick::GetPOV(int pov) const {
return 0; // TODO
}
/**
* Get buttons based on an enumerated type.
*
@@ -233,18 +201,39 @@ bool Joystick::GetButton(ButtonType button) const {
}
}
/**
* Get the number of axis for a joystick
*
* @return the number of axis for the current joystick
*/
int Joystick::GetAxisCount() const { return 0; }
/**
* Get the axis type of a joystick axis.
*
* @return the axis type of a joystick axis.
*/
int Joystick::GetAxisType(int axis) const { return 0; }
/**
* Get the number of buttons for a joystick.
*
* @return the number of buttons on the current joystick
*/
int Joystick::GetButtonCount() const { return 0; }
/**
* Get the channel currently associated with the specified axis.
*
* @param axis The axis to look up the channel for.
* @return The channel fr the axis.
*/
int Joystick::GetAxisChannel(AxisType axis) { return m_axes[axis]; }
int Joystick::GetAxisChannel(AxisType axis) const { return m_axes[axis]; }
/**
* Set the channel associated with a specified axis.
*
* @param axis The axis to set the channel for.
* @param axis The axis to set the channel for.
* @param channel The channel to set the axis to.
*/
void Joystick::SetAxisChannel(AxisType axis, int channel) {
@@ -275,7 +264,7 @@ float Joystick::GetDirectionRadians() const {
* Get the direction of the vector formed by the joystick and its origin
* in degrees.
*
* uses std::acos(-1) to represent Pi due to absence of readily accessable PI
* uses std::acos(-1) to represent Pi due to absence of readily accessible Pi
* constant in C++
*
* @return The direction of the vector in degrees

View File

@@ -0,0 +1,137 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. 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. */
/*----------------------------------------------------------------------------*/
#include "XboxController.h"
#include "DriverStation.h"
using namespace frc;
/**
* Construct an instance of an Xbox controller.
*
* The joystick index is the USB port on the Driver Station.
*
* @param port The port on the Driver Station that the joystick is plugged into
* (0-5).
*/
XboxController::XboxController(int port)
: GamepadBase(port), m_ds(DriverStation::GetInstance()) {}
/**
* Get the X axis value of the controller.
*
* @param hand Side of controller whose value should be returned.
*/
float XboxController::GetX(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawAxis(0);
} else {
return GetRawAxis(4);
}
}
/**
* Get the Y axis value of the controller.
*
* @param hand Side of controller whose value should be returned.
*/
float XboxController::GetY(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawAxis(1);
} else {
return GetRawAxis(5);
}
}
/**
* Read the value of the bumper button on the controller.
*
* @param hand Side of controller whose value should be returned.
*/
bool XboxController::GetBumper(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawButton(5);
} else {
return GetRawButton(6);
}
}
/**
* 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.
*/
bool XboxController::GetStickButton(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawButton(9);
} else {
return GetRawButton(10);
}
}
/**
* Get the trigger axis value of the controller.
*
* @param hand Side of controller whose value should be returned.
*/
float XboxController::GetTriggerAxis(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawAxis(2);
} else {
return GetRawAxis(3);
}
}
/**
* Read the value of the A button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetAButton() const { return GetRawButton(1); }
/**
* Read the value of the B button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetBButton() const { return GetRawButton(2); }
/**
* Read the value of the X button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetXButton() const { return GetRawButton(3); }
/**
* Read the value of the Y button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetYButton() const { return GetRawButton(4); }
/**
* Read the value of the back button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetBackButton() const { return GetRawButton(7); }
/**
* Read the value of the start button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetStartButton() const { return GetRawButton(8); }

View File

@@ -9,15 +9,14 @@ package edu.wpi.first.wpilibj;
import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
import edu.wpi.first.wpilibj.hal.HAL;
import edu.wpi.first.wpilibj.hal.HAL;
/**
* Handle input from standard Joysticks connected to the Driver Station. 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 and the mapping of ports to hardware
* buttons depends on the code in the driver station.
* buttons depends on the code in the Driver Station.
*/
public class Joystick extends GenericHID {
public class Joystick extends JoystickBase {
static final byte kDefaultXAxis = 0;
static final byte kDefaultYAxis = 1;
@@ -55,16 +54,7 @@ public class Joystick extends GenericHID {
}
}
/**
* Represents a rumble output on the JoyStick.
*/
public enum RumbleType {
kLeftRumble, kRightRumble
}
private final DriverStation m_ds;
private final int m_port;
private final byte[] m_axes;
private final byte[] m_buttons;
private int m_outputs;
@@ -72,10 +62,10 @@ public class Joystick extends GenericHID {
private short m_rightRumble;
/**
* Construct an instance of a joystick. The joystick index is the usb port on the drivers
* Construct an instance of a joystick. The joystick index is the USB port on the drivers
* station.
*
* @param port The port on the driver station that the joystick is plugged into.
* @param port The port on the Driver Station that the joystick is plugged into.
*/
public Joystick(final int port) {
this(port, AxisType.kNumAxis.value, ButtonType.kNumButton.value);
@@ -98,15 +88,16 @@ public class Joystick extends GenericHID {
* <p>This constructor allows the subclass to configure the number of constants for axes and
* buttons.
*
* @param port The port on the driver station that the joystick is plugged into.
* @param port The port on the Driver Station that the joystick is plugged into.
* @param numAxisTypes The number of axis types in the enum.
* @param numButtonTypes The number of button types in the enum.
*/
protected Joystick(int port, int numAxisTypes, int numButtonTypes) {
super(port);
m_ds = DriverStation.getInstance();
m_axes = new byte[numAxisTypes];
m_buttons = new byte[numButtonTypes];
m_port = port;
}
/**
@@ -116,7 +107,8 @@ public class Joystick extends GenericHID {
* @param hand Unused
* @return The X value of the joystick.
*/
public double getX(Hand hand) {
@Override
public final double getX(Hand hand) {
return getRawAxis(m_axes[AxisType.kX.value]);
}
@@ -127,18 +119,13 @@ public class Joystick extends GenericHID {
* @param hand Unused
* @return The Y value of the joystick.
*/
public double getY(Hand hand) {
@Override
public final double getY(Hand hand) {
return getRawAxis(m_axes[AxisType.kY.value]);
}
/**
* Get the Z value of the joystick. This depends on the mapping of the joystick connected to the
* current port.
*
* @param hand Unused
* @return The Z value of the joystick.
*/
public double getZ(Hand hand) {
@Override
public final double getZ(Hand hand) {
return getRawAxis(m_axes[AxisType.kZ.value]);
}
@@ -169,7 +156,7 @@ public class Joystick extends GenericHID {
* @return The value of the axis.
*/
public double getRawAxis(final int axis) {
return m_ds.getStickAxis(m_port, axis);
return m_ds.getStickAxis(getPort(), axis);
}
/**
@@ -202,7 +189,7 @@ public class Joystick extends GenericHID {
* For the current joystick, return the number of axis.
*/
public int getAxisCount() {
return m_ds.getStickAxisCount(m_port);
return m_ds.getStickAxisCount(getPort());
}
/**
@@ -231,6 +218,16 @@ public class Joystick extends GenericHID {
return getRawButton(m_buttons[ButtonType.kTop.value]);
}
@Override
public int getPOV(int pov) {
return m_ds.getStickPOV(getPort(), pov);
}
@Override
public int getPOVCount() {
return m_ds.getStickPOVCount(getPort());
}
/**
* This is not supported for the Joystick. This method is only here to complete the GenericHID
* interface.
@@ -252,34 +249,14 @@ public class Joystick extends GenericHID {
* @return The state of the button.
*/
public boolean getRawButton(final int button) {
return m_ds.getStickButton(m_port, (byte) button);
return m_ds.getStickButton(getPort(), (byte) button);
}
/**
* For the current joystick, return the number of buttons.
*/
public int getButtonCount() {
return m_ds.getStickButtonCount(m_port);
}
/**
* Get the angle in degrees of a POV on the joystick.
*
* <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)
* @return the angle of the POV in degrees, or -1 if the POV is not pressed.
*/
public int getPOV(int pov) {
return m_ds.getStickPOV(m_port, pov);
}
/**
* For the current joystick, return the number of POVs.
*/
public int getPOVCount() {
return m_ds.getStickPOVCount(m_port);
return m_ds.getStickButtonCount(getPort());
}
/**
@@ -357,34 +334,7 @@ public class Joystick extends GenericHID {
* @return A boolean that is true if the controller is an xbox controller.
*/
public boolean getIsXbox() {
return m_ds.getJoystickIsXbox(m_port);
}
/**
* Get the HID type of the current joystick.
*
* @return The HID type value of the current joystick.
*/
public int getType() {
return m_ds.getJoystickType(m_port);
}
/**
* Get the name of the current joystick.
*
* @return The name of the current joystick.
*/
public String getName() {
return m_ds.getJoystickName(m_port);
}
/**
* Get the port number of the joystick.
*
* @return The port number of the joystick.
*/
public int getPort() {
return m_port;
return m_ds.getJoystickIsXbox(getPort());
}
/**
@@ -393,17 +343,43 @@ public class Joystick extends GenericHID {
* @return the axis type of a joystick axis.
*/
public int getAxisType(int axis) {
return m_ds.getJoystickAxisType(m_port, axis);
return m_ds.getJoystickAxisType(getPort(), axis);
}
/**
* Set the rumble output for the joystick. The DS currently supports 2 rumble values, left rumble
* and right rumble.
* Get the type of the HID.
*
* @param type Which rumble value to set
* @param value The normalized value (0 to 1) to set the rumble to
* @return the type of the HID.
*/
public void setRumble(RumbleType type, float value) {
@Override
public HIDType getType() {
return HIDType.values()[m_ds.getJoystickType(getPort())];
}
/**
* Get the name of the HID.
*
* @return the name of the HID.
*/
@Override
public String getName() {
return m_ds.getJoystickName(getPort());
}
@Override
public void setOutput(int outputNumber, boolean value) {
m_outputs = (m_outputs & ~(1 << (outputNumber - 1))) | ((value ? 1 : 0) << (outputNumber - 1));
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
@Override
public void setOutputs(int value) {
m_outputs = value;
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
@Override
public void setRumble(RumbleType type, double value) {
if (value < 0) {
value = 0;
} else if (value > 1) {
@@ -414,28 +390,6 @@ public class Joystick extends GenericHID {
} else {
m_rightRumble = (short) (value * 65535);
}
HAL.setJoystickOutputs((byte) m_port, m_outputs, m_leftRumble, m_rightRumble);
}
/**
* Set a single HID output value for the joystick.
*
* @param outputNumber The index of the output to set (1-32)
* @param value The value to set the output to
*/
public void setOutput(int outputNumber, boolean value) {
m_outputs = (m_outputs & ~(1 << (outputNumber - 1))) | ((value ? 1 : 0) << (outputNumber - 1));
HAL.setJoystickOutputs((byte) m_port, m_outputs, m_leftRumble, m_rightRumble);
}
/**
* Set all HID output values for the joystick.
*
* @param value The 32 bit output value (1 bit for each output)
*/
public void setOutputs(int value) {
m_outputs = value;
HAL.setJoystickOutputs((byte) m_port, m_outputs, m_leftRumble, m_rightRumble);
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
}

View File

@@ -0,0 +1,260 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. 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 edu.wpi.first.wpilibj;
import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
import edu.wpi.first.wpilibj.hal.HAL;
/**
* Handle input from Xbox 360 or Xbox One controllers connected to the Driver Station.
*
* <p>This class handles Xbox input that comes from the Driver Station. Each time a value is
* requested the most recent value is returend. 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 XboxController extends GamepadBase {
private DriverStation m_ds;
private int m_outputs;
private short m_leftRumble;
private short m_rightRumble;
/**
* Construct an instance of a joystick. The joystick index is the USB port on the drivers
* station.
*
* @param port The port on the Driver Station that the joystick is plugged into.
*/
public XboxController(final int port) {
super(port);
m_ds = DriverStation.getInstance();
// HAL.report(tResourceType.kResourceType_XboxController, port);
HAL.report(tResourceType.kResourceType_Joystick, port);
}
/**
* Get the X axis value of the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The X axis value of the controller.
*/
@Override
public double getX(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawAxis(0);
} else {
return getRawAxis(4);
}
}
/**
* Get the Y axis value of the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The Y axis value of the controller.
*/
@Override
public double getY(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawAxis(1);
} else {
return getRawAxis(5);
}
}
/**
* Get the value of the axis.
*
* @param axis The axis to read, starting at 0.
* @return The value of the axis.
*/
public double getRawAxis(final int axis) {
return m_ds.getStickAxis(getPort(), axis);
}
/**
* Read the value of the bumper button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
@Override
public boolean getBumper(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawButton(5);
} else {
return getRawButton(6);
}
}
/**
* This is not supported for the XboxController. This method is only here to complete the
* GenericHID interface.
*
* @param hand This parameter is ignored for the Joystick class and is only here to complete the
* GenericHID interface.
* @return The state of the trigger (always false)
*/
public boolean getTrigger(Hand hand) {
return false;
}
/**
* This is not supported for the XboxController. This method is only here to complete the
* GenericHID interface.
*
* @param hand This parameter is ignored for the Joystick class and is only here to complete the
* GenericHID interface.
* @return The state of the top button (always false)
*/
public boolean getTop(Hand hand) {
return false;
}
/**
* Get the button value (starting at button 1).
*
* <p>The appropriate button is returned as a boolean value.
*
* @param button The button number to be read (starting at 1).
* @return The state of the button.
*/
public boolean getRawButton(final int button) {
return m_ds.getStickButton(getPort(), (byte) button);
}
/**
* Get the trigger axis value of the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The trigger axis value of the controller.
*/
public double getTriggerAxis(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawAxis(2);
} else {
return getRawAxis(3);
}
}
/**
* Read the value of the A button on the controller.
*
* @return The state of the button.
*/
public boolean getAButton() {
return getRawButton(1);
}
/**
* Read the value of the B button on the controller.
*
* @return The state of the button.
*/
public boolean getBButton() {
return getRawButton(2);
}
/**
* Read the value of the X button on the controller.
*
* @return The state of the button.
*/
public boolean getXButton() {
return getRawButton(3);
}
/**
* Read the value of the Y button on the controller.
*
* @return The state of the button.
*/
public boolean getYButton() {
return getRawButton(4);
}
/**
* 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.
*/
@Override
public boolean getStickButton(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawButton(9);
} else {
return getRawButton(10);
}
}
/**
* Read the value of the back button on the controller.
*
* @return The state of the button.
*/
public boolean getBackButton() {
return getRawButton(7);
}
/**
* Read the value of the start button on the controller.
*
* @return The state of the button.
*/
public boolean getStartButton() {
return getRawButton(8);
}
@Override
public int getPOV(int pov) {
return m_ds.getStickPOV(getPort(), pov);
}
@Override
public int getPOVCount() {
return m_ds.getStickPOVCount(getPort());
}
@Override
public HIDType getType() {
return HIDType.values()[m_ds.getJoystickType(getPort())];
}
@Override
public String getName() {
return m_ds.getJoystickName(getPort());
}
@Override
public void setOutput(int outputNumber, boolean value) {
m_outputs = (m_outputs & ~(1 << (outputNumber - 1))) | ((value ? 1 : 0) << (outputNumber - 1));
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
@Override
public void setOutputs(int value) {
m_outputs = value;
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
@Override
public void setRumble(RumbleType type, double value) {
if (value < 0) {
value = 0;
} else if (value > 1) {
value = 1;
}
if (type == RumbleType.kLeftRumble) {
m_leftRumble = (short) (value * 65535);
} else {
m_rightRumble = (short) (value * 65535);
}
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
}

View File

@@ -0,0 +1,58 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. 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 edu.wpi.first.wpilibj;
/**
* Gamepad Interface.
*/
public abstract class GamepadBase extends GenericHID {
public GamepadBase(int port) {
super(port);
}
public abstract double getRawAxis(int axis);
/**
* Is the bumper pressed.
*
* @param hand which hand
* @return true if the bumper is pressed
*/
public abstract boolean getBumper(Hand hand);
/**
* Is the bumper pressed.
*
* @return true if the bumper is pressed
*/
public boolean getBumper() {
return getBumper(Hand.kRight);
}
public abstract boolean getStickButton(Hand hand);
public boolean getStickButton() {
return getStickButton(Hand.kRight);
}
public abstract boolean getRawButton(int button);
public abstract int getPOV(int pov);
public abstract int getPOVCount();
public abstract HIDType getType();
public abstract String getName();
public abstract void setOutput(int outputNumber, boolean value);
public abstract void setOutputs(int value);
public abstract void setRumble(RumbleType type, double value);
}

View File

@@ -11,6 +11,40 @@ package edu.wpi.first.wpilibj;
* GenericHID Interface.
*/
public abstract class GenericHID {
/**
* Represents a rumble output on the JoyStick.
*/
public enum RumbleType {
kLeftRumble, kRightRumble
}
public enum HIDType {
kUnknown(-1),
kXInputUnknown(0),
kXInputGamepad(1),
kXInputWheel(2),
kXInputArcadeStick(3),
kXInputFlightStick(4),
kXInputDancePad(5),
kXInputGuitar(6),
kXInputGuitar2(7),
kXInputDrumKit(8),
kXInputGuitar3(11),
kXInputArcadePad(19),
kHIDJoystick(20),
kHIDGamepad(21),
kHIDDriving(22),
kHIDFlight(23),
kHID1stPerson(24);
@SuppressWarnings("MemberName")
public final int value;
private HIDType(int value) {
this.value = value;
}
}
/**
* Which hand the Human Interface Device is associated with.
*/
@@ -25,6 +59,12 @@ public abstract class GenericHID {
}
}
private final int m_port;
public GenericHID(int port) {
m_port = port;
}
/**
* Get the x position of the HID.
*
@@ -59,37 +99,6 @@ public abstract class GenericHID {
*/
public abstract double getY(Hand hand);
/**
* Get the z position of the HID.
*
* @return the z position
*/
public final double getZ() {
return getZ(Hand.kRight);
}
/**
* Get the z position of the HID.
*
* @param hand which hand, left or right
* @return the z position
*/
public abstract double getZ(Hand hand);
/**
* Get the twist value.
*
* @return the twist value
*/
public abstract double getTwist();
/**
* Get the throttle.
*
* @return the throttle value
*/
public abstract double getThrottle();
/**
* Get the raw axis.
*
@@ -98,57 +107,6 @@ public abstract class GenericHID {
*/
public abstract double getRawAxis(int which);
/**
* Is the trigger pressed.
*
* @return true if pressed
*/
public final boolean getTrigger() {
return getTrigger(Hand.kRight);
}
/**
* Is the trigger pressed.
*
* @param hand which hand
* @return true if the trigger for the given hand is pressed
*/
public abstract boolean getTrigger(Hand hand);
/**
* Is the top button pressed.
*
* @return true if the top button is pressed
*/
public final boolean getTop() {
return getTop(Hand.kRight);
}
/**
* Is the top button pressed.
*
* @param hand which hand
* @return true if hte top button for the given hand is pressed
*/
public abstract boolean getTop(Hand hand);
/**
* Is the bumper pressed.
*
* @return true if the bumper is pressed
*/
public final boolean getBumper() {
return getBumper(Hand.kRight);
}
/**
* Is the bumper pressed.
*
* @param hand which hand
* @return true if hte bumper is pressed
*/
public abstract boolean getBumper(Hand hand);
/**
* Is the given button pressed.
*
@@ -157,9 +115,70 @@ public abstract class GenericHID {
*/
public abstract boolean getRawButton(int button);
/**
* Get the angle in degrees of a POV 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).
*
* @param pov The index of the POV to read (starting at 0)
* @return the angle of the POV in degrees, or -1 if the POV is not pressed.
*/
public abstract int getPOV(int pov);
public int getPOV() {
return getPOV(0);
}
/**
* For the current HID, return the number of POVs.
*/
public abstract int getPOVCount();
/**
* Get the port number of the HID.
*
* @return The port number of the HID.
*/
public int getPort() {
return m_port;
}
/**
* Get the type of the HID.
*
* @return the type of the HID.
*/
public abstract HIDType getType();
/**
* Get the name of the HID.
*
* @return the name of the HID.
*/
public abstract String getName();
/**
* Set a single HID output value for the HID.
*
* @param outputNumber The index of the output to set (1-32)
* @param value The value to set the output to
*/
public abstract void setOutput(int outputNumber, boolean value);
/**
* Set all HID output values for the HID.
*
* @param value The 32 bit output value (1 bit for each output)
*/
public abstract void setOutputs(int value);
/**
* Set the rumble output for the HID. The DS currently supports 2 rumble values, left rumble and
* right rumble.
*
* @param type Which rumble value to set
* @param value The normalized value (0 to 1) to set the rumble to
*/
public abstract void setRumble(RumbleType type, double value);
}

View File

@@ -0,0 +1,91 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2016. 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 edu.wpi.first.wpilibj;
/**
* JoystickBase Interface.
*/
public abstract class JoystickBase extends GenericHID {
public JoystickBase(int port) {
super(port);
}
/**
* Get the z position of the HID.
*
* @param hand which hand, left or right
* @return the z position
*/
public abstract double getZ(Hand hand);
public double getZ() {
return getZ(Hand.kRight);
}
/**
* Get the twist value.
*
* @return the twist value
*/
public abstract double getTwist();
/**
* Get the throttle.
*
* @return the throttle value
*/
public abstract double getThrottle();
/**
* Is the trigger pressed.
*
* @return true if pressed
*/
public final boolean getTrigger() {
return getTrigger(Hand.kRight);
}
/**
* Is the trigger pressed.
*
* @param hand which hand
* @return true if the trigger for the given hand is pressed
*/
public abstract boolean getTrigger(Hand hand);
/**
* Is the top button pressed.
*
* @return true if the top button is pressed
*/
public final boolean getTop() {
return getTop(Hand.kRight);
}
/**
* Is the top button pressed.
*
* @param hand which hand
* @return true if hte top button for the given hand is pressed
*/
public abstract boolean getTop(Hand hand);
public abstract int getPOV(int pov);
public abstract int getPOVCount();
public abstract HIDType getType();
public abstract String getName();
public abstract void setOutput(int outputNumber, boolean value);
public abstract void setOutputs(int value);
public abstract void setRumble(RumbleType type, double value);
}

View File

@@ -7,62 +7,30 @@
package edu.wpi.first.wpilibj;
/**
* Handle input from standard Joysticks connected to the Driver Station. 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 and the mapping of ports to hardware
* buttons depends on the code in the driver station.
* buttons depends on the code in the Driver Station.
*/
public class Joystick extends GenericHID {
public class Joystick extends JoystickBase {
static final byte kDefaultXAxis = 1;
static final byte kDefaultYAxis = 2;
static final byte kDefaultZAxis = 3;
static final byte kDefaultTwistAxis = 3;
static final byte kDefaultThrottleAxis = 4;
static final byte kDefaultXAxis = 0;
static final byte kDefaultYAxis = 1;
static final byte kDefaultZAxis = 2;
static final byte kDefaultTwistAxis = 2;
static final byte kDefaultThrottleAxis = 3;
static final int kDefaultTriggerButton = 1;
static final int kDefaultTopButton = 2;
/**
* Represents an analog axis on a joystick.
*/
public static class AxisType {
public enum AxisType {
kX(0), kY(1), kZ(2), kTwist(3), kThrottle(4), kNumAxis(5);
/**
* The integer value representing this enumeration
*/
@SuppressWarnings("MemberName")
public final int value;
static final int kX_val = 0;
static final int kY_val = 1;
static final int kZ_val = 2;
static final int kTwist_val = 3;
static final int kThrottle_val = 4;
static final int kNumAxis_val = 5;
/**
* axis: x-axis
*/
public static final AxisType kX = new AxisType(kX_val);
/**
* axis: y-axis
*/
public static final AxisType kY = new AxisType(kY_val);
/**
* axis: z-axis
*/
public static final AxisType kZ = new AxisType(kZ_val);
/**
* axis: twist
*/
public static final AxisType kTwist = new AxisType(kTwist_val);
/**
* axis: throttle
*/
public static final AxisType kThrottle = new AxisType(kThrottle_val);
/**
* axis: number of axis
*/
public static final AxisType kNumAxis = new AxisType(kNumAxis_val);
private AxisType(int value) {
this.value = value;
@@ -70,45 +38,31 @@ public class Joystick extends GenericHID {
}
/**
* Represents a digital button on the JoyStick
* Represents a digital button on the JoyStick.
*/
public static class ButtonType {
public enum ButtonType {
kTrigger(0), kTop(1), kNumButton(2);
/**
* The integer value representing this enumeration
*/
@SuppressWarnings("MemberName")
public final int value;
static final int kTrigger_val = 0;
static final int kTop_val = 1;
static final int kNumButton_val = 2;
/**
* button: trigger
*/
public static final ButtonType kTrigger = new ButtonType((kTrigger_val));
/**
* button: top button
*/
public static final ButtonType kTop = new ButtonType(kTop_val);
/**
* button: num button types
*/
public static final ButtonType kNumButton = new ButtonType((kNumButton_val));
private ButtonType(int value) {
this.value = value;
}
}
private DriverStation m_ds;
private final int m_port;
private final DriverStation m_ds;
private final byte[] m_axes;
private final byte[] m_buttons;
private int m_outputs;
private short m_leftRumble;
private short m_rightRumble;
/**
* Construct an instance of a joystick. The joystick index is the usb port on the drivers
* Construct an instance of a joystick. The joystick index is the USB port on the drivers
* station.
*
* @param port The port on the driver station that the joystick is plugged into.
* @param port The port on the Driver Station that the joystick is plugged into.
*/
public Joystick(final int port) {
this(port, AxisType.kNumAxis.value, ButtonType.kNumButton.value);
@@ -121,24 +75,24 @@ public class Joystick extends GenericHID {
m_buttons[ButtonType.kTrigger.value] = kDefaultTriggerButton;
m_buttons[ButtonType.kTop.value] = kDefaultTopButton;
}
/**
* Protected version of the constructor to be called by sub-classes.
*
* This constructor allows the subclass to configure the number of constants for axes and
* <p>This constructor allows the subclass to configure the number of constants for axes and
* buttons.
*
* @param port The port on the driver station that the joystick is plugged into.
* @param port The port on the Driver Station that the joystick is plugged into.
* @param numAxisTypes The number of axis types in the enum.
* @param numButtonTypes The number of button types in the enum.
*/
protected Joystick(int port, int numAxisTypes, int numButtonTypes) {
super(port);
m_ds = DriverStation.getInstance();
m_axes = new byte[numAxisTypes];
m_buttons = new byte[numButtonTypes];
m_port = port;
}
/**
@@ -148,7 +102,8 @@ public class Joystick extends GenericHID {
* @param hand Unused
* @return The X value of the joystick.
*/
public double getX(Hand hand) {
@Override
public final double getX(Hand hand) {
return getRawAxis(m_axes[AxisType.kX.value]);
}
@@ -159,18 +114,13 @@ public class Joystick extends GenericHID {
* @param hand Unused
* @return The Y value of the joystick.
*/
public double getY(Hand hand) {
@Override
public final double getY(Hand hand) {
return getRawAxis(m_axes[AxisType.kY.value]);
}
/**
* Get the Z value of the joystick. This depends on the mapping of the joystick connected to the
* current port.
*
* @param hand Unused
* @return The Z value of the joystick.
*/
public double getZ(Hand hand) {
@Override
public final double getZ(Hand hand) {
return getRawAxis(m_axes[AxisType.kZ.value]);
}
@@ -197,43 +147,50 @@ public class Joystick extends GenericHID {
/**
* Get the value of the axis.
*
* @param axis The axis to read [1-6].
* @param axis The axis to read, starting at 0.
* @return The value of the axis.
*/
public double getRawAxis(final int axis) {
return m_ds.getStickAxis(m_port, axis);
return m_ds.getStickAxis(getPort(), axis);
}
/**
* For the current joystick, return the axis determined by the argument.
*
* This is for cases where the joystick axis is returned programatically, otherwise one of the
* <p>This is for cases where the joystick axis is returned programatically, otherwise one of the
* previous functions would be preferable (for example getX()).
*
* @param axis The axis to read.
* @return The value of the axis.
*/
public double getAxis(final AxisType axis) {
switch (axis.value) {
case AxisType.kX_val:
switch (axis) {
case kX:
return getX();
case AxisType.kY_val:
case kY:
return getY();
case AxisType.kZ_val:
case kZ:
return getZ();
case AxisType.kTwist_val:
case kTwist:
return getTwist();
case AxisType.kThrottle_val:
case kThrottle:
return getThrottle();
default:
return 0.0;
}
}
/**
* For the current joystick, return the number of axis.
*/
public int getAxisCount() {
return 0;
}
/**
* Read the state of the trigger on the joystick.
*
* Look up which button has been assigned to the trigger and read its state.
* <p>Look up which button has been assigned to the trigger and read its state.
*
* @param hand This parameter is ignored for the Joystick class and is only here to complete the
* GenericHID interface.
@@ -246,7 +203,7 @@ public class Joystick extends GenericHID {
/**
* Read the state of the top button on the joystick.
*
* Look up which button has been assigned to the top and read its state.
* <p>Look up which button has been assigned to the top and read its state.
*
* @param hand This parameter is ignored for the Joystick class and is only here to complete the
* GenericHID interface.
@@ -256,6 +213,16 @@ public class Joystick extends GenericHID {
return getRawButton(m_buttons[ButtonType.kTop.value]);
}
@Override
public int getPOV(int pov) {
return 0;
}
@Override
public int getPOVCount() {
return 0;
}
/**
* This is not supported for the Joystick. This method is only here to complete the GenericHID
* interface.
@@ -269,40 +236,37 @@ public class Joystick extends GenericHID {
}
/**
* Get the button value for buttons 1 through 12.
* Get the button value (starting at button 1).
*
* The buttons are returned in a single 16 bit value with one bit representing the state of each
* button. The appropriate button is returned as a boolean value.
* <p>The appropriate button is returned as a boolean value.
*
* @param button The button number to be read.
* @param button The button number to be read (starting at 1).
* @return The state of the button.
*/
public boolean getRawButton(final int button) {
return m_ds.getStickButton(m_port, button);
return m_ds.getStickButton(getPort(), (byte) button);
}
/**
* Get the state of a POV on the joystick.
*
* @return the angle of the POV in degrees, or -1 if the POV is not pressed.
* For the current joystick, return the number of buttons.
*/
public int getPOV(int pov) {
return 0; // TODO
public int getButtonCount() {
return 0;
}
/**
* Get buttons based on an enumerated type.
*
* The button type will be looked up in the list of buttons and then read.
* <p>The button type will be looked up in the list of buttons and then read.
*
* @param button The type of button to read.
* @return The state of the button.
*/
public boolean getButton(ButtonType button) {
switch (button.value) {
case ButtonType.kTrigger_val:
switch (button) {
case kTrigger:
return getTrigger();
case ButtonType.kTop_val:
case kTop:
return getTop();
default:
return false;
@@ -311,7 +275,7 @@ public class Joystick extends GenericHID {
/**
* Get the magnitude of the direction vector formed by the joystick's current position relative to
* its origin
* its origin.
*
* @return The magnitude of the direction vector
*/
@@ -320,7 +284,7 @@ public class Joystick extends GenericHID {
}
/**
* Get the direction of the vector formed by the joystick and its origin in radians
* Get the direction of the vector formed by the joystick and its origin in radians.
*
* @return The direction of the vector in radians
*/
@@ -329,9 +293,9 @@ public class Joystick extends GenericHID {
}
/**
* Get the direction of the vector formed by the joystick and its origin in degrees
* Get the direction of the vector formed by the joystick and its origin in degrees.
*
* uses acos(-1) to represent Pi due to absence of readily accessable Pi constant in C++
* <p>Uses acos(-1) to represent Pi due to absence of readily accessable Pi constant in C++
*
* @return The direction of the vector in degrees
*/
@@ -358,4 +322,66 @@ public class Joystick extends GenericHID {
public void setAxisChannel(AxisType axis, int channel) {
m_axes[axis.value] = (byte) channel;
}
/**
* Get the value of isXbox for the current joystick.
*
* @return A boolean that is true if the controller is an xbox controller.
*/
public boolean getIsXbox() {
return false;
}
/**
* Get the axis type of a joystick axis.
*
* @return the axis type of a joystick axis.
*/
public int getAxisType(int axis) {
return 0;
}
/**
* Get the type of the HID.
*
* @return the type of the HID.
*/
@Override
public HIDType getType() {
return HIDType.kUnknown;
}
/**
* Get the name of the HID.
*
* @return the name of the HID.
*/
@Override
public String getName() {
return "";
}
@Override
public void setOutput(int outputNumber, boolean value) {
m_outputs = (m_outputs & ~(1 << (outputNumber - 1))) | ((value ? 1 : 0) << (outputNumber - 1));
}
@Override
public void setOutputs(int value) {
m_outputs = value;
}
@Override
public void setRumble(RumbleType type, double value) {
if (value < 0) {
value = 0;
} else if (value > 1) {
value = 1;
}
if (type == RumbleType.kLeftRumble) {
m_leftRumble = (short) (value * 65535);
} else {
m_rightRumble = (short) (value * 65535);
}
}
}