传动系统#

介绍#

驱动系统使机器人能够连续移动或移动设定的距离,按度数或朝向某个方向旋转,并对其旋转方向的变化做出反应。

如果传动系统配置有惯性传感器GPS传感器陀螺仪传感器,则传动系统可以通过跟踪传感器的航向和旋转来更精确地进行前进、后退和转弯运动。

驱动系统类别还包含多种配置方法,可用于设置行驶和转弯速度、定义停止行为、应用超时以避免执行停滞,以及手动更新机器人的航向或旋转值。这些功能在设计自主行为或实时调整时提供了极大的灵活性。

This page uses drivetrain as the example Drivetrain name. Replace it with your own configured name as needed.

以下是可用方法列表:

操作——移动或转向机器人。

  • drive – Drives the robot continuously forward or in reverse using the current drive velocity.

  • drive_for – Drives the robot forward or in reverse for a set distance.

  • turn – Rotates the robot continuously left or right using the current turn velocity.

  • turn_for – Rotates the robot left or right for a specified angle.

  • turn_to_heading – Rotates the robot to face a specific absolute heading.

  • turn_to_rotation – Rotates the robot to reach a specific cumulative rotation.

  • stop – Stops the drivetrain using the configured stopping mode.

  • calibrate_drivetrain – Calibrates the drivetrain’s configured Inertial, GPS, or Gyro Sensor.

变异器 – 配置速度、停止行为和超时时间。

检查员——检查传动系统是否运转。

  • is_done – Returns whether the drivetrain is not currently moving.

  • is_moving – Returns whether the drivetrain is currently moving.

  • heading – Returns the drivetrain’s heading angle (0 to 359.99 degrees).

  • rotation – Returns how much the drivetrain has turned since the project started.

  • velocity – Returns the drivetrain’s current velocity.

  • current – Returns the drivetrain’s current draw.

  • power – Returns the drivetrain’s power usage.

  • torque – Returns the drivetrain’s torque.

  • efficiency – Returns the drivetrain’s efficiency.

  • temperature – Returns the drivetrain motors’ temperature.

  • get_timeout – Returns the current timeout setting.

构造函数 - 手动初始化传动系统。

行动#

drive#

drive moves the drivetrain forward or in reverse using the current drive velocity. This method runs continuously until another Drivetrain method interrupts it or the project stops.

Usage:
drivetrain.drive(direction, velocity, units)

参数

描述

direction

The direction in which the robot drives:

  • FORWARD
  • REVERSE

velocity

可选。传动系统运动的速度,以浮点数或整数表示。如果未指定速度或先前设置为,则默认速度为50%。

units

Optional. The unit to represent the velocity:

  • RPM (default) – Rotations per minute
  • PERCENT
  • VelocityUnits.DPS – Degrees per second
# Drive forward, then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()

# Drive forward at 360 degrees per second
drivetrain.drive(FORWARD, 360.0, VelocityUnits.DPS)

drive_for#

drive_for moves the drivetrain forward or in reverse for a specified distance using the current drive velocity.

Usage:
drivetrain.drive_for(direction, distance, units, velocity, units_v, wait)

参数

描述

direction

The direction in which the robot drives:

  • FORWARD
  • REVERSE

distance

传动系统将移动的距离。

units

The units representing the distance:

  • INCHES (default)
  • MM – Millimeters

velocity

可选。传动系统运动的速度,以浮点数或整数表示。如果未指定速度或先前设置为,则默认速度为50%。

units_v

Optional. The unit to represent the velocity:

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

wait

Optional.

  • wait=True (default) – The project waits until drive_for is complete before executing the next line of code.
  • wait=False - The project starts the action and moves on to the next line of code right away, without waiting for drive_for to finish.

# Drive forwards and backwards
drivetrain.drive_for(FORWARD, 5)
wait(1, SECONDS)
drivetrain.drive_for(REVERSE, 5)

turn#

turn rotates the drivetrain continuously left or right using the current turn velocity. The drivetrain will keep turning until another Drivetrain method runs or the project stops.

Usage:
drivetrain.turn(direction, velocity, units)

参数

描述

direction

The direction in which to turn:

  • LEFT
  • RIGHT

velocity

可选。传动系统旋转速度,以浮点数或整数表示。如果未指定速度或先前设置为,则默认速度为50%。

units

Optional. The unit that represents the velocity:

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

# Turn right, then stop
drivetrain.turn(RIGHT)
wait(2, SECONDS)
drivetrain.stop()

turn_for#

turn_for rotates the drivetrain left or right for a specific number of degrees using the current turn velocity.

Usage:
drivetrain.turn_for(direction, angle, units, velocity, units_v, wait)

参数

描述

direction

The direction in which to turn:

  • LEFT
  • RIGHT

angle

传动系统将旋转的角度数,以浮点数或整数表示。

units

The unit that represents the rotational value:

  • DEGREES (default)
  • TURNS

velocity

可选。传动系统旋转速度,以浮点数或整数表示。如果未指定速度或先前设置为,则默认速度为50%。

units_v

Optional. The unit that represents the velocity:

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

wait

Optional.

  • wait=True (default) – The project waits until turn_for is complete before executing the next line of code.
  • wait=False - The project starts the action and moves on to the next line of code right away, without waiting for turn_for to finish.

# Turn left, then turn around to the right
drivetrain.turn_for(LEFT, 90)
drivetrain.turn_for(RIGHT, 180)

turn_to_heading#

turn_to_heading rotates the drivetrain to face a specific rotational value using the current turn velocity.

注意: 仅当传动系统在“设备”窗口中配置了惯性传感器GPS 传感器陀螺仪传感器 时,此方法才可用。

Usage:
drivetrain.turn_to_heading(angle, units, velocity, units_v, wait)

参数

描述

angle

驱动系统转向方向的参数,可以是浮点数或整数。

units

The unit that represents the rotational value:

  • DEGREES

velocity

可选。传动系统旋转速度,以浮点数或整数表示。如果未指定速度或先前设置为,则默认速度为50%。

units_v

Optional. The unit that represents the velocity:

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

wait

Optional.

  • wait=True (default) – The project waits until turn_to_heading is complete before executing the next line of code.
  • wait=False - The project starts the action and moves on to the next line of code right away, without waiting for turn_to_heading to finish.

# Turn to face the cardinal directions
drivetrain.turn_to_heading(90, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_heading(180, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_heading(270, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_heading(0, DEGREES)

# Turn twice slowly
drivetrain.turn_to_heading(90, DEGREES, 20, PERCENT)
wait(1, SECONDS)
drivetrain.turn_to_heading(180, DEGREES, 20, PERCENT)

turn_to_rotation#

turn_to_rotation rotates the drivetrain to face a specific rotational value using the current turn velocity.

注意: 仅当传动系统在“设备”窗口中配置了惯性传感器GPS 传感器陀螺仪传感器 时,此方法才可用。

Usage:
drivetrain.turn_to_rotation(angle, units, velocity, units_v, wait)

参数

描述

angle

驱动系统转向的旋转值,以浮点数或整数表示。

units

The unit that represents the rotational value:

  • DEGREES
  • TURNS

velocity

可选。传动系统旋转速度,以浮点数或整数表示。如果未指定速度或先前设置为,则默认速度为50%。

units_v

Optional. The unit that represents the velocity:

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

wait

Optional.

  • wait=True (default) – The project waits until turn_to_rotation is complete before executing the next line of code.
  • wait=False - The project starts the action and moves on to the next line of code right away, without waiting for turn_to_heading to finish.

# Turn left, then spin in a circle clockwise and face right
drivetrain.turn_to_rotation(-90, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_rotation(450, DEGREES)

# Turn left then slowly spin in a circle clockwise
drivetrain.turn_to_rotation(-90, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_rotation(450, DEGREES, 20, PERCENT)

stop#

stop immediately stops all movement of the drivetrain.

Usage:
drivetrain.stop(mode)

参数

描述

模式

Optional. The stopping behavior to use when the drivetrain stops:

  • BRAKE – Stops immediately.
  • COAST – Slows gradually to a stop.
  • HOLD – Stops and resists movement using motor feedback.
If the stopping mode is not specified or previously set, the default behavior is BRAKE.

# Drive forward, then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()

calibrate_drivetrain#

calibrate_drivetrain calibrates the drivetrain’s configured Inertial, GPS, or Gyro Sensor. All subsequent commands will wait for the calibration to complete before executing. Calibration is an internal procedure that measures and compensates for sensor noise and drift over a specified period. During this time, the sensor must remain completely still (i.e., on a stable surface without any external movement). Movement during calibration will produce inaccurate results.

注意: 仅当传动系统在“设备”窗口中配置了惯性传感器GPS 传感器陀螺仪传感器 时,此方法才可用。

Usage:
calibrate_drivetrain()

参数

描述

此方法没有参数。

变异体#

set_drive_velocity#

set_drive_velocity sets the movement velocity as a percentage for all subsequent drive methods in the project. By default, this is 50%.

Usage:
drivetrain.set_drive_velocity(velocity, units)

参数

描述

velocity

传动系统的运动速度,以浮点数或整数表示。

units

Optional. The unit that represents the velocity:

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

# Drive forward at the default velocity
drivetrain.drive_for(FORWARD, 100, MM)
wait(1, SECONDS)

# Drive slower
drivetrain.set_drive_velocity(20, PERCENT)
drivetrain.drive_for(FORWARD, 100, MM)
wait(1, SECONDS)

# Drive faster
drivetrain.set_drive_velocity(100, PERCENT)
drivetrain.drive_for(FORWARD, 100, MM)
wait(1, SECONDS)

set_turn_velocity#

set_turn_velocity(velocity, units) sets the default velocity as a percentage for all subsequent turn methods in the project. By default, this is 50%.

Usage:
drivetrain.set_turn_velocity(velocity, units)

参数

描述

velocity

传动系统的转速,以浮点数或整数表示。

units

Optional. The unit that represents the velocity:

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

# Turn at the default velocity
drivetrain.turn_for(RIGHT, 100)
wait(1, SECONDS)

# Turn slower
drivetrain.set_turn_velocity(20, PERCENT)
drivetrain.turn_for(RIGHT, 100)
wait(1, SECONDS)

# Turn faster
drivetrain.set_turn_velocity(100, PERCENT)
drivetrain.turn_for(RIGHT, 100)
wait(1, SECONDS)

set_stopping#

set_stopping sets how the drivetrain behaves when drivetrain movement ends.

Usage:
drivetrain.set_stopping(mode)

参数

描述

mode

How the drivetrain will stop:

  • BRAKE – Stops immediately.
  • COAST – Slows gradually to a stop.
  • HOLD – Stops and resists movement using motor feedback.

set_timeout#

set_timeout sets a time limit for how long a Drivetrain 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 block.

注意: 传动系统的时限用于防止未到达目标位置的传动系统命令停止其他命令的执行。

Usage:
drivetrain.set_timeout(timeout, units)

参数

描述

value

电机指令在停止并执行下一个指令之前将运行的最大秒数,以整数或浮点数表示。

units

Optional. The unit that represents the time:

  • SECONDS
  • MSEC (default) – Milliseconds

set_heading#

set_heading sets the robot’s current heading to a specified value.

注意: 仅当传动系统在“设备”窗口中配置了惯性传感器GPS 传感器陀螺仪传感器 时,此方法才可用。

Usage:
drivetrain.set_heading(heading, units)

参数

描述

heading

新标题以浮点数或整数形式表示。

units

Optional. The unit that represents the heading:

  • DEGREES (default)
  • TURNS

# Face the new 0 degrees
drivetrain.set_heading(90, DEGREES)
drivetrain.turn_to_heading(0, DEGREES)

set_rotation#

set_rotation sets the robot’s current cumulative rotation value.

注意: 仅当传动系统在“设备”窗口中配置了惯性传感器GPS 传感器陀螺仪传感器 时,此方法才可用。

Usage:
drivetrain.set_rotation(rotation, units)

参数

描述

rotation

新的旋转值,以浮点数或整数表示。

units

Optional. The unit that represents the heading:

  • DEGREES (default)
  • TURNS

# Spin counterclockwise two times
drivetrain.set_rotation(720, DEGREES)
drivetrain.turn_to_rotation(0, DEGREES)

set_turn_threshold#

set_turn_threshold sets the turn threshold for a smart drivetrain. The threshold value is used to determine that turns are complete. If this is too large, then turns will not be accurate. If too small, then turns may not complete.

注意: 仅当传动系统在“设备”窗口中配置了惯性传感器GPS 传感器陀螺仪传感器 时,此方法才可用。

Usage:
drivetrain.set_turn_threshold(value)

参数

描述

value

转弯阈值,以度为单位,可设置为浮点数或整数。默认转弯阈值为 1 度。

set_turn_constant#

set_turn_constant sets the turn constant for a smart drivetrain. Smart drivetrains use a simple P controller when doing turns. This constant, generally known as kp, is the gain used in the equation that turns angular error into motor velocity.

注意: 仅当传动系统在“设备”窗口中配置了惯性传感器GPS 传感器陀螺仪传感器 时,此方法才可用。

Usage:
drivetrain.set_turn_constant(value)

参数

描述

value

新的转弯常数范围为 0.1–4.0。默认值为 1.0。

set_turn_direction_reverse#

set_turn_direction_reverse sets the smart drivetrain to be reversed. This method works the same as setting the reverse parameter to True when constructing a SmartDrive.

注意: 仅当传动系统在“设备”窗口中配置了惯性传感器GPS 传感器陀螺仪传感器 时,此方法才可用。

Usage:
drivetrain.set_turn_direction_reverse(value)

参数

描述

value

Boolean value to set the direction reversed or not:

  • True – Reverse the smart drivetrain’s direction
  • False – Return the smart drivetrain’s direction to its default

获取器#

is_done#

is_done returns a Boolean indicating whether a drivetrain is not currently moving.

  • True – The drivetrain is not moving.

  • False – The drivetrain is moving.

Note: is_done only detects movement from methods that have a wait parameter.

Usage:
drivetrain.is_done()

参数

描述

此方法没有参数。

is_moving#

is_moving returns a Boolean indicating whether a drivetrain is currently moving.

  • True – The drivetrain is moving.

  • False – The drivetrain is not moving.

Note: is_moving only detects movement from methods that have a wait parameter.

Usage:
Usage:
drivetrain.is_moving()

参数

描述

此方法没有参数。

heading#

heading returns the drivetrain’s heading angle as a float.

注意: 仅当传动系统在“设备”窗口中配置了惯性传感器GPS 传感器陀螺仪传感器 时,此方法才可用。

Usage:
drivetrain.heading(units)

参数

描述

units

Optional. The unit that represents the rotational value:

  • DEGREES (default) – 0.00 to 359.99
  • TURNS

# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print("Heading: ")
brain.screen.print(drivetrain.heading())

rotation#

rotation returns how much the drivetrain has turned since the project started as a float: positive for clockwise, negative for counterclockwise.

注意: 仅当传动系统在“设备”窗口中配置了惯性传感器GPS 传感器陀螺仪传感器 时,此方法才可用。

Usage:
drivetrain.rotation(units)

参数

描述

units

Optional. The unit that represents the rotational value:

  • DEGREES (default)
  • TURNS

# Display the rotation after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print("Rotation: ")
brain.screen.print(drivetrain.rotation())

velocity#

velocity returns the current velocity of a drivetrain as a float.

Usage:
drivetrain.velocity(units)

参数

描述

units

Optional. The unit that represents the velocity:

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

current#

current returns the current of the drivetrain in amps.

Usage:
drivetrain.current(units)

参数

描述

units

Optional. The unit that represents the current:

  • CurrentUnits.AMP – Amps

power#

power returns the average power of the drivetrain in watts.

Usage:
drivetrain.power()

参数

描述

此方法没有参数。

torque#

torque returns the average torque of the drivetrain.

Usage:
drivetrain.torque(units)

参数

描述

units

The unit that represents the torque:

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

efficiency#

efficiency returns the average efficiency of the drivetrain in percent.

Usage:
drivetrain.efficiency()

参数

描述

此方法没有参数。

temperature#

temperature returns the average temperature of the drivetrain.

Usage:
drivetrain.temperature(units)

参数

描述

units

Optional. The units that represent the temperature:

  • TemperatureUnits.CELSIUS (default)
  • TemperatureUnits.FAHRENHEIT

get_timeout#

get_timeout returns the currently set timeout in milliseconds.

drivetrain.get_timeout()

参数

描述

此方法没有参数。

构造函数#

Constructors are used to manually create DriveTrain and SmartDrive objects, which are necessary for configuring a drivetrain outside of VEXcode.

DriveTrain#

DriveTrain creates a drivetrain without an Inertial Sensor, GPS Sensor, or Gyro Sensor.

Usage:
DriveTrain(lm, rm, wheelTravel, trackWidth, wheelBase, units, externalGearRatio)

范围

描述

lm

左侧运动神经元运动神经元组的名称。

rm

右侧电机电机组的名称。

wheelTravel

可选。驱动轮的周长。默认值为 300 毫米。

trackWidth

可选。传动系统的轮距。默认值为 320 毫米。

wheelBase

可选。传动系统的轴距。默认值为 320 毫米。

units

Optional. The units that represent wheelTravel, trackWidth and wheelBase:

  • MM (default) – Millimeters
  • INCHES

externalGearRatio

可选。用于补偿齿轮传动距离的齿轮比(如果使用齿轮传动)。

Note: Motors and/or Motor Groups must be created first before they can be used to create an object with the DriveTrain constructor.

# Create the Motors
left_motor = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
right_motor = Motor(Ports.PORT2, GearSetting.RATIO_18_1, True)

# Create a drivetrain with default values
drivetrain = DriveTrain(left_motor, right_motor)

# Create the motors
left_motor = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
right_motor = Motor(Ports.PORT2, GearSetting.RATIO_18_1, True)

"""
Create a drivetrain with the following values:
- wheelTravel = 319.19
- trackWidth = 295
- wheelBase = 40
- units = MM (Millimeters)
- externalGearRatio - 1
"""
drivetrain = DriveTrain(left_motor, right_motor, 319.19, 295, 40, MM, 1)

SmartDrive#

SmartDrive creates a drivetrain configured with an Inertial Sensor, GPS Sensor, or Gyro Sensor.

Usage:
SmartDrive(lm, rm, g, wheelTravel, trackWidth, wheelBase, units, externalGearRatio)

范围

描述

lm

左侧运动运动组的名称。

rm

右侧电机电机组的名称。

g

惯性传感器GPS传感器陀螺仪传感器的名称。

wheelTravel

可选。驱动轮的周长。默认值为 300 毫米。

trackWidth

可选。传动系统的轮距。默认值为 320 毫米。

wheelBase

可选。传动系统的轴距。默认值为 320 毫米。

units

Optional. The units that represent wheelTravel, trackWidth and wheelBase:

  • MM (default) – Millimeters
  • INCHES

externalGearRatio

可选。用于补偿齿轮传动距离的齿轮比(如果使用齿轮传动)。

Note: Motors and/or Motor Groups must be created first before they can be used to create an object with the SmartDrive constructor.

# Create the Motors
left_motor = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
right_motor = Motor(Ports.PORT2, GearSetting.RATIO_18_1, True)

# Create a Gyro Sensor in Port A
gyro_a = Gyro(brain.three_wire_port.a)


# Create a drivetrain with default values
drivetrain = DriveTrain(left_motor, right_motor, gyro_a)

# Create the Motors
left_motor = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
right_motor = Motor(Ports.PORT2, GearSetting.RATIO_18_1, True)

# Create a Gyro Sensor in Port A
gyro_a = Gyro(brain.three_wire_port.a)

"""
Create a drivetrain with the following values:
- wheelTravel = 319.19
- trackWidth = 295
- wheelBase = 40
- units = MM (Millimeters)
- externalGearRatio - 1
"""
drivetrain = DriveTrain(left_motor, right_motor, gyro_a, 319.19, 295, 40, MM, 1)