WPILibC++ 2024.1.1-beta-4
Joystick.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <array>
8
9#include <units/angle.h>
10
11#include "frc/GenericHID.h"
12
13namespace frc {
14
15/**
16 * Handle input from standard Joysticks connected to the Driver Station.
17 *
18 * This class handles standard input that comes from the Driver Station. Each
19 * time a value is requested the most recent value is returned. There is a
20 * single class instance for each joystick and the mapping of ports to hardware
21 * buttons depends on the code in the Driver Station.
22 */
23class Joystick : public GenericHID {
24 public:
25 static constexpr int kDefaultXChannel = 0;
26 static constexpr int kDefaultYChannel = 1;
27 static constexpr int kDefaultZChannel = 2;
28 static constexpr int kDefaultTwistChannel = 2;
29 static constexpr int kDefaultThrottleChannel = 3;
30
33
34 /**
35 * Construct an instance of a joystick.
36 *
37 * The joystick index is the USB port on the Driver Station.
38 *
39 * @param port The port on the Driver Station that the joystick is plugged
40 * into (0-5).
41 */
42 explicit Joystick(int port);
43
44 ~Joystick() override = default;
45
46 Joystick(Joystick&&) = default;
48
49 /**
50 * Set the channel associated with the X axis.
51 *
52 * @param channel The channel to set the axis to.
53 */
54 void SetXChannel(int channel);
55
56 /**
57 * Set the channel associated with the Y axis.
58 *
59 * @param channel The channel to set the axis to.
60 */
61 void SetYChannel(int channel);
62
63 /**
64 * Set the channel associated with the Z axis.
65 *
66 * @param channel The channel to set the axis to.
67 */
68 void SetZChannel(int channel);
69
70 /**
71 * Set the channel associated with the twist axis.
72 *
73 * @param channel The channel to set the axis to.
74 */
75 void SetTwistChannel(int channel);
76
77 /**
78 * Set the channel associated with the throttle axis.
79 *
80 * @param channel The channel to set the axis to.
81 */
82 void SetThrottleChannel(int channel);
83
84 /**
85 * Get the channel currently associated with the X axis.
86 *
87 * @return The channel for the axis.
88 */
89 int GetXChannel() const;
90
91 /**
92 * Get the channel currently associated with the Y axis.
93 *
94 * @return The channel for the axis.
95 */
96 int GetYChannel() const;
97
98 /**
99 * Get the channel currently associated with the Z axis.
100 *
101 * @return The channel for the axis.
102 */
103 int GetZChannel() const;
104
105 /**
106 * Get the channel currently associated with the twist axis.
107 *
108 * @return The channel for the axis.
109 */
110 int GetTwistChannel() const;
111
112 /**
113 * Get the channel currently associated with the throttle axis.
114 *
115 * @return The channel for the axis.
116 */
118
119 /**
120 * Get the X value of the current joystick.
121 *
122 * This depends on the mapping of the joystick connected to the current port.
123 */
124 double GetX() const;
125
126 /**
127 * Get the Y value of the current joystick.
128 *
129 * This depends on the mapping of the joystick connected to the current port.
130 */
131 double GetY() const;
132
133 /**
134 * Get the Z value of the current joystick.
135 *
136 * This depends on the mapping of the joystick connected to the current port.
137 */
138 double GetZ() const;
139
140 /**
141 * Get the twist value of the current joystick.
142 *
143 * This depends on the mapping of the joystick connected to the current port.
144 */
145 double GetTwist() const;
146
147 /**
148 * Get the throttle value of the current joystick.
149 *
150 * This depends on the mapping of the joystick connected to the current port.
151 */
152 double GetThrottle() const;
153
154 /**
155 * Read the state of the trigger on the joystick.
156 *
157 * Look up which button has been assigned to the trigger and read its state.
158 *
159 * @return The state of the trigger.
160 */
161 bool GetTrigger() const;
162
163 /**
164 * Whether the trigger was pressed since the last check.
165 *
166 * @return Whether the button was pressed since the last check.
167 */
169
170 /**
171 * Whether the trigger was released since the last check.
172 *
173 * @return Whether the button was released since the last check.
174 */
176
177 /**
178 * Constructs an event instance around the trigger button's digital signal.
179 *
180 * @param loop the event loop instance to attach the event to.
181 * @return an event instance representing the trigger button's digital signal
182 * attached to the given loop.
183 */
185
186 /**
187 * Read the state of the top button on the joystick.
188 *
189 * Look up which button has been assigned to the top and read its state.
190 *
191 * @return The state of the top button.
192 */
193 bool GetTop() const;
194
195 /**
196 * Whether the top button was pressed since the last check.
197 *
198 * @return Whether the button was pressed since the last check.
199 */
201
202 /**
203 * Whether the top button was released since the last check.
204 *
205 * @return Whether the button was released since the last check.
206 */
208
209 /**
210 * Constructs an event instance around the top button's digital signal.
211 *
212 * @param loop the event loop instance to attach the event to.
213 * @return an event instance representing the top button's digital signal
214 * attached to the given loop.
215 */
217
218 /**
219 * Get the magnitude of the direction vector formed by the joystick's
220 * current position relative to its origin.
221 *
222 * @return The magnitude of the direction vector
223 */
224 double GetMagnitude() const;
225
226 /**
227 * Get the direction of the vector formed by the joystick and its origin
228 * in radians.
229 *
230 * @return The direction of the vector in radians
231 * @deprecated Use GetDirection() instead.
232 */
233 [[deprecated("Use GetDirection() instead.")]]
234 double GetDirectionRadians() const;
235
236 /**
237 * Get the direction of the vector formed by the joystick and its origin
238 * in degrees.
239 *
240 * @return The direction of the vector in degrees
241 * @deprecated Use GetDirection() instead.
242 */
243 [[deprecated("Use GetDirection() instead.")]]
244 double GetDirectionDegrees() const;
245
246 /**
247 * Get the direction of the vector formed by the joystick and its origin.
248 *
249 * @return The direction of the vector.
250 */
251 units::radian_t GetDirection() const;
252
253 private:
254 enum Axis { kX, kY, kZ, kTwist, kThrottle, kNumAxes };
255 enum Button { kTrigger = 1, kTop = 2 };
256
257 std::array<int, Axis::kNumAxes> m_axes;
258};
259
260} // namespace frc
This class provides an easy way to link actions to inputs.
Definition: BooleanEvent.h:31
A declarative way to bind a set of actions to a loop and execute them when the loop is polled.
Definition: EventLoop.h:15
Handle input from standard HID devices connected to the Driver Station.
Definition: GenericHID.h:24
Handle input from standard Joysticks connected to the Driver Station.
Definition: Joystick.h:23
int GetYChannel() const
Get the channel currently associated with the Y axis.
double GetDirectionDegrees() const
Get the direction of the vector formed by the joystick and its origin in degrees.
void SetTwistChannel(int channel)
Set the channel associated with the twist axis.
bool GetTrigger() const
Read the state of the trigger on the joystick.
double GetTwist() const
Get the twist value of the current joystick.
int GetTwistChannel() const
Get the channel currently associated with the twist axis.
ButtonType
Definition: Joystick.h:32
@ kTopButton
Definition: Joystick.h:32
@ kTriggerButton
Definition: Joystick.h:32
bool GetTriggerReleased()
Whether the trigger was released since the last check.
static constexpr int kDefaultThrottleChannel
Definition: Joystick.h:29
bool GetTopPressed()
Whether the top button was pressed since the last check.
void SetThrottleChannel(int channel)
Set the channel associated with the throttle axis.
bool GetTriggerPressed()
Whether the trigger was pressed since the last check.
BooleanEvent Top(EventLoop *loop) const
Constructs an event instance around the top button's digital signal.
~Joystick() override=default
int GetZChannel() const
Get the channel currently associated with the Z axis.
static constexpr int kDefaultXChannel
Definition: Joystick.h:25
double GetX() const
Get the X value of the current joystick.
Joystick(int port)
Construct an instance of a joystick.
int GetXChannel() const
Get the channel currently associated with the X axis.
double GetThrottle() const
Get the throttle value of the current joystick.
double GetZ() const
Get the Z value of the current joystick.
bool GetTop() const
Read the state of the top button on the joystick.
bool GetTopReleased()
Whether the top button was released since the last check.
static constexpr int kDefaultTwistChannel
Definition: Joystick.h:28
static constexpr int kDefaultYChannel
Definition: Joystick.h:26
Joystick & operator=(Joystick &&)=default
void SetYChannel(int channel)
Set the channel associated with the Y axis.
double GetDirectionRadians() const
Get the direction of the vector formed by the joystick and its origin in radians.
double GetY() const
Get the Y value of the current joystick.
Joystick(Joystick &&)=default
int GetThrottleChannel() const
Get the channel currently associated with the throttle axis.
BooleanEvent Trigger(EventLoop *loop) const
Constructs an event instance around the trigger button's digital signal.
void SetZChannel(int channel)
Set the channel associated with the Z axis.
AxisType
Definition: Joystick.h:31
@ kThrottleAxis
Definition: Joystick.h:31
@ kTwistAxis
Definition: Joystick.h:31
@ kXAxis
Definition: Joystick.h:31
@ kYAxis
Definition: Joystick.h:31
@ kZAxis
Definition: Joystick.h:31
static constexpr int kDefaultZChannel
Definition: Joystick.h:27
double GetMagnitude() const
Get the magnitude of the direction vector formed by the joystick's current position relative to its o...
units::radian_t GetDirection() const
Get the direction of the vector formed by the joystick and its origin.
void SetXChannel(int channel)
Set the channel associated with the X axis.
Definition: AprilTagPoseEstimator.h:15