2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-06-10 22:03:15 -07:00
|
|
|
/* Copyright (c) 2008-2019 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
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Joystick.h"
|
2016-09-14 20:52:06 -07:00
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/HAL.h>
|
2019-07-31 22:15:22 -07:00
|
|
|
#include <wpi/math>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/DriverStation.h"
|
|
|
|
|
#include "frc/WPIErrors.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2017-10-29 17:21:50 -07:00
|
|
|
Joystick::Joystick(int port) : GenericHID(port) {
|
2018-05-16 19:53:16 -07:00
|
|
|
m_axes[Axis::kX] = kDefaultXChannel;
|
|
|
|
|
m_axes[Axis::kY] = kDefaultYChannel;
|
|
|
|
|
m_axes[Axis::kZ] = kDefaultZChannel;
|
|
|
|
|
m_axes[Axis::kTwist] = kDefaultTwistChannel;
|
|
|
|
|
m_axes[Axis::kThrottle] = kDefaultThrottleChannel;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Joystick, port + 1);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-29 17:21:50 -07:00
|
|
|
void Joystick::SetXChannel(int channel) { m_axes[Axis::kX] = channel; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-10-29 17:21:50 -07:00
|
|
|
void Joystick::SetYChannel(int channel) { m_axes[Axis::kY] = channel; }
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2017-10-29 17:21:50 -07:00
|
|
|
void Joystick::SetZChannel(int channel) { m_axes[Axis::kZ] = channel; }
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2017-10-29 17:21:50 -07:00
|
|
|
void Joystick::SetTwistChannel(int channel) { m_axes[Axis::kTwist] = channel; }
|
2017-10-27 21:45:56 -07:00
|
|
|
|
|
|
|
|
void Joystick::SetThrottleChannel(int channel) {
|
2017-10-29 17:21:50 -07:00
|
|
|
m_axes[Axis::kThrottle] = channel;
|
2017-10-27 21:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-29 17:21:50 -07:00
|
|
|
int Joystick::GetXChannel() const { return m_axes[Axis::kX]; }
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2017-10-29 17:21:50 -07:00
|
|
|
int Joystick::GetYChannel() const { return m_axes[Axis::kY]; }
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2017-10-29 17:21:50 -07:00
|
|
|
int Joystick::GetZChannel() const { return m_axes[Axis::kZ]; }
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2017-10-29 17:21:50 -07:00
|
|
|
int Joystick::GetTwistChannel() const { return m_axes[Axis::kTwist]; }
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2017-10-29 17:21:50 -07:00
|
|
|
int Joystick::GetThrottleChannel() const { return m_axes[Axis::kThrottle]; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double Joystick::GetX(JoystickHand hand) const {
|
2018-01-26 17:26:10 -08:00
|
|
|
return GetRawAxis(m_axes[Axis::kX]);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double Joystick::GetY(JoystickHand hand) const {
|
2018-01-26 17:26:10 -08:00
|
|
|
return GetRawAxis(m_axes[Axis::kY]);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-01-26 17:26:10 -08:00
|
|
|
double Joystick::GetZ() const { return GetRawAxis(m_axes[Axis::kZ]); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-01-26 17:26:10 -08:00
|
|
|
double Joystick::GetTwist() const { return GetRawAxis(m_axes[Axis::kTwist]); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double Joystick::GetThrottle() const {
|
2018-01-26 17:26:10 -08:00
|
|
|
return GetRawAxis(m_axes[Axis::kThrottle]);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-29 17:21:50 -07:00
|
|
|
bool Joystick::GetTrigger() const { return GetRawButton(Button::kTrigger); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
bool Joystick::GetTriggerPressed() {
|
2017-10-29 17:21:50 -07:00
|
|
|
return GetRawButtonPressed(Button::kTrigger);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
bool Joystick::GetTriggerReleased() {
|
2017-10-29 17:21:50 -07:00
|
|
|
return GetRawButtonReleased(Button::kTrigger);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-29 17:21:50 -07:00
|
|
|
bool Joystick::GetTop() const { return GetRawButton(Button::kTop); }
|
2015-06-15 12:34:57 -04:00
|
|
|
|
2017-10-29 17:21:50 -07:00
|
|
|
bool Joystick::GetTopPressed() { return GetRawButtonPressed(Button::kTop); }
|
2014-12-05 20:13:23 -05:00
|
|
|
|
2017-10-29 17:21:50 -07:00
|
|
|
bool Joystick::GetTopReleased() { return GetRawButtonReleased(Button::kTop); }
|
2017-10-27 21:45:56 -07:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double Joystick::GetDirectionDegrees() const {
|
2019-07-31 22:15:22 -07:00
|
|
|
return (180 / wpi::math::pi) * GetDirectionRadians();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|