mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
SCRIPT Move java files
This commit is contained in:
committed by
Peter Johnson
parent
7ca1be9bae
commit
c350c5f112
@@ -0,0 +1,126 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import edu.wpi.first.hal.ControlWord;
|
||||
|
||||
/** A wrapper around Driver Station control word. */
|
||||
public class DSControlWord {
|
||||
private final ControlWord m_controlWord = new ControlWord();
|
||||
|
||||
/**
|
||||
* DSControlWord constructor.
|
||||
*
|
||||
* <p>Upon construction, the current Driver Station control word is read and stored internally.
|
||||
*/
|
||||
public DSControlWord() {
|
||||
refresh();
|
||||
}
|
||||
|
||||
/** Update internal Driver Station control word. */
|
||||
public final void refresh() {
|
||||
DriverStation.refreshControlWordFromCache(m_controlWord);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be enabled.
|
||||
*
|
||||
* @return True if the robot is enabled, false otherwise.
|
||||
*/
|
||||
public boolean isEnabled() {
|
||||
return m_controlWord.getEnabled() && m_controlWord.getDSAttached();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be disabled.
|
||||
*
|
||||
* @return True if the robot should be disabled, false otherwise.
|
||||
*/
|
||||
public boolean isDisabled() {
|
||||
return !isEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the Robot is e-stopped.
|
||||
*
|
||||
* @return True if the robot is e-stopped, false otherwise.
|
||||
*/
|
||||
public boolean isEStopped() {
|
||||
return m_controlWord.getEStop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be running in
|
||||
* autonomous mode.
|
||||
*
|
||||
* @return True if autonomous mode should be enabled, false otherwise.
|
||||
*/
|
||||
public boolean isAutonomous() {
|
||||
return m_controlWord.getAutonomous();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be running in
|
||||
* autonomous mode and enabled.
|
||||
*
|
||||
* @return True if autonomous should be set and the robot should be enabled.
|
||||
*/
|
||||
public boolean isAutonomousEnabled() {
|
||||
return m_controlWord.getAutonomous()
|
||||
&& m_controlWord.getEnabled()
|
||||
&& m_controlWord.getDSAttached();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be running in
|
||||
* operator-controlled mode.
|
||||
*
|
||||
* @return True if operator-controlled mode should be enabled, false otherwise.
|
||||
*/
|
||||
public boolean isTeleop() {
|
||||
return !(isAutonomous() || isTest());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be running in
|
||||
* operator-controller mode and enabled.
|
||||
*
|
||||
* @return True if operator-controlled mode should be set and the robot should be enabled.
|
||||
*/
|
||||
public boolean isTeleopEnabled() {
|
||||
return !m_controlWord.getAutonomous()
|
||||
&& !m_controlWord.getTest()
|
||||
&& m_controlWord.getEnabled()
|
||||
&& m_controlWord.getDSAttached();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the Driver Station requires the robot to be running in test
|
||||
* mode.
|
||||
*
|
||||
* @return True if test mode should be enabled, false otherwise.
|
||||
*/
|
||||
public boolean isTest() {
|
||||
return m_controlWord.getTest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value indicating whether the Driver Station is attached.
|
||||
*
|
||||
* @return True if Driver Station is attached, false otherwise.
|
||||
*/
|
||||
public boolean isDSAttached() {
|
||||
return m_controlWord.getDSAttached();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets if the driver station attached to a Field Management System.
|
||||
*
|
||||
* @return true if the robot is competing on a field being controlled by a Field Management System
|
||||
*/
|
||||
public boolean isFMSAttached() {
|
||||
return m_controlWord.getFMSAttached();
|
||||
}
|
||||
}
|
||||
1510
wpilibj/src/main/java/org/wpilib/driverstation/DriverStation.java
Normal file
1510
wpilibj/src/main/java/org/wpilib/driverstation/DriverStation.java
Normal file
File diff suppressed because it is too large
Load Diff
1311
wpilibj/src/main/java/org/wpilib/driverstation/Gamepad.java
Normal file
1311
wpilibj/src/main/java/org/wpilib/driverstation/Gamepad.java
Normal file
File diff suppressed because it is too large
Load Diff
500
wpilibj/src/main/java/org/wpilib/driverstation/GenericHID.java
Normal file
500
wpilibj/src/main/java/org/wpilib/driverstation/GenericHID.java
Normal file
@@ -0,0 +1,500 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import edu.wpi.first.hal.DriverStationJNI;
|
||||
import edu.wpi.first.math.Pair;
|
||||
import edu.wpi.first.wpilibj.DriverStation.POVDirection;
|
||||
import edu.wpi.first.wpilibj.event.BooleanEvent;
|
||||
import edu.wpi.first.wpilibj.event.EventLoop;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Handle input from standard HID devices connected to the Driver Station.
|
||||
*
|
||||
* <p>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 device and
|
||||
* the mapping of ports to hardware buttons depends on the code in the Driver Station.
|
||||
*/
|
||||
public class GenericHID {
|
||||
/** Represents a rumble output on the Joystick. */
|
||||
public enum RumbleType {
|
||||
/** Left rumble motor. */
|
||||
kLeftRumble,
|
||||
/** Right rumble motor. */
|
||||
kRightRumble,
|
||||
/** Both left and right rumble motors. */
|
||||
kBothRumble
|
||||
}
|
||||
|
||||
/** USB HID interface type. */
|
||||
public enum HIDType {
|
||||
/** Unknown. */
|
||||
kUnknown(-1),
|
||||
/** XInputUnknown. */
|
||||
kXInputUnknown(0),
|
||||
/** XInputGamepad. */
|
||||
kXInputGamepad(1),
|
||||
/** XInputWheel. */
|
||||
kXInputWheel(2),
|
||||
/** XInputArcadeStick. */
|
||||
kXInputArcadeStick(3),
|
||||
/** XInputFlightStick. */
|
||||
kXInputFlightStick(4),
|
||||
/** XInputDancePad. */
|
||||
kXInputDancePad(5),
|
||||
/** XInputGuitar. */
|
||||
kXInputGuitar(6),
|
||||
/** XInputGuitar2. */
|
||||
kXInputGuitar2(7),
|
||||
/** XInputDrumKit. */
|
||||
kXInputDrumKit(8),
|
||||
/** XInputGuitar3. */
|
||||
kXInputGuitar3(11),
|
||||
/** XInputArcadePad. */
|
||||
kXInputArcadePad(19),
|
||||
/** HIDJoystick. */
|
||||
kHIDJoystick(20),
|
||||
/** HIDGamepad. */
|
||||
kHIDGamepad(21),
|
||||
/** HIDDriving. */
|
||||
kHIDDriving(22),
|
||||
/** HIDFlight. */
|
||||
kHIDFlight(23),
|
||||
/** HID1stPerson. */
|
||||
kHID1stPerson(24);
|
||||
|
||||
/** HIDType value. */
|
||||
public final int value;
|
||||
|
||||
private static final Map<Integer, HIDType> map = new HashMap<>();
|
||||
|
||||
HIDType(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
static {
|
||||
for (HIDType hidType : HIDType.values()) {
|
||||
map.put(hidType.value, hidType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an HIDType with the given value.
|
||||
*
|
||||
* @param value HIDType's value.
|
||||
* @return HIDType with the given value.
|
||||
*/
|
||||
public static HIDType of(int value) {
|
||||
return map.get(value);
|
||||
}
|
||||
}
|
||||
|
||||
private final int m_port;
|
||||
private int m_outputs;
|
||||
private int m_leftRumble;
|
||||
private int m_rightRumble;
|
||||
private final Map<EventLoop, Map<Integer, BooleanEvent>> m_buttonCache = new HashMap<>();
|
||||
private final Map<EventLoop, Map<Pair<Integer, Double>, BooleanEvent>> m_axisLessThanCache =
|
||||
new HashMap<>();
|
||||
private final Map<EventLoop, Map<Pair<Integer, Double>, BooleanEvent>> m_axisGreaterThanCache =
|
||||
new HashMap<>();
|
||||
private final Map<EventLoop, Map<Integer, BooleanEvent>> m_povCache = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Construct an instance of a device.
|
||||
*
|
||||
* @param port The port index on the Driver Station that the device is plugged into.
|
||||
*/
|
||||
public GenericHID(int port) {
|
||||
m_port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the button value (starting at button 1).
|
||||
*
|
||||
* <p>The buttons are returned in a single 16 bit value with one bit representing the state of
|
||||
* each button. The appropriate button is returned as a boolean value.
|
||||
*
|
||||
* <p>This method returns true if the button is being held down at the time that this method is
|
||||
* being called.
|
||||
*
|
||||
* @param button The button number to be read (starting at 1)
|
||||
* @return The state of the button.
|
||||
*/
|
||||
public boolean getRawButton(int button) {
|
||||
return DriverStation.getStickButton(m_port, (byte) button);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the button was pressed since the last check. Button indexes begin at 1.
|
||||
*
|
||||
* <p>This method returns true if the button went from not pressed to held down since the last
|
||||
* time this method was called. This is useful if you only want to call a function once when you
|
||||
* press the button.
|
||||
*
|
||||
* @param button The button index, beginning at 0.
|
||||
* @return Whether the button was pressed since the last check.
|
||||
*/
|
||||
public boolean getRawButtonPressed(int button) {
|
||||
return DriverStation.getStickButtonPressed(m_port, (byte) button);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the button was released since the last check. Button indexes begin at 1.
|
||||
*
|
||||
* <p>This method returns true if the button went from held down to not pressed since the last
|
||||
* time this method was called. This is useful if you only want to call a function once when you
|
||||
* release the button.
|
||||
*
|
||||
* @param button The button index, beginning at 0.
|
||||
* @return Whether the button was released since the last check.
|
||||
*/
|
||||
public boolean getRawButtonReleased(int button) {
|
||||
return DriverStation.getStickButtonReleased(m_port, button);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an event instance around this button's digital signal.
|
||||
*
|
||||
* @param button the button index
|
||||
* @param loop the event loop instance to attach the event to.
|
||||
* @return an event instance representing the button's digital signal attached to the given loop.
|
||||
*/
|
||||
public BooleanEvent button(int button, EventLoop loop) {
|
||||
synchronized (m_buttonCache) {
|
||||
var cache = m_buttonCache.computeIfAbsent(loop, k -> new HashMap<>());
|
||||
return cache.computeIfAbsent(button, k -> new BooleanEvent(loop, () -> getRawButton(k)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the axis.
|
||||
*
|
||||
* @param axis The axis to read, starting at 0.
|
||||
* @return The value of the axis.
|
||||
*/
|
||||
public double getRawAxis(int axis) {
|
||||
return DriverStation.getStickAxis(m_port, axis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the angle of a POV on the HID.
|
||||
*
|
||||
* @param pov The index of the POV to read (starting at 0). Defaults to 0.
|
||||
* @return the angle of the POV.
|
||||
*/
|
||||
public POVDirection getPOV(int pov) {
|
||||
return DriverStation.getStickPOV(m_port, pov);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the angle of the default POV (index 0) on the HID.
|
||||
*
|
||||
* @return the angle of the POV.
|
||||
*/
|
||||
public POVDirection getPOV() {
|
||||
return getPOV(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a BooleanEvent instance based around this angle of a POV on the HID.
|
||||
*
|
||||
* @param angle POV angle.
|
||||
* @param loop the event loop instance to attach the event to.
|
||||
* @return a BooleanEvent instance based around this angle of a POV on the HID.
|
||||
*/
|
||||
public BooleanEvent pov(POVDirection angle, EventLoop loop) {
|
||||
return pov(0, angle, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a BooleanEvent instance based around this angle of a POV on the HID.
|
||||
*
|
||||
* @param pov index of the POV to read (starting at 0). Defaults to 0.
|
||||
* @param angle POV angle.
|
||||
* @param loop the event loop instance to attach the event to.
|
||||
* @return a BooleanEvent instance based around this angle of a POV on the HID.
|
||||
*/
|
||||
public BooleanEvent pov(int pov, POVDirection angle, EventLoop loop) {
|
||||
synchronized (m_povCache) {
|
||||
var cache = m_povCache.computeIfAbsent(loop, k -> new HashMap<>());
|
||||
// angle.value is a 4 bit bitfield
|
||||
return cache.computeIfAbsent(
|
||||
pov * 16 + angle.value, k -> new BooleanEvent(loop, () -> getPOV(pov) == angle));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a BooleanEvent instance based around the up direction of the default (index 0) POV
|
||||
* on the HID.
|
||||
*
|
||||
* @param loop the event loop instance to attach the event to.
|
||||
* @return a BooleanEvent instance based around the up direction a POV on the HID.
|
||||
*/
|
||||
public BooleanEvent povUp(EventLoop loop) {
|
||||
return pov(POVDirection.Up, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a BooleanEvent instance based around the up right direction of the default (index 0)
|
||||
* POV on the HID.
|
||||
*
|
||||
* @param loop the event loop instance to attach the event to.
|
||||
* @return a BooleanEvent instance based around the up right direction of a POV on the HID.
|
||||
*/
|
||||
public BooleanEvent povUpRight(EventLoop loop) {
|
||||
return pov(POVDirection.UpRight, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a BooleanEvent instance based around the right direction of the default (index 0)
|
||||
* POV on the HID.
|
||||
*
|
||||
* @param loop the event loop instance to attach the event to.
|
||||
* @return a BooleanEvent instance based around the right direction of a POV on the HID.
|
||||
*/
|
||||
public BooleanEvent povRight(EventLoop loop) {
|
||||
return pov(POVDirection.Right, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a BooleanEvent instance based around the down right direction of the default (index
|
||||
* 0) POV on the HID.
|
||||
*
|
||||
* @param loop the event loop instance to attach the event to.
|
||||
* @return a BooleanEvent instance based around the down right direction of a POV on the HID.
|
||||
*/
|
||||
public BooleanEvent povDownRight(EventLoop loop) {
|
||||
return pov(POVDirection.DownRight, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a BooleanEvent instance based around the down direction of the default (index 0) POV
|
||||
* on the HID.
|
||||
*
|
||||
* @param loop the event loop instance to attach the event to.
|
||||
* @return a BooleanEvent instance based around the down direction of a POV on the HID.
|
||||
*/
|
||||
public BooleanEvent povDown(EventLoop loop) {
|
||||
return pov(POVDirection.Down, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a BooleanEvent instance based around the down left direction of the default (index
|
||||
* 0) POV on the HID.
|
||||
*
|
||||
* @param loop the event loop instance to attach the event to.
|
||||
* @return a BooleanEvent instance based around the down left direction of a POV on the HID.
|
||||
*/
|
||||
public BooleanEvent povDownLeft(EventLoop loop) {
|
||||
return pov(POVDirection.DownLeft, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a BooleanEvent instance based around the left direction of the default (index 0) POV
|
||||
* on the HID.
|
||||
*
|
||||
* @param loop the event loop instance to attach the event to.
|
||||
* @return a BooleanEvent instance based around the left direction of a POV on the HID.
|
||||
*/
|
||||
public BooleanEvent povLeft(EventLoop loop) {
|
||||
return pov(POVDirection.Left, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a BooleanEvent instance based around the up left direction of the default (index 0)
|
||||
* POV on the HID.
|
||||
*
|
||||
* @param loop the event loop instance to attach the event to.
|
||||
* @return a BooleanEvent instance based around the up left direction of a POV on the HID.
|
||||
*/
|
||||
public BooleanEvent povUpLeft(EventLoop loop) {
|
||||
return pov(POVDirection.UpLeft, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a BooleanEvent instance based around the center (not pressed) of the default (index
|
||||
* 0) POV on the HID.
|
||||
*
|
||||
* @param loop the event loop instance to attach the event to.
|
||||
* @return a BooleanEvent instance based around the center of a POV on the HID.
|
||||
*/
|
||||
public BooleanEvent povCenter(EventLoop loop) {
|
||||
return pov(POVDirection.Center, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an event instance that is true when the axis value is less than {@code threshold},
|
||||
* attached to the given loop.
|
||||
*
|
||||
* @param axis The axis to read, starting at 0
|
||||
* @param threshold The value below which this event should return true.
|
||||
* @param loop the event loop instance to attach the event to.
|
||||
* @return an event instance that is true when the axis value is less than the provided threshold.
|
||||
*/
|
||||
public BooleanEvent axisLessThan(int axis, double threshold, EventLoop loop) {
|
||||
synchronized (m_axisLessThanCache) {
|
||||
var cache = m_axisLessThanCache.computeIfAbsent(loop, k -> new HashMap<>());
|
||||
return cache.computeIfAbsent(
|
||||
Pair.of(axis, threshold),
|
||||
k -> new BooleanEvent(loop, () -> getRawAxis(axis) < threshold));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an event instance that is true when the axis value is greater than {@code
|
||||
* threshold}, attached to the given loop.
|
||||
*
|
||||
* @param axis The axis to read, starting at 0
|
||||
* @param threshold The value above which this event should return true.
|
||||
* @param loop the event loop instance to attach the event to.
|
||||
* @return an event instance that is true when the axis value is greater than the provided
|
||||
* threshold.
|
||||
*/
|
||||
public BooleanEvent axisGreaterThan(int axis, double threshold, EventLoop loop) {
|
||||
synchronized (m_axisGreaterThanCache) {
|
||||
var cache = m_axisGreaterThanCache.computeIfAbsent(loop, k -> new HashMap<>());
|
||||
return cache.computeIfAbsent(
|
||||
Pair.of(axis, threshold),
|
||||
k -> new BooleanEvent(loop, () -> getRawAxis(axis) > threshold));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the maximum axis index for the joystick.
|
||||
*
|
||||
* @return the maximum axis index for the joystick
|
||||
*/
|
||||
public int getAxesMaximumIndex() {
|
||||
return DriverStation.getStickAxesMaximumIndex(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of axes for the HID.
|
||||
*
|
||||
* @return the number of axis for the current HID
|
||||
*/
|
||||
public int getAxesAvailable() {
|
||||
return DriverStation.getStickAxesAvailable(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the maximum POV index for the joystick.
|
||||
*
|
||||
* @return the maximum POV index for the joystick
|
||||
*/
|
||||
public int getPOVsMaximumIndex() {
|
||||
return DriverStation.getStickPOVsMaximumIndex(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* For the current HID, return the number of POVs.
|
||||
*
|
||||
* @return the number of POVs for the current HID
|
||||
*/
|
||||
public int getPOVsAvailable() {
|
||||
return DriverStation.getStickPOVsAvailable(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the maximum button index for the joystick.
|
||||
*
|
||||
* @return the maximum button index for the joystick
|
||||
*/
|
||||
public int getButtonsMaximumIndex() {
|
||||
return DriverStation.getStickButtonsMaximumIndex(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* For the current HID, return the number of buttons.
|
||||
*
|
||||
* @return the number of buttons for the current HID
|
||||
*/
|
||||
public long getButtonsAvailable() {
|
||||
return DriverStation.getStickButtonsAvailable(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if the HID is connected.
|
||||
*
|
||||
* @return true if the HID is connected
|
||||
*/
|
||||
public boolean isConnected() {
|
||||
return DriverStation.isJoystickConnected(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the type of the HID.
|
||||
*
|
||||
* @return the type of the HID.
|
||||
*/
|
||||
public HIDType getType() {
|
||||
return HIDType.of(DriverStation.getJoystickType(m_port));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the HID.
|
||||
*
|
||||
* @return the name of the HID.
|
||||
*/
|
||||
public String getName() {
|
||||
return DriverStation.getJoystickName(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the port number of the HID.
|
||||
*
|
||||
* @return The port number of the HID.
|
||||
*/
|
||||
public int getPort() {
|
||||
return m_port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a single HID output value for the HID.
|
||||
*
|
||||
* @param outputNumber The index of the output to set (1-32)
|
||||
* @param value The value to set the output to
|
||||
*/
|
||||
public void setOutput(int outputNumber, boolean value) {
|
||||
m_outputs = (m_outputs & ~(1 << (outputNumber - 1))) | ((value ? 1 : 0) << (outputNumber - 1));
|
||||
DriverStationJNI.setJoystickOutputs((byte) m_port, m_outputs, m_leftRumble, m_rightRumble);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set all HID output values for the HID.
|
||||
*
|
||||
* @param value The 32 bit output value (1 bit for each output)
|
||||
*/
|
||||
public void setOutputs(int value) {
|
||||
m_outputs = value;
|
||||
DriverStationJNI.setJoystickOutputs((byte) m_port, m_outputs, m_leftRumble, m_rightRumble);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the rumble output for the HID. The DS currently supports 2 rumble values, left rumble and
|
||||
* right rumble.
|
||||
*
|
||||
* @param type Which rumble value to set
|
||||
* @param value The normalized value (0 to 1) to set the rumble to
|
||||
*/
|
||||
public void setRumble(RumbleType type, double value) {
|
||||
value = Math.clamp(value, 0, 1);
|
||||
int rumbleValue = (int) (value * 65535);
|
||||
switch (type) {
|
||||
case kLeftRumble -> this.m_leftRumble = rumbleValue;
|
||||
case kRightRumble -> this.m_rightRumble = rumbleValue;
|
||||
default -> {
|
||||
this.m_leftRumble = rumbleValue;
|
||||
this.m_rightRumble = rumbleValue;
|
||||
}
|
||||
}
|
||||
|
||||
DriverStationJNI.setJoystickOutputs(
|
||||
(byte) this.m_port, this.m_outputs, this.m_leftRumble, this.m_rightRumble);
|
||||
}
|
||||
}
|
||||
341
wpilibj/src/main/java/org/wpilib/driverstation/Joystick.java
Normal file
341
wpilibj/src/main/java/org/wpilib/driverstation/Joystick.java
Normal file
@@ -0,0 +1,341 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.event.BooleanEvent;
|
||||
import edu.wpi.first.wpilibj.event.EventLoop;
|
||||
|
||||
/**
|
||||
* Handle input from Flight Joysticks connected to the Driver Station.
|
||||
*
|
||||
* <p>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.
|
||||
*/
|
||||
public class Joystick extends GenericHID {
|
||||
/** Default X axis channel. */
|
||||
public static final byte kDefaultXChannel = 0;
|
||||
|
||||
/** Default Y axis channel. */
|
||||
public static final byte kDefaultYChannel = 1;
|
||||
|
||||
/** Default Z axis channel. */
|
||||
public static final byte kDefaultZChannel = 2;
|
||||
|
||||
/** Default twist axis channel. */
|
||||
public static final byte kDefaultTwistChannel = 2;
|
||||
|
||||
/** Default throttle axis channel. */
|
||||
public static final byte kDefaultThrottleChannel = 3;
|
||||
|
||||
/** Represents an analog axis on a joystick. */
|
||||
public enum AxisType {
|
||||
/** X axis. */
|
||||
kX(0),
|
||||
/** Y axis. */
|
||||
kY(1),
|
||||
/** Z axis. */
|
||||
kZ(2),
|
||||
/** Twist axis. */
|
||||
kTwist(3),
|
||||
/** Throttle axis. */
|
||||
kThrottle(4);
|
||||
|
||||
/** AxisType value. */
|
||||
public final int value;
|
||||
|
||||
AxisType(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
/** Represents a digital button on a joystick. */
|
||||
public enum ButtonType {
|
||||
/** kTrigger. */
|
||||
kTrigger(1),
|
||||
/** kTop. */
|
||||
kTop(2);
|
||||
|
||||
/** ButtonType value. */
|
||||
public final int value;
|
||||
|
||||
ButtonType(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
private final byte[] m_axes = new byte[AxisType.values().length];
|
||||
|
||||
/**
|
||||
* Construct an instance of a joystick.
|
||||
*
|
||||
* @param port The port index on the Driver Station that the joystick is plugged into.
|
||||
*/
|
||||
public Joystick(final int port) {
|
||||
super(port);
|
||||
|
||||
m_axes[AxisType.kX.value] = kDefaultXChannel;
|
||||
m_axes[AxisType.kY.value] = kDefaultYChannel;
|
||||
m_axes[AxisType.kZ.value] = kDefaultZChannel;
|
||||
m_axes[AxisType.kTwist.value] = kDefaultTwistChannel;
|
||||
m_axes[AxisType.kThrottle.value] = kDefaultThrottleChannel;
|
||||
|
||||
HAL.reportUsage("HID", port, "Joystick");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the channel associated with the X axis.
|
||||
*
|
||||
* @param channel The channel to set the axis to.
|
||||
*/
|
||||
public void setXChannel(int channel) {
|
||||
m_axes[AxisType.kX.value] = (byte) channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the channel associated with the Y axis.
|
||||
*
|
||||
* @param channel The channel to set the axis to.
|
||||
*/
|
||||
public void setYChannel(int channel) {
|
||||
m_axes[AxisType.kY.value] = (byte) channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the channel associated with the Z axis.
|
||||
*
|
||||
* @param channel The channel to set the axis to.
|
||||
*/
|
||||
public void setZChannel(int channel) {
|
||||
m_axes[AxisType.kZ.value] = (byte) channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the channel associated with the throttle axis.
|
||||
*
|
||||
* @param channel The channel to set the axis to.
|
||||
*/
|
||||
public void setThrottleChannel(int channel) {
|
||||
m_axes[AxisType.kThrottle.value] = (byte) channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the channel associated with the twist axis.
|
||||
*
|
||||
* @param channel The channel to set the axis to.
|
||||
*/
|
||||
public void setTwistChannel(int channel) {
|
||||
m_axes[AxisType.kTwist.value] = (byte) channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channel currently associated with the X axis.
|
||||
*
|
||||
* @return The channel for the axis.
|
||||
*/
|
||||
public int getXChannel() {
|
||||
return m_axes[AxisType.kX.value];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channel currently associated with the Y axis.
|
||||
*
|
||||
* @return The channel for the axis.
|
||||
*/
|
||||
public int getYChannel() {
|
||||
return m_axes[AxisType.kY.value];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channel currently associated with the Z axis.
|
||||
*
|
||||
* @return The channel for the axis.
|
||||
*/
|
||||
public int getZChannel() {
|
||||
return m_axes[AxisType.kZ.value];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channel currently associated with the twist axis.
|
||||
*
|
||||
* @return The channel for the axis.
|
||||
*/
|
||||
public int getTwistChannel() {
|
||||
return m_axes[AxisType.kTwist.value];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channel currently associated with the throttle axis.
|
||||
*
|
||||
* @return The channel for the axis.
|
||||
*/
|
||||
public int getThrottleChannel() {
|
||||
return m_axes[AxisType.kThrottle.value];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the X value of the joystick. This depends on the mapping of the joystick connected to the
|
||||
* current port. On most joysticks, positive is to the right.
|
||||
*
|
||||
* @return The X value of the joystick.
|
||||
*/
|
||||
public final double getX() {
|
||||
return getRawAxis(m_axes[AxisType.kX.value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Y value of the joystick. This depends on the mapping of the joystick connected to the
|
||||
* current port. On most joysticks, positive is to the back.
|
||||
*
|
||||
* @return The Y value of the joystick.
|
||||
*/
|
||||
public final double getY() {
|
||||
return getRawAxis(m_axes[AxisType.kY.value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the z position of the HID.
|
||||
*
|
||||
* @return the z position
|
||||
*/
|
||||
public final double getZ() {
|
||||
return getRawAxis(m_axes[AxisType.kZ.value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the twist value of the current joystick. This depends on the mapping of the joystick
|
||||
* connected to the current port.
|
||||
*
|
||||
* @return The Twist value of the joystick.
|
||||
*/
|
||||
public final double getTwist() {
|
||||
return getRawAxis(m_axes[AxisType.kTwist.value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the throttle value of the current joystick. This depends on the mapping of the joystick
|
||||
* connected to the current port.
|
||||
*
|
||||
* @return The Throttle value of the joystick.
|
||||
*/
|
||||
public final double getThrottle() {
|
||||
return getRawAxis(m_axes[AxisType.kThrottle.value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the state of the trigger on the joystick.
|
||||
*
|
||||
* @return The state of the trigger.
|
||||
*/
|
||||
public boolean getTrigger() {
|
||||
return getRawButton(ButtonType.kTrigger.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the trigger was pressed since the last check.
|
||||
*
|
||||
* @return Whether the button was pressed since the last check.
|
||||
*/
|
||||
public boolean getTriggerPressed() {
|
||||
return getRawButtonPressed(ButtonType.kTrigger.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the trigger was released since the last check.
|
||||
*
|
||||
* @return Whether the button was released since the last check.
|
||||
*/
|
||||
public boolean getTriggerReleased() {
|
||||
return getRawButtonReleased(ButtonType.kTrigger.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public BooleanEvent trigger(EventLoop loop) {
|
||||
return button(ButtonType.kTrigger.value, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the state of the top button on the joystick.
|
||||
*
|
||||
* @return The state of the top button.
|
||||
*/
|
||||
public boolean getTop() {
|
||||
return getRawButton(ButtonType.kTop.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the top button was pressed since the last check.
|
||||
*
|
||||
* @return Whether the button was pressed since the last check.
|
||||
*/
|
||||
public boolean getTopPressed() {
|
||||
return getRawButtonPressed(ButtonType.kTop.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the top button was released since the last check.
|
||||
*
|
||||
* @return Whether the button was released since the last check.
|
||||
*/
|
||||
public boolean getTopReleased() {
|
||||
return getRawButtonReleased(ButtonType.kTop.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public BooleanEvent top(EventLoop loop) {
|
||||
return button(ButtonType.kTop.value, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the magnitude of the vector formed by the joystick's current position relative to its
|
||||
* origin.
|
||||
*
|
||||
* @return The magnitude of the direction vector
|
||||
*/
|
||||
public double getMagnitude() {
|
||||
return Math.hypot(getX(), getY());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the direction of the vector formed by the joystick and its origin in radians. 0 is forward
|
||||
* and clockwise is positive. (Straight right is π/2.)
|
||||
*
|
||||
* @return The direction of the vector in radians
|
||||
*/
|
||||
public double getDirectionRadians() {
|
||||
// https://docs.wpilib.org/en/stable/docs/software/basic-programming/coordinate-system.html#joystick-and-controller-coordinate-system
|
||||
// A positive rotation around the X axis moves the joystick right, and a
|
||||
// positive rotation around the Y axis moves the joystick backward. When
|
||||
// treating them as translations, 0 radians is measured from the right
|
||||
// direction, and angle increases clockwise.
|
||||
//
|
||||
// It's rotated 90 degrees CCW (y is negated and the arguments are reversed)
|
||||
// so that 0 radians is forward.
|
||||
return Math.atan2(getX(), -getY());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the direction of the vector formed by the joystick and its origin in degrees. 0 is forward
|
||||
* and clockwise is positive. (Straight right is 90.)
|
||||
*
|
||||
* @return The direction of the vector in degrees
|
||||
*/
|
||||
public double getDirectionDegrees() {
|
||||
return Math.toDegrees(getDirectionRadians());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user