传动系统#

介绍#

传动系统控制机器人的运动,使其能够精确地前进、转弯和停止。

以下是所有方法的列表:

动作——移动和转动机器人。

  • drive – Moves the drivetrain in a specified direction indefinitely.

  • drive_for – Moves the drivetrain for a set distance.

  • drive_until – Moves the drivetrain until a condition is met.

  • turn – Turns the drivetrain left or right indefinitely.

  • turn_for – Turns the drivetrain for a set distance.

  • turn_to_heading – Turns the drivetrain to a specified heading.

  • turn_to_rotation – Turns the drivetrain to a specified rotation.

  • stop – Stops a drivetrain with configurable behavior.

变异器——设置默认移动和转动速度。

  • 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 drivetrain’s heading to a specific value.

  • set_rotation – Sets a drivetrain’s rotation to a specific value.

Getters – 返回机器人状态和位置。

  • get_heading – Returns a drivetrain’s current heading.

  • get_rotation – Returns a drivetrain’s current rotation.

  • get_velocity – Returns a drivetrain’s current velocity.

  • get_yaw – Returns the yaw of the robot in degrees.

  • get_roll – Returns the roll of the robot in degrees.

  • get_pitch – Returns the pitch of the robot in degrees.

  • get_crashed – Returns whether the robot has crashed.

  • is_stopped – Returns whether a drivetrain is currently not moving.

行动#

drive#

驱动 使传动系统无限期地沿指定方向移动。

用法:
drivetrain.drive(方向)

参数

描述

方向

行驶方向:

  • 前进
  • 后退

# Build Used: Super Code Base 2.0
def main():
    # Drive forward then stop
    drivetrain.drive(FORWARD)
    wait(2, SECONDS)
    drivetrain.stop()

# Start threads — Do not delete
start_thread(main)

drive_for#

drive_for 将传动系统沿指定方向移动一段设定的距离。

用法:
drivetrain.drive_for(方向、距离、单位、等待)

参数

描述

方向

行驶方向:

  • 前进
  • 后退

距离

动力传动系统移动的距离,以浮点数或整数表示。

单位

The unit that represents the distance:

  • MM – Millimeters
  • INCHES (default)

等待

可选

  • wait=True (默认)– 项目等待 drive_for 完成后再执行下一行代码。6
  • wait=False - 项目启动操作并立即
下一行代码,无需等待 drive_for 完成。12

# Build Used: Super Code Base 2.0
def main():
    # Drive back and forth
    drivetrain.drive_for(FORWARD, 100, MM)
    drivetrain.drive_for(REVERSE, 4, INCHES)
    

# Start threads — Do not delete
start_thread(main)

drive_until#

drive_until 将传动系统向前移动,直到满足某个条件。

用法:
drivetrain.drive_until(条件,等待)

参数

描述

条件

停止动力传动系统条件:

  • CRASH – 动力传动系统撞到物体时停止。4
  • OBJECT – 眼动传感器检测到物体时停止。必须配置眼动传感器才能使用此参数

等待

可选

  • wait=True (默认)– 项目等待 drive_until 完成后再执行下一行代码。6
  • wait=False - 项目启动操作并立即转到下一行代码,无需等待 drive_until 完成

# Build Used: Super Code Base 2.0
def main():
    # Turn right after a crash
    drivetrain.drive_until(CRASH)
    drivetrain.turn_for(RIGHT, 90)

# Start threads — Do not delete
start_thread(main)

turn#

无限地向左或向右转动传动系统。

用法:
drivetrain.turn(方向)

参数

描述

方向

转弯方向:

# Build Used: Super Code Base 2.0
def main():
    # Turn right and left, then stop
    drivetrain.turn(RIGHT)
    wait(2, SECONDS)
    drivetrain.turn(LEFT)
    wait(2, SECONDS)
    drivetrain.stop()

# Start threads — Do not delete
start_thread(main)

turn_for#

turn_for 将传动系统向左或向右转动指定的角度或圈数。

用法:
drivetrain.turn_for(方向,角度,等待)

参数

描述

方向

转弯方向:

角度

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

等待

可选

  • wait=True (默认)– 项目等待 turn_for 完成后再执行下一行代码。6
  • wait=False - 项目开始操作并立即
下一行代码,无需等待 turn_for 完成。12

# Build Used: Super Code Base 2.0
def main():
    # Turn right then left
    drivetrain.turn_for(RIGHT, 90)
    drivetrain.turn_for(LEFT, 90)

# Start threads — Do not delete
start_thread(main)

turn_to_heading#

turn_to_heading 将传动系统转向指定的航向。

用法:
drivetrain.turn_to_heading(角度,等待)

参数

描述

角度

转动传动系统的方向,以浮点数或整数度为单位。

等待

  • 立即
下一行代码,而不等待 turn_to_heading 完成。12

# Build Used: Super Code Base 2.0
def main():
    # Turn to face the cardinal directions
    drivetrain.turn_to_heading(90)
    wait(1, SECONDS)
    drivetrain.turn_to_heading(180)
    wait(1, SECONDS)
    drivetrain.turn_to_heading(270)
    wait(1, SECONDS)
    drivetrain.turn_to_heading(0)

# Start threads — Do not delete
start_thread(main)

turn_to_rotation#

turn_to_rotation 将传动系统转到指定的旋转方向。

用法:
drivetrain.turn_to_rotation(角度,等待)

参数

描述

角度

以浮点数或整数度数表示的旋转传动系统朝向。

等待

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_rotation to finish.

# Build Used: Super Code Base 2.0
def main():
    # Spin around twice
    drivetrain.turn_to_rotation(720)

# Start threads — Do not delete
start_thread(main)

stop#

停止 停止传动系统。

用法:
drivetrain.stop()

参数

描述

该方法没有参数。

# Build Used: Super Code Base 2.0
def main():
    # Drive forward then stop
    drivetrain.drive(FORWARD)
    wait(2, SECONDS)
    drivetrain.stop()

# Start threads — Do not delete
start_thread(main)

修改器#

set_drive_velocity#

set_drive_velocity 设置传动系统的默认移动速度。此速度设置将用于后续调用任何传动系统函数。

用法:
drivetrain.set_drive_velocity(velocity)

参数

描述

速度

传动系统移动的速度,以浮点数或整数表示,范围从 0% 到 100%。

# Build Used: Super Code Base 2.0
def main():
    # Drive at different velocities
    drivetrain.drive_for(FORWARD, 150, MM)
    wait(1, SECONDS)
    # Drive slow
    drivetrain.set_drive_velocity(20)
    drivetrain.drive_for(REVERSE, 50, MM)
    wait(1, SECONDS)
    # Drive fast
    drivetrain.set_drive_velocity(100)
    drivetrain.drive_for(FORWARD, 150, MM)

# Start threads — Do not delete
start_thread(main)

set_turn_velocity#

set_turn_velocity 设置传动系统的默认转弯速度。此速度设置将用于后续调用任何传动系统函数。

用法:
drivetrain.set_turn_velocity(velocity)

参数

描述

速度

传动系统转动的速度,以浮点数或整数表示,范围从 0% 到 100%。

# Build Used: Super Code Base 2.0
def main():
    # Turn at different velocities
    drivetrain.turn_for(RIGHT, 180)
    wait(1, SECONDS)
    # Turn fast
    drivetrain.set_turn_velocity(100)
    drivetrain.turn_for(RIGHT, 180)

# Start threads — Do not delete
start_thread(main)

set_stopping#

set_stopping 设置传动系统的停止模式。

用法:
drivetrain.set_stopping(brake)

参数

描述

刹车

传动系统

  • 保持 并使用电机反馈阻止运动。12
# Build Used: Super Code Base 2.0
def main():
    drivetrain.set_drive_velocity(100)
    # Drive and then brake
    drivetrain.set_stopping(BRAKE)
    drivetrain.drive(FORWARD)
    wait(2, SECONDS)
    drivetrain.stop()

# Start threads — Do not delete
start_thread(main)

set_timeout#

set_timeout 设置传动系统函数等待达到目标的时间限制。如果传动系统无法在设定的时间内完成运动,它将自动停止并继续执行下一个函数。

**注意:**传动系统的时间限制用于防止未达到目标位置的传动系统功能停止项目其余部分的执行。

用法:
drivetrain.set_timeout(值,单位)

参数

描述

运动功能在停止并移动到下一个功能之前运行的最大秒数(整数或浮点数)。

单位

可选。表示时间的单位:

  • SECONDS
  • MSEC (默认)- 毫秒

# Build Used: Super Code Base 2.0
def main():
    # Turn right after driving forward
    drivetrain.set_timeout(1, SECONDS)
    drivetrain.drive_for(FORWARD, 25, INCHES)
    drivetrain.turn_for(RIGHT, 90)

# Start threads — Do not delete
start_thread(main)

set_heading#

set_heading 将传动系统的航向设置为指定值。

用法:
drivetrain.set_heading(value)

参数

描述

新航向使用的度值。

# Build Used: Super Code Base 2.0
def main():
    # Face the new 0 degree heading
    drivetrain.set_heading(90)
    drivetrain.turn_to_heading(0)

# Start threads — Do not delete
start_thread(main)

set_rotation#

set_rotation 将传动系统的旋转设置为指定值。

用法:
drivetrain.set_rotation(value)

参数

描述

用于新旋转的度数值。

# Build Used: Super Code Base 2.0
def main():
    # Spin counterclockwise two times
    drivetrain.set_rotation(720)
    drivetrain.turn_to_rotation(0)

# Start threads — Do not delete
start_thread(main)

吸气剂#

get_heading#

get_heading 以浮点数形式返回动力传动系统的当前航向。

用法:
drivetrain.get_heading()

参数

描述

该方法没有参数。

# Build Used: Super Code Base 2.0
def main():
    # Monitor the heading while turning
    monitor_sensor("drivetrain.get_heading")
    drivetrain.turn_for(RIGHT, 450)


# Start threads — Do not delete
start_thread(main)

get_rotation#

get_rotation 返回惯性传感器的当前旋转角度(以度为单位)。

用法:
drivetrain.get_rotation()

参数

描述

该方法没有参数。

# Build Used: Super Code Base 2.0
def main():
    # Monitor the rotation while turning
    monitor_sensor("drivetrain.get_rotation")
    drivetrain.turn_for(RIGHT, 450)


# Start threads — Do not delete
start_thread(main)

get_velocity#

get_velocity 以百分比浮点数返回传动系统的当前速度。

用法:
drivetrain.get_velocity()

参数

描述

该方法没有参数。

# Build Used: Super Code Base 2.0
def main():
    # Display the velocity of the robot before and while moving
    console.print("Start: ")
    console.print(drivetrain.get_velocity())
    console.new_line()
    drivetrain.drive(FORWARD)
    wait(1, SECONDS)
    console.print("Moving: ")
    console.print(drivetrain.get_velocity())
    drivetrain.stop()


# Start threads — Do not delete
start_thread(main)

get_yaw#

get_yaw 以浮点数形式返回机器人当前的偏航角。

用法:
drivetrain.get_yaw()

参数

描述

该方法没有参数。

# Build Used: Super Code Base 2.0
def main():
    # Change the LED color based on the yaw while
    # moving the robot by hand
    monitor_sensor("drivetrain.get_yaw")
    while True:
        if drivetrain.get_yaw() > 0:
            bumper.set_color(GREEN)
        elif drivetrain.get_yaw() < 0:
            bumper.set_color(RED)
        else:
            bumper.set_color(NONE)

# Start threads — Do not delete
start_thread(main)

get_roll#

get_roll 以浮点数形式返回机器人当前的滚动角度。

用法:
drivetrain.get_roll()

参数

描述

该方法没有参数。

# Build Used: Super Code Base 2.0
def main():
    # Change the LED color based on the roll while
    # moving the robot by hand
    monitor_sensor("drivetrain.get_roll")
    while True:
        if drivetrain.get_roll() > 0:
            bumper.set_color(GREEN)
        elif drivetrain.get_roll() < 0:
            bumper.set_color(RED)
        else:
            bumper.set_color(NONE)

# Start threads — Do not delete
start_thread(main)

get_pitch#

get_pitch 以浮点数形式返回机器人当前的俯仰角度。

用法:
drivetrain.get_pitch()

参数

描述

该方法没有参数。

# Build Used: Super Code Base 2.0
def main():
    # Change the LED color based on the pitch while
    # moving the robot by hand
    monitor_sensor("drivetrain.get_pitch")
    while True:
        if drivetrain.get_pitch() > 0:
            bumper.set_color(GREEN)
        elif drivetrain.get_pitch() < 0:
            bumper.set_color(RED)
        else:
            bumper.set_color(NONE)

# Start threads — Do not delete
start_thread(main)

get_crashed#

get_crashed 返回一个布尔值,指示机器人是否已经崩溃。

  • – 机器人已崩溃。

  • 错误 – 机器人未崩溃。

用法:
drivetrain.get_crashed()

参数

描述

该方法没有参数。

# Build Used: Super Code Base 2.0
def main():
    # Drive until a crash
    drivetrain.drive(FORWARD)
    while not drivetrain.get_crashed():
        wait(50, MSEC)
    drivetrain.stop()
    drivetrain.turn_for(RIGHT, 90)

# Start threads — Do not delete
start_thread(main)

is_stopped#

is_stopped 返回一个布尔值,指示传动系统当前是否没有移动。

  • – 传动系统停止。

  • 错误 – 传动系统正在移动。

用法:
drivetrain.is_stopped()

参数

描述

该方法没有参数。

# Build Used: Super Code Base 2.0
def main():
    # Turn when the drivetrain is done moving forward
    drivetrain.drive_for(FORWARD, 200, MM, wait=False)
    wait(0.25, SECONDS)
    while True:
        if drivetrain.is_stopped():
            drivetrain.turn_for(RIGHT, 360)
            break
        else:
            console.print("Still moving...")
            wait(0.1, SECONDS)
            console.clear()

# Start threads — Do not delete
start_thread(main)