控制器#

介绍#

VEX IQ(第二代)控制器 API 提供了 VEX IQ(第二代)控制器和 VEX IQ(第二代)大脑之间的通信方法。这使得大脑能够检测控制器上的不同按钮以及操纵杆轴的变化。

以下是可用方法的列表:

操作 - 启用或禁用控制器。

Getters——从控制器操纵杆和按钮返回值。

  • pressing – 返回指定按钮是否被按下。

  • position – 返回操纵杆指定轴的位置。

回调——通过回调方法与控制器交互。

  • pressed – 按下指定按钮时调用一个函数。

  • released – 当指定按钮被释放时调用一个函数。

  • 已更改 – 当操纵杆的轴发生变化时调用一个函数。

构造函数——手动初始化控制器。

行动#

远程控制代码已启用#

RemoteControlCodeEnabled 是一个变量,可以设置为布尔值,用于从“设备”菜单中启用或禁用控制器配置的操作。控制器默认启用。它可以设置为以下任一值:

  • true — 启用控制器配置的操作。

  • false — 禁用控制器配置的操作。

用法:
RemoteControlCodeEnabled = false

// Drive with controller after pressing a button
  RemoteControlCodeEnabled = false;
  Drivetrain.turn(right);
  while (true){
    if(Controller.ButtonEUp.pressing()){
      Drivetrain.stop();
      RemoteControlCodeEnabled = true;
      break;
    }
  }
  Brain.Screen.print("Controller Active");

吸气剂#

紧迫#

“pressing” 返回一个整数,指示控制器上的特定按钮当前是否被按下。此方法必须在特定的按钮对象上调用,例如“ButtonEDown”(请参阅下文的完整按钮对象列表)。

  • 1-指定的按钮被按下。

  • 0——指定的按钮未被按下。

用法:
十个可用按钮对象之一可用于此方法,如下所示:

按钮

命令

按钮向下

Controller.ButtonEDown.pressing()E Down 按钮

ButtonEUp

Controller.ButtonEUp.pressing()E 向上 按钮

ButtonFDown

Controller.ButtonFDown.pressing()F Down 按钮

ButtonFUp

Controller.ButtonFUp.pressing()F 向上 按钮

ButtonL3

Controller.ButtonL3.pressing()左操纵杆按钮 - 仅限 IQ(第二代)控制器

ButtonLDown

Controller.ButtonLDown.pressing()L 向下 按钮

ButtonLUp

Controller.ButtonLUp.pressing()L 向上 按钮

ButtonR3

Controller.ButtonR3.pressing()右操纵杆按钮 - 仅限 IQ(第二代)控制器

ButtonRDown

Controller.ButtonRDown.pressing()R 向下 按钮

ButtonRUp

Controller.ButtonRUp.pressing()R 向上 按钮

参数

描述

该方法没有参数。

// Drive forward when a button is pressed
while (true){
    if(Controller.ButtonEUp.pressing()){
      Drivetrain.drive(forward);
    }
    else{
      Drivetrain.stop();
    }
  }

位置#

position 返回操纵杆指定轴的位置,以 -100 到 100 之间的整数表示,代表百分比。此方法必须在特定的轴对象上调用,例如 AxisA(请参阅下文的完整轴对象列表)。

用法:
可以使用此方法的四个可用轴之一,如下所示:

命令

AxisA

Controller.AxisA.position()左操纵杆的垂直轴

AxisB

Controller.AxisB.position()左操纵杆的水平轴

AxisC

Controller.AxisC.position()右操纵杆的水平轴

AxisD

Controller.AxisD.position()右操纵杆的垂直轴

参数

描述

该方法没有参数。

// Turn depending on the joystick position
while (true){
    if (Controller.AxisC.position() < 0){
      Drivetrain.turn(left);
    }
    else if (Controller.AxisC.position() > 0){
      Drivetrain.turn(right);
    }
    else{
      Drivetrain.stop();
    }
  }

打回来#

按下#

pressed 注册了一个函数,当控制器上的特定按钮被按下时会调用该函数。此方法必须在特定的按钮对象上调用,例如 ButtonEDown(请参阅下文的完整按钮对象列表)。

用法:
十个可用按钮对象之一可用于此方法,如下所示:

按钮

命令

按钮向下

Controller.ButtonEDown.pressed(callback)E Down 按钮

ButtonEUp

Controller.ButtonEUp.pressed(callback)E 向上 按钮

ButtonFDown

Controller.ButtonFDown.pressed(callback)F Down 按钮

ButtonFUp

Controller.ButtonFUp.pressed(callback)F 向上 按钮

ButtonL3

Controller.ButtonL3.pressed(callback)左操纵杆按钮 - 仅限 IQ(第二代)

ButtonLDown

Controller.ButtonLDown.pressed(callback)L 向下 按钮

ButtonLUp

Controller.ButtonLUp.pressed(callback)L 向上 按钮

ButtonR3

Controller.ButtonR3.pressed(callback)右操纵杆按钮 - 仅限 IQ(第二代)

ButtonRDown

Controller.ButtonRDown.pressed(callback)R 向下 按钮

ButtonRUp

Controller.ButtonRUp.pressed(callback)R 向上 按钮

范围

描述

打回来

按下按钮时将调用的 函数

//Turn when a button is pressed
void buttonPressed(){
  Drivetrain.turnFor(right, 90, degrees);
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Check for EUp to be pressed
  Controller.ButtonEUp.pressed(buttonPressed);
}

发布#

released 注册了一个函数,当控制器上的特定按钮被释放时调用。此方法必须在特定的按钮对象上调用,例如 ButtonEDown(请参阅下文的完整按钮对象列表)。

用法:
十个可用按钮对象之一可用于此方法,如下所示:

按钮

命令

按钮向下

Controller.ButtonEDown.released(callback)E Down 按钮

ButtonEUp

Controller.ButtonEUp.released(callback)E 向上 按钮

ButtonFDown

Controller.ButtonFDown.released(callback)F Down 按钮

ButtonFUp

Controller.ButtonFUp.released(callback)F 向上 按钮

ButtonL3

Controller.ButtonL3.released(callback)左操纵杆按钮 - 仅限 IQ(第二代)

ButtonLDown

Controller.ButtonLDown.released(callback)L 向下 按钮

ButtonLUp

Controller.ButtonLUp.released(callback)L 向上 按钮

ButtonR3

Controller.ButtonR3.released(callback)右操纵杆按钮 - 仅限 IQ(第二代)

ButtonRDown

Controller.ButtonRDown.released(callback)R 向下 按钮

ButtonRUp

Controller.ButtonRUp.released(callback)R 向上 按钮

范围

描述

打回来

释放按钮时将调用的 function

//Turn when a button is pressed
void buttonReleased(){
  Drivetrain.turnFor(left, 90, degrees);
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Check for EUp to be released
  Controller.ButtonEUp.released(buttonReleased);
}

改变#

changed 注册了一个函数,当操纵杆位置发生变化时会调用该函数。此方法必须在特定的轴对象上调用,例如 AxisA(请参阅下文的完整轴对象列表)。

用法:
可以使用此方法的四个可用轴之一,如下所示:

命令

AxisA

Controller.AxisA.changed(callback)左操纵杆的垂直轴

AxisB

Controller.AxisB.changed(callback)左操纵杆的水平轴

AxisC

Controller.AxisC.changed(callback)右操纵杆的水平轴

AxisD

Controller.AxisD.changed(callback)右操纵杆的垂直轴

参数

描述

打回来

先前定义的 函数,当轴的值发生变化时执行。

// Function to drive when the joystick is moved
void moveJoystick(){
    Drivetrain.driveFor(forward, 200, mm);
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Call when AxisA changes
  Controller.AxisA.changed(moveJoystick);
}

构造函数#

控制器#

controller 创建一个控制器对象。

用法:
controller()

范围

描述

该方法没有参数。

// Create a new object "Controller" with the controller class.
controller Controller = controller();
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Move forward and backward with controller buttons 
  while (true){
    if(Controller.ButtonEUp.pressing()){
      Drivetrain.drive(forward);
    }
    else if(Controller.ButtonEDown.pressing()){
      Drivetrain.drive(reverse);
    }
    else{
      Drivetrain.stop();
    }
  }
}