RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
Joystick.java
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
5package org.wpilib.driverstation;
6
7import java.util.Objects;
8import org.wpilib.event.BooleanEvent;
9import org.wpilib.event.EventLoop;
10import org.wpilib.util.UsageReporting;
11
19public class Joystick implements HIDDevice {
21 public static final byte DEFAULT_X_CHANNEL = 0;
22
24 public static final byte DEFAULT_Y_CHANNEL = 1;
25
27 public static final byte DEFAULT_Z_CHANNEL = 2;
28
30 public static final byte DEFAULT_TWIST_CHANNEL = 2;
31
33 public static final byte DEFAULT_THROTTLE_CHANNEL = 3;
34
36 public enum AxisType {
38 X(0),
40 Y(1),
42 Z(2),
47
49 public final int value;
50
51 AxisType(int value) {
52 this.value = value;
53 }
54 }
55
57 public enum ButtonType {
61 TOP(2);
62
64 public final int value;
65
66 ButtonType(int value) {
67 this.value = value;
68 }
69 }
70
71 private final byte[] m_axes = new byte[AxisType.values().length];
72
73 private final GenericHID m_hid;
74
80 @Override
81 public GenericHID getHID() {
82 return m_hid;
83 }
84
90 public Joystick(final int port) {
91 this(DriverStation.getGenericHID(port));
92 }
93
99 public Joystick(final GenericHID hid) {
100 m_hid = Objects.requireNonNull(hid, "Provided HID object cannot be null");
101 m_axes[AxisType.X.value] = DEFAULT_X_CHANNEL;
102 m_axes[AxisType.Y.value] = DEFAULT_Y_CHANNEL;
103 m_axes[AxisType.Z.value] = DEFAULT_Z_CHANNEL;
104 m_axes[AxisType.TWIST.value] = DEFAULT_TWIST_CHANNEL;
106
107 UsageReporting.reportUsage("HID", hid.getPort(), "Joystick");
108 }
109
115 public void setXChannel(int channel) {
116 m_axes[AxisType.X.value] = (byte) channel;
117 }
118
124 public void setYChannel(int channel) {
125 m_axes[AxisType.Y.value] = (byte) channel;
126 }
127
133 public void setZChannel(int channel) {
134 m_axes[AxisType.Z.value] = (byte) channel;
135 }
136
142 public void setThrottleChannel(int channel) {
143 m_axes[AxisType.THROTTLE.value] = (byte) channel;
144 }
145
151 public void setTwistChannel(int channel) {
152 m_axes[AxisType.TWIST.value] = (byte) channel;
153 }
154
160 public int getXChannel() {
161 return m_axes[AxisType.X.value];
162 }
163
169 public int getYChannel() {
170 return m_axes[AxisType.Y.value];
171 }
172
178 public int getZChannel() {
179 return m_axes[AxisType.Z.value];
180 }
181
187 public int getTwistChannel() {
188 return m_axes[AxisType.TWIST.value];
189 }
190
196 public int getThrottleChannel() {
197 return m_axes[AxisType.THROTTLE.value];
198 }
199
209 public boolean getRawButton(int button) {
210 return m_hid.getRawButton(button);
211 }
212
219 public boolean getRawButtonPressed(int button) {
220 return m_hid.getRawButtonPressed(button);
221 }
222
229 public boolean getRawButtonReleased(int button) {
230 return m_hid.getRawButtonReleased(button);
231 }
232
239 public double getRawAxis(int axis) {
240 return m_hid.getRawAxis(axis);
241 }
242
252 public POVDirection getPOV(int pov) {
253 return m_hid.getPOV(pov);
254 }
255
265 return m_hid.getPOV();
266 }
267
274 public final double getX() {
275 return m_hid.getRawAxis(m_axes[AxisType.X.value]);
276 }
277
284 public final double getY() {
285 return m_hid.getRawAxis(m_axes[AxisType.Y.value]);
286 }
287
293 public final double getZ() {
294 return m_hid.getRawAxis(m_axes[AxisType.Z.value]);
295 }
296
303 public final double getTwist() {
304 return m_hid.getRawAxis(m_axes[AxisType.TWIST.value]);
305 }
306
313 public final double getThrottle() {
314 return m_hid.getRawAxis(m_axes[AxisType.THROTTLE.value]);
315 }
316
322 public boolean getTrigger() {
323 return m_hid.getRawButton(ButtonType.TRIGGER.value);
324 }
325
331 public boolean getTriggerPressed() {
332 return m_hid.getRawButtonPressed(ButtonType.TRIGGER.value);
333 }
334
340 public boolean getTriggerReleased() {
341 return m_hid.getRawButtonReleased(ButtonType.TRIGGER.value);
342 }
343
352 return m_hid.button(ButtonType.TRIGGER.value, loop);
353 }
354
360 public boolean getTop() {
361 return m_hid.getRawButton(ButtonType.TOP.value);
362 }
363
369 public boolean getTopPressed() {
370 return m_hid.getRawButtonPressed(ButtonType.TOP.value);
371 }
372
378 public boolean getTopReleased() {
379 return m_hid.getRawButtonReleased(ButtonType.TOP.value);
380 }
381
390 return m_hid.button(ButtonType.TOP.value, loop);
391 }
392
399 public double getMagnitude() {
400 return Math.hypot(getX(), getY());
401 }
402
409 public double getDirectionRadians() {
410 // https://docs.wpilib.org/en/stable/docs/software/basic-programming/coordinate-system.html#joystick-and-controller-coordinate-system
411 // A positive rotation around the X axis moves the joystick right, and a
412 // positive rotation around the Y axis moves the joystick backward. When
413 // treating them as translations, 0 radians is measured from the right
414 // direction, and angle increases clockwise.
415 //
416 // It's rotated 90 degrees CCW (y is negated and the arguments are reversed)
417 // so that 0 radians is forward.
418 return Math.atan2(getX(), -getY());
419 }
420
427 public double getDirectionDegrees() {
428 return Math.toDegrees(getDirectionRadians());
429 }
430}
static GenericHID getGenericHID(int port)
boolean getRawButtonReleased(int button)
BooleanEvent top(EventLoop loop)
boolean getRawButton(int button)
static final byte DEFAULT_X_CHANNEL
Definition Joystick.java:21
BooleanEvent trigger(EventLoop loop)
static final byte DEFAULT_Y_CHANNEL
Definition Joystick.java:24
Joystick(final GenericHID hid)
Definition Joystick.java:99
static final byte DEFAULT_Z_CHANNEL
Definition Joystick.java:27
void setThrottleChannel(int channel)
POVDirection getPOV(int pov)
static final byte DEFAULT_THROTTLE_CHANNEL
Definition Joystick.java:33
static final byte DEFAULT_TWIST_CHANNEL
Definition Joystick.java:30
void setTwistChannel(int channel)
boolean getRawButtonPressed(int button)
static void reportUsage(String resource, String data)