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