ロボット専用のPython#

導入#

ヒーローボット「フロップ」には、インテークモーター、アームモーター、およびIQ AIビジョンセンサーが搭載されています。

すべての標準VEXcode VR Pythonコマンドは、IQ 26-27 Level Upプレイグラウンドで使用できます。

以下は、利用可能なロボット固有のPythonコマンドの一覧です。

動作 – ロボットのモーターを動かし、追跡する。

  • アクション

    • spin – モーターを一方向に永久に回転させます。

    • spin_for – モーターを特定の角度または回転数だけ回転させます。

    • spin_to_position – モーターを特定の位置まで回転させます。

    • stop – モーターの回転を停止します。

  • 設定

    • set_velocity – モーターの回転速度を設定します。

    • set_timeout – モーターが動作を完了しようとする時間を設定します。

    • set_position – モーターの現在の位置を新しい値に変更します。

  • 価値観

    • is_done – モーターの動作が完了したかどうかを返します。

    • is_spinning – モーターが回転しているかどうかを返します。

    • position – モーターの現在の位置を返します。

    • velocity – モーターの回転速度を返します。

AIビジョン – IQ AIビジョンセンサーを使用して物体をキャプチャおよび分析します。

  • ゲッターズ

    • take_snapshot – センサーの現在のフレームを特定のシグネチャにフィルタリングし、検出されたオブジェクトのタプルを返します。

  • 物件

    • .width – 検出されたオブジェクトの幅をピクセル単位で返します。

    • .height – 検出されたオブジェクトの高さをピクセル単位で返します。

    • .centerX – 検出されたオブジェクトの中心の x 座標を返します。

    • .centerY – 検出されたオブジェクトの中心の y 座標を返します。

    • .originX – 検出されたオブジェクトのバウンディングボックスの左上隅の x 座標を返します。

    • .originY – 検出されたオブジェクトのバウンディングボックスの左上隅の y 座標を返します。

    • .id – 検出された AI 分類の ID を返します。

このページに掲載されている例では、Playgroundのデフォルトの開始位置を使用しています。

モーション#

Flopには、ロボット専用のモーターが2つ搭載されています 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)

スピン_for#

spin_for はモーターを特定の距離だけ回転させます。2 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 はモーターを特定の位置まで回転させます。2 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#

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)

AIビジョン#

スナップショットを撮る#

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 を使用した後にタプルに格納される各オブジェクトには、7 つのプロパティが含まれます。

.id を除くすべてのプロパティ値は、take_snapshot が使用された時点で IQ AI Vision Sensor の視野内での検出されたオブジェクトの位置とサイズを表します。これらの値は、センサーの 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 を整数として返します。

AI分類

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)