blob: 8d464cf9d2cb963bb3f2ab8866b83d368688c415 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08002/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
Brian Silverman41cdd3e2019-01-19 19:48:58 -08003/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "frc/Joystick.h"
9
10#include <cmath>
11
James Kuszmaul4b81d302019-12-14 20:53:14 -080012#include <hal/FRCUsageReporting.h>
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080013#include <wpi/math>
Brian Silverman41cdd3e2019-01-19 19:48:58 -080014
15#include "frc/DriverStation.h"
16#include "frc/WPIErrors.h"
17
18using namespace frc;
19
Brian Silverman41cdd3e2019-01-19 19:48:58 -080020Joystick::Joystick(int port) : GenericHID(port) {
21 m_axes[Axis::kX] = kDefaultXChannel;
22 m_axes[Axis::kY] = kDefaultYChannel;
23 m_axes[Axis::kZ] = kDefaultZChannel;
24 m_axes[Axis::kTwist] = kDefaultTwistChannel;
25 m_axes[Axis::kThrottle] = kDefaultThrottleChannel;
26
James Kuszmaul4b81d302019-12-14 20:53:14 -080027 HAL_Report(HALUsageReporting::kResourceType_Joystick, port + 1);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080028}
29
30void Joystick::SetXChannel(int channel) { m_axes[Axis::kX] = channel; }
31
32void Joystick::SetYChannel(int channel) { m_axes[Axis::kY] = channel; }
33
34void Joystick::SetZChannel(int channel) { m_axes[Axis::kZ] = channel; }
35
36void Joystick::SetTwistChannel(int channel) { m_axes[Axis::kTwist] = channel; }
37
38void Joystick::SetThrottleChannel(int channel) {
39 m_axes[Axis::kThrottle] = channel;
40}
41
Brian Silverman41cdd3e2019-01-19 19:48:58 -080042int Joystick::GetXChannel() const { return m_axes[Axis::kX]; }
43
44int Joystick::GetYChannel() const { return m_axes[Axis::kY]; }
45
46int Joystick::GetZChannel() const { return m_axes[Axis::kZ]; }
47
48int Joystick::GetTwistChannel() const { return m_axes[Axis::kTwist]; }
49
50int Joystick::GetThrottleChannel() const { return m_axes[Axis::kThrottle]; }
51
52double Joystick::GetX(JoystickHand hand) const {
53 return GetRawAxis(m_axes[Axis::kX]);
54}
55
56double Joystick::GetY(JoystickHand hand) const {
57 return GetRawAxis(m_axes[Axis::kY]);
58}
59
60double Joystick::GetZ() const { return GetRawAxis(m_axes[Axis::kZ]); }
61
62double Joystick::GetTwist() const { return GetRawAxis(m_axes[Axis::kTwist]); }
63
64double Joystick::GetThrottle() const {
65 return GetRawAxis(m_axes[Axis::kThrottle]);
66}
67
Brian Silverman41cdd3e2019-01-19 19:48:58 -080068bool Joystick::GetTrigger() const { return GetRawButton(Button::kTrigger); }
69
70bool Joystick::GetTriggerPressed() {
71 return GetRawButtonPressed(Button::kTrigger);
72}
73
74bool Joystick::GetTriggerReleased() {
75 return GetRawButtonReleased(Button::kTrigger);
76}
77
78bool Joystick::GetTop() const { return GetRawButton(Button::kTop); }
79
80bool Joystick::GetTopPressed() { return GetRawButtonPressed(Button::kTop); }
81
82bool Joystick::GetTopReleased() { return GetRawButtonReleased(Button::kTop); }
83
Brian Silverman41cdd3e2019-01-19 19:48:58 -080084double Joystick::GetMagnitude() const {
85 return std::sqrt(std::pow(GetX(), 2) + std::pow(GetY(), 2));
86}
87
88double Joystick::GetDirectionRadians() const {
89 return std::atan2(GetX(), -GetY());
90}
91
92double Joystick::GetDirectionDegrees() const {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080093 return (180 / wpi::math::pi) * GetDirectionRadians();
Brian Silverman41cdd3e2019-01-19 19:48:58 -080094}