C++#
The VEX EXP C++ API Reference explains the classes, objects, constructors, member functions, and variables used in VEXcode EXP C++ projects.
Use this reference when you want to understand how a device or feature is created, check which function signature to use, or compare related member functions in a class.
C++ commands are used in VEXcode EXP C++ projects. C++ API information can also support projects written in Visual Studio Code with the VEX Visual Studio Code Extension.
How to Read a C++ Class Page#
A C++ class page usually follows this order:
Introduction - Explains what the class represents and when it is useful.
Derived Classes - Lists any classes that are based on this class, if applicable.
Class Constructor or Constructors - Shows the syntax used to create an object from the class.
Class Destructor - Shows the syntax used to destroy the object and release its resources, if applicable.
Parameters - Explains the constructor parameters used when creating the object.
Example - Shows how to create the object in code.
Member Functions - Lists the functions that belong to the class, often grouped by purpose.
Function Entries - Explains each member function, including available functions, parameters, return values, notes, and examples.
Not every page has every section. For example, some pages do not include derived classes, and some functions do not have parameters or return values.
Common C++ API Elements#
Element |
What it means |
|---|---|
Class |
The C++ type for a device or feature, such as |
Object |
A named instance created from a class, such as |
Constructor |
The code used to create an object from a class. |
Member Function |
A command called on an object, such as |
Available Function |
One exact function signature that can be used for a member function. Some member functions have more than one available function. |
Parameter |
A value passed into a constructor or function to control what it does. |
Return Value |
A value a function gives back, such as |
Basic C++ Vocabulary#
Term |
What it means |
|---|---|
Variable |
A name that stores a value, such as |
Type |
The kind of data something stores, such as |
String |
Text inside quotes, such as |
int |
A positive or negative whole number type, such as |
double |
A number type that can include a decimal, such as |
bool |
A value that is either |
Argument |
A value passed into a member function or function call, such as |
Constructors and Objects#
Before using member functions from many C++ classes, an object must exist. The constructor shows how to create that object.
For example, a motor object can be created by giving the constructor the port or configuration information it needs:
motor Motor1 = motor(PORT1, false);
After the object is created, its member functions can be called:
Motor1.spin(forward);
In VEXcode, device configuration is often generated automatically by the Device Menu. In Visual Studio Code with the VEX extension, constructors may need to be written manually.
Choosing the Right Available Function#
Some member functions have more than one available function. Each function uses a different set of parameters, so choose the one that matches the values your project needs to provide.
For example, a drivetrain function may have one version that uses the current velocity and another version that lets the project choose a velocity:
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.
Example Function Entry#
drive#
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.
Available Functions
void drive(directionType dir);
void drive(directionType dir, double velocity, velocityUnits units);
Parameter |
Type |
Description |
|---|---|---|
|
|
The direction the robot moves: |
|
|
The velocity to drive with from 0% to 100% when using |
|
|
The velocity unit: |
Return Value
This function does not return a value.
Notes
driveis non-waiting; the project will continue executing the next line of code immediately after the call.The drivetrain will continue moving until
stopis called or another drivetrain movement function is executed.Functions such as
isDoneandisMovingare not applicable todrive, since it does not use target-based movement.
Example
// Drive forward and back
myDrivetrain.drive(forward);
wait(2, seconds);
myDrivetrain.drive(reverse, 25, rpm);
wait(2, seconds);
myDrivetrain.stop();
Waiting and Non-Waiting Behavior#
Some C++ movement functions wait until an action is finished before the project moves to the next line of code. Other functions start an action and allow the next line to run right away.
Waiting - The project waits until the function finishes.
Non-waiting - The project starts the function and immediately continues to the next line.
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.
Commands in Subsections#
C++ commands are organized by device or topic. For example, commands for the Drivetrain, Motion, Brain, Controller, Smart Port devices, 3-Wire devices, Console, Logic, and VEXlink are grouped into their own subsections.