汽车和汽车集团#

介绍#

VEX V5 Motion 类别提供了与电机和电机组交互的方法。

This page uses motor_1 and motor_group_1 as the example motor and motor group name respectively. Replace them with your own configured names as needed.

以下是所有方法的列表:

动作 – 控制电机的运动。

  • spin – Spins a motor or motor group in a specified direction indefinitely.

  • spinFor – Spins a motor or motor group for a specified distance.

  • spinToPosition – Spins a motor to an absolute position.

  • stop – Stops a motor or motor group with configurable behavior.

变异器——改变引擎的不同属性。

  • setPosition – Sets the motor or motor group’s position to a specific value.

  • setVelocity – Sets the default velocity for the motor or motor group.

  • setStopping – Sets the stop behavior (brake, coast, or hold).

  • setMaxTorque – Sets the maximum torque for a motor or motor group.

  • setTimeout – Limits how long a motor function waits before giving up if movement is blocked.

  • setReversed – Sets only a motor to have its direction be reversed.

  • resetPosition – Sets only a motor’s position to 0.

  • convertVelocity – Converts the velocity of a motor or motor group to a different unit.

Getters – 返回来自电机的数据。

  • isDone – Returns whether a motor or motor group is currently not spinning.

  • isSpinning – Returns whether a motor or motor group is currently spinning.

  • position – Returns a motor or motor group’s current position.

  • velocity – Returns a motor or motor group’s current velocity.

  • current – Returns the current being used by a motor or motor group.

  • direction – Returns the spin direction of a motor or motor group.

  • power – Returns the amount of power being used by a motor or motor group.

  • torque – Returns the torque generated by a motor or motor group.

  • efficiency – Returns the efficiency of a motor or motor group.

  • temperature – Returns the temperature of a motor or motor group.

  • getTimeout – Returns the current timeout for a motor or motor group as an integer in milliseconds.

  • getMotorCartridge – Returns the gear cartridge setting for a motor.

  • command – Returns the last velocity sent to a motor as a double.

  • installed – Returns a Boolean indicating whether a motor is connected.

  • value – Returns the value of a motor as an integer.

  • count – Returns the number of motors in a motor group as an integer.

构造函数 – 手动初始化和配置电机及电机组。

行动#

spin#

spin spins a motor or motor group in the specified direction indefinitely.

Default Usage:
Motor1.spin(direction);

Overload Usages:
Motor1.spin(direction, velocity, velocityUnits);
Motor1.spin(direction, voltage, voltageUnits);

参数

描述

direction

The direction in which to spin the motor or motor group:

  • forward
  • reverse

velocity

电机或电机组的旋转速度将达到两倍。

velocityUnits

The unit that will represent the velocity:

  • percent
  • dps – degrees per second
  • rpm – rotations per minute

voltage

电机运转所需的电压。

voltageUnits

The unit that will represent the voltage:

  • voltageUnits::volt – volts
  • voltageUnits::mV – millivolts
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Spin forward, then stop
  Motor1.spin(forward);
  wait(1, seconds);
  Motor1.stop();
}

spinFor#

spinFor spins a Motor or Motor Group for a set amount in a specified direction.

Default Usage:
Motor1.spinFor(direction, rotation, units, wait);

Overload Usages:
Motor1.spinFor(rotation, units, velocity, units_v, wait);
Motor1.spinFor(direction, rotation, units, velocity, units_v, wait);
Motor1.spinFor(rotation, units, wait);
Motor1.spinFor(time, units_t, velocity, units_v);
Motor1.spinFor(direction, time, units_t, velocity, units_v);
Motor1.spinFor(time, units_t);
Motor1.spinFor(direction, time, units_t);

参数

描述

direction

The direction in which to spin the motor or motor group:

  • forward
  • reverse

rotation

电机能旋转多远。

units

The unit that will represent the rotation:

  • degrees
  • turns

wait

Optional.

  • true (default) – The robot waits until spinFor is complete before executing the next line of code.
  • false – The robot starts the action and moves on to the next line of code right away, without waiting for spinFor to finish.

velocity

电机或电机组的旋转速度将达到两倍。

units_v

The unit that will represent the velocity:

  • dps – degrees per second
  • rpm – rotations per minute
  • velocityUnits::pct – percent

time

The amount of time the motor will spin for. wait cannot be used when using the time parameter.

units_t

The unit that represents the time:

  • seconds
  • msec – milliseconds
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Spin 1 turn fast, then 1 turn slow
  Motor1.spinFor(forward, 1, turns, true);
  Motor1.spinFor(reverse, 1, turns, 20, velocityUnits::pct, true);
}

spinToPosition#

spinToPosition spins the motor or motor group to a specified position.

Default Usage:
Motor1.spinToPosition(position, units, wait);

Overload Usages:
Motor1.spinToPosition(position, positionUnits, velocity, velocityUnits, wait);

参数

描述

position

电机或电机组将旋转到的位置。

units

The unit that will represent the position:

  • degrees
  • turns

wait

Optional.

  • true (default) – The robot waits until spinToPosition is complete before executing the next line of code.
  • false – The robot starts the action and moves on to the next line of code right away, without waiting for spinToPosition to finish.

velocity

电机或电机组的旋转速度将达到两倍。

velocityUnits

The unit that will represent the velocity:

  • dps – degrees per second
  • rpm – rotations per minute
  • velocityUnits::pct – percent
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Spin forward, then go to 90°
  Motor1.spin(forward);
  wait(1, seconds);
  Motor1.spinToPosition(90, degrees);
}

stop#

stop stops the motor or motor group from spinning.

Default Usage:
Motor1.stop();

Overload Usages:
Motor1.stop(mode);

参数

描述

mode

How the motor or motor group will stop:

  • brake – Stops immediately.
  • coast – Slows gradually to a stop.
  • hold – Stops and resists movement using motor feedback.

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

  // Spin and coast to a stop
  Motor1.setVelocity(100, percent);
  Motor1.spin(forward);
  wait(1, seconds);
  Motor1.stop(coast);
}

变异体#

setPosition#

setPosition sets a specific position value to a motor or motor group, which updates the encoder reading.

Usage:
Motor1.setPosition(position, units);

参数

描述

position

指定电机或电机组将旋转到的位置。

units

The unit that will represent the rotation:

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

  // Set a new zero, then go to 0°
  Motor1.setPosition(180, degrees);
  Motor1.spinToPosition(0, degrees);
}

setVelocity#

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.

Usage:
Motor1.setVelocity(velocity, units);

参数

描述

velocity

电机或电机组的旋转速度将达到两倍。

units

The unit that represents the velocity:

  • percent
  • rpm – rotations per minute
  • dps – degrees per second.
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // 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#

setStopping sets how a motor or motor group behaves when it stops.

Usage:
Motor1.setStopping(mode);

参数

描述

mode

How the motor or motor group will stop:

  • brake – Stops immediately.
  • coast – Slows gradually to a stop.
  • hold – Stops and resists movement using motor feedback.

setMaxTorque#

setMaxTorque sets the maximum torque that a motor or motor group can spin at.

Usage:
Motor1.setMaxTorque(value, units);

参数

描述

value

电机或电机组能够达到的最大扭矩。

units

The unit that represents the torque:

  • percent
  • Nm – Newton meters.
  • InLb – inch pounds.
  • amp – amps.

setTimeout#

setTimeout sets a time limit for how long a motor command will wait to reach its target. If the robot cannot complete the movement within the set time, it will stop automatically and continue with the next command.

**注意:**电机的时间限制用于防止未到达目标位置的电机指令停止堆栈中其他指令的执行。

Usage:
Motor1.setTimeout(time, units);

参数

描述

time

电机指令在停止并执行下一个指令之前将运行的最大时间单位数。

units

The unit that represents the timeout:

  • seconds
  • msec – milliseconds
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Stop a long move after 1 seconds
  Motor1.setTimeout(1, seconds);
  Motor1.spinFor(forward, 2, turns);
  Motor1.spinToPosition(0, degrees);
}

setReversed#

setReversed sets the motor to be reversed. This method works the same as setting the reverse parameter to true when constructing a motor.

Usage:
Motor1.setReversed(reversed);

参数

描述

reversed

A Boolean representing whether or not the motor is reversed:

  • true – Reverse the motor direction.
  • false – Return to the motor’s default configuration.

resetPosition#

resetPosition sets the motor or motor group’s position to 0.

Usage:
Motor1.resetPosition();

参数

描述

此方法没有参数。

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

  // Spin to 360°, then reset the encoder
  Motor1.spinToPosition(360, degrees);
  Motor1.resetPosition();

  // Now spin to 90° from this new zero point
  Motor1.spinToPosition(90, degrees);
}

convertVelocity#

convertVelocity converts the velocity in the given units to specified units based on the motor gearing.

Usage:
Motor1.convertVelocity(velocity, units, unitsout)

参数

描述

velocity

要转换的速度。

units

  • rpm – rotations per minute
  • dps – degrees per second.

unitsout

  • rpm – rotations per minute
  • dps – degrees per second.

获取器#

isDone#

isDone returns a Boolean indicating whether a motor or motor group is not currently spinning.

  • 1 – The motor or motor group is done spinning.

  • 0 – The motor or motor group is still spinning.

Usage:
Motor1.isDone()

参数

描述

此方法没有参数。

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

  // 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#

isSpinning returns a Boolean indicating whether a motor or motor group is currently spinning.

  • 1 – The motor or motor group is spinning.

  • 0 – The motor or motor group is not spinning.

Usage:
Motor1.isSpinning()

参数

描述

此方法没有参数。

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

  // 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#

position returns the current position of a motor or motor group as a double.

Usage:
Motor1.position(units)

参数

描述

units

The unit that will represent the position:

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

  // Spin, then show the final position
  Motor1.spin(forward);
  wait(1, seconds);
  Motor1.stop();
  Brain.Screen.print("Pos: %f", Motor1.position(degrees));
}

velocity#

velocity returns the current velocity of a motor or motor group as a double.

Usage:
Motor1.velocity(units)

参数

描述

units

The unit that will represent the velocity:

  • percent
  • rpm – rotations per minute
  • dps – degrees per second

current#

current returns the current of a motor or motor group as a double.

Usage:
Motor1.current(units)

参数

描述

units

The unit that represents the current:

  • amp – amps.
  • percent

power#

power returns the current power of the motor as a double.

Usage:
Motor1.power(units)

参数

描述

units

The unit that represents the power:

  • watt

torque#

torque returns the current torque of the motor as a double.

Usage:
Motor1.torque(units)

参数

描述

units

The unit that represents the torque:

  • Nm – Newton meters.
  • InLb – inch pounds.
  • amp – amps.

efficiency#

efficiency returns the efficiency of the motor in percent as a double.

Usage:
Motor1.efficiency()

参数

描述

此方法没有参数。

temperature#

temperature returns the current temperature of the motor as a double.

Usage:
Motor1.temperature(units)

参数

描述

units

The unit that represents the temperature:

  • celsius
  • fahrenheit

direction#

direction returns the current direction a motor or motor group is spinning in:

  • forward

  • reverse

Usage:
Motor1.direction()

参数

描述

此方法没有参数。

getTimeout#

getTimeout returns the current timeout for a motor or motor group as an integer in milliseconds.

Usage:
Motor1.getTimeout()

参数

描述

此方法没有参数。

getMotorCartridge#

getMotorCartridge returns the gear cartridge setting for a motor in:

  • ratio36_1: A 36:1 gear ratio. A red gearbox for a V5 SmartMotor.

  • ratio18_1: A 18:1 gear ratio. A green gearbox for a V5 SmartMotor.

  • ratio6_1: A 6:1 gear ratio. A blue gearbox for a V5 SmartMotor.

Usage:
Motor1.getMotorCartridge()

参数

描述

此方法没有参数。

command#

command returns the last velocity sent to a motor as a double.

Usage:
Motor1.command(units)

参数

描述

units

  • rpm – rotations per minute
  • dps – degrees per second.

    installed#

    installed returns a Boolean indicating whether a motor is connected.

    • 1 – The motor is connected.

    • 0 – The motor is not connected.

    Usage:
    Motor1.installed()

    参数

    描述

    此方法没有参数。

    value#

    value returns the value of a motor as an integer.

    Usage:
    Motor1.value()

    参数

    描述

    此方法没有参数。

    count#

    count returns the number of motors in a motor group as an integer. This method will not work with individual Motors.

    Usage:
    MotorGroup1.count()

    参数

    描述

    此方法没有参数。

    构造函数#

    Constructors are used to manually create motor and motor_group objects, which are necessary for configuring a motor or motor group outside of VEXcode.

    motor#

    motor creates an object of the motor Class in the specified port.

    Default Usage:
    motor Motor1 = motor(port);

    过载使用次数:

    motor Motor1 = motor(port, reverse);

    motor Motor1 = motor(port, gearRatio);

    motor Motor1 = motor(port, gearRatio, reverse);

    参数

    描述

    port

    Which Smart Port that the Motor is connected to as PORT followed by the port number, ranging from 1 to 21.

    reverse

    A Boolean indicating whether or not the Motor’s directions are configured in reverse:

    • true – The Motor is reversed.
    • false – The Motor is not reversed.

    gearRatio

    The gear ratio of the Motor as one of the following:

    • ratio36_1: A 36:1 gear ratio. A red gearbox for a V5 SmartMotor.
    • ratio18_1: A 18:1 gear ratio. A green gearbox for a V5 SmartMotor.
    • ratio6_1: A 6:1 gear ratio. A blue gearbox for a V5 SmartMotor.
    int main() {
      // Initializing Robot Configuration. DO NOT REMOVE!
      vexcodeInit();
    
      // Make a reversed motor on Port 1
      motor Motor1 = motor(PORT1, true);
    }
    
    
    int main() {
      // Initializing Robot Configuration. DO NOT REMOVE!
      vexcodeInit();
    
      // Construct a V5 Motor with a 6:1 Turbo Gear Cartridge.
      motor Motor1 = motor(PORT1, ratio6_1);
    }
    
    
    int main() {
      // Initializing Robot Configuration. DO NOT REMOVE!
      vexcodeInit();
    
      // Construct a V5 Motor with a 36:1 Torque Gear Cartridge
      // that runs in reverse.
      motor Motor1 = motor(PORT1, ratio36_1, true);
    }
    
    

    motor_group#

    motor_group creates an object of the motor_group Class in the specified port. A motor_group is a collection of individual motor objects.

    Usage:
    motor_group MotorGroup1 = motor_group(motor1, motor2);

    参数

    描述

    motor

    The configured motor to add to the motor_group. Up to 12 motor objects can be added to the group.

    int main() {
      // Initializing Robot Configuration. DO NOT REMOVE!
      vexcodeInit();
    
      // Create two motors
      motor motorA = motor(PORT1, ratio18_1 false);
      motor motorB = motor(PORT2, ratio18_1, false);
    
      // Create a motor group with those motors
      motor_group MotorGroup1 = motor_group(motorA, motorB);
    }