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