機器人專用 Python#

介紹#

英雄機器人 Flop 包含一個進氣馬達、一個機械手臂馬達和一個 IQ AI 視覺感測器。

所有標準的 VEXcode VR Python 指令都可以在 IQ 26-27 Level Up Playground 中使用。

以下是所有可用的機器人專用 Python 命令列表:

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

  • 操作

    • spin – 使馬達永遠朝一個方向旋轉。

    • spin_for – 使馬達旋轉特定角度或圈數。

    • spin_to_position – 將馬達旋轉到指定位置。

    • stop – 停止馬達旋轉。

  • 設定

  • 價值觀

    • is_done – 返回馬達是否停止運轉。

    • is_spinning – 返回馬達是否正在旋轉。

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

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

AI視覺-使用IQ AI視覺感測器捕捉和分析物體。

  • Getters

    • take_snapshot – 將感測器的目前訊框過濾為特定簽名,並傳回偵測到的物件元組。

  • 特性

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

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

    • .centerX – 傳回偵測到的物件的中心的 x 座標。

    • .centerY – 傳回偵測到的物件中心的 y 座標。

    • .originX – 傳回偵測到的物件邊界框左上角的 x 座標。

    • .originY – 傳回偵測到的物件邊界框左上角的 y 座標。

    • .id – 傳回偵測到的 AI 分類的 ID。

本頁範例使用 Playground 的預設起始位置。

運動#

Flop 有兩個機器人專用馬達: intake_motor 和 arm_motor兩者都使用標準馬達指令。

方向常數:

  • intake_motor: FORWARD = 進氣(收集); REVERSE = 排氣(排出)

  • arm_motor: FORWARD = 向上(抬起); REVERSE = 向下(放下)

旋轉#

spin 表示馬達將一直朝指定方向旋轉。馬達將持續旋轉,直到收到下一個指令。

Usage:
motor.spin(direction)

參數

描述

direction

旋轉方向: FORWARD 或 REVERSE。

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

spin_for#

spin_for 使馬達旋轉指定距離。除非傳遞了 wait=False ,否則項目會等待馬達完成旋轉後再執行下一個指令。

Usage:
motor.spin_for(direction, amount, unit)

參數

描述

direction

旋轉方向: FORWARD 或 REVERSE。

amount

旋轉距離。可以是整數或小數。

unit

計量單位: DEGREES 或 TURNS。

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

自旋至位置#

spin_to_position 將馬達旋轉到指定位置。除非傳遞了 wait=False ,否則項目會等待馬達完成旋轉後再執行下一個指令。

Usage:
motor.spin_to_position(position, unit)

參數

描述

position

要旋轉到的位置。可以是整數或小數。

unit

計量單位: DEGREES 或 TURNS。

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_to_position(1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

停止#

stop 停止馬達旋轉。

Usage:
motor.stop()

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)
    wait(1, SECONDS)
    intake_motor.stop()

# VR threads — Do not delete
vr_thread(main)

設定速度#

set_velocity 設定馬達旋轉速度,以 0 到 100 之間的百分比表示。

Usage:
motor.set_velocity(velocity, unit)

參數

描述

velocity

馬達轉速,以百分比表示,範圍從 0 到 100。

unit

計量單位: PERCENT.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.set_velocity(100, PERCENT)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

設定超時#

set_timeout 設定馬達在停止前嘗試完成一次運動的秒數。

Usage:
motor.set_timeout(timeout, unit)

參數

描述

timeout

馬達嘗試完成一次動作的秒數。

unit

計量單位: SECONDS。

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.set_timeout(2, SECONDS)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

設定位置#

set_position 將馬達的目前位置變更為新值,重設其編碼器。

Usage:
motor.set_position(position, unit)

參數

描述

position

新的位置值。

unit

計量單位: DEGREES 或 TURNS。

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.set_position(0, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

已完成#

is_done 傳回布林值,指示馬達是否已完成其運動。

  • True – 馬達已停止運轉。

  • False – 馬達仍在運轉。

Usage:
motor.is_done()

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS, wait=False)
    while not arm_motor.is_done():
        wait(5, MSEC)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

正在旋轉#

is_spinning 傳回一個布林值,指示馬達目前是否正在旋轉。

  • True – 馬達正在旋轉。

  • False – 馬達不轉動。

Usage:
motor.is_spinning()

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)
    while intake_motor.is_spinning():
        wait(5, MSEC)
    intake_motor.stop()

# VR threads — Do not delete
vr_thread(main)

位置#

position 返回馬達的目前位置。

Usage:
motor.position(unit)

參數

描述

unit

計量單位: DEGREES 或 TURNS。

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    while arm_motor.position(DEGREES) < 360:
        arm_motor.spin(FORWARD)
        wait(2, MSEC)
    arm_motor.stop()
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

速度#

velocity 返回馬達目前旋轉速度的百分比,範圍從 -100 到 100。

Usage:
motor.velocity(unit)

參數

描述

unit

計量單位: PERCENT.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)
    wait(0.2, SECONDS)
    brain.screen.print(intake_motor.velocity(PERCENT))

# VR threads — Do not delete
vr_thread(main)

人工智慧視覺#

拍攝快照#

take_snapshot 將 IQ AI Vision Sensor 影格中的資料過濾為單一簽章(感應器可以辨識的事物的保存描述,例如場上的遊戲元素),並傳回一個元組。

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 Bean Bags.
  • AiVision.ALL_TAGS - Detects AprilTags, found on the sides of the Goals.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Turn to face a red Bean Bag
    drivetrain.set_turn_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.centerX < 140:
                    drivetrain.turn(LEFT)
                elif ai_object.centerX > 180:
                    drivetrain.turn(RIGHT)
                else:
                    drivetrain.stop()
                    return
                break

# VR threads — Do not delete
vr_thread(main)

特性#

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

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

。寬度#

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

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Approach the Bean Bag, then pick it up
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.width > 200:
                    drivetrain.stop()
                    intake_motor.spin(FORWARD)
                    return
                else:
                    drivetrain.drive(FORWARD)
                break

# VR threads — Do not delete
vr_thread(main)

。高度#

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

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Approach the Bean Bag, then pick it up
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.height > 110:
                    drivetrain.stop()
                    intake_motor.spin(FORWARD)
                    return
                else:
                    drivetrain.drive(FORWARD)
                break

# VR threads — Do not delete
vr_thread(main)

.centerX#

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

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Steer to keep the Bean Bag centered
    drivetrain.set_turn_velocity(30, PERCENT)
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.centerX < 140:
                    drivetrain.turn_for(LEFT, 5, DEGREES)
                else:
                    drivetrain.drive_for(FORWARD, 150, MM)
                    intake_motor.spin(FORWARD)
                    return
                break

# VR threads — Do not delete
vr_thread(main)

.centerY#

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

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Print the y-position of the Bean Bag
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    for ai_object in ai_objects:
        if ai_object.id == GameElements.RED_BEANBAG:
            brain.screen.print(ai_object.centerY)
            break

# VR threads — Do not delete
vr_thread(main)

.originX#

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

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Steer to keep the left edge centered
    drivetrain.set_turn_velocity(30, PERCENT)
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.originX < 100:
                    drivetrain.turn_for(LEFT, 5, DEGREES)
                else:
                    drivetrain.drive_for(FORWARD, 150, MM)
                    intake_motor.spin(FORWARD)
                    return
                break

# VR threads — Do not delete
vr_thread(main)

.originY#

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

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Print the y-position of the Bean Bag's top edge
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    for ai_object in ai_objects:
        if ai_object.id == GameElements.RED_BEANBAG:
            brain.screen.print(ai_object.originY)
            break

# VR threads — Do not delete
vr_thread(main)

。 ID#

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

人工智慧分類

ID

藍豆袋

0

紅豆袋

1

黃豆袋

2

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Pick up the Bean Bag if it's red
    drivetrain.set_drive_velocity(30, PERCENT)
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    for ai_object in ai_objects:
        if ai_object.id == GameElements.RED_BEANBAG:
            intake_motor.spin(FORWARD)
            drivetrain.drive_for(FORWARD, 150, MM)
            break

# VR threads — Do not delete
vr_thread(main)


def main():
    # Stop when you see the Red Goal
    drivetrain.set_turn_velocity(30, PERCENT)
    drivetrain.turn(RIGHT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_TAGS)
        for ai_object in ai_objects:
            if ai_object.id == 1:
                drivetrain.stop()
                brain.screen.print("Found the Red Goal!")
                return

# VR threads — Do not delete
vr_thread(main)