2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-23 22:06:13 -07:00
|
|
|
/* Copyright (c) 2008-2017 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Joystick.h"
|
2016-09-14 20:52:06 -07:00
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <array>
|
2016-09-14 20:52:06 -07:00
|
|
|
#include <cmath>
|
|
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <HAL/HAL.h>
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "DriverStation.h"
|
|
|
|
|
#include "WPIErrors.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
constexpr int Joystick::kMinNumAxes;
|
|
|
|
|
|
|
|
|
|
constexpr double kPi = 3.14159265358979323846;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct an instance of a joystick.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2016-11-18 23:05:37 -08:00
|
|
|
* The joystick index is the USB port on the Driver Station.
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2016-11-18 23:05:37 -08:00
|
|
|
* @param port The port on the Driver Station that the joystick is plugged into
|
2016-05-20 17:30:37 -07:00
|
|
|
* (0-5).
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
Joystick::Joystick(int port)
|
|
|
|
|
: GenericHID(port), m_axes(std::max(GetAxisCount(), kMinNumAxes)) {
|
|
|
|
|
m_axes[static_cast<int>(Axis::kX)] = kDefaultXAxis;
|
|
|
|
|
m_axes[static_cast<int>(Axis::kY)] = kDefaultYAxis;
|
|
|
|
|
m_axes[static_cast<int>(Axis::kZ)] = kDefaultZAxis;
|
|
|
|
|
m_axes[static_cast<int>(Axis::kTwist)] = kDefaultTwistAxis;
|
|
|
|
|
m_axes[static_cast<int>(Axis::kThrottle)] = kDefaultThrottleAxis;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Joystick, port);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-27 21:45:56 -07:00
|
|
|
* Set the channel associated with a specified axis.
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* @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) {
|
|
|
|
|
m_axes[axis] = channel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the channel associated with the X axis.
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* @param channel The channel to set the axis to.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
void Joystick::SetXChannel(int channel) {
|
|
|
|
|
m_axes[static_cast<int>(Axis::kX)] = channel;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
/**
|
|
|
|
|
* Set the channel associated with the Y axis.
|
|
|
|
|
*
|
|
|
|
|
* @param axis The axis to set the channel for.
|
|
|
|
|
* @param channel The channel to set the axis to.
|
|
|
|
|
*/
|
|
|
|
|
void Joystick::SetYChannel(int channel) {
|
|
|
|
|
m_axes[static_cast<int>(Axis::kY)] = channel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the channel associated with the Z axis.
|
|
|
|
|
*
|
|
|
|
|
* @param axis The axis to set the channel for.
|
|
|
|
|
* @param channel The channel to set the axis to.
|
|
|
|
|
*/
|
|
|
|
|
void Joystick::SetZChannel(int channel) {
|
|
|
|
|
m_axes[static_cast<int>(Axis::kZ)] = channel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the channel associated with the twist axis.
|
|
|
|
|
*
|
|
|
|
|
* @param axis The axis to set the channel for.
|
|
|
|
|
* @param channel The channel to set the axis to.
|
|
|
|
|
*/
|
|
|
|
|
void Joystick::SetTwistChannel(int channel) {
|
|
|
|
|
m_axes[static_cast<int>(Axis::kTwist)] = channel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the channel associated with the throttle axis.
|
|
|
|
|
*
|
|
|
|
|
* @param axis The axis to set the channel for.
|
|
|
|
|
* @param channel The channel to set the axis to.
|
|
|
|
|
*/
|
|
|
|
|
void Joystick::SetThrottleChannel(int channel) {
|
|
|
|
|
m_axes[static_cast<int>(Axis::kThrottle)] = channel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the channel currently associated with the X axis.
|
|
|
|
|
*
|
|
|
|
|
* @return The channel for the axis.
|
|
|
|
|
*/
|
|
|
|
|
int Joystick::GetXChannel() const { return m_axes[static_cast<int>(Axis::kX)]; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the channel currently associated with the Y axis.
|
|
|
|
|
*
|
|
|
|
|
* @return The channel for the axis.
|
|
|
|
|
*/
|
|
|
|
|
int Joystick::GetYChannel() const { return m_axes[static_cast<int>(Axis::kY)]; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the channel currently associated with the Z axis.
|
|
|
|
|
*
|
|
|
|
|
* @return The channel for the axis.
|
|
|
|
|
*/
|
|
|
|
|
int Joystick::GetZChannel() const { return m_axes[static_cast<int>(Axis::kZ)]; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the channel currently associated with the twist axis.
|
|
|
|
|
*
|
|
|
|
|
* @return The channel for the axis.
|
|
|
|
|
*/
|
|
|
|
|
int Joystick::GetTwistChannel() const {
|
|
|
|
|
return m_axes[static_cast<int>(Axis::kTwist)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the channel currently associated with the throttle axis.
|
|
|
|
|
*
|
|
|
|
|
* @return The channel for the axis.
|
|
|
|
|
*/
|
|
|
|
|
int Joystick::GetThrottleChannel() const {
|
|
|
|
|
return m_axes[static_cast<int>(Axis::kThrottle)];
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the X value of the joystick.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* This depends on the mapping of the joystick connected to the current port.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
|
|
|
|
* @param hand This parameter is ignored for the Joystick class and is only
|
|
|
|
|
* here to complete the GenericHID interface.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double Joystick::GetX(JoystickHand hand) const {
|
2017-10-27 21:45:56 -07:00
|
|
|
return GetRawAxis(m_axes[kDefaultXAxis]);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the Y value of the joystick.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* This depends on the mapping of the joystick connected to the current port.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
|
|
|
|
* @param hand This parameter is ignored for the Joystick class and is only
|
|
|
|
|
* here to complete the GenericHID interface.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double Joystick::GetY(JoystickHand hand) const {
|
2017-10-27 21:45:56 -07:00
|
|
|
return GetRawAxis(m_axes[kDefaultYAxis]);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the Z value of the current joystick.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* This depends on the mapping of the joystick connected to the current port.
|
|
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
double Joystick::GetZ() const { return GetRawAxis(m_axes[kDefaultZAxis]); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the twist value of the current joystick.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* This depends on the mapping of the joystick connected to the current port.
|
|
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
double Joystick::GetTwist() const {
|
|
|
|
|
return GetRawAxis(m_axes[kDefaultTwistAxis]);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the throttle value of the current joystick.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* This depends on the mapping of the joystick connected to the current port.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double Joystick::GetThrottle() const {
|
2017-10-27 21:45:56 -07:00
|
|
|
return GetRawAxis(m_axes[kDefaultThrottleAxis]);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* For the current joystick, return the axis determined by the argument.
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* This is for cases where the joystick axis is returned programatically,
|
2016-05-20 17:30:37 -07:00
|
|
|
* otherwise one of the previous functions would be preferable (for example
|
|
|
|
|
* GetX()).
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param axis The axis to read.
|
|
|
|
|
* @return The value of the axis.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double Joystick::GetAxis(AxisType axis) const {
|
2015-06-25 15:07:55 -04:00
|
|
|
switch (axis) {
|
|
|
|
|
case kXAxis:
|
2017-10-27 21:45:56 -07:00
|
|
|
return GetX();
|
2015-06-25 15:07:55 -04:00
|
|
|
case kYAxis:
|
2017-10-27 21:45:56 -07:00
|
|
|
return GetY();
|
2015-06-25 15:07:55 -04:00
|
|
|
case kZAxis:
|
2017-10-27 21:45:56 -07:00
|
|
|
return GetZ();
|
2015-06-25 15:07:55 -04:00
|
|
|
case kTwistAxis:
|
2017-10-27 21:45:56 -07:00
|
|
|
return GetTwist();
|
2015-06-25 15:07:55 -04:00
|
|
|
case kThrottleAxis:
|
2017-10-27 21:45:56 -07:00
|
|
|
return GetThrottle();
|
2015-06-25 15:07:55 -04:00
|
|
|
default:
|
|
|
|
|
wpi_setWPIError(BadJoystickAxis);
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the state of the trigger on the joystick.
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* Look up which button has been assigned to the trigger and read its state.
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return The state of the trigger.
|
|
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
bool Joystick::GetTrigger() const {
|
|
|
|
|
return GetRawButton(static_cast<int>(Button::kTrigger));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-27 21:45:56 -07:00
|
|
|
* Whether the trigger was pressed since the last check.
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* @return Whether the button was pressed since the last check.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
bool Joystick::GetTriggerPressed() {
|
|
|
|
|
return GetRawButtonPressed(static_cast<int>(Button::kTrigger));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-27 21:45:56 -07:00
|
|
|
* Whether the trigger was released since the last check.
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* @return Whether the button was released since the last check.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
bool Joystick::GetTriggerReleased() {
|
|
|
|
|
return GetRawButtonReleased(static_cast<int>(Button::kTrigger));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-05 20:13:23 -05:00
|
|
|
/**
|
2017-10-27 21:45:56 -07:00
|
|
|
* Read the state of the top button on the joystick.
|
2014-12-05 20:13:23 -05:00
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* Look up which button has been assigned to the top and read its state.
|
2016-08-07 08:19:19 -07:00
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* @return The state of the top button.
|
2016-08-07 08:19:19 -07:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
bool Joystick::GetTop() const {
|
|
|
|
|
return GetRawButton(static_cast<int>(Button::kTop));
|
2016-08-07 08:19:19 -07:00
|
|
|
}
|
2015-06-15 12:34:57 -04:00
|
|
|
|
2014-12-05 20:13:23 -05:00
|
|
|
/**
|
2017-10-27 21:45:56 -07:00
|
|
|
* Whether the top button was pressed since the last check.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* @return Whether the button was pressed since the last check.
|
2014-12-05 20:13:23 -05:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
bool Joystick::GetTopPressed() {
|
|
|
|
|
return GetRawButtonPressed(static_cast<int>(Button::kTop));
|
2014-12-05 20:13:23 -05:00
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2017-10-27 21:45:56 -07:00
|
|
|
* Whether the top button was released since the last check.
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* @return Whether the button was released since the last check.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
bool Joystick::GetTopReleased() {
|
|
|
|
|
return GetRawButtonReleased(static_cast<int>(Button::kTop));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Joystick* Joystick::GetStickForPort(int port) {
|
|
|
|
|
static std::array<std::unique_ptr<Joystick>, DriverStation::kJoystickPorts>
|
|
|
|
|
joysticks{};
|
|
|
|
|
auto stick = joysticks[port].get();
|
|
|
|
|
if (stick == nullptr) {
|
|
|
|
|
joysticks[port] = std::make_unique<Joystick>(port);
|
|
|
|
|
stick = joysticks[port].get();
|
|
|
|
|
}
|
|
|
|
|
return stick;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
2017-10-27 21:45:56 -07:00
|
|
|
* Get buttons based on an enumerated type.
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* 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.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
bool Joystick::GetButton(ButtonType button) const {
|
|
|
|
|
int temp = button;
|
|
|
|
|
return GetRawButton(static_cast<int>(static_cast<Button>(temp)));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the magnitude of the direction vector formed by the joystick's
|
2016-05-20 17:30:37 -07:00
|
|
|
* current position relative to its origin.
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return The magnitude of the direction vector
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double Joystick::GetMagnitude() const {
|
2016-09-14 20:52:06 -07:00
|
|
|
return std::sqrt(std::pow(GetX(), 2) + std::pow(GetY(), 2));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the direction of the vector formed by the joystick and its origin
|
2016-05-20 17:30:37 -07:00
|
|
|
* in radians.
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return The direction of the vector in radians
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double Joystick::GetDirectionRadians() const {
|
2016-09-14 20:52:06 -07:00
|
|
|
return std::atan2(GetX(), -GetY());
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the direction of the vector formed by the joystick and its origin
|
2016-05-20 17:30:37 -07:00
|
|
|
* in degrees.
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2016-09-14 20:52:06 -07:00
|
|
|
* uses std::acos(-1) to represent Pi due to absence of readily accessible Pi
|
2013-12-15 18:30:16 -05:00
|
|
|
* constant in C++
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return The direction of the vector in degrees
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double Joystick::GetDirectionDegrees() const {
|
2017-10-27 21:45:56 -07:00
|
|
|
return (180 / kPi) * GetDirectionRadians();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|