コントロール#
導入#
Pythonによる制御では、ロボットに待機するタイミング、動作を繰り返すタイミング、意思決定の方法、プロジェクトを終了するタイミングなどを指示できます。
以下は、メソッドと主要なPythonキーワードを含む、すべてのコントロールの一覧です。
wait– 指定された時間だけプロジェクトを一時停止します。for– シーケンス内の各項目に対してコードを繰り返します。if– 条件が真の場合にコードを実行します。if/else– 条件に応じて異なるコードを実行します。if/elif/else– 複数の条件を順番にチェックします。while– 条件が真である間、コードを繰り返します。break– ループを即座に終了します。stop_program– 実行中のプログラムを終了します。pass– アクションが必要ない場合に使用されるプレースホルダー。
待って#
wait は、次のコード行に進む前に、指定された時間だけ一時停止します。
Usage:wait(time, units)
パラメータ |
説明 |
|---|---|
|
待機時間。正の整数または小数で指定します。 |
|
待ち時間を表す単位: |
# Move right for one second, then stop
robot.move_at(90)
wait(1, SECONDS)
robot.stop_all_movement()
のために#
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
コンポーネント |
説明 |
|---|---|
|
イテレーションにおける現在の要素を格納する一時変数。 |
|
ループ処理される要素の集合(例:リスト、文字列、範囲)。 |
# Move in a square path.
for index in range(4):
robot.move_for(50, 0)
robot.turn_for(RIGHT, 90)
# Print each item in the list
colors = ["Red", "Green", "Blue"]
for color in colors:
robot.screen.print(color)
robot.screen.next_row()

もし#
if runs the indented block of code if the condition is True.
使用法:
if condition:
pass
コンポーネント |
説明 |
|---|---|
|
ステートメント 実行時にチェックされる式または変数。これが |
# Kick when the screen is pressed
while True:
if robot.screen.pressing():
robot.kicker.kick(MEDIUM)
wait(0.1, SECONDS)
条件付き/条件なし#
if かつ else 条件が True または Falseであるかどうかに基づいて、どのインデントされたコードブロックが実行されるかを決定します。
使用法:
if condition:
pass
else:
pass
コンポーネント |
説明 |
|---|---|
|
ステートメントの実行時にチェックされる式または変数。これが |
# Show one emoji when the screen is pressed,
# and a different emoji when not pressed
while True:
if robot.screen.pressing():
robot.screen.show_emoji(EXCITED)
else:
robot.screen.show_emoji(BORED)
wait(0.1, SECONDS)
if/elif/else#
if/elif/else 構造は、条件に基づいてどのインデントされたコードブロックを実行するかを選択します。
ifruns its block if the condition isTrue.elif、以前の条件がすべてFalseの場合にのみ追加の条件をチェックします。複数のelifステートメントを使用できます。else、前の条件のいずれも真でない場合にのみブロックを実行しますTrue。
使用法:
if condition:
pass
elif condition:
pass
else:
pass
コンポーネント |
説明 |
|---|---|
|
ステートメントの実行時にチェックされる式または変数。最初に |
# Move the robot forward or reverse
# based on the position of the joystick
def when_axis_changed():
position = controller.axis1.position()
if position > 0:
robot.move_at(0)
elif position < 0:
robot.move_at(180)
else:
robot.stop_all_movement()
controller.axis1.changed(when_axis_changed)
その間#
while 、条件が Trueである限りコードを繰り返し実行します。また、条件として True 使用することで「永久」のように使用したり、条件に not を追加することで「待機」のように使用することもできます。以下の例を参照してください。
使用法:
while condition:
pass
コンポーネント |
説明 |
|---|---|
|
各反復処理の前にチェックされる式または変数。0 が |
# Keep the LEDs green while the robot is moving
robot.move_for(200, 0, wait=False)
while robot.is_move_active():
robot.led.on(ALL_LEDS, GREEN)
wait(50, MSEC)
robot.led.on(ALL_LEDS, BLACK)
while True:
# Continually flash all LEDs red then green.
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
# Wait until the screen is pressed before
# turning off the LEDs
while not robot.screen.pressing():
robot.led.on(ALL_LEDS, RED)
wait(50, MSEC)
robot.led.on(ALL_LEDS, BLACK)
壊す#
break はループを即座に終了します。2 break 無限ループになる可能性のあるループを終了させるために使用できます。
# Flash LEDs until the screen is pressed
while True:
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
if robot.screen.pressing():
break
# Turn the LEDs Off
robot.led.off(ALL_LEDS)
プログラムを停止する#
stop_program は実行中のプロジェクトを終了します。
Usage:robot.stop_program()
パラメータ |
説明 |
|---|---|
このメソッドにはパラメータがありません。 |
# Stop the project once the screen is pressed
while True:
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
if robot.screen.pressing():
robot.stop_program()
合格#
pass は将来のコードのためのプレースホルダーであり、空のループ、条件分岐、関数におけるエラーを回避するために使用できます。
if condition:
pass
def function():
pass