控制器#

介绍#

The IQ (2nd gen) Controller is represented by the controller class. You create a controller object and can access joystick input and button states.

IQ (2nd gen) additionally supports IQ (1st gen) controllers with a blue Smart Radio installed.

Derived Classes#

The controller class provides the following derived classes:

  • Axis — Provides access to joystick axis input values and change events.

  • Button — Provides access to button state and button event callbacks.

Class Constructors#

1 Creates a controller using the primary controller type.
Typically used when a single controller is connected.

controller();

2 Creates a controller for the specified controller type.
Used when two controllers are connected.

controller(
    controllerType id );

Class Destructor#

~controller();

参数#

范围

Type

描述

id

controllerType

The type of controller being created:

  • primary — The primary controller connected to the Brain.
  • partner — The partner (second) controller connected to the Brain.

Notes#

  • Only one primary and one partner controller may exist in a single project.

Example#

// Create a controller instance
controller Controller = controller();

Member Functions#

The controller class includes the following member functions:

  • installed — Checks whether the controller is connected to the brain.

installed#

Checks whether the controller is installed/connected.

Available Functions
bool installed();

Parameters

This function does not accept any parameters.

Return Values

Returns a Boolean indicating whether the controller is installed.

  • true — The controller is installed/connected.
  • false — The controller is not installed/connected.

Global Variables#

When one or more controllers are configured in the Devices window, VEXcode IQ provides a global variable that controls the execution of controller-configured actions.

远程控制代码已启用#

Enables or disables controller-configured actions defined in the Devices configuration.

Assignment
RemoteControlCodeEnabled = false;

Values
  • true — Controller-configured actions are enabled.
  • false — Controller-configured actions are disabled.
Notes
  • Controller-configured actions are enabled by default.

Examples
// Drive forward or backward using the left joystick
RemoteControlCodeEnabled = false;

while (true) {
    if (Controller1.Axis3.position() > 0) {
        Drivetrain.drive(forward);
    } else if (Controller1.Axis3.position() < 0) {
        Drivetrain.drive(reverse);

    // Press A to use the controller configured actions
    } else if (Controller1.ButtonA.pressing()) {
        break;
    } else {
        Drivetrain.stop();
    }
    wait(20, msec);
}
RemoteControlCodeEnabled = true;