#

介绍#

The axis class is derived from the controller base class and provides access to the EXP Controller’s joysticks, allowing your robot to monitor changes in position along the four available axes.

一个 VEX EXP 控制器,摇杆周围的轴都已标注。轴 1 和轴 2 位于右侧摇杆周围,轴 3 和轴 4 位于左侧摇杆周围。

使用权#

The controller class provides four axis objects (Axis1Axis4). Each object is an instance of the axis class and can be accessed through a controller instance.

轴对象

用法示例

描述

Axis1

Controller.Axis1

右摇杆水平轴。

Axis2

Controller.Axis2

右摇杆垂直轴。

Axis3

Controller.Axis3

左摇杆垂直轴。

Axis4

Controller.Axis4

左摇杆水平轴。

笔记#

  • The axis object is provided by the controller. It is not constructed directly.

成员功能#

The Axis class includes the following member functions:

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

  • 已更改 — 注册一个在操纵杆位置改变时要调用的函数。

Before calling any axis member functions, a controller instance must be created, as shown below:

// Create an EXP controller instance
controller Controller = controller();

位置#

返回操纵杆轴的位置,数值为 -100 到 100 之间的整数,表示百分比。

Available Functions
int32_t  position( 
  percentUnits units = percentUnits::pct ) const

Parameters

范围

类型

描述

units

percentUnits

The unit that represents the position:

  • percent / pct — percent

Return Values

This function returns an int32_t as a percent from -100 to 100.

Examples

这个函数可以对任何控制器轴对象调用,例如:

  • Controller.Axis1
  • Controller.Axis2
  • Controller.Axis3
  • Controller.Axis4
while (true) {
    if (Controller.Axis4.position() > 10) {
        Drivetrain.turn(right);
    } else if (Controller.Axis4.position() < -10) {
        Drivetrain.turn(left);
    } else {
        Drivetrain.stop();
    }
    wait(20, msec);
}

已更改#

注册一个在操纵杆轴位置改变时运行的回调函数。

Available Functions

The controller provides four axis objects (Axis1Axis4):

void changed( 
  void (* callback)(void) ) const;

Parameters

范围

类型

描述

callback

void (*)(void)

预先定义的回调函数,当轴值发生变化时会自动调用。该函数必须符合所需的回调函数签名。有关更多信息,请参阅回调函数

Return Values

此函数不返回值。

Examples

这个函数可以对任何控制器轴对象调用,例如:

  • Controller.Axis1
  • Controller.Axis2
  • Controller.Axis3
  • Controller.Axis4

Define the callback function (outside of int main())

// Play a rumble pattern when the left joystick moves
void rumblePattern() {
    Controller.rumble("..--");
    wait(1, seconds);
}

Register the callback inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Call the rumble function when the left joystick moves
  Controller.Axis4.changed(rumblePattern);
}