2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
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>
|
|
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2022-06-14 08:48:16 +03:00
|
|
|
#include "frc/event/BooleanEvent.h"
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Joystick::SetXChannel(int channel) {
|
|
|
|
|
m_axes[Axis::kX] = channel;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Joystick::SetYChannel(int channel) {
|
|
|
|
|
m_axes[Axis::kY] = channel;
|
|
|
|
|
}
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Joystick::SetZChannel(int channel) {
|
|
|
|
|
m_axes[Axis::kZ] = channel;
|
|
|
|
|
}
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2020-12-28 12:58:06 -08: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
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int Joystick::GetXChannel() const {
|
|
|
|
|
return m_axes[Axis::kX];
|
|
|
|
|
}
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int Joystick::GetYChannel() const {
|
|
|
|
|
return m_axes[Axis::kY];
|
|
|
|
|
}
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int Joystick::GetZChannel() const {
|
|
|
|
|
return m_axes[Axis::kZ];
|
|
|
|
|
}
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int Joystick::GetTwistChannel() const {
|
|
|
|
|
return m_axes[Axis::kTwist];
|
|
|
|
|
}
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int Joystick::GetThrottleChannel() const {
|
|
|
|
|
return m_axes[Axis::kThrottle];
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2021-08-14 20:00:46 +03:00
|
|
|
double Joystick::GetX() const {
|
2018-01-26 17:26:10 -08:00
|
|
|
return GetRawAxis(m_axes[Axis::kX]);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-08-14 20:00:46 +03:00
|
|
|
double Joystick::GetY() const {
|
2018-01-26 17:26:10 -08:00
|
|
|
return GetRawAxis(m_axes[Axis::kY]);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
double Joystick::GetZ() const {
|
|
|
|
|
return GetRawAxis(m_axes[Axis::kZ]);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2020-12-28 12:58:06 -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
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08: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
|
|
|
}
|
|
|
|
|
|
2022-06-14 08:48:16 +03:00
|
|
|
BooleanEvent Joystick::Trigger(EventLoop* loop) const {
|
|
|
|
|
return BooleanEvent(loop, [this]() { return this->GetTrigger(); });
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool Joystick::GetTop() const {
|
|
|
|
|
return GetRawButton(Button::kTop);
|
|
|
|
|
}
|
2015-06-15 12:34:57 -04:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool Joystick::GetTopPressed() {
|
|
|
|
|
return GetRawButtonPressed(Button::kTop);
|
|
|
|
|
}
|
2014-12-05 20:13:23 -05:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool Joystick::GetTopReleased() {
|
|
|
|
|
return GetRawButtonReleased(Button::kTop);
|
|
|
|
|
}
|
2017-10-27 21:45:56 -07:00
|
|
|
|
2022-06-14 08:48:16 +03:00
|
|
|
BooleanEvent Joystick::Top(EventLoop* loop) const {
|
|
|
|
|
return BooleanEvent(loop, [this]() { return this->GetTop(); });
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double Joystick::GetMagnitude() const {
|
2022-12-14 01:29:09 -05:00
|
|
|
return std::hypot(GetX(), GetY());
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2023-05-26 00:43:52 -04:00
|
|
|
units::radian_t Joystick::GetDirection() const {
|
2025-01-19 20:34:07 -08:00
|
|
|
// https://docs.wpilib.org/en/stable/docs/software/basic-programming/coordinate-system.html#joystick-and-controller-coordinate-system
|
|
|
|
|
// A positive rotation around the X axis moves the joystick right, and a
|
|
|
|
|
// positive rotation around the Y axis moves the joystick backward. When
|
|
|
|
|
// treating them as translations, 0 radians is measured from the right
|
|
|
|
|
// direction, and angle increases clockwise.
|
|
|
|
|
//
|
|
|
|
|
// It's rotated 90 degrees CCW (y is negated and the arguments are reversed)
|
|
|
|
|
// so that 0 radians is forward.
|
2024-12-31 13:31:05 -08:00
|
|
|
return units::radian_t{std::atan2(GetX(), -GetY())};
|
2023-05-26 00:43:52 -04:00
|
|
|
}
|