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