機器人專用 Python#

介紹#

Hero Bot Flex 包含爪子馬達、手臂馬達、AI 視覺感測器、距離感測器和遊戲定位系統 (GPS) 感測器。

所有標準的 VEXcode VR 方法都可以在 V5 26-27 覆蓋 Playground 中使用。

以下是所有可用的機器人專用方法清單:

驅動系統-控制機器人的移動和轉向。

  • 操作

    • 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 — 傳回機器人是否正在移動,以布林值表示。

    • velocity — 返回機器人行駛的速度。

運動控制-移動並追蹤機器人的馬達。

  • 操作

    • spin — 使馬達永遠正轉或反轉。

    • spin_for — 使馬達旋轉指定的距離。

    • spin_to_position — 將馬達旋轉到特定位置。

    • stop — 停止馬達旋轉。

  • 變異因子

  • Getters

    • is_done — 返回馬達是否停止移動,以布林值表示。

    • is_spinning — 返回馬達是否旋轉,以布林值表示。

    • position — 返回馬達的目前位置。

    • velocity — 返回馬達的旋轉速度。

人工智慧視覺——使用人工智慧視覺感測器捕捉和分析物體。

  • Getters

    • take_snapshot – 傳回特定簽章偵測到的物件元組。

  • 特性

    • .width – 偵測到的物件的寬度(以像素為單位)。

    • .height – 偵測到的物體的高度(以像素為單位)。

    • .centerX – 物件中心的 X 座標(以像素為單位)。

    • .centerY – 物體中心的 Y 座標(以像素為單位)。

    • .originX – 物件左上角的 X 座標(以像素為單位)。

    • .originY – 物件左上角的 Y 座標(以像素為單位)。

    • .id – 偵測到的物件的分類 ID。

控制台 – 將資訊列印到控制台並監控數值。

感知-利用機器人的各種感測器。

  • 距離

    • found_object – 傳回距離感測器是否偵測到範圍內的物件。

    • object_distance – 返回距離感測器與最近偵測到的物件之間的距離。

  • 全球定位系統

    • x_position – 返回場地中 GPS 感測器的目前 x 座標。

    • y_position – 返回場地中 GPS 感測器的目前 y 座標。

    • heading – 根據 GPS 感測器返回機器人目前所面向的航向。

本頁範例使用遊樂場賽前檢查清單中的起始位置 C。

傳動系統#

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

行動#

駕駛#

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

Usage:
drivetrain.drive(direction)

參數

描述

direction

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

def main():
    # Raise the Arm, drive to the Goal, release the Pin
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive(FORWARD)
    wait(0.5, SECONDS)
    drivetrain.stop()
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

驅動#

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():
    # Raise the Arm, drive to the Goal, release the Pin
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive_for(FORWARD, 10, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

轉動#

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

Usage:
drivetrain.turn(direction)

參數

描述

direction

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

def main():
    # Score a Pin in a Neutral Goal
    drivetrain.turn(LEFT)
    wait(0.78, SECONDS)
    drivetrain.stop()
    arm_motor.spin_for(FORWARD, 800, DEGREES)
    drivetrain.drive_for(FORWARD, 25, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

轉彎#

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():
    # Score a Pin in a Neutral Goal
    drivetrain.turn_for(LEFT, 110, DEGREES)
    arm_motor.spin_for(FORWARD, 800, DEGREES)
    drivetrain.drive_for(FORWARD, 25, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

轉向航向#

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

起始航向為0度。

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

參數

描述

heading

機器人應面向的航向,範圍從 -359 度到 359 度。

units

可選。角度單位: DEGREES (預設值)。

wait

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

def main():
    # Score a Pin in a Neutral Goal
    drivetrain.turn_to_heading(-110, DEGREES)
    arm_motor.spin_for(FORWARD, 800, DEGREES)
    drivetrain.drive_for(FORWARD, 25, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

轉向旋轉#

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

旋轉角度是指機器人轉動的幅度,以度為單位。專案開始時,旋轉角度設定為 0 度。向右轉動會增加旋轉角度,向左轉動會減少旋轉角度。

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

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

參數

描述

rotation

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

units

可選。角度單位: DEGREES (預設值)。

wait

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

def main():
    # Score a Pin in a Neutral Goal
    drivetrain.turn_to_rotation(-110, DEGREES)
    arm_motor.spin_for(FORWARD, 800, DEGREES)
    drivetrain.drive_for(FORWARD, 25, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

停止#

stop 停止機器人的移動。

Usage:
drivetrain.stop()

參數

描述

此方法沒有參數。

def main():
    # Score a Pin in a Neutral Goal
    drivetrain.turn(LEFT)
    wait(0.78, SECONDS)
    drivetrain.stop()
    arm_motor.spin_for(FORWARD, 800, DEGREES)
    drivetrain.drive_for(FORWARD, 25, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

突變體#

設定標題#

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

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

Usage:
drivetrain.set_heading(heading, units)

參數

描述

heading

要設定的機器人航向值(以度為單位)。

units

可選。角度單位: DEGREES (預設值)。

def main():
    # Make facing the Neutral Goal the new heading
    drivetrain.set_heading(110, DEGREES)
    drivetrain.turn_to_heading(0, DEGREES)

# VR threads — Do not delete
vr_thread(main)

設定旋轉#

旋轉角度是指機器人轉動的幅度,以度為單位。 0 set_rotation 將機器人的目前旋轉角度變更為新值。

Usage:
drivetrain.set_rotation(rotation, units)

參數

描述

rotation

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

units

可選。角度單位: DEGREES (預設值)。

def main():
    # Make facing the Neutral Goal the new rotation
    drivetrain.set_rotation(110, DEGREES)
    drivetrain.turn_to_rotation(0, DEGREES)

# VR threads — Do not delete
vr_thread(main)

設定超時#

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

Usage:
drivetrain.set_timeout(time, units)

參數

描述

time

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

units

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

def main():
    # Turn to face the Neutral Goal
    drivetrain.set_timeout(0.8, SECONDS)
    drivetrain.turn_for(LEFT, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

設定驅動速度#

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

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

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

Usage:
drivetrain.set_drive_velocity(velocity, units)

參數

描述

velocity

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

units

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

def main():
    # Score a Pin in a Neutral Goal
    drivetrain.turn_for(LEFT, 110, DEGREES)
    arm_motor.spin_for(FORWARD, 800, DEGREES)
    drivetrain.set_drive_velocity(100, PERCENT)
    drivetrain.drive_for(FORWARD, 25, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

設定轉彎速度#

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

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

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

Usage:
drivetrain.set_turn_velocity(velocity, units)

參數

描述

velocity

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

units

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

def main():
    # Turn to face the Neutral Goal faster
    drivetrain.set_turn_velocity(100, PERCENT)
    drivetrain.turn_for(LEFT, 110, DEGREES)

# VR threads — Do not delete
vr_thread(main)

獲取器#

標題#

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():
    # Turn to the Goal, then show heading
    drivetrain.turn_for(LEFT, 110, DEGREES)
    brain.screen.print("Heading: ")
    brain.screen.print(drivetrain.heading(DEGREES))

# VR threads — Do not delete
vr_thread(main)

旋轉#

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():
    # Spin to find a Pin, then show rotation
    drivetrain.turn_for(RIGHT, 450, DEGREES)
    brain.screen.print("Rotation: ")
    brain.screen.print(drivetrain.rotation(DEGREES))

# VR threads — Do not delete
vr_thread(main)

已完成#

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

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

  • False — 機器人仍在移動。

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

Usage:
drivetrain.is_done()

參數

描述

此方法沒有參數。

def main():
    # Score a Pin in a Neutral Goal
    drivetrain.turn_for(LEFT, 110, DEGREES)
    arm_motor.spin_for(FORWARD, 800, DEGREES)
    drivetrain.drive_for(FORWARD, 25, INCHES, wait=False)
    wait(0.5, SECONDS)
    while not drivetrain.is_done():
        wait(5, MSEC)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

正在移動#

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

  • True — 機器人正在移動。

  • False — 機器人沒有移動。

Usage:
drivetrain.is_moving()

參數

描述

此方法沒有參數。

def main():
    # Raise the Arm once stopped near the Goal
    drivetrain.turn_for(LEFT, 110, DEGREES)
    drivetrain.drive_for(FORWARD, 25, INCHES, wait=False)
    wait (0.5, SEC)
    while drivetrain.is_moving():
        wait(5, MSEC)
    arm_motor.spin_for(FORWARD, 800, DEGREES)

# VR threads — Do not delete
vr_thread(main)

速度#

velocity 返回機器人行駛的速度,以 -100% 到 100% 的百分比表示。

Usage:
drivetrain.velocity(units)

參數

描述

units

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

def main():
    # Show speed while driving to the Goal
    drivetrain.drive_for(FORWARD, 150, MM)
    brain.screen.print("Velocity: ")
    brain.screen.print(drivetrain.velocity(PERCENT))

# VR threads — Do not delete
vr_thread(main)

運動#

Flex 使用機械手臂馬達來升降機械手臂,使用機械爪馬達來打開和關閉機械爪。

每個馬達都有其自身的運動方向。方向描述解釋了每個方向如何驅動 Flex 上的馬達運動。

行動#

旋轉#

spin 會使馬達沿著兩個方向之一持續旋轉。馬達將持續旋轉,直到收到其他指令,例如改變旋轉方向或停止。

用法:
此方法可以使用兩種可用的馬達物件之一,如下所示:

引擎

命令

arm_motor

arm_motor.spin(direction) — 機械手臂

claw_motor

claw_motor.spin(direction) — 爪電機

參數

描述

direction

The direction the motor spins:

  • FORWARD — Moves the Arm Motor up or closes the Claw Motor.
  • REVERSE — Moves the Arm Motor down or opens the Claw Motor.

def main():
    # Raise the Arm before driving toward a Pin
    arm_motor.spin(FORWARD)
    wait(1, SECONDS)
    arm_motor.stop()

# VR threads — Do not delete
vr_thread(main)

spin_for#

spin_for 會使馬達旋轉指定距離。旋轉距離是相對於馬達目前位置的。程式會等待馬達停止旋轉後才執行下一行程式碼。

用法:
此方法可以使用兩種可用的馬達物件之一,如下所示:

引擎

命令

arm_motor

arm_motor.spin_for(direction, distance, units, wait) — 機械手臂

claw_motor

claw_motor.spin_for(direction, distance, units, wait) — 爪電機

參數

描述

direction

The direction the motor spins:

  • FORWARD — Moves the Arm Motor up or closes the Claw Motor.
  • REVERSE — Moves the Arm Motor down or opens the Claw Motor.

distance

馬達旋轉的距離。 0 度 DEGREES 使用整數)。 2 圈 TURNS 可以使用整數或小數)。

units

可選。距離單位: DEGREES (預設)或 TURNS。

wait

Optional.

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

def main():
    # Raise the Arm to carry the Pin to the Goal
    arm_motor.spin_for(FORWARD, 800, DEGREES)

# VR threads — Do not delete
vr_thread(main)

自旋至位置#

spin_to_position 將馬達旋轉到特定位置。

馬達的位置是指它旋轉的距離,以 DEGREES 或 TURNS進行測量。一圈等於 360 度。在專案開始時,機械手臂馬達的位置設定為 0 度。爪形馬達是個例外——它的初始位置為 467 度(完全閉合)。也可以使用 set_position 方法設定馬達位置。

位置值是絕對值。這意味著旋轉方向取決於馬達的當前位置。

例如,如果馬達從0度開始旋轉到720度,它將正轉兩圈。如果它接著旋轉到360度,它將反轉一圈,因為360小於720。

用法:
此方法可以使用兩種可用的馬達物件之一,如下所示:

引擎

命令

arm_motor

arm_motor.spin_to_position(angle, units, wait=True) — 機械手臂

claw_motor

claw_motor.spin_to_position(angle, units, wait=True) — 爪電機

參數

描述

angle

馬達旋轉到的位置值。 0 度 DEGREES 使用整數)。 2 圈 TURNS 可以使用整數或小數)。

units

可選。位置單位: DEGREES (預設)或 TURNS。

wait

Optional.

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

def main():
    # Raise the Arm, then release the Pin
    arm_motor.spin_to_position(800, DEGREES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

停止#

stop 停止馬達旋轉。

用法:
此方法可以使用兩種可用的馬達物件之一,如下所示:

引擎

命令

arm_motor

arm_motor.stop() — 機械手臂

claw_motor

claw_motor.stop() — 爪電機

參數

描述

此方法沒有參數。

def main():
    # Raise the Arm partway to hold the Pin
    arm_motor.spin(FORWARD)
    wait(1, SECONDS)
    arm_motor.stop()

# VR threads — Do not delete
vr_thread(main)

突變體#

設定位置#

馬達的位置是指它旋轉的距離,以 DEGREES 或 TURNS測量。 set_position 用於將馬達的目前位置變更為新值。

例如,如果馬達旋轉了 180 度,將位置設為 0 度會將該位置從 180 度重設為 0 度。然後,馬達可以根據這個新值旋轉到其他位置。

用法:
此方法可以使用兩種可用的馬達物件之一,如下所示:

引擎

命令

arm_motor

arm_motor.set_position(position, units) — 機械手臂

claw_motor

claw_motor.set_position(position, units) — 爪電機

參數

描述

position

馬達位置值設定。 0 度 DEGREES 使用整數。 2 圈 TURNS 可以使用整數或小數。

units

可選。位置單位: DEGREES (預設)或 TURNS。

def main():
    # Reset encoder, raise Arm, place Pin
    arm_motor.set_position(0, DEGREES)
    arm_motor.spin_to_position(800, DEGREES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

設定速度#

set_velocity 告訴馬達旋轉的速度。百分比越高,馬達旋轉速度越快;百分比越低,馬達旋轉速度越慢。

每個項目開始時,每個馬達預設以 50% 的速度旋轉。

注意:更高的速度會使馬達轉速更快,但精度可能會降低。更低的速度會使馬達轉速更慢,但精度可能會更高。

用法:
此方法可以使用兩種可用的馬達物件之一,如下所示:

引擎

命令

arm_motor

arm_motor.set_velocity(velocity, units) — 機械手臂

claw_motor

claw_motor.set_velocity(velocity, units) — 爪電機

參數

描述

velocity

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

units

速度單位: PERCENT。

def main():
    # Raise the Arm fast to release the Pin
    arm_motor.set_velocity(100, PERCENT)
    arm_motor.spin_for(FORWARD, 800, DEGREES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

設定超時#

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

用法:
此方法可以使用兩種可用的馬達物件之一,如下所示:

引擎

命令

arm_motor

arm_motor.set_timeout(value, units) — 機械手臂

claw_motor

claw_motor.set_timeout(value, units) — 爪電機

參數

描述

value

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

units

時間單位: SECONDS 或 MSEC (毫秒)。

def main():
    # Score a Pin in a Neutral Goal
    drivetrain.turn_for(LEFT, 110, DEGREES)
    arm_motor.set_timeout(1, SECONDS)
    arm_motor.spin_for(FORWARD, 1800, DEGREES)
    drivetrain.drive_for(FORWARD, 25, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

獲取器#

已完成#

is_done 返回馬達是否停止運動,返回值為布林值。這可用於根據馬達的運動狀態控制其他行為的執行時間。

  • True — 馬達停止運轉。

  • False — 馬達仍在運轉。

此方法與以下具有 wait 參數的 Motion 方法搭配使用:spin_for 和 spin_to_position。

用法:
此方法可以使用兩種可用的馬達物件之一,如下所示:

引擎

命令

arm_motor

arm_motor.is_done() — 機械手臂

claw_motor

claw_motor.is_done() — 爪電機

參數

描述

此方法沒有參數。

def main():
    # Raise the Arm while driving to the Goal
    arm_motor.spin_to_position(800, DEGREES, wait=False)
    wait(0.1, SECONDS)
    while not arm_motor.is_done():
        drivetrain.drive(FORWARD)
        wait(5, MSEC)
    drivetrain.stop()

# VR threads — Do not delete
vr_thread(main)

正在旋轉#

is_spinning 傳回馬達是否正在旋轉的布林值。這可用於根據馬達的運動狀態控制其他行為的時序。

  • True — 馬達正在旋轉。

  • False — 馬達不轉動。

此方法與以下具有 wait 參數的 Motion 方法搭配使用:spin_for 和 spin_to_position。

用法:
此方法可以使用兩種可用的馬達物件之一,如下所示:

引擎

命令

arm_motor

arm_motor.is_spinning() — 機械手臂電機

claw_motor

claw_motor.is_spinning() — 爪電機

參數

描述

此方法沒有參數。

def main():
    # Raise the Arm while driving to the Goal, then release the Pin
    arm_motor.spin_to_position(800, DEGREES, wait=False)
    wait(0.1, SECONDS)
    while arm_motor.is_spinning():
        drivetrain.drive_for(FORWARD, 25, MM)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

位置#

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

專案開始時,機械手臂馬達的初始位置設定為 0 度。爪形馬達是個例外——它的初始位置為 467 度(完全閉合)。如果馬達向前旋轉一整圈,位置就會增加;如果馬達向反方向旋轉,位置就會減少。

用法:
此方法可以使用兩種可用的馬達物件之一,如下所示:

引擎

命令

arm_motor

arm_motor.position(units) — 機械手臂

claw_motor

claw_motor.position(units) — 爪電機

參數

描述

units

將馬達位置恢復到: DEGREES 或 TURNS。

def main():
    # Raise the Arm, then release the Pin
    while arm_motor.position(DEGREES) < 800:
        arm_motor.spin(FORWARD)
        wait(2, MSEC)
    arm_motor.stop()
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

速度#

速度velocity。

正值表示馬達正轉,負值表示馬達反轉。

用法:
此方法可以使用兩種可用的馬達物件之一,如下所示:

引擎

命令

arm_motor

arm_motor.velocity(units) — 機械手臂電機

claw_motor

claw_motor.velocity(units) — 爪電機

參數

描述

units

要回傳的速度單位: PERCENT。

def main():
    # Show Arm Motor velocity while raising
    arm_motor.set_velocity(100, PERCENT)
    arm_motor.spin_to_position(800, DEGREES, wait=False)
    wait(0.2, SECONDS)
    brain.screen.print(arm_motor.velocity(PERCENT))

# VR threads — Do not delete
vr_thread(main)

人工智慧視覺#

獲取器#

拍攝快照#

take_snapshot 將來自 AI 視覺感測器幀的資料過濾為單一簽名——感測器可以識別的事物的保存描述,例如場上的遊戲元素——並返回一個元組。

The tuple stores objects ordered from largest to smallest by width, starting at index 0. Each object’s properties can be accessed using its index. An empty tuple is returned if no matching objects are detected.

Usage:
ai_vision.take_snapshot(signature)

參數

描述

signature

Filters the dataset to only include data of the given signature. Available signatures are:

  • AiVision.ALL_AIOBJS - Detects all game elements.
  • GameElements.RED_BLUE_PIN - Detects RedBluePin game elements.
  • GameElements.BLUE_YELLOW_PIN - Detects BlueYellowPin game elements.
  • GameElements.RED_YELLOW_PIN - Detects RedYellowPin game elements.
  • GameElements.YELLOW_PIN - Detects YellowPin game elements.
  • GameElements.CUP - Detects Cup game elements.

def main():
    # Raise the Arm, drive to the Goal, release the Pin
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive_for(FORWARD, 10, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

    drivetrain.turn_for(LEFT, 70, DEGREES)

    # Turn to face the Pin
    drivetrain.set_turn_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        if ai_objects:
            if ai_objects[0].centerX < 140:
                drivetrain.turn(LEFT)
            elif ai_objects[0].centerX > 180:
                drivetrain.turn(RIGHT)
            else:
                drivetrain.stop()
                break

# VR threads — Do not delete
vr_thread(main)

特性#

使用 take_snapshot 後,儲存在元組中的每個物件都包含七個屬性。

除 .id 外,所有屬性值均描述了在執行 take_snapshot 操作時,AI 視覺感測器視野中偵測到的物體的位置和大小。這些值以像素為單位,基於感測器的 320 x 240 像素解析度。

。寬度#

.width 傳回偵測到的物件在像素中的寬度,該值是 1 到 320 之間的整數。

def main():
    # Raise the Arm, drive to the Goal, release the Pin
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive_for(FORWARD, 10, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

    drivetrain.turn_for(LEFT, 70, DEGREES)

    # Approach the next Cup and Pin
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        if ai_objects:
            if ai_objects[0].width > 140:
                drivetrain.stop()
                break
            else:
                drivetrain.drive(FORWARD)

# VR threads — Do not delete
vr_thread(main)

。高度#

.height 傳回偵測到的物件的高度(以像素為單位),該值是 1 到 240 之間的整數。

def main():
    # Raise the Arm, drive to the Goal, release the Pin
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive_for(FORWARD, 10, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

    drivetrain.turn_for(LEFT, 70, DEGREES)

    # Approach the next Cup and Pin
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        if ai_objects:
            if ai_objects[0].height > 200:
                drivetrain.stop()
                break
            else:
                drivetrain.drive(FORWARD)

# VR threads — Do not delete
vr_thread(main)

.centerX#

.centerX 傳回偵測到的物件中心的 x 座標(以像素為單位),該值是 0 到 320 之間的整數。

def main():
    # Raise the Arm, drive to the Goal, release the Pin
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive_for(FORWARD, 10, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

    drivetrain.turn_for(LEFT, 70, DEGREES)

    # Turn and approach the next Cup and Pin
    drivetrain.set_turn_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        if ai_objects:
            if ai_objects[0].centerX < 140:
                drivetrain.turn_for(LEFT, 5, DEGREES)
            else: 
                drivetrain.drive_for(FORWARD, 25, INCHES)
                break

# VR threads — Do not delete
vr_thread(main)

.centerY#

.centerY 傳回偵測到的物件中心的 y 座標(以像素為單位),該值是 0 到 240 之間的整數。

def main():
    # Raise the Arm, drive to the Goal, release the Pin
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive_for(FORWARD, 10, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

    drivetrain.turn_for(LEFT, 70, DEGREES)

    # Approach the next Cup and Pin
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        if ai_objects:
            if ai_objects[0].centerY > 230:
                drivetrain.stop()
                break
            else:
                drivetrain.drive(FORWARD)

# VR threads — Do not delete
vr_thread(main)

.originX#

.originX 傳回偵測到的物件邊界框左上角的 x 座標(以像素為單位),該值是 0 到 320 之間的整數。

def main():
    # Raise the Arm, drive to the Goal, release the Pin
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive_for(FORWARD, 10, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

    drivetrain.turn_for(LEFT, 70, DEGREES)

    # Turn and approach the next Cup and Pin
    drivetrain.set_turn_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        if ai_objects:
            if ai_objects[0].originX < 100:
                drivetrain.turn_for(LEFT, 5, DEGREES)
            else:
                drivetrain.drive_for(FORWARD, 25, INCHES)
                break

# VR threads — Do not delete
vr_thread(main)

.originY#

.originY 傳回偵測到的物件邊界框左上角的 y 座標(以像素為單位),該值是 0 到 240 之間的整數。

def main():
    # Raise the Arm, drive to the Goal, release the Pin
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive_for(FORWARD, 10, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

    drivetrain.turn_for(LEFT, 70, DEGREES)

    # Approach the next Cup and Pin
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        if ai_objects:
            if ai_objects[0].originY > 140:
                drivetrain.stop()
                break
            else:
                drivetrain.drive(FORWARD)

# VR threads — Do not delete
vr_thread(main)

。 ID#

.id 傳回偵測到的 AI 分類的 ID,以整數形式表示。

人工智慧分類

ID

簽名

紅藍針

0

GameElements.RED_BLUE_PIN

藍黃針

1

GameElements.BLUE_YELLOW_PIN

紅黃針

2

GameElements.RED_YELLOW_PIN

黃針

3

GameElements.YELLOW_PIN

杯子

4

GameElements.CUP

def main():
    # Raise the Arm, drive to the Goal, release the Pin
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive_for(FORWARD, 10, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

    drivetrain.turn_for(LEFT, 70, DEGREES)

    # Display the ID of the next closest object
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        if ai_objects:
            brain.screen.print(ai_objects[0].id)
            break

# VR threads — Do not delete
vr_thread(main)

安慰#

項目運行時,控制台會顯示文字、數字和變數值。控制台方法還可以將遊標移至新行、清除控制台以及在「監視器」標籤中新增變數或感測器值。

列印#

腦螢幕列印#

brain.screen.print 使用目前遊標位置在控制台中顯示文字、數字或變數值。

當您希望下一個列印值從新行開始時,請使用 brain.screen.next_row。

Usage:
brain.screen.print(value)

參數

描述

value

若要在控制台中顯示的文字、數字或變數值。

def main():
    # Display a message in the Console
    brain.screen.print("Hello, robot!")

# VR threads — Do not delete
vr_thread(main)

brain.screen.下一行#

brain.screen.next_row 將遊標移到控制台下一行的第 1 列。下一個列印的值將顯示在該行。

Usage:
brain.screen.next_row()

參數

描述

此方法沒有參數。

def main():
    # Print on two rows
    brain.screen.print("Row 1")
    brain.screen.next_row()
    brain.screen.print("Row 2")

# VR threads — Do not delete
vr_thread(main)

brain.screen.clear_screen#

brain.screen.clear_screen 清除控制台中的所有行並將遊標移回第 1 行。

Usage:
brain.screen.clear_screen()

參數

描述

此方法沒有參數。

def main():
    # Display text, then clear it after two seconds
    brain.screen.print("This will disappear...")
    wait(2, SECONDS)
    brain.screen.clear_screen()

# VR threads — Do not delete
vr_thread(main)

監視器#

監控變數#

monitor_variable 將一個或多個預定義變數新增至 VEXcode 的「監視器」標籤。這樣,您就可以在專案運行時監視變數值的變化。

要成功監控 monitor_variable 變數必須是全域變數。請以字串形式提供每個變數名。

Usage:
monitor_variable(“variable”)
monitor_variable(“variable1”, “variable2”)

參數

描述

variable

要監控的預定義全域變數的名稱,以字串形式給出。若要監控多個變量,請用逗號分隔每個變數名稱。

def main():
    # Monitor the number of loops
    global loops
    monitor_variable("loops")

    # Turn in a full circle 3 times
    for loops in range(12):
        drivetrain.turn_for(RIGHT, 90, DEGREES)

# VR threads — Do not delete
vr_thread(main)

監控感測器#

monitor_sensor 將一個或多個感測器值新增至 VEXcode 的「監視器」標籤。這樣,您就可以在專案運行時觀察感測器值的變化。

請以字串形式提供每個感測器的值。

Usage:
monitor_sensor(“sensor”)
monitor_sensor(“sensor1”, “sensor2”)

參數

描述

sensor

The sensor value to monitor, given as a string. To monitor more than one sensor value, separate each sensor value with a comma. Supported sensor identifiers include:

  • Brain
    • brain.timer.time
  • Drivetrain
    • drivetrain.is_done
    • drivetrain.is_moving
    • drivetrain.heading
    • drivetrain.rotation
    • drivetrain.velocity
  • Arm Motor
    • arm_motor.is_done
    • arm_motor.is_spinning
    • arm_motor.position
    • arm_motor.velocity
  • Claw Motor
    • claw_motor.is_done
    • claw_motor.is_spinning
    • claw_motor.position
    • claw_motor.velocity
  • Distance Sensor
    • distance.is_object_detected
    • distance.object_distance_mm
    • distance.object_distance_inches
  • GPS Sensor
    • gps.position_mm
    • gps.position_inches
    • gps.heading
  • AI Vision Sensor
    • ai_vision.object_count
    • ai_vision.height
    • ai_vision.width
    • ai_vision.centerX
    • ai_vision.centerY
    • ai_vision.originX
    • ai_vision.originY
    • ai_vision.id

def main():
    # Monitor the robot's heading
    monitor_sensor("drivetrain.heading")
    drivetrain.turn_for(RIGHT, 450, DEGREES)

# VR threads — Do not delete
vr_thread(main)

感測#

距離#

找到的對象#

found_object 返回距離感測器目前是否偵測到 2000 毫米內的物體。

  • True — 距離感測器偵測到物體。

  • False — 距離感測器未偵測到物體。

Usage:
distance.found_object()

參數

描述

此方法沒有參數。

def main():
    # Turn until something is detected, then release the Pin
    drivetrain.turn(RIGHT)
    while True:
        wait(5, MSEC)
        if distance.found_object():
            drivetrain.stop()
            break
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive_for(FORWARD, 10, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

物體距離#

object_distance 返回距離感測器與最近偵測到的物體之間的距離。

Usage:
distance.object_distance(units)

參數

描述

units

距離單位: MM 或 INCHES。

def main():
    # Drive until 200 mm from the Goal, then release the Pin
    while distance.object_distance(MM) > 200:
        wait(5, MSEC)
        drivetrain.drive(FORWARD)
    drivetrain.stop()
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive_for(FORWARD, 10, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

全球定位系統#

x_position#

x_position 返回場上 GPS(遊戲定位系統™)感測器的目前 x 座標。

Usage:
gps.x_position(units)

參數

描述

單位

偏移值的單位: INCHES 或 MM

def main():
    # Raise the Arm, drive to the Goal, release the Pin
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive(FORWARD)
    while not gps.x_position(MM) > -1370:
        wait(2, MSEC)
    drivetrain.stop()
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

y 位置#

y_position 返回場上 GPS(遊戲定位系統™)感測器的目前 y 座標。

Usage:
gps.y_position(units)

參數

描述

單位

偏移值的單位: INCHES 或 MM

def main():
    # Raise the Arm, drive to the Goal, release the Pin
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive(FORWARD)
    while not gps.y_position(MM) < -450:
        wait(2, MSEC)
    drivetrain.stop()
    claw_motor.spin_for(REVERSE, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

標題#

heading 返回機器人目前面向的航向,該航向基於 GPS(遊戲定位系統™)感測器的讀數,範圍為 0 到 359 度。

Usage:
gps.heading()

參數

描述

此方法沒有參數。

def main():
    # Raise the Arm, drive to the Goal, release the Pin
    monitor_sensor("gps.heading")
    arm_motor.spin_for(FORWARD, 300, DEGREES)
    drivetrain.drive_for(FORWARD, 10, INCHES)
    claw_motor.spin_for(REVERSE, 180, DEGREES)

    # Turn left to face the Neutral Goal
    drivetrain.set_turn_velocity(30, PERCENT)
    drivetrain.turn(LEFT)
    while not gps.heading() < 18:
        wait(2, MSEC)
    drivetrain.stop()


# VR threads — Do not delete
vr_thread(main)