傳動系統#

介紹#

動力系統控制VR機器人的駕駛和轉向。動力系統可以前進或後退、左轉或右轉、轉向特定航向並追蹤其旋轉。

傳動系統編碼方式有很多種。以下列出了所有傳動系統編碼方法:

動作-移動和轉向機器人。

  • drive — 使機器人無限向前或向後移動。

  • drive_for — 使機器人向前或向後移動指定的距離。

  • turn — 讓機器人永遠向左或向右轉。

  • turn_for — 使機器人向左或向右旋轉指定的度數。

  • turn_to_heading — 將機器人轉向 -359 到 359 度之間的特定航向。

  • turn_to_rotation — 將機器人旋轉到指定的方向。

  • stop — 停止機器人的移動。

變異器 — 調整傳動系統設定。

Getters — 檢查傳動系統狀態。

  • heading — 返回機器人的目前航向,範圍從 0 到 359.99 度。

  • rotation — 返回機器人的目前旋轉角度。

  • is_done — 傳回機器人是否已完成移動,以布林值表示。

  • is_moving — 傳回機器人是否正在移動,以布林值表示。

行動#

drive#

drive 使機器人無限前進或後退。機器人將持續移動,直到收到其他指令,例如轉向或停止。

Usage:
drivetrain.drive(direction)

參數

描述

direction

機器人移動方向: FORWARD 或 REVERSE。

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

# VR threads — Do not delete
vr_thread(main)

drive_for#

drive_for 使機器人前進或後退指定距離。程式會等待機器人完成移動後才執行下一行程式碼。

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

參數

描述

direction

機器人移動方向: FORWARD 或 REVERSE。

distance

機器人行駛的距離。這可以是整數 integerfloat)。

units

可選。距離單位: INCHES (預設)或 MM (毫米)。

wait

可選 wait=True (預設值)使專案等待機器人完成移動後再執行下一行程式碼。 2 wait=False 使下一行程式碼立即運作。

def main():
    # Drive forward and backward
    drivetrain.drive_for(FORWARD, 200, MM)
    drivetrain.drive_for(REVERSE, 200, MM)

# VR threads — Do not delete
vr_thread(main)

turn#

turn 表示機器人無限向左或向右轉向。機器人將持續轉向,直到收到其他指令,例如行駛或停止。

Usage:
drivetrain.turn(direction)

參數

描述

direction

機器人轉向的方向: LEFT 或 RIGHT。

def main():
    # Turn right and left, then stop
    drivetrain.turn(RIGHT)
    wait(2, SECONDS)
    drivetrain.turn(LEFT)
    wait(2, SECONDS)
    drivetrain.stop()

# VR threads — Do not delete
vr_thread(main)

turn_for#

turn_for 參數使機器人向左或向右旋轉指定角度。旋轉角度是相對於機器人目前位置的。程式會等待機器人完成旋轉後才執行下一行程式碼。

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

參數

描述

direction

機器人轉向的方向: LEFT 或 RIGHT。

angle

The number of degrees the robot turns. This can be an integer or decimal (float).

units

可選。旋轉單位: DEGREES (預設值)。

wait

可選 wait=True (預設值)表示程式會等待機器人完成轉向後再執行下一行程式碼。 2 wait=False 表示程式會立即執行下一行程式碼。

def main():
    # Turn the robot right and left
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    wait(1, SECONDS)
    drivetrain.turn_for(LEFT, 90, DEGREES)

# VR threads — Do not delete
vr_thread(main)

turn_to_heading#

航向角是指機器人朝向的方向,以度為單位。 `turn_to_heading 將機器人轉向 -359 度到 359 度之間的特定航向角。機器人將沿著最短路徑轉向以達到目標航向角。

機器人的初始航向角為0度。

程式會等到機器人完成轉向後才執行下一行程式碼。

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

參數

描述

angle

機器人應面向的方向,以度為單位。可以是 integer 的整數,也可以是 -359 到 359 之間的十進位數(float)。

units

可選。旋轉單位: DEGREES (預設值)。

wait

可選 wait=True (預設值)表示程式會等待機器人完成轉向後再執行下一行程式碼。 2 wait=False 表示程式會立即執行下一行程式碼。

def main():
    # 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)

# VR threads — Do not delete
vr_thread(main)

turn_to_rotation#

turn_to_rotation 將機器人旋轉到指定角度。

旋轉角度是指機器人轉動的程度,以度為單位。在專案開始時,旋轉角度值設定為 0 度。也可以使用 set_rotation 方法設定旋轉角度。

旋轉角度是絕對值。這意味著轉彎方向取決於機器人目前的旋轉角度。向右轉會增加旋轉角度,向左轉彎會減少旋轉角度。

例如,如果機器人從0度開始,你將其旋轉720度,它會向右轉兩次。如果你再將其旋轉360度,它會向左轉一次,因為360小於720。

程式會等到機器人完成轉向後才執行下一行程式碼。

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

參數

描述

angle

機器人將旋轉到的旋轉值,以度為單位。這可以是整數 integerfloat)。

units

可選。旋轉單位: DEGREES (預設值)。

wait

可選 wait=True (預設值)表示程式會等待機器人完成轉向後再執行下一行程式碼。 2 wait=False 表示程式會立即執行下一行程式碼。

def main():
    # 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)

# VR threads — Do not delete
vr_thread(main)

stop#

stop 停止機器人的移動。

Usage:
drivetrain.stop()

參數

描述

此方法沒有參數。

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

# VR threads — Do not delete
vr_thread(main)

突變體#

set_heading#

航向角是指機器人面向的方向,以度為單位。 0 set_heading 將機器人的目前航向角變更為新的航向角值。

例如,如果機器人轉向右側,將航向設定為 0 度,則該右側位置將成為新的 0 度航向。然後,機器人可以根據新的航向轉向其他位置。

Usage:
drivetrain.set_heading(heading, units)

參數

描述

heading

The heading value, in degrees, to set for the robot. This can be an integer or decimal (float).

units

可選。航向單位: DEGREES (預設值)。

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

# VR threads — Do not delete
vr_thread(main)

set_rotation#

旋轉角度是指機器人轉動的程度,以度為單位。專案開始時,旋轉角度值設定為 0 度。 `set_rotation 會將機器人的目前旋轉角度變更為新值。

例如,如果機器人向右轉了兩圈,它的旋轉角度將為 720 度。將旋轉角度設為 0 度會將旋轉角度從 720 度重設為 0 度。然後,機器人可以根據這個新的旋轉角度進行旋轉。

Usage:
drivetrain.set_rotation(rotation, units)

參數

描述

rotation

要設定的機器人旋轉值,以度為單位。這可以是整數 integerfloat)。

units

可選。旋轉單位: DEGREES (預設值)。

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

# VR threads — Do not delete
vr_thread(main)

set_timeout#

set_timeout 設定機器人嘗試完成一個動作的逾時時間。如果機器人無法在規定時間內完成,它將停止嘗試並執行下一行程式碼。這可以防止機器人卡在某個動作上。

Usage:
drivetrain.set_timeout(value, units)

參數

描述

value

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

units

可選。時間單位: SECONDS (預設)或 MSEC (毫秒)。

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

# VR threads — Do not delete
vr_thread(main)

set_drive_velocity#

set_drive_velocity 告訴機器人行駛速度。百分比越高,機器人行駛速度越快;百分比越低,機器人行駛速度越慢。

每個專案開始時,機器人預設以 50% 的速度行駛。

注意:速度越高,機器人行駛速度越快,但精度可能越低;速度越低,機器人行駛速度越慢,但精度可能越高。

Usage:
drivetrain.set_drive_velocity(velocity, units)

參數

描述

velocity

行駛速度,範圍從 0% 到 100%。可以是整數 integerfloat)。

units

可選。速度單位: PERCENT (預設值)。

def main():
    # 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)

# VR threads — Do not delete
vr_thread(main)

set_turn_velocity#

set_turn_velocity 告訴機器人轉彎的速度。百分比越高,機器人轉彎越快;百分比越低,機器人轉彎越慢。

每個專案開始時,機器人預設以 50% 的速度旋轉。

注意:速度越高,機器人轉彎越快,但精度可能越低;速度越低,機器人轉彎越慢,但精度可能越高。

Usage:
drivetrain.set_turn_velocity(velocity, units)

參數

描述

velocity

轉彎速度,範圍從 0% 到 100%。可以是整數 integerfloat)。

units

可選。速度單位: PERCENT (預設值)。

def main():
    # Turn at different velocities
    # Default velocity
    drivetrain.turn_for(RIGHT, 120, DEGREES)
    wait(1, SECONDS)
    # Turn slower
    drivetrain.set_turn_velocity(20, PERCENT)
    drivetrain.turn_for(RIGHT, 120, DEGREES)
    wait(1, SECONDS)
    # Turn faster
    drivetrain.set_turn_velocity(100, PERCENT)
    drivetrain.turn_for(RIGHT, 120, DEGREES)

# VR threads — Do not delete
vr_thread(main)

獲取器#

heading#

A heading is the direction the robot is facing, measured in degrees. heading returns the robot’s current heading from 0 to 359.99 degrees.

機器人的初始航向角為0度。

Usage:
drivetrain.heading(units)

參數

描述

units

可選。航向單位: DEGREES (預設值)。

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

# VR threads — Do not delete
vr_thread(main)

rotation#

Rotation is how much the robot has turned, measured in degrees. At the beginning of a project, the rotation value is set to 0 degrees. rotation returns the robot’s current rotation.

向右轉會增加旋轉角度,向左轉會減少旋轉角度。例如,向右轉兩圈將得到 720 度的旋轉角度。

Usage:
drivetrain.rotation(units)

參數

描述

units

可選。旋轉單位: DEGREES (預設值)。

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

# VR threads — Do not delete
vr_thread(main)

is_done#

is_done 傳回機器人是否已完成移動,回傳值為布林值。這可用於根據機器人的移動狀態控制其他行為的執行時間。

  • True — 機器人已停止移動。

  • False — 機器人仍在移動。

此方法與以下具有 wait 參數的傳動系統方法搭配使用:drive_for, turn_for, turn_to_heading, 和 turn_to_rotation。

Usage:
drivetrain.is_done()

參數

描述

此方法沒有參數。

is_moving#

is_moving 返回機器人是否正在移動,返回值為布林值。這可用於根據機器人的移動狀態控制其他行為的執行時間。

  • True — 機器人正在移動。

  • False — 機器人沒有移動。

Usage:
drivetrain.is_moving()

參數

描述

此方法沒有參數。