Skip to content

HID Control

Tom Tzook edited this page Nov 13, 2020 · 2 revisions

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.

API

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 HID

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

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

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();

Buttons and Actions

In addition to basic functionality, buttons are also a type of Trigger and thus can be used to activate Actions 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

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();

Special HIDs

Other than the basic HID interface, there are also specialized types that fit specific devices:

  • Joystick: a flight joystick, with a single stick in the center and a variety of buttons.
  • XboxController: a console controller like the different Xbox controllers and even playstation controllers.