Motor and Motor Group#
Introduction#
The motor class provides control of a VEX V5 Smart Motor, including motion control, velocity control, position tracking, torque reporting, and configuration.
The motor_group class allows multiple VEX V5 Smart Motors to be controlled together as a single unit. A motor_group applies the same motion commands, velocity settings, stopping behavior, and torque limits to all motors within the group, simplifying programming when multiple motors must operate in sync.
Motor#
Class Constructors#
1 — Creates a
motorobject for the V5 Smart Motor connected to the specified Smart Port.
The motor automatically uses the detected internalgearSetting.motor( int32_t index );
2 — Creates a
motorobject on the specified Smart Port and optionally reverses its direction.
The motor automatically uses the detected internalgearSetting.motor( int32_t index, bool reverse );
3 — Creates a
motorobject on the specified Smart Port with the specifiedgearSetting.motor( int32_t index, gearSetting gears );
4 — Creates a
motorobject on the specified Smart Port with the specifiedgearSettingand optional reversed direction.motor( int32_t index, gearSetting gears, bool reverse );
Class Destructor#
Destroys the motor object and releases associated resources.
~motor();
Parameters#
Parameter |
Type |
Description |
|---|---|---|
|
|
The Smart Port that the motor is connected to, written as |
|
|
Sets the positive direction of motor rotation:
|
|
|
Specifies the internal gear cartridge ratio.
|
Notes#
Gear setting should match the physical cartridge installed in the motor.
Example#
// Create the motor instance in Smart Port 1
motor LeftMotor = motor(PORT1);
// Creates the motor instance with different settings
motor RightMotor = motor(
PORT2, // Smart Port 2
ratio18_1, // Gear setting is 18
true);
Motor Group#
Class Constructors#
motor_group( motor &m1, Args&... m2 );
Class Destructor#
Destroys the motor_group object and releases associated resources.
~motor_group();
Parameters#
Parameter |
Type |
Description |
|---|---|---|
|
|
The first motor added to the group. |
|
|
Additional motors added to the group. The maximum is four motors to one motor group. |
Example#
// Create individual motor instances
motor LeftMotor = motor(PORT1);
motor RightMotor = motor(PORT2);
// Create a motor group with both motors
motor_group DriveMotors = motor_group(LeftMotor, RightMotor);
Member Functions#
The motor and motor_group classes includes the following member functions:
spin— Spins the motor or motor group in a specified direction.spinFor— Spins the motor or motor group for a specified amount of rotation or time.spinToPosition— Spins the motor or motor group to a specified absolute position.stop— Stops the motor or motor group using a selected braking mode.setPosition— Sets the motor or motor group’s current encoder position.setVelocity— Sets the default velocity for a motor or motor group.setStopping— Sets how a motor or motor group behaves when it stops.setMaxTorque— Sets the maximum torque that a motor or motor group can spin at.setTimeout— Sets a time limit for how long a motor or motor group command will wait to reach its target.isDone— Returns a Boolean indication whether a motor or motor group is currently not spinning.isSpinning— Returns a Boolean indicating whether a motor or motor group is currently spinning.position— Returns the current position of a motor or motor group.velocity— Returns the current velocity of a motor or motor group.current— Returns the motor or motor group’s (or motor or motor group group’s) electrical current draw.power— Returns the electrical power supplied to the motor or motor group.torque— Returns the motor or motor group’s (or motor or motor group group’s) current torque value.efficiency— Returns the efficiency of the motor or motor group.temperature— Returns the temperature of the motor or motor group.voltage— Returns the electrical voltage supplied to the motor or motor group.direction— Returns the direction that the motor or motor group is spinning.installed— Returns whether the motor or motor group is detected on the specified Smart Port.count— Returns the number of motors in the motor group.
Before calling any motor or motor_group member functions, an instance must be created, as shown below:
/* This constructor is required when using VS Code.
Motor and Motor Group configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */
// Create the motor instance in Smart Port 1
motor Motor1 = motor(PORT1);
spin#
Spins the motor or motor group in the specified direction indefinitely.
Available Functions1 — Spins the motor or motor group using the currently configured motor velocity.
void spin( directionType dir );
2 — Spins the motor or motor group at the specified velocity.
void spin( directionType dir, double velocity, velocityUnits units );
Parameters3 — Spins the motor or motor group using the specified voltage.
void spin( directionType dir, double voltage, voltageUnits units );
Parameter |
Type |
Description |
|---|---|---|
|
|
The direction in which the motor or motor group spins:
|
|
|
The velocity value applied to the motor or motor group. |
|
|
The unit used to represent velocity:
|
|
|
The voltage value applied to the motor or motor group. |
|
|
The unit used to represent voltage:
|
This function does not return a value.
NotesThis function is non-waiting and the project will continue executing immediately after the call.
The motor or motor group will continue spinning until
stopis called or another movement function is executed.Functions such as
isDoneandisSpinningare not applicable tospin, since it does not use target-based movement (doesn’t have arotationortimeparameter).
// Spin forward, then stop
Motor1.spin(forward);
wait(1, seconds);
Motor1.stop();
spinFor#
Spins a motor or motor group for a set amount in a specified direction.
Available Functions1 — Spins for a target rotation at the velocity provided in the function call.
bool spinFor( double rotation, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion = true );
2 — Accepts a direction parameter and spins for a target rotation at the velocity provided in the function call.
bool spinFor( directionType dir, double rotation, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion = true );
3 — Spins for a target rotation using the currently configured motor velocity.
bool spinFor( double rotation, rotationUnits units, bool waitForCompletion = true );
4 — Accepts a direction parameter and spins for a target rotation using the currently configured motor velocity.
bool spinFor( directionType dir, double rotation, rotationUnits units, bool waitForCompletion = true );
5 — Spins for a target time at the velocity provided in the function call.
bool spinFor( double time, timeUnits units, double velocity, velocityUnits units_v );
6 — Accepts a direction parameter and spins for a target time at the velocity provided in the function call.
bool spinFor( directionType dir, double time, timeUnits units, double velocity, velocityUnits units_v );
7 — Spins for a target time using the currently configured motor velocity.
bool spinFor( double time, timeUnits units );
Parameters8 — Accepts a direction parameter and spins for a target time using the currently configured motor velocity.
bool spinFor( directionType dir, double time, timeUnits units );
Parameter |
Type |
Description |
|---|---|---|
|
|
The direction in which the motor or motor group spins:
|
|
|
The target rotation amount. |
|
|
The unit used to represent
|
|
|
The amount of time that the motor or motor group will spin for. |
|
|
The unit used to represent
|
|
|
The velocity value applied to the motor or motor group. |
|
|
The unit used to represent
|
|
|
|
Returns a Boolean indicating whether the motor or motor group was able to reach the target value.
true— The motor or motor group reached the target value.false— The motor or motor group was not able to reach the target value orwaitForCompletionis set tofalse.
Executing another motor movement function (such as
spinorstop) whilespinForis in progress will interrupt the current movement.Because
spinForis target-based (uses arotationortimeparameter), functions such asisDoneandisSpinningwork withspinFor.
// Spin 1 turn fast, then 1 turn slow
Motor1.spinFor(forward, 1, turns, true);
Motor1.spinFor(reverse, 1, turns, 20, velocityUnits::pct, true);
spinToPosition#
Spins the motor or motor group to a specified position.
Available Functions1 — Spins to an absolute target rotation at the velocity provided in the function call.
bool spinToPosition( double rotation, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion = true );
Parameters2 — Spins to an absolute target rotation using the currently configured motor velocity.
bool spinToPosition( double rotation, rotationUnits units, bool waitForCompletion = true );
Parameter |
Type |
Description |
|---|---|---|
|
|
The absolute target rotation value. |
|
|
The unit used to represent
|
|
|
The velocity value applied to the motor or motor group. |
|
|
The unit used to represent
|
|
|
|
Returns a Boolean indicating whether the call was successful.
true— The motor or motor group begins the requested target-based movement.false— The call fails (for example, due to a parameter error) orwaitForCompletionis set tofalse.
Executing another motor movement function (such as
spinorspinFor) whilespinToPositionis in progress will interrupt the current movement.Because
spinToPositionis target-based (uses arotationparameter), functions such asisDoneandisSpinningwork withspinToPosition.
// Spin forward, then go to 90°
Motor1.spin(forward);
wait(1, seconds);
Motor1.spinToPosition(90, degrees);
stop#
Stops the motor or motor group from spinning.
Available Functions1 — Stops the motor or motor group using the currently configured brake mode.
void stop();
Parameters2 — Stops the motor or motor group using the brake mode provided in the function call.
void stop( brakeType mode );
Parameter |
Type |
Description |
|---|---|---|
|
|
The brake mode used when stopping the motor or motor group:
|
This function does not return a value.
NotesCalling
stopimmediately stops any current motor movement.If no brake mode is specified, the motor or motor group uses the currently configured brake mode.
stopinterrupts any active target-based movement (such asspinFororspinToPosition).
// Spin and coast to a stop
Motor1.setVelocity(100, percent);
Motor1.spin(forward);
wait(1, seconds);
Motor1.stop(coast);
setPosition#
Sets a specific position value to a motor or motor group, which updates the encoder reading.
Available Functionsvoid setPosition(
double value,
rotationUnits units );
Parameter |
Type |
Description |
|---|---|---|
|
|
The position value to assign to the motor’s or motor group’s encoder(s). |
|
|
The unit used to represent
|
This function does not return a value.
NotesAfter calling
setPosition, subsequent calls tospinToPositionwill use the updated position as the reference.
setVelocity#
Sets the default velocity for a motor or motor group. This velocity setting will be used for subsequent calls to any motor functions if a specific velocity is not provided.
Available Functionsvoid setVelocity(
double velocity,
velocityUnits units );
Parameter |
Type |
Description |
|---|---|---|
|
|
The velocity value to apply to the motor. |
|
|
The unit used to represent
|
This function does not return a value.
NotesAny subsequent motor movement function (such as
spin,spinFor, orspinToPosition) that does not explicitly specify a velocity will use this value.
// Try default, then slow, then fast
Motor1.spinFor(forward, 180, degrees);
wait(1, seconds);
Motor1.setVelocity(20, percent);
Motor1.spinFor(reverse, 180, degrees);
wait(1, seconds);
Motor1.setVelocity(100, percent);
Motor1.spinFor(forward, 180, degrees);
setStopping#
Sets how a motor or motor group behaves when it stops.
Available Functionsvoid setStopping(
brakeType mode );
Parameter |
Type |
Description |
|---|---|---|
|
|
The stopping mode applied when
|
This function does not return a value.
NotesAny subsequent call to
stopwithout a specified brake mode will use this value.
setMaxTorque#
Sets the maximum torque that a motor or motor group can spin at.
Available Functions1 — Sets the maximum torque as a percentage of the motor’s or motor group’s rated torque.
void setMaxTorque( double value, percentUnits units );
2 — Sets the maximum torque using physical torque units.
void setMaxTorque( double value, torqueUnits units );
Parameters3 — Sets the maximum torque by limiting motor current.
void setMaxTorque( double value, currentUnits units );
Parameter |
Type |
Description |
|---|---|---|
|
|
The maximum torque value to set. |
|
|
Specifies torque as a percentage:
|
|
|
Specifies torque in physical units such as:
|
|
|
Specifies torque by limiting motor current:
|
This function does not return a value.
NotesWhen using
currentUnits, torque is limited by restricting the motor’s or motor group’s current draw.
setTimeout#
Sets a time limit for how long a motor command will wait to reach its target. If the motor or motor group cannot complete the movement within the set time, it will stop automatically and continue with the next command.
Available Functionsvoid setTimeout(
int32_t time,
timeUnits units );
Parameter |
Type |
Description |
|---|---|---|
|
|
The maximum amount of time the motor or motor group is allowed to attempt reaching a target position. |
|
|
The unit used to represent
|
The motor’s or motor group’s time limit is used to prevent motor commands that do not reach their target position from stopping the execution of other commands in the stack.
If the motor or motor group does not reach its target position before the timeout expires, it will stop automatically.
The timeout applies to functions that have a target value such as
spinForandspinToPosition.
// Stop a long move after 1 seconds
Motor1.setTimeout(1, seconds);
Motor1.spinFor(forward, 2, turns);
Motor1.spinToPosition(0, degrees);
isDone#
Returns a Boolean indication whether a motor or motor group is currently not spinning.
Available Functionsbool isDone();
This function does not accept any parameters.
Return ValuesReturns a Boolean indicating whether a target-based motor movement has completed:
true— The motor or motor group has finished its target-based movement.false— The motor or motor group is still attempting to reach its target.
isDoneonly applies to target-based movement functions such asspinForandspinToPosition.It does not apply to continuous movement functions such as
spin.
// Spin forward until the motor is done spinning
Motor1.spinFor(forward, 200, degrees, false);
while (true) {
if (Motor1.isDone()) {
Drivetrain.stop();
} else {
Drivetrain.drive(forward);
}
}
isSpinning#
Returns a Boolean indicating whether a motor or motor group is currently spinning.
Available Functionsbool isSpinning();
This function does not accept any parameters.
Return ValuesReturns a Boolean indicating whether the motor or motor group is currently spinning:
true— The motor or motor group is actively spinning.false— The motor or motor group is not spinning.
It works with both continuous movement functions (such as
spin) and target-based functions (such asspinForandspinToPosition).This function reports motor activity, not whether a target has been reached. To check if a target-based movement has completed, use
isDone.
// Spin forward until the motor is done spinning
Motor1.spinFor(forward, 200, degrees, false);
while (true) {
if (Motor1.isSpinning()) {
Drivetrain.drive(forward);
} else {
Drivetrain.stop();
}
}
position#
Returns the current position of a motor or motor group.
Available Functionsdouble position(
rotationUnits units );
Parameter |
Type |
Description |
|---|---|---|
|
|
The unit used to represent the motor or motor group position:
|
Returns a double representing the current encoder position of the motor or motor group in the specified units.
Calling
setPositionchanges the reference point for this value.The returned value reflects the current encoder reading and does not indicate whether the motor or motor group is actively moving.
When called on a
motor_group, this function returns the position of the first motor in the group.
// Spin, then show the final position
Motor1.spin(forward);
wait(1, seconds);
Motor1.stop();
Brain.Screen.print("Pos: %f", Motor1.position(degrees));
velocity#
Returns the current velocity of a motor or motor group.
Available Functionsdouble velocity(
velocityUnits units );
Parameter |
Type |
Description |
|---|---|---|
|
|
The unit used to represent velocity:
|
Returns a double representing the motor’s or motor group’s current velocity in the specified units.
Notes
When called on a
motor_group, this function returns the velocity of the first motor in the group.
current#
Returns the current of a motor or motor group.
Available Functions1 — Returns the motor’s or motor group’s electrical current in amps.
double current( currentUnits units = amp );
Parameters2 — Returns the motor’s or motor group’s electrical current as a percentage of maximum rated current.
double current( percentUnits units );
Parameter |
Type |
Description |
|---|---|---|
|
|
The unit used to represent current:
|
|
|
Represents the current as percentage of the motor’s maximum rated current:
|
Returns a double representing the motor’s or motor group’s electrical current in the specified units.
Notes
When called on a
motor_group, this function returns the sum electrical current for all motors in the group.
power#
Returns the electrical power supplied to the motor or motor group.
Available Functionsdouble power(
powerUnits units = watt );
Parameters
Parameter |
Type |
Description |
|---|---|---|
|
|
The unit used to represent the motor power:
|
Return Values
Returns a double representing the motor’s or motor group’s electrical power in watts.
Notes
When called on a
motor_group, this function returns the electrical power of the first motor in the group.
torque#
Returns the mechanical output torque of the motor or motor group.
Available Functionsdouble torque(
torqueUnits units = Nm );
Parameters
Parameter |
Type |
Description |
|---|---|---|
|
|
The unit used to represent the motor torque:
|
Return Values
Returns a double representing the motor’s or motor group’s mechanical output torque in the specified units.
Notes
When called on a
motor_group, this function returns the mechanical output torque of the first motor in the group.
efficiency#
Returns the efficiency of the motor or motor group.
Available Functionsdouble efficiency(
percentUnits units = percent );
Parameter |
Type |
Description |
|---|---|---|
|
|
The unit used to represent motor efficiency:
|
Return Values
Returns a double representing the motor’s or motor group’s efficiency as a percentage.
Notes
When called on a
motor_group, this function returns the efficiency of the first motor in the group.
temperature#
Returns the temperature of the motor or motor group.
Available Functions1 — Returns motor or motor group temperature as a percentage of maximum operating temperature.
double temperature( percentUnits units = percent );
2 — Returns motor or motor group temperature in physical temperature units.
double temperature( temperatureUnits units );
Parameters
Parameter |
Type |
Description |
|---|---|---|
|
|
The unit used to represent motor or motor group temperature as a percentage:
|
|
|
The unit used to represent motor or motor group temperature in degrees:
|
Return Values
Returns a double representing the motor’s or motor group’s temperature in the specified units.
Notes
The operating temperature range for the motor is approximately
20°C(68°F) to70°C(158°F).When called on a
motor_group, this function returns the temperature of the first motor in the group.
voltage#
Returns the electrical voltage supplied to the motor or motor group.
Available Functionsdouble voltage(
voltageUnits units = volt );
Parameters
Parameter |
Type |
Description |
|---|---|---|
|
|
The unit to represent the voltage:
|
Return Values
Returns a double representing the motor’s or motor group’s electrical voltage in the specified units.
Notes
When called on a
motor_group, this function returns the electrical voltage of the first motor in the group.
direction#
Returns the direction that the motor or motor group is currently spinning.
Available FunctionsdirectionType direction();
Parameters
This function does not take any parameters.
Return Values
Returns a directionType indicating the motor’s or motor group’s current direction:
forward— The motor or motor group is spinning forward / in a positive direction.reverse— The motor is spinning or motor group in reverse / in a negative direction.
installed#
Returns whether the motor or motor group is connected to the V5 Brain.
Available Functionsbool installed();
Parameters
This function does not take any parameters.
Return Values
Returns a bool indicating if the motor or motor group is connected.
true— The motor or motor group is connected.false— The motor or motor group is not connected.
count#
Returns the number of motors currently included in the motor group.
Available Functionsint32_t count();
Parameters
This function does not take any parameters.
Return Values
Returns an int32_t representing the total number of motors in the motor group.