传动系统#
介绍#
传动系统控制机器人的运动,使其能够精确地前进、转弯和停止。
当配置陀螺仪传感器或脑惯性传感器时,它还可以用作智能传动系统。
For the examples below, the configured drivetrain will be named drivetrain and will be used in all subsequent examples throughout this API documentation when referring to DriveTrain and SmartDrive class methods.
以下是所有方法的列表:
动作——移动和转动机器人。
drive– Moves the drivetrain in a specified direction indefinitely.drive_for– Moves the drivetrain for a set distance.turn– Turns the drivetrain left or right indefinitely.turn_for– Turns the drivetrain for a set distance.turn_to_heading– Turns the smart drivetrain to a specified heading using sensors.turn_to_rotation– Turns the smart drivetrain to a specific rotational value.stop– Stops a drivetrain with configurable behavior.calibrate_drivetrain– Calibrate the Inertial Sensor.
变异器——设置默认移动和转动速度。
set_drive_velocity– Sets the default moving velocity for the drivetrain.set_turn_velocity– Sets the turning moving velocity for the drivetrain.set_stopping– Sets the stop behavior (brake, coast, or hold).set_timeout– Limits how long a drivetrain function waits before giving up if movement is blocked.set_heading– Sets a smart drivetrain’s heading to a specific value.set_rotation– Sets a smart drivetrain’s rotational value to a specific value.set_turn_threshold– Sets the turn threshold for a smart drivetrain.set_turn_constant– Sets the turn constant for a smart drivetrain.
Getters – 返回机器人状态和位置。
heading– Returns a smart drivetrain’s current heading.rotation– Returns a smart drivetrain’s current rotational value.velocity– Returns a drivetrain’s current velocity.current– Returns the current of the drivetrain.is_moving– Returns whether a drivetrain is currently moving.is_done– Returns whether a drivetrain is currently not moving.is_turning– Returns whether a drivetrain is currently turning.power– Returns the amount of power being used by a drivetrain.torque– Returns the torque generated by a drivetrain.efficiency– Returns the efficiency of a drivetrain.temperature– Returns the temperature of the drivetrain.
构造函数——手动初始化和配置传动系统。
DriveTrain– Creates a basic drivetrain.SmartDrive– Creates a drivetrain configured with a Gyro Sensor or Brain Inertial Sensor.
行动#
drive#
drive moves the drivetrain in a specified direction indefinitely.
Usage:
drivetrain.drive(direction, velocity, units)
参数 |
描述 |
|---|---|
|
The direction in which to drive:
|
|
可选。动力传动系统移动的速度,以浮点数或整数表示。如果未指定速度,则默认速度为 50%。 |
|
Optional. The unit that represents the velocity:
|
# Drive forward then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()
# Drive slowly in reverse then stop
drivetrain.drive(REVERSE, 20, PERCENT)
wait(2, SECONDS)
drivetrain.stop()
drive_for#
drive_for moves the drivetrain in a specified direction for a set distance.
Usage:
drivetrain.drive_for(direction, distance, units, velocity, units_v, wait)
参数 |
描述 |
|---|---|
|
The direction in which to drive:
|
|
动力传动系统移动的距离,以浮点数或整数表示。 |
|
The unit that represents the distance:
|
|
可选。动力传动系统移动的速度,以浮点数或整数表示。如果未指定速度,则默认速度为 50%。 |
|
Optional. The unit that represents the velocity:
|
|
Optional.
|
# Drive forward and backward
drivetrain.drive_for(FORWARD, 10)
drivetrain.drive_for(REVERSE, 10)
# Quickly drive forward and backward
drivetrain.drive_for(FORWARD, 200, MM, 100, PERCENT)
drivetrain.drive_for(REVERSE, 200, MM, 100, PERCENT)
turn#
turn turns the drivetrain left or right indefinitely.
Usage:
drivetrain.turn(direction, velocity, units)
参数 |
描述 |
|---|---|
|
The direction in which to turn:
|
|
可选。动力传动系统的转动速度,以浮点数或整数表示。如果未指定速度,则默认速度为 50%。 |
|
Optional. The unit that represents the velocity:
|
# Turn right and left, then stop
drivetrain.turn(RIGHT)
wait(2, SECONDS)
drivetrain.turn(LEFT)
wait(2, SECONDS)
drivetrain.stop()
# Quickly turn right and left, then stop
drivetrain.turn(RIGHT, 100, PERCENT)
wait(2, SECONDS)
drivetrain.turn(LEFT, 100, PERCENT)
wait(2, SECONDS)
drivetrain.stop()
turn_for#
turn_for turns the drivetrain left or right for a specified angle or rotations.
Usage:
drivetrain.turn_for(direction, angle, units, velocity, units_v, wait)
参数 |
描述 |
|---|---|
|
The direction in which to turn:
|
|
传动系统转动的度数,以浮点数或整数表示。 |
|
The unit that represents the rotational value:
|
|
可选。动力传动系统的转动速度,以浮点数或整数表示。如果未指定速度,则默认速度为 50%。 |
|
Optional. The unit that represents the velocity:
|
|
Optional.
|
# Turn the robot right and left
drivetrain.turn_for(RIGHT, 90, DEGREES)
wait(1, SECONDS)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Quickly turn the robot right and left
drivetrain.turn_for(RIGHT, 90, DEGREES, 100, PERCENT)
wait(1, SECONDS)
drivetrain.turn_for(LEFT, 90, DEGREES, 100, PERCENT)
turn_to_heading#
turn_to_heading turns a smart drivetrain to a specified heading.
注意:此方法仅适用于配置了陀螺仪传感器或脑惯性传感器的传动系统。
Usage:
drivetrain.turn_to_heading(angle, units, velocity, units_v, wait)
参数 |
描述 |
|---|---|
|
以浮点数或整数形式表示传动系统转向的方向。 |
|
The unit that represents the rotational value:
|
|
可选。电机或电机组的旋转速度,以浮点数或整数表示。如果未指定速度,则默认速度为 50%。 |
|
Optional. The unit that represents the velocity:
|
|
Optional.
|
# 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 turns a smart drivetrain to a specified rotational value.
注意:此方法仅适用于配置了陀螺仪传感器或脑惯性传感器的传动系统。
Usage:
drivetrain.turn_to_rotation(angle, units, velocity, units_v, wait)
参数 |
描述 |
|---|---|
|
将传动系统转向的旋转值作为浮点数或整数。 |
|
The unit that represents the rotational value:
|
|
可选。电机或电机组的旋转速度,以浮点数或整数表示。如果未指定速度,则默认速度为 50%。 |
|
Optional. The unit that represents the velocity:
|
|
Optional.
|
# 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 stops a drivetrain.
Usage:
drivetrain.stop(mode)
参数 |
描述 |
|---|---|
|
Optional. How the drivetrain will stop:
|
# Drive forward then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()
# Drive forward and coast to a stop
drivetrain.set_drive_velocity(100, PERCENT)
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop(COAST)
calibrate_drivetrain#
calibrate_drivetrain calibrates the Gyro Sensor or Brain Inertial Sensor that is configured with the drivetrain. Calibration should be done when the drivetrain is not moving.
This is a method generated by the VEXcode Robot Configuration when a Drivetrain is configured in the Devices window. It will not function outside of VEXcode IQ (2nd gen).
**注意:**大脑将在每个项目开始时自动校准。
Usage:
calibrate_drivetrain()
参数 |
描述 |
|---|---|
该方法没有参数。 |
修改器#
set_drive_velocity#
set_drive_velocity sets the default moving velocity for a drivetrain. This velocity setting will be used for subsequent calls to any drivetrain functions if a specific velocity is not provided.
Usage:
drivetrain.set_drive_velocity(velocity, units)
参数 |
描述 |
|---|---|
|
动力传动系统移动的速度,以浮点数或整数表示。 |
|
Optional. The unit that represents the velocity:
|
# Drive forward at different velocities
# Default velocity
drivetrain.drive_for(FORWARD, 150, MM)
wait(1, SECONDS)
# Drive slower
drivetrain.set_drive_velocity(20, PERCENT)
drivetrain.drive_for(FORWARD, 150, MM)
wait(1, SECONDS)
# Drive faster
drivetrain.set_drive_velocity(100, PERCENT)
drivetrain.drive_for(FORWARD, 150, MM)
set_turn_velocity#
set_turn_velocity sets the default turning velocity for a drivetrain. This velocity setting will be used for subsequent calls to any drivetrain functions if a specific velocity is not provided.
Usage:
drivetrain.set_turn_velocity(velocity, units)
参数 |
描述 |
|---|---|
|
动力传动系统转动的速度,以浮点数或整数表示。 |
|
Optional. The unit that represents the velocity:
|
# Turn at different velocities
# Default velocity
drivetrain.turn_for(RIGHT, 360)
wait(1, SECONDS)
# Turn slower
drivetrain.set_turn_velocity(20, PERCENT)
drivetrain.turn_for(RIGHT, 360)
wait(1, SECONDS)
# Turn faster
drivetrain.set_turn_velocity(100, PERCENT)
drivetrain.turn_for(RIGHT, 360)
set_stopping#
set_stopping sets the stopping mode for a drivetrain.
Usage:
drivetrain.set_stopping(mode)
参数 |
描述 |
|---|---|
|
How the drivetrain will stop:
|
set_timeout#
set_timeout sets a time limit for how long a drivetrain function will wait to reach its target. If the drivetrain cannot complete the movement within the set time, it will stop automatically and continue with the next function.
**注意:**传动系统的时间限制用于防止未达到目标位置的传动系统功能停止项目其余部分的执行。
Usage:
drivetrain.set_timeout(value, units)
参数 |
描述 |
|---|---|
|
运动功能在停止并移动到下一个功能之前运行的最大秒数(整数或浮点数)。 |
|
Optional. The unit that represents the time:
|
# Turn right after driving forward
drivetrain.set_timeout(1, SECONDS)
drivetrain.drive_for(FORWARD, 25, INCHES)
drivetrain.turn_for(RIGHT, 90)
set_heading#
set_heading sets the heading of a smart drivetrain.
注意:此方法仅适用于配置了陀螺仪传感器或脑惯性传感器的传动系统。
Usage:
drivetrain.set_heading(heading, units)
参数 |
描述 |
|---|---|
|
新的标题为浮点数或整数。 |
|
Optional. The unit that represents the heading:
|
# Face the new 0 degrees
drivetrain.set_heading(90, DEGREES)
drivetrain.turn_to_heading(0, DEGREES)
set_rotation#
set_rotation method sets the rotation for the smart drivetrain.
注意:此方法仅适用于配置了陀螺仪传感器或脑惯性传感器的传动系统。
Usage:
drivetrain.set_rotation(rotation, units)
参数 |
描述 |
|---|---|
|
新的旋转值为浮点数或整数。 |
|
Optional. The unit that represents the heading:
|
# Spin counterclockwise two times
drivetrain.set_rotation(720, DEGREES)
drivetrain.turn_to_rotation(0, DEGREES)
set_turn_threshold#
set_turn_threshold method 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.
注意:此方法仅适用于配置了陀螺仪传感器或脑惯性传感器的传动系统。
Usage:
drivetrain.set_turn_threshold(value)
参数 |
描述 |
|---|---|
|
以度为单位设置的转弯阈值,可以是浮点数或整数。默认转弯阈值为 1 度。 |
# Set the turn threshold to 5 degrees
drivetrain.set_turn_constant(5)
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.
注意:此方法仅适用于配置了陀螺仪传感器或脑惯性传感器的传动系统。
Usage:
drivetrain.set_turn_constant(value)
参数 |
描述 |
|---|---|
|
新的转弯常数,范围为 0.1 - 4.0。默认值为 1.0。 |
# Increase turn gain (kp) to 2.0
drivetrain.set_turn_constant(2.0)
吸气剂#
heading#
heading returns the current heading of a smart drivetrain as a float.
注意:此方法仅适用于配置了陀螺仪传感器或脑惯性传感器的传动系统。
Usage:
drivetrain.heading(units)
参数 |
描述 |
|---|---|
|
Optional. The unit that represents the rotational value:
|
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print("Heading: ")
brain.screen.print(drivetrain.heading())
rotation#
rotation returns the current rotational value of a smart drivetrain as a float.
注意:此方法仅适用于配置了陀螺仪传感器或脑惯性传感器的传动系统。
Usage:
drivetrain.rotation(units)
参数 |
描述 |
|---|---|
|
Optional. The unit that represents the rotational value:
|
# 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)
参数 |
描述 |
|---|---|
|
Optional. The unit that represents the velocity:
|
current#
current returns the current of the drivetrain in amps.
Usage:
drivetrain.current(units)
参数 |
描述 |
|---|---|
|
Optional. The unit that represents the current:
|
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.
Usage:
drivetrain.is_moving()
参数 |
描述 |
|---|---|
该方法没有参数。 |
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 works with Drivetrain methods that have a wait parameter.
Usage:
drivetrain.is_done()
参数 |
描述 |
|---|---|
该方法没有参数。 |
is_turning#
is_moving returns a Boolean indicating whether a smart drivetrain is currently turning.
True– The smart drivetrain is turning.False– The smart drivetrain is not turning.
Note: This method will only work with a drivetrain that has been configured with a Gyro Sensor or Brain Inertial Sensor and only with Drivetrain methods that have a wait parameter.
Usage:
drivetrain.is_turning()
参数 |
描述 |
|---|---|
该方法没有参数。 |
power#
power returns the power that the drivetrain is using in watts.
Usage:
drivetrain.power()
参数 |
描述 |
|---|---|
该方法没有参数。 |
torque#
torque returns the average torque of the drivetrain.
Usage:
drivetrain.torque(units)
参数 |
描述 |
|---|---|
|
The unit that represents the torque:
|
efficiency#
efficiency returns the average efficiency of the drivetrain as a percent.
Usage:
drivetrain.efficiency()
参数 |
描述 |
|---|---|
该方法没有参数。 |
temperature#
temperature returns the average temperature of the drivetrain.
Usage:
drivetrain.temperature(units)
参数 |
描述 |
|---|---|
|
Optional. The units that represent the temperature:
|
构造函数#
Constructors are used to manually create DriveTrain and SmartDrive objects, which are necessary for configuring a drivetrain outside of VEXcode.
**注意:**为了创建动力传动系统,必须已经创建至少两个 Motor 或 MotorGroup 对象。
Drivetrain#
DriveTrain creates a drivetrain without a Gyro Sensor or Brain Inertial Sensor.
Usage:
DriveTrain(lm, rm, wheelTravel, trackWidth, wheelBase, units, externalGearRatio)
范围 |
描述 |
|---|---|
|
|
|
|
|
可选。传动系统车轮的周长。默认值为 300 毫米。 |
|
可选。传动系统的轮距。默认值为 320 毫米。 |
|
可选。传动系统的轴距。默认值为 320 毫米。 |
|
Optional. The unit that represents the
|
|
可选。如果传动比为整数,则用于补偿驱动距离的传动比。 |
# Create left and right drive motor objects
left_drive_smart = Motor(Ports.PORT1, 1.0, False)
right_drive_smart = Motor(Ports.PORT2, 1.0, True)
# Build a 2-motor drivetrain object with those motors
drivetrain = DriveTrain(left_drive_smart, right_drive_smart, 200, 173, 76, MM, 1)
# Create two motor group objects for left and right motors
left_motor_a = Motor(Ports.PORT1, 1.0, False)
left_motor_b = Motor(Ports.PORT2, 1.0, False)
left_drive_smart = MotorGroup(left_motor_a, left_motor_b)
right_motor_a = Motor(Ports.PORT3, 1.0, True)
right_motor_b = Motor(Ports.PORT4, 1.0, True)
right_drive_smart = MotorGroup(right_motor_a, right_motor_b)
# Create a 4-motor drivetrain
# wheelTravel 200, trackWidth 173, wheelBase 76 all in mm
# externalGearRatio 1
drivetrain = DriveTrain(left_drive_smart, right_drive_smart, 200, 173, 76, MM, 1)
Smart Drivetrain#
SmartDrive creates a drivetrain with a Gyro Sensor or Brain Inertial Sensor.
Usage:
SmartDrive(lm, rm, g, wheelTravel, trackWidth, wheelBase, units, externalGearRatio)
范围 |
描述 |
|---|---|
|
|
|
|
|
|
|
可选。传动系统车轮的周长。默认值为 300 毫米。 |
|
可选。传动系统的轮距。默认值为 320 毫米。 |
|
可选。传动系统的轴距。默认值为 320 毫米。 |
|
Optional. The unit that represents the
|
|
可选。如果传动比为整数,则用于补偿驱动距离的传动比。 |
# Create the Inertial Sensor
brain_inertial = Inertial()
# Make left and right drive motors
left_drive_smart = Motor(Ports.PORT1, 1.0, False)
right_drive_smart = Motor(Ports.PORT6, 1.0, True)
# Create a SmartDrive object with wheelTravel 200
drivetrain = SmartDrive(left_drive_smart, right_drive_smart, brain_inertial, 200)
如果制作带有多个电机的智能传动系统,则需要先单独创建 电机,然后再将它们分组到 电机组中。
# Create the Inertial Sensor
brain_inertial = Inertial()
# Create two motor groups for left and right motors
left_motor_a = Motor(Ports.PORT1, 1.0, False)
left_motor_b = Motor(Ports.PORT2, 1.0, False)
left_drive_smart = MotorGroup(left_motor_a, left_motor_b)
right_motor_a = Motor(Ports.PORT5, 1.0, True)
right_motor_b = Motor(Ports.PORT6, 1.0, True)
right_drive_smart = MotorGroup(right_motor_a, right_motor_b)
# Create a SmartDrive object with wheelTravel 200 mm
drivetrain = SmartDrive(left_drive_smart, right_drive_smart, brain_inertial, 200)