-
Notifications
You must be signed in to change notification settings - Fork 0
HID Control
Human Interface Devices or HID refers to devices which allow humans to interact with computers, such as gamepads, keyboards and such. Using such devices, we can allow users/drivers to control the operation of the robot.
The flashlib.core.hid
component provides an API to access information from different HIDs which are connected to the robot.
A device for human control is represented by the Hid
interface. Such devices have different types of controls on them. Those controls rely different types of information:
- Axes: analog input data with both direction (back/forth) and power (percentage). Represented by the
Axis
interface. - Buttons: boolean input data which can either be down or up. Represented by the
Button
interface. - POV: (Point of View) direction input data in degrees, representing the direction the control is pointed towards.
Each HID
as a different number of these controls.
Acquiring an instance of the Hid
interface is done via the HidInterface
component which is provided from
RobotControl
and is universally accessible via RunningRobot.getControl().getHidInterface()
.
Each Hid
under HidInterface
is represented by HidChannel
which is a unique identifier for each different Hid
.
HidChannel channel = ...;
HidInterface hidInterface = RunningRobot.getControl().getHidInterface();
Hid hid = hidInterface.newHid(channel);
To acquire an HidChannel
instance, it is necessary to be familiar with the specific implementation used for HidInterface
. Due to this limitation, there is no universal method of acquiring HidChannel
.
Axis controls are represented by a non-negative integer number (referred to as axisIndex
). Each axis has a different index representing it; however there is no guarantee that the indices are sequential in order.
Hid hid = ...
Axis axis = hid.getAxis(axisIndex);
// get the current axis value.
// this will return a value from -1 to 1.
// the value has 2 properties:
// - Direction represented by the sign of the value (+/-). Where positive means forward or right, and negative refers to backward or left (depending on the orientation of the axis.
// - Power represented by the absolute value as a percentage. i.e 0.3 is 30%.
double value = axis.getAsDouble();
The axis interface has additional methods, like:
// Inverts the directions of the axis such that + is now -, and - is now +.
axis.setInverted(true);
Button controls are represented by a non-negative integer number (referred to as buttonIndex
). Each button has a different index representing it; however there is no guarantee that the indices are sequential in order.
Hid hid = ...
Button button = hid.getButton(buttonIndex);
// get the current button value.
// this will return a boolean value of either true or false where:
// - true = button is down
// - false = button is up
boolean value = button.getAsBoolean();
In addition to basic functionality, buttons are also a type of Trigger
and thus can be used to activate Action
s using button.whenActive
and other methods from the Trigger
interface.
For example:
button.whileActive(new ShootAction());
This snippet will cause the action ShootAction
to run while the button is down.
POV controls are represented by a non-negative integer number (referred to as povIndex
). Each POV has a different index representing it; however there is no guarantee that the indices are sequential in order.
Hid hid = ...
Pov pov = hid.getPov(povIndex);
// get the current pov value.
// this will return an integer value which can be either:
// - -1 if the POV is not pressed or
// - 0->360 (degrees) indicating the direction of the press.
int value = pov.getAsInt();
Other than the basic HID
interface, there are also specialized types that fit specific devices.
A flight joystick, with a single stick in the center, a variety of buttons and a single POV. Such
devices are represented by Joystick
interface and acquired from HidInterface.newJoystick
:
HidChannel channel = ...;
HidInterface hidInterface = RunningRobot.getControl().getHidInterface();
Joystick joystick = hidInterface.newJoystick(channel);
The HidChannel
used follows the same rules as with HidInterface.newHid
such that a call to newHid
with the same HidChannel
instance will produce the an Hid
interface representing the joystick.
Usage of controls follows the API of Hid
. However, there are several enhancements for comfort:
- Joysticks generally possess some similarities in axes available. Thanks to this, it is possible to use the
Joystick.getAxis(JoystickAxis)
overload, which receives values from theJoystickAxis
enum instead of an index. However, some axes will not be represented by the enum, since they are not shared by all joysticks. - Similarly, the
Joystick.getButton(JoystickButton)
overload provides easier access to buttons. - Since joysticks possess a single POV, the
Joystick.getPov()
overload can be used instead of providing an index.
A console controller like the different Xbox controllers and even playstation controllers. Such devices have a specific layout of controls and poses a specialized POV. They can be acquired with HidInterface.newXboxController
:
HidChannel channel = ...;
HidInterface hidInterface = RunningRobot.getControl().getHidInterface();
XboxController controller = hidInterface.newXboxController(channel);
The HidChannel
used follows the same rules as with HidInterface.newHid
such that a call to newHid
with the same HidChannel
instance will produce the an Hid
interface representing the controller.
Usage of controls follows the API of Hid
. However, there are several enhancements for comfort:
- Controllers generally possess similar (if not identical) layout of axes. Thanks to this, it is possible to use the
XboxController.getAxis(XboxAxis)
overload, which receives values from theXboxAxis
enum instead of an index. - Similarly, the
XboxController.getButton(XboxButton)
overload provides easier access to buttons. - Controllers possess a single specialized POV called D-PAD. This control is represented in a special
Dpad
interface and can be accessed withXboxController.getDpad()
XboxController controller = ...
Dpad dpad = controller.getDpad();
// Can be used just like with the normal Pov interface
int value = dpad.getAsInt();
// However, DPADs are special because they have 4 buttons indicating directions. These can be accessed from
// the interface and used just like any button
Button up = dpad.up();
boolean isDown = up.getAsBoolean();
up.whenActive(new SomeActionToRun());