コントロール#

導入#

Pythonによる制御機能を使うと、ロボットに待機するタイミング、動作を繰り返すタイミング、意思決定の方法、そしてプロジェクトを終了するタイミングなどを指示できます。

以下は、メソッドや主要なPythonキーワードを含む、利用可能なコントロールの一覧です。

  • wait – 指定された時間だけプロジェクトを一時停止します。

  • for – シーケンス内の各項目に対してコードを繰り返します。

  • if – 条件が真の場合にコードを実行します。

  • if/else – 条件に応じて異なるコードを実行します。

  • if/elif/else – 複数の条件を順番にチェックします。

  • while – 条件が真である間、コードを繰り返します。

  • break – ループを即座に終了します。

  • stop_program – 実行中のプログラムを終了します。

  • pass – アクションが必要ない場合に使用されるプレースホルダー。

待って#

wait は、次のコード行に進む前に、指定された時間だけ一時停止します。

Usage:
wait(time, units)

パラメータ

説明

time

待機時間。正の整数または小数で指定します。

units

待ち時間を表す単位: MSEC (デフォルト) – ミリ秒、または SECONDS

# Build Used: Super Code Base 2.0
def main():
    # Turn right for one second
    drivetrain.turn(RIGHT)
    wait(1, SECONDS)
    drivetrain.stop()

# Start threads — Do not delete
start_thread(main)

のために#

for repeats a set of actions a specific number of times. for can be used to loop through items in lists, tuples, dictionaries, sets, strings, or for a specified amount of loops with range.

使用例:

for value in expression_list:
    pass

コンポーネント

説明

value

イテレーションにおける現在の要素を格納する一時変数。

expression_list

ループ処理される要素の集合(例:リスト、文字列、範囲)。

# Build Used: Super Code Base 2.0
def main():
    # Move in a square path
    for index in range(4):
        drivetrain.drive_for(FORWARD, 100, MM)
        drivetrain.turn_for(RIGHT, 90)

# Start threads — Do not delete
start_thread(main)

もし#

if runs the indented block of code if the condition is True.

使用法:

if condition:
    pass

コンポーネント

説明

condition

ステートメント 実行時にチェックされる式または変数。これが Trueの場合、if ブロック内のコードが実行されます。これが Falseの場合、ブロックはスキップされます。

# Build Used: Super Code Base 2.0
def main():
    # Turn when the bumper is pressed
    while True:
        if bumper.is_pressed():
            drivetrain.turn_for(RIGHT, 90)
        wait(0.1, SECONDS)

# Start threads — Do not delete
start_thread(main)

if/else#

if かつ else 条件が True または Falseであるかどうかに基づいて、どのインデントされたコードブロックが実行されるかを決定します。

使用法:

if condition:
    pass
else:
    pass

コンポーネント

説明

condition

ステートメントの実行時にチェックされる式または変数。これが Trueの場合、 if ブロック内のコードが実行されます。これが Falseの場合、代わりに else ブロック内のコードが実行されます。

# Build Used: Super Code Base 2.0
def main():
    # Turn when the bumper is pressed
    while True:
        if bumper.is_pressed():
            drivetrain.turn(RIGHT)
        else:
            drivetrain.stop()
        wait(0.1, SECONDS)

# Start threads — Do not delete
start_thread(main)

if/elif/else#

if/elif/else 構造は、条件に基づいてどのインデントされたコードブロックを実行するかを選択します。

  • if runs its block if the condition is True.

  • elif 、以前の条件がすべて Falseの場合にのみ追加の条件をチェックします。複数の elif ステートメントを使用できます。

  • else 、前の条件のいずれも真でない場合にのみブロックを実行します True。

使用法:

if condition:
    pass
elif condition:
    pass
else:
    pass

コンポーネント

説明

condition

ステートメントの実行時にチェックされる式または変数。最初に True 条件によって、どのブロックが実行されるかが決定されます。 Trueがない場合は、 else が実行されます。

# Build Used: Super Code Base 2.0
def main():
    # Change the LED while turning
    drivetrain.turn(RIGHT)
    while True:
        if drivetrain.get_heading() < 120:
            bumper.set_color(OFF)
        elif drivetrain.get_heading() < 240:
            bumper.set_color(RED)
        else:
            bumper.set_color(GREEN)
        wait(0.2, SECONDS)

# Start threads — Do not delete
start_thread(main)

その間#

while 、条件が Trueである限りコードを繰り返し実行します。また、条件として True 使用することで「永久」のように使用したり、条件に not を追加することで「待機」のように使用することもできます。以下の例を参照してください。

使用法:

while condition:
    pass

コンポーネント

説明

condition

各反復処理の前にチェックされる式または変数。0 が Trueの場合、ループは続行されます。2 が Falseの場合、ループは停止します。

# Build Used: Super Code Base 2.0
def main():
    # Turn when the bumper is pressed
    while True:
        if bumper.is_pressed():
            drivetrain.turn(RIGHT)
        else:
            drivetrain.stop()
        wait(0.1, SECONDS)

# Start threads — Do not delete
start_thread(main)

# Build Used: Super Code Base 2.0
def main():
    # Turn the other direction when the bumper is pressed
    drivetrain.turn(LEFT)
    while not bumper.is_pressed():
        wait(0.2, SECONDS)
    drivetrain.turn(RIGHT)

# Start threads — Do not delete
start_thread(main)

壊す#

break はループを即座に終了します。2 break 無限ループになる可能性のあるループを終了させるために使用できます。

使用例:

break

パラメータ

説明

このキーワードにはパラメータがありません。

# Build Used: Super Code Base 2.0
def main():
    # Stop turning after pressing the bumper
    while True:
        drivetrain.turn(RIGHT)
        if bumper.is_pressed():
            break
        wait(0.2, SECONDS)
    drivetrain.stop()
    bumper.set_color(RED)

# Start threads — Do not delete
start_thread(main)

プログラムを停止する#

stop_program は実行中のプロジェクトを終了します。

Usage:
stop_program()

パラメータ

説明

このメソッドにはパラメータがありません。

# Build Used: Super Code Base 2.0
def main():
    # Stop the project after pressing the bumper
    while True:
        drivetrain.turn(RIGHT)
        if bumper.is_pressed():
            break
        wait(0.2, SECONDS)
    stop_program()

# Start threads — Do not delete
start_thread(main)

合格#

pass は将来のコードのためのプレースホルダーであり、空のループ、条件分岐、関数におけるエラーを回避するために使用できます。

使用例:

pass

パラメータ

説明

このキーワードにはパラメータがありません。

if condition:
    pass

def function():
    pass