blob: c2cdaea8e2685d420c1b9e0f240c7371eca0036c [file] [log] [blame]
Brian Silverman8fce7482020-01-05 13:18:21 -08001/*----------------------------------------------------------------------------*/
Austin Schuh1e69f942020-11-14 15:06:14 -08002/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */
Brian Silverman8fce7482020-01-05 13:18:21 -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
12#include <hal/FRCUsageReporting.h>
13#include <wpi/math>
14
Brian Silverman8fce7482020-01-05 13:18:21 -080015using namespace frc;
16
17Joystick::Joystick(int port) : GenericHID(port) {
18 m_axes[Axis::kX] = kDefaultXChannel;
19 m_axes[Axis::kY] = kDefaultYChannel;
20 m_axes[Axis::kZ] = kDefaultZChannel;
21 m_axes[Axis::kTwist] = kDefaultTwistChannel;
22 m_axes[Axis::kThrottle] = kDefaultThrottleChannel;
23
24 HAL_Report(HALUsageReporting::kResourceType_Joystick, port + 1);
25}
26
27void Joystick::SetXChannel(int channel) { m_axes[Axis::kX] = channel; }
28
29void Joystick::SetYChannel(int channel) { m_axes[Axis::kY] = channel; }
30
31void Joystick::SetZChannel(int channel) { m_axes[Axis::kZ] = channel; }
32
33void Joystick::SetTwistChannel(int channel) { m_axes[Axis::kTwist] = channel; }
34
35void Joystick::SetThrottleChannel(int channel) {
36 m_axes[Axis::kThrottle] = channel;
37}
38
39int Joystick::GetXChannel() const { return m_axes[Axis::kX]; }
40
41int Joystick::GetYChannel() const { return m_axes[Axis::kY]; }
42
43int Joystick::GetZChannel() const { return m_axes[Axis::kZ]; }
44
45int Joystick::GetTwistChannel() const { return m_axes[Axis::kTwist]; }
46
47int Joystick::GetThrottleChannel() const { return m_axes[Axis::kThrottle]; }
48
49double Joystick::GetX(JoystickHand hand) const {
50 return GetRawAxis(m_axes[Axis::kX]);
51}
52
53double Joystick::GetY(JoystickHand hand) const {
54 return GetRawAxis(m_axes[Axis::kY]);
55}
56
57double Joystick::GetZ() const { return GetRawAxis(m_axes[Axis::kZ]); }
58
59double Joystick::GetTwist() const { return GetRawAxis(m_axes[Axis::kTwist]); }
60
61double Joystick::GetThrottle() const {
62 return GetRawAxis(m_axes[Axis::kThrottle]);
63}
64
65bool Joystick::GetTrigger() const { return GetRawButton(Button::kTrigger); }
66
67bool Joystick::GetTriggerPressed() {
68 return GetRawButtonPressed(Button::kTrigger);
69}
70
71bool Joystick::GetTriggerReleased() {
72 return GetRawButtonReleased(Button::kTrigger);
73}
74
75bool Joystick::GetTop() const { return GetRawButton(Button::kTop); }
76
77bool Joystick::GetTopPressed() { return GetRawButtonPressed(Button::kTop); }
78
79bool Joystick::GetTopReleased() { return GetRawButtonReleased(Button::kTop); }
80
81double Joystick::GetMagnitude() const {
82 return std::sqrt(std::pow(GetX(), 2) + std::pow(GetY(), 2));
83}
84
85double Joystick::GetDirectionRadians() const {
86 return std::atan2(GetX(), -GetY());
87}
88
89double Joystick::GetDirectionDegrees() const {
90 return (180 / wpi::math::pi) * GetDirectionRadians();
91}