ロボット専用のPython#

導入#

ヒーローボット「フレックス」には、クローモーター、アームモーター、AIビジョンセンサー、距離センサー、およびゲームポジショニングシステム(GPS)センサーが搭載されています。

すべての標準VEXcode VRメソッドは、V5 26-27 Override Playgroundで使用できます。

以下は、利用可能なロボット固有のメソッドの一覧です。

駆動系 – ロボットの移動と旋回。

  • アクション

    • drive — ロボットを永久に前進または後退させます。

    • drive_for — ロボットを特定の距離だけ前進または後退させます。

    • turn — ロボットを永久に左または右に回転させます。

    • turn_for — ロボットを特定の角度だけ左または右に回転させます。

    • turn_to_heading — ロボットを-359度から359度までの特定の方向に向けて回転させます。

    • turn_to_rotation — ロボットを特定の回転方向に回転させます。

    • stop — ロボットの動きを停止します。

  • ミューテーター

    • set_heading — ロボットの現在の進行方向を新しい進行方向に変更します。

    • set_rotation — ロボットの現在の回転を新しい回転に変更します。

    • set_timeout — ロボットが動作を完了しようとする時間を設定します。

    • set_drive_velocity — ロボットに走行速度を指示します。

    • set_turn_velocity — ロボットに旋回速度を指示します。

  • ゲッターズ

    • heading — ロボットの現在の方位を0度から359.99度の範囲で返します。

    • rotation — ロボットの現在の回転を返します。

    • is_done — ロボットの移動が完了したかどうかをブール値として返します。

    • is_moving — ロボットが移動しているかどうかをブール値として返します。

    • velocity — ロボットの走行速度を返します。

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

  • アクション

    • spin — モーターを永久に正転または逆転させます。

    • spin_for — モーターを特定の距離だけ回転させます。

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

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

  • ミューテーター

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

    • set_velocity — モーターに回転速度を指示します。

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

  • ゲッターズ

    • is_done — モーターの動作が完了したかどうかをブール値として返します。

    • is_spinning — モーターが回転しているかどうかをブール値として返します。

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

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

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

  • ゲッターズ

    • take_snapshot – 特定のシグネチャに対して検出されたオブジェクトのタプルを返します。

  • 物件

    • .width – 検出されたオブジェクトの幅(ピクセル単位)。

    • .height – 検出されたオブジェクトの高さ(ピクセル単位)。

    • .centerX – オブジェクトの中心のX座標(ピクセル単位)。

    • .centerY – オブジェクトの中心の Y 座標 (ピクセル単位)。

    • .originX – オブジェクトの左上隅のX座標(ピクセル単位)。

    • .originY – オブジェクトの左上隅の Y 座標 (ピクセル単位)。

    • .id – 検出されたオブジェクトの分類 ID。

コンソール – コンソールに出力し、値を監視します。

  • 印刷

  • モニター

    • monitor_variable — 1 つ以上の定義済み変数をモニター タブに追加します。

    • monitor_sensor — モニター タブに 1 つ以上のセンサー値を追加します。

センシング – ロボットの各種センサーを活用する。

  • 距離

    • found_object – 距離センサーが範囲内のオブジェクトを検出したかどうかを返します。

    • object_distance – 距離センサーと最も近い検出されたオブジェクト間の距離を返します。

  • GPS

    • 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)

ドライブ_for#

drive_for は、ロボットを特定の距離だけ前進または後退させます。プロジェクトは、ロボットの移動が完了するまで待機し、その後次のコードが実行されます。

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

パラメータ

説明

direction

ロボットの移動方向: FORWARD または REVERSE。

distance

ロボットが走行する距離。これは、 integer または小数 (float ) になります。

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

ロボットが回転する角度。これは、整数 integerfloat ) のいずれかになります。

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)

見出しへ#

方位とは、ロボットが向いている方向のことで、度数で表されます。0 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度回転する場合、右に2回転します。その後、360度回転する場合、360度は720度より小さいため、左に1回転します。

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#

set_timeout は、ロボットが動作を完了するまでの試行時間を秒単位で設定します。ロボットがこの時間内に動作を完了できない場合、試行を停止して次のコード行に進みます。これにより、ロボットが動作中に停止してしまうのを防ぎます。

Usage:
drivetrain.set_timeout(time, units)

パラメータ

説明

time

ロボットが動作を完了しようと試みる秒数。これは、正の整数 integerfloat ) になります。

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#

set_drive_velocity は、ロボットの走行速度を指定します。パーセンテージが高いほどロボットは速く走行し、低いほど遅く走行します。

すべてのプロジェクトは、ロボットがデフォルトで50%の速度で走行するところから始まります。

注: 速度を速くするとロボットの走行速度は速くなりますが、精度が低下する可能性があります。速度を遅くするとロボットの走行速度は遅くなりますが、精度は向上する可能性があります。

Usage:
drivetrain.set_drive_velocity(velocity, units)

パラメータ

説明

velocity

0%から100%までの走行速度。0は整数、 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#

set_turn_velocity は、ロボットの旋回速度を指定します。パーセンテージが高いほどロボットの旋回速度は速くなり、低いほど旋回速度は遅くなります。

すべてのプロジェクトは、ロボットがデフォルトで50%の速度で旋回するところから始まります。

注: 速度が速いほどロボットの旋回速度は速くなりますが、精度が低下する可能性があります。速度が遅いほどロボットの旋回速度は遅くなりますが、精度は向上する可能性があります。

Usage:
drivetrain.set_turn_velocity(velocity, units)

パラメータ

説明

velocity

0%から100%までの旋回速度。0は整数、 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)

ゲッター#

見出し#

方位とは、ロボットが向いている方向のことで、度数で表されます。0 heading は ロボットの現在の方位を0度から359.99度の範囲で返します。

ロボットの初期進行方向は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.

右に回すと回転角度が大きくなり、左に回すと回転角度が小さくなります。例えば、右に2回転すると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が 場合は、ロボットの移動が完了したかどうかをブール値で返します。1の場合は、ロボットの移動に基づいて他の動作のタイミングを制御するために使用できます。

  • True — ロボットの移動が完了しました。

  • False — ロボットはまだ動いています。

このメソッドは、 wait パラメータを持つ以下の Drivetrain メソッドと連携して動作します: 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 は、モーターを 2 方向のいずれかで永久に回転させます。モーターは、別の方向に回転させたり停止させたりするなどの別の動作が与えられるまで回転し続けます。

使用方法:
このメソッドでは、以下に示すように、使用可能な2つのモーターオブジェクトのうちの1つを使用できます。

モーター

指示

arm_motor

arm_motor.spin(direction) — アームモーター

claw_motor

claw_motor.spin(direction) — クローモーター

パラメータ

説明

direction

モーターの回転方向:

  • FORWARD — アームモーターを上に移動させるか、クローモーターを閉じます。
  • REVERSE — アームモーターを下に移動させるか、クローモーターを開きます。

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)

スピン_for#

spin_for は、モーターを特定の距離だけ回転させます。回転はモーターの現在位置を基準としています。プロジェクトは、モーターの回転が終了するまで待機してから、次のコードを実行します。

使用方法:
このメソッドでは、以下に示すように、使用可能な2つのモーターオブジェクトのうちの1つを使用できます。

モーター

指示

arm_motor

arm_motor.spin_for(direction, distance, units, wait) — アームモーター

claw_motor

claw_motor.spin_for(direction, distance, units, wait) — クローモーター

パラメータ

説明

direction

モーターの回転方向:

  • FORWARD — アームモーターを上に移動させるか、クローモーターを閉じます。
  • REVERSE — アームモーターを下に移動させるか、クローモーターを開きます。

distance

モーターが回転する距離。0 DEGREES 整数を使用します。2 TURNS 整数または小数を使用できます。

units

オプション。距離の単位: DEGREES (デフォルト)または TURNS。

wait

オプション。

  • wait=True (デフォルト) — モーターの回転が終了するまでプロジェクトを待機させ、次のコード行を実行します。
  • wait=False — 次のコード行をすぐに実行します。

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で測定されます。1回転は360度に相当します。プロジェクトの開始時には、アームモーターの位置は0度に設定されます。クローモーターは例外で、467度(完全に閉じた状態)から始まります。モーターの位置は、set_positionメソッドを使用して設定することもできます。

位置値は絶対値です。つまり、回転方向はモーターの現在位置によって決まります。

例えば、モーターが0度の位置から始まり、720度の位置まで回転した場合、2回転前進します。その後、360度の位置まで回転した場合、360度は720度より小さいため、1回転後退します。

使用方法:
このメソッドでは、以下に示すように、使用可能な2つのモーターオブジェクトのうちの1つを使用できます。

モーター

指示

arm_motor

arm_motor.spin_to_position(angle, units, wait=True) — アームモーター

claw_motor

claw_motor.spin_to_position(angle, units, wait=True) — クローモーター

パラメータ

説明

angle

モーターが回転する位置の値 DEGREES 整数を使用します。2 TURNS 整数または小数を使用できます。

units

オプション。位置単位: DEGREES (デフォルト)または TURNS。

wait

オプション。

  • wait=True (デフォルト) — モーターの回転が終了するまでプロジェクトを待機させ、次のコード行を実行します。
  • wait=False — 次のコード行をすぐに実行します。

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 モーターの回転を停止します。

使用方法:
このメソッドでは、以下に示すように、使用可能な2つのモーターオブジェクトのうちの1つを使用できます。

モーター

指示

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)

変異誘発者#

位置を設定する#

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

例えば、モーターが180度まで回転している場合、位置を0度に設定すると、その位置は180度から0度にリセットされます。その後、モーターはその新しい値に基づいて回転できるようになります。

使用方法:
このメソッドでは、以下に示すように、使用可能な2つのモーターオブジェクトのうちの1つを使用できます。

モーター

指示

arm_motor

arm_motor.set_position(position, units) — アームモーター

claw_motor

claw_motor.set_position(position, units) — クローモーター

パラメータ

説明

position

モーターに設定する位置値 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%の速度で回転する状態から始まります。

注: 速度を速くするとモーターの回転速度は速くなりますが、精度が低下する可能性があります。速度を遅くするとモーターの回転速度は遅くなりますが、精度は向上する可能性があります。

使用方法:
このメソッドでは、以下に示すように、使用可能な2つのモーターオブジェクトのうちの1つを使用できます。

モーター

指示

arm_motor

arm_motor.set_velocity(velocity, units) — アームモーター

claw_motor

claw_motor.set_velocity(velocity, units) — クローモーター

パラメータ

説明

velocity

回転速度は0%から100%までです。これは integer または小数(float )を指定できます。

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#

set_timeout set_timeout は、モーターが動作を完了するまでの試行時間を設定します。モーターがその時間内に動作を完了できない場合、試行を停止して次のコード行に進みます。これにより、モーターが動作中に停止してしまうのを防ぎます。

使用方法:
このメソッドでは、以下に示すように、使用可能な2つのモーターオブジェクトのうちの1つを使用できます。

モーター

指示

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 パラメータを持つ次のモーションメソッドと連携して動作します: spin_for および spin_to_position。

使用方法:
このメソッドでは、以下に示すように、使用可能な2つのモーターオブジェクトのうちの1つを使用できます。

モーター

指示

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 パラメータを持つ次のモーションメソッドと連携して動作します: spin_for および spin_to_position。

使用方法:
このメソッドでは、以下に示すように、使用可能な2つのモーターオブジェクトのうちの1つを使用できます。

モーター

指示

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度(完全に閉じた状態)から始まります。モーターが1回転すると位置が増加し、反対方向に回転すると位置が減少します。

使用方法:
このメソッドでは、以下に示すように、使用可能な2つのモーターオブジェクトのうちの1つを使用できます。

モーター

指示

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 モーターの回転速度を返します。

正の値はモーターが正方向に回転していることを意味し、負の値はモーターが逆方向に回転していることを意味します。

使用方法:
このメソッドでは、以下に示すように、使用可能な2つのモーターオブジェクトのうちの1つを使用できます。

モーター

指示

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)

AIビジョン#

ゲッター#

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

take_snapshot は、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

指定されたシグネチャのデータのみを含むようにデータセットをフィルタリングします。使用可能なシグネチャは次のとおりです。

  • AiVision.ALL_AIOBJS - すべてのゲーム要素を検出します。
  • GameElements.RED_BLUE_PIN - RedBluePin ゲーム要素を検出します。
  • GameElements.BLUE_YELLOW_PIN - BlueYellowPin ゲーム要素を検出します。
  • GameElements.RED_YELLOW_PIN - RedYellowPin ゲーム要素を検出します。
  • GameElements.YELLOW_PIN - YellowPin ゲーム要素を検出します。
  • GameElements.CUP - 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)

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

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

AI分類

ID

サイン

RedBluePin

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.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 はコンソールからすべての行をクリアし、カーソルを 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 のモニター タブに 1 つ以上の定義済み変数を追加します。これにより、プロジェクトの実行中に変数の値の変化を監視できます。

変数を正常に監視するには 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 のモニター タブに 1 つ以上のセンサー値を追加します。これにより、プロジェクトの実行中にセンサー値の変化を監視できます。

各センサー値を文字列として指定してください。

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 は、距離センサーが現在 2000mm 以内に物体を検出しているかどうかを返します。

  • 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)

GPS#

x座標#

x_position は、フィールド上の GPS (Game Positioning System™) センサーの現在の 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 (Game Positioning System™) センサーの現在の 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 (Game Positioning System™) センサーの読み取り値に基づいて、ロボットが現在向いている方位を 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)