发动机#

介绍#

Motors and motor groups control how parts of a robot move. A Motor controls one configured IQ Smart Motor, while a MotorGroup controls multiple configured IQ Smart Motors together so they move in tandem. Motors and motor groups can be used to raise an arm, turn a claw, spin a wheel, or move another part of a build. Two motors can work together as a drivetrain to move and turn the whole robot.

Each motor or motor group is configured in the Devices window. Depending on the build, motor and motor group names can change. This page uses motor_1 and motor_group_1 as example names. Replace them with your own configured names as needed.

VEX IQ(第二代)电机,前进方向用加号表示,箭头表示向左(或逆时针)旋转。 VEX IQ(第二代)电机显示反向旋转,用减号和箭头表示其向右(顺时针)旋转。

By default, FORWARD spins a motor counterclockwise, and REVERSE spins a motor clockwise. If a motor is set to reverse, those directions will be switched.

电机和电机组的编码方式有很多种。以下列出了所有 Motor 和 MotorGroup 方法:

操作——停止和旋转电机及电机组。

  • spin — Spins a motor or motor group forward or reverse forever.

  • spin_for — Spins a motor or motor group for a specified distance.

  • spin_to_position — Spins a motor or motor group to a specific position.

  • stop — Stops a motor or motor group from spinning.

变异器 — 调整电机和电机组设置。

  • set_velocity — Tells a motor or motor group how fast to spin.

  • set_max_torque — Sets how hard a motor or motor group is allowed to push while spinning.

  • set_position — Changes the motor or motor group’s current position to a new value.

  • set_stopping — Sets how a motor or motor group will stop moving: by braking, coasting, or holding.

  • set_timeout — Sets how much time a motor or motor group will try to finish a movement.

  • set_reversed — Changes whether a motor’s spin direction is reversed.

  • reset_position — Changes a motor or motor group’s current position to 0.

Getters — 检查电机和电机组状态。

  • position — Returns the motor or motor group’s current position.

  • velocity — Returns how fast the motor or motor group is spinning.

  • current — Returns how much electrical current the motor or motor group is using.

  • is_done — Returns whether the motor or motor group is finished moving, as a Boolean value.

  • is_spinning — Returns whether the motor or motor group is spinning, as a Boolean value.

  • direction — Returns the direction the motor or motor group is spinning.

  • power — Returns how much power the motor or motor group is using.

  • torque — Returns how much torque the motor or motor group is using.

  • efficiency — Returns how efficiently the motor or motor group is using power.

  • temperature — Returns the temperature of the motor or motor group.

  • count — Returns the number of motors in a motor group.

  • get_timeout — Returns the motor or motor group’s current timeout duration.

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

行动#

spin#

spin spins a motor or motor group forward or reverse forever. The motor or motor group will continue to spin until it is given another action, like spinning in a different direction or stopping.

Usage:
motor_1.spin(direction, velocity, units)

参数

描述

direction

The direction the motor or motor group spins: FORWARD or REVERSE.

velocity

Optional. The velocity to spin with from 0% to 100% when using PERCENT. This can be an integer or decimal (float).

units

Optional. The velocity unit:

  • PERCENT
  • RPM — Rotations per minute
  • VelocityUnits.DPS — Degrees per second

# Spin the motor forward, then stop
motor_1.spin(FORWARD)
wait(1, SECONDS)
motor_1.stop()

spin_for#

spin_for spins a motor or motor group for a specific distance. The spin is relative to the current position of the motor or motor group. The project will wait until the motor or motor group is done spinning before the next line of code runs.

Usage:
motor_1.spin_for(direction, angle, units, velocity, units_v, wait)

参数

描述

direction

The direction the motor or motor group spins: FORWARD or REVERSE.

angle

The distance the motor or motor group spins. This can be an integer or decimal (float).

units

Optional. The distance unit: DEGREES (default) or TURNS.

velocity

Optional. The velocity to spin with from 0% to 100% when using PERCENT. This can be an integer or decimal (float).

units_v

Optional. The velocity unit:

  • PERCENT
  • RPM (default) — Rotations per minute

wait

Optional.

  • wait=True (default) — Makes the project wait until the motor or motor group is done spinning before the next line of code runs.
  • wait=False — Makes the next line of code run right away.

# Spin the motor forward, then put it
# at 90 degrees
motor_1.spin(FORWARD)
wait(2, SECONDS)
motor_1.spin_to_position(90, DEGREES)

# Spin the motor at different velocities
# Using default velocity
motor_1.spin_for(FORWARD, 150, MM)
wait(1, SECONDS)
# Spin slower
motor_1.spin_for(REVERSE, 150, MM, 20, PERCENT)
wait(1, SECONDS)
# Spin faster
motor_1.spin_for(FORWARD, 150, MM, 100, PERCENT)

spin_to_position#

spin_to_position spins a motor or motor group to a specific position.

A motor or motor group’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. At the beginning of a project, the motor or motor group position is set to 0 degrees. The motor or motor group position can also be set using the set_position method.

位置值是绝对值。这意味着旋转方向取决于电机或电机组的当前位置。

例如,如果电机或电机组从 0 度开始旋转到 720 度,它将正转两圈。如果之后它旋转到 360 度,它将反转一圈,因为 360 小于 720。

Usage:
motor_1.spin_to_position(rotation, units, velocity, units_v, wait)

参数

描述

rotation

The position value the motor or motor group will spin to. This can be an integer or decimal (float).

units

The position unit: DEGREES or TURNS.

velocity

Optional. The velocity to spin with from 0% to 100% when using PERCENT. This can be an integer or decimal (float).

units_v

Optional. The velocity unit:

  • PERCENT
  • RPM — Rotations per minute
  • VelocityUnits.DPS — Degrees per second

wait

Optional.

  • wait=True (default) — Makes the project wait until the motor or motor group is done spinning before the next line of code runs.
  • wait=False — Makes the next line of code run right away.

# Spin the motor forward, then
# put it at 90 degrees
motor_1.spin(FORWARD)
wait(2, SECONDS)
motor_1.spin_to_position(90, DEGREES)

# Spin forward then quickly reverse
# to 0 degrees
motor_1.spin(FORWARD)
wait(2, SECONDS)
motor_1.spin_to_position(0, DEGREES, 100, PERCENT, wait=True)

stop#

stop stops a motor or motor group from spinning.

Usage:
motor_1.stop(mode)

参数

描述

mode

Optional. How the motor or motor group will stop:

  • COAST — Slows to a stop.
  • BRAKE (default) — Stops immediately.
  • HOLD — Stops immediately and holds the motor’s position.

# Spin the motor forward, then stop
motor_1.spin(FORWARD)
wait(1, SECONDS)
motor_1.stop()

# Spin the motor and coast to a stop
motor_1.set_velocity(100, PERCENT)
motor_1.spin(FORWARD)
wait(2, SECONDS)
motor_1.stop(COAST)

修改器#

set_velocity#

set_velocity tells a motor or motor group how fast to spin. A higher percentage makes the motor or motor group spin faster and a lower percentage makes the motor or motor group spin slower.

每个项目开始时,每个电机或电机组默认以 50% 的速度旋转。

**注意:**更高的速度会使电机或电机组旋转得更快,但精度可能会降低。更低的速度会使电机或电机组旋转得更慢,但精度可能会更高。

Usage:
motor_1.set_velocity(velocity, units)

参数

描述

velocity

The velocity to spin with from 0% to 100% when using PERCENT. This can be an integer or decimal (float).

units

Optional. The velocity unit:

  • PERCENT
  • RPM — Rotations per minute
  • VelocityUnits.DPS — Degrees per second

# Spin the motor at different velocities
# Using default velocity
motor_1.spin_for(FORWARD, 150, MM)
wait(1, SECONDS)
# Spin slower
motor_1.set_velocity(20, PERCENT)
motor_1.spin_for(REVERSE, 150, MM)
wait(1, SECONDS)
# Spin faster
motor_1.set_velocity(100, PERCENT)
motor_1.spin_for(FORWARD, 150, MM)

set_max_torque#

扭矩表示电机或电机组在旋转时能够产生的推力或拉力的大小。

set_max_torque sets the most torque a motor or motor group is allowed to use.

较高的百分比可以让电机或电机组施加更大的推力,例如在举起重物时。较低的百分比则会限制电机或电机组的推力。这有助于在电机或电机组卡住或达到其运动极限时保护机器人。

每个项目开始时,每个电机或电机组的扭矩默认设置为 50%。

Usage:
motor_1.set_max_torque(value, units)

参数

描述

value

The max torque the motor or motor group can use. This can be an integer or decimal (float).

units

The torque unit:

  • PERCENT
  • TorqueUnits.NM — Newton meters
  • TorqueUnits.INLB — Inch pounds

# Spin the motor at different torque values
# Default
motor_1.spin_to_position(45, DEGREES)
# Spin at a lower torque
motor_1.set_max_torque(20, PERCENT)
motor_1.spin_to_position(0, DEGREES)
# Spin at the max torque
motor_1.set_max_torque(100, PERCENT)
motor_1.spin_to_position(45, DEGREES)

set_position#

A motor or motor group’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. set_position changes the motor or motor group’s current position to a new value.

例如,如果电机或电机组旋转了 180 度,将其位置设置为 0 度会将该位置从 180 度重置为 0 度。然后,电机或电机组可以根据该新值旋转到相应位置。

Usage:
motor_1.set_position(position, units)

参数

描述

position

The position value to set for the motor or motor group. This can be an integer or decimal (float).

units

The position unit: DEGREES or TURNS.

# Position the motor at the
# new 0 degrees
motor_1.set_position(180, DEGREES)
motor_1.spin_to_position(0)

set_stopping#

set_stopping sets how a motor or motor group will stop moving: by braking, coasting, or holding.

Usage:
motor_1.set_stopping(mode)

参数

描述

mode

How the motor or motor group will stop:

  • BRAKE — Stops immediately.
  • COAST — Slows to a stop.
  • HOLD — Stops immediately and holds the motor’s position.

# Spin the motor and coast to a stop
motor_1.set_velocity(100, PERCENT)
motor_1.set_stopping(COAST)
motor_1.spin(FORWARD)
wait(2, SECONDS)
motor_1.stop()

set_timeout#

set_timeout sets how much time a motor or motor group will try to finish a movement. If the motor or motor group cannot finish in that time, it will stop trying and move on to the next line of code. This keeps the motor or motor group from getting stuck on a movement.

Usage:
motor_1.set_timeout(value, units)

参数

描述

value

The amount of time the motor or motor group can try to finish a movement. This can be a positive integer or decimal (float).

units

Optional. The unit of time:

  • SECONDS
  • MSEC (default) — Milliseconds

# Set a 0.5 second timeout for motor actions
motor_1.set_timeout(0.5, SECONDS)

# Try a long move that will time out early
motor_1.spin_for(FORWARD, 2, TURNS)
motor_1.spin_to_position(0, DEGREES)

set_reversed#

set_reversed changes whether a motor’s spin direction is reversed. This method works the same as setting the reverse parameter to True when constructing a Motor.

注意:此方法适用于电机,而不适用于电机组。

Usage:
motor_1.set_reversed(value)

参数

描述

value

Whether the motor’s direction is reversed:

  • True — Reverses the motor’s direction.
  • False — Returns the motor’s direction to its default.

# Change the reverse direction while spinning
motor_1.spin_for(FORWARD, 45)
motor_1.set_reversed(True)
motor_1.spin_for(FORWARD, 45)

reset_position#

reset_position changes the motor or motor group’s current position to 0.

Usage:
motor_1.reset_position()

参数

描述

该方法没有参数。

# Spin to 360 degrees, then reset the encoder
motor_1.spin_to_position(360, DEGREES)
motor_1.reset_position()

# Move again to 90 degrees from new zero point
motor_1.spin_to_position(90, DEGREES)

吸气剂#

position#

A motor or motor group’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. position returns the motor or motor group’s current position.

项目开始时,电机或电机组的位置设置为 0 度。如果电机或电机组正转一圈,则位置为 360 度或 1 圈。如果电机或电机组反向旋转,则位置为负值。

Usage:
motor_1.position(units)

参数

描述

units

Optional. The unit to return the motor or motor group position in: DEGREES (default) or TURNS.

# Spin the motor and display the
# ending position
motor_1.spin(FORWARD)
wait(1, SECONDS)
motor_1.stop()
brain.screen.print(motor_1.position())

velocity#

velocity returns how fast the motor or motor group is spinning.

正值表示电机或电机组正转,负值表示电机或电机组反转。

Usage:
motor_1.velocity(units)

参数

描述

units

Optional. The velocity unit to return:

  • PERCENT
  • RPM (default) — Rotations per minute
  • VelocityUnits.DPS — Degrees per second

current#

current returns how much electrical current the motor or motor group is using, measured in amps from 0.0 to 1.2 A. Current is the amount of electricity flowing through the motor or motor group.

电流值越高,表示电机或电机组消耗的电流越大。这种情况可能发生在电机或电机组提升重物、推动物体或试图移动卡住的物体时。

这可以用来检查电机或电机组在运动过程中是否运转吃力。如果电流持续过高,电机可能会发热或功率利用率降低。

Usage:
motor_1.current(units)

参数

描述

units

Optional. The current unit to return:

  • CurrentUnits.AMP — Amps

is_done#

is_done returns whether the motor or motor group is finished moving, as a Boolean value. This can be used to control the timing of other behaviors based on the motor’s movement.

  • True — The motor or motor group is finished moving.

  • False — The motor or motor group is still moving.

This method works together with the following Motion methods that have the wait parameter: spin_for and spin_to_position.

Usage:
motor_1.is_done()

参数

描述

该方法没有参数。

is_spinning#

is_spinning returns whether the motor or motor group is spinning, as a Boolean value. This can be used to control the timing of other behaviors based on the motor’s movement.

  • True — The motor or motor group is spinning.

  • False — The motor or motor group is not spinning.

This method works together with the following Motion methods that have the wait parameter: spin_for and spin_to_position.

Usage:
motor_1.is_spinning()

参数

描述

该方法没有参数。

direction#

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

  • FORWARD

  • REVERSE

Usage:
motor_1.direction()

参数

描述

该方法没有参数。

power#

power returns how much power the motor or motor group is using in watts. Power shows how quickly the motor or motor group is using energy.

功率值越高,意味着电机或电机组的能耗速度越快。这种情况可能发生在电机或电机组提升重物、推动物体或试图在卡住时移动时。

这可以用来比较运动情况,或者检查电机或电机组是否运转吃力。如果功率持续偏高,电机可能会发热或能量利用效率降低。

Usage:
motor_1.power()

参数

描述

该方法没有参数。

torque#

扭矩表示电机或电机组在旋转时扭转、推动或拉动的力度。

torque returns how much torque the motor or motor group is using.

扭矩值越高,意味着电机或电机组的推拉力越大。这种情况可能发生在电机或电机组提升重物、推动物体或试图移动卡住的物体时。

这可以用来检查运动系统或运动系统是否出现困难,或者比较不同动作需要多少推力。

To set the torque of a motor or motor group, use set_max_torque.

Usage:
motor_1.torque(units)

参数

描述

units

The torque unit to return:

  • TorqueUnits.NM (default) — Newton meters
  • TorqueUnits.INLB — Inch pounds

efficiency#

efficiency returns how efficiently the motor or motor group is using power, as a percentage from 0% to 100%.

效率值表示电机或电机组的功率有多少用于运动。效率值越高,意味着电机或电机组用于运动的功率越多。当电机或电机组高负荷运转但运动量很小时,例如被卡住或抵住障碍物时,效率值就会降低。

这可以用来比较运动情况,或者检查电机或电机组是否浪费电力而不是将其用于运动。

Usage:
motor_1.efficiency()

参数

描述

该方法没有参数。

temperature#

temperature returns the temperature of the motor or motor group.

电机温度显示电机或电机组的温度。温度越高,表示电机或电机组在工作过程中温度越高。为了确保电机始终以最佳性能运行,其温度应保持在 55 摄氏度以下。

如果电机或电机组过热,它会降低最大电流以保护自身。当温度达到 70 摄氏度时,电机将停止运转,直到冷却下来。

这可以用来检查电机或电机组在重复运动、长时间运行或推动物体时是否过热。

Usage:
motor_1.temperature(units)

参数

描述

units

Optional. The temperature unit to return:

  • TemperatureUnits.CELSIUS (default)
  • TemperatureUnits.FAHRENHEIT

count#

count returns the number of motors in a motor group.

注意:此方法适用于电机组,而不适用于电机。

Usage:
motor_group_1.count()

参数

描述

该方法没有参数。

get_timeout#

get_timeout returns the motor or motor group’s current timeout in milliseconds.

注意:此方法适用于电机,而不适用于电机组。

Usage:
motor_1.get_timeout()

参数

描述

该方法没有参数。

构造函数#

Constructors are used to manually create Motor and MotorGroup objects outside of the Devices window.

Motor#

Motor creates a motor.

Usage:
motor_1 = Motor(smartport, gears, reverse)

范围

描述

smartport

The Smart Port that the motor is connected to, written as Ports.PORTx, where x is the port number.

gears

Optional. The motor’s gear ratio:

  • GearSetting.RATIO_1_1 (default) — 1:1 ratio
  • GearSetting.RATIO_2_1 — 2:1 ratio
  • GearSetting.RATIO_3_1 — 3:1 ratio

reverse

Optional. Sets whether the motor’s spin direction is reversed:

  • True — Reverses the motor.
  • False (default) — Does not reverse the motor.

# Create a reversed motor
motor_1 = Motor(Ports.PORT1, 1.0, True)

Motor Group#

MotorGroup creates a motor group.

Usage:
motor_group_1 = MotorGroup(motors)

范围

描述

motors

The names of previously created Motor objects to put in the motor group. Up to 12 Motor objects can be added to the group.

# Create two motors
motor_group_1_motor_a = Motor(Ports.PORT1, False)
motor_group_1_motor_b = Motor(Ports.PORT6, True)

# Create a motor group with those motors
motor_group_1 = MotorGroup(motor_group_1_motor_a, motor_group_1_motor_b)