C++#

VEX EXP C++ API 参考文档解释了 VEXcode EXP C++ 项目中使用的类、对象、构造函数、成员函数和变量。

当您想要了解设备或功能的创建方式、检查要使用的函数签名或比较类中的相关成员函数时,请使用此参考资料。

VEXcode EXP C++ 项目使用 C++ 命令。VEX Visual Studio Code 扩展也支持使用 Visual Studio Code 编写的项目,并提供 C++ API 信息。

如何阅读 C++ 类页面#

C++ 课程页面通常遵循以下顺序:

  1. 简介 - 解释该类代表什么以及何时有用。

  2. 派生类 - 列出所有基于此类的类(如果适用)。

  3. 类构造函数或构造函数 - 显示用于从类创建对象的语法。

  4. 类析构函数 - 显示用于销毁对象并释放其资源(如果适用)的语法。

  5. 参数 - 说明创建对象时使用的构造函数参数。

  6. 示例 - 展示如何在代码中创建对象。

  7. 成员函数 - 列出属于该类的函数,通常按用途分组。

  8. 函数条目 - 解释每个成员函数,包括可用函数、参数、返回值、注释和示例。

并非每个页面都包含所有部分。例如,有些页面不包含派生类,有些函数没有参数或返回值。

常用C++ API元素#

元素

它的含义

班级

The C++ type for a device or feature, such as motor or drivetrain.

目的

A named instance created from a class, such as motor Motor1 or drivetrain myDrivetrain.

构造器

用于从类创建对象的代码。

成员功能

A command called on an object, such as myDrivetrain.drive(…).

可用功能

每个成员函数只能使用一个确切的函数签名。有些成员函数可能有多个可用的函数签名。

范围

传递给构造函数或函数的值,用于控制其执行的操作。

返回值

A value a function gives back, such as bool, int32_t, or double.

C++基础词汇#

学期

它的含义

多变的

A name that stores a value, such as int speed = 50;.

类型

The kind of data something stores, such as int, double, or bool.

细绳

Text inside quotes, such as “Hello”.

int

A positive or negative whole number type, such as 90.

双倍的

A number type that can include a decimal, such as 25.5.

布尔值

A value that is either true or false.

争论

A value passed into a member function or function call, such as forward in myDrivetrain.drive(forward).

构造函数和对象#

在使用许多 C++ 类的成员函数之前,必须先创建一个对象。构造函数展示了如何创建该对象。

例如,可以通过向构造函数提供所需的端口或配置信息来创建电机对象:

motor Motor1 = motor(PORT1, false);

对象创建完成后,即可调用其成员函数:

Motor1.spin(forward);

在 VEXcode 中,设备配置通常由设备菜单自动生成。而在安装了 VEX 扩展的 Visual Studio Code 中,可能需要手动编写构造函数。

选择合适的可用功能#

有些成员函数有多个可用版本。每个版本使用的参数集都不同,因此请选择与项目所需参数值相匹配的版本。

例如,传动系统函数可能有一个版本使用当前速度,而另一个版本允许项目选择速度:

void drive(directionType dir);

void drive(directionType dir, double velocity, velocityUnits units);

Both functions call drive, but the second function gives more control because it includes velocity parameters.

示例函数输入#

驾驶#

drive moves the robot forward or reverse forever. The robot will continue to move until it is given another action, like turning or stopping. For RPM or DPS, the limit is changed by the gear ratio of the drivetrain.

可用功能

void drive(directionType dir);

void drive(directionType dir, double velocity, velocityUnits units);

范围

类型

描述

dir

directionType

The direction the robot moves: forward or reverse.

velocity

double

The velocity to drive with from 0% to 100% when using velocityUnits::pct, from 0 to 600 rpm when using rpm, or from 0 to 3600 degrees per second when using dps. If no velocity is provided, the robot drives at the current drive velocity.

units

velocityUnits

The velocity unit: velocityUnits::pct (percent), rpm (rotations per minute), or dps (degrees per second).

返回值

此函数不返回值。

笔记

  • drive is non-waiting; the project will continue executing the next line of code immediately after the call.

  • The drivetrain will continue moving until stop is called or another drivetrain movement function is executed.

  • Functions such as isDone and isMoving are not applicable to drive, since it does not use target-based movement.

例子

// Drive forward and back
myDrivetrain.drive(forward);
wait(2, seconds);
myDrivetrain.drive(reverse, 25, rpm);
wait(2, seconds);
myDrivetrain.stop();

等待行为和非等待行为#

一些 C++ 代码移动函数会等待当前操作完成后才执行下一行代码。而另一些函数则会在执行完一个操作后立即执行下一行代码。

  • 等待中 - 项目将等待函数执行完毕。

  • 无需等待 - 项目启动函数后立即继续执行下一行。

Some functions include a parameter such as waitForCompletion to choose this behavior. For those functions, true usually makes the project wait, while false lets the next line run right away.

小节中的命令#

C++ 命令按设备或主题进行组织。例如,驱动系统、运动控制、大脑、控制器、智能端口设备、三线设备、控制台、逻辑和 VEXlink 的命令分别归入各自的子章节。