スイッチ#

導入#

Switchブロックは、生徒がブロックベースのコーディングからテキストベースのコーディングへスムーズに移行できるよう支援します。Switchブロックを使用すると、生徒はブロックプロジェクト内でPythonコードを直接入力できます。コマンド、条件、値、関数定義などを記述しながら、プロジェクトの残りの部分はブロックで構築できます。個々のブロックは、対応するPythonコマンドに変換することも可能です。

Python コマンド、メソッド、演算子の入力方法については、VEX CTE Python API を参照してください。

SwitchブロックにはPythonコードが含まれているため、プロジェクトが期待どおりに動作するためには、スペル、句読点、インデントが正しい必要があります。

以下は、すべてのスイッチブロックのリストです。

ブロックの切り替え — ブロックプロジェクトにPythonコードを追加します。

  • スタック — 他のスタックブロックと順番に実行される Python コマンドを入力するスペースを提供します。

  • Cブロック — ifwhile for ステートメントなどの Python 制御コード while提供します。

  • C Expandable — Provides a space to type Python control code with multiple branches, such as if, elif, or else.

  • Boolean — True または Falseを返す Python コードを入力するスペースを提供します。

  • レポーター — 値を返すPythonコードを入力するスペースを提供します。

  • Hat — Python 関数を定義するためのスペースを提供します。

スタック#

スタックブロックは、他のスタックブロックと順番に実行されるPythonコマンドを入力するためのスペースを提供します。

スタックブロック#
[] :: custom-switch

スタックブロックには、1行または複数行のPythonコードを含めることができます。

起動時に、スタックブロックを使用して6軸アームを所定の位置に移動させます。#
開始したとき :: hat events
[Move the 6-Axis Arm to a standard position at the start of the project.]
[arm.move_to(120, 120, 75, False)] :: custom-switch

C棟#

The C Block provides a space to type Python control code, such as if, while, or for statements. Blocks placed inside the C Block run as part of that Python control structure.

C棟#
if [] :: custom-switch
終わり

Cブロックで使用できるPython制御コードには以下が含まれます。

起動時に、Cブロックを使用して6軸アームが所定の位置に到達できるかどうかを確認します。#
開始したとき :: hat events
[Check if the 6-Axis Arm can move to a position.]
if [if not arm.can_arm_reach_to(0, 0, 0):] :: custom-switch
[The 6-Axis Arm can't move to this position.] をコンソールに表示 ◀ カーソルを次の行へ移動する
終わり

C拡張可能#

The C Expandable block provides a space to type Python control code with multiple branches, such as if, elif, or else. Additional branches can be added by selecting the plus arrow.

C 拡張ブロック#
if [] :: custom-switch-expand
終わり

C Expandableブロックで使用できるPython制御コードには、以下のものが含まれます。

起動時に、C拡張ブロックを使用して、6軸アームが特定の位置に到達できるかどうかを確認します。#
開始したとき
[Check if the 6-Axis Arm can or can't move to a position.]
if [if not arm.can_arm_reach_to(0, 0, 0):] :: custom-switch-expand
[The 6-Axis Arm can't move to this position.] をコンソールに表示 ◀ カーソルを次の行へ移動する
else [else:] :: custom-switch-expand
[The 6-Axis Arm can move to this position.] をコンソールに表示 ◀ カーソルを次の行へ移動する
終わり

ブール値#

ブールブロックは True または Falseを返すPythonコードを入力するスペースを提供します。六角形の入力を持つブロック内に配置できます。

ブールブロック#
<[] :: custom-switch>

ブールブロックにはPythonの条件を含めることができます。

Python条件を含むブールブロック#
<[bumper_a.is_pressed()] :: custom-switch>

ブールブロックでは、論理演算子も使用できます。

Pythonの論理演算子を使用したブールブロック#
<[bumper_a.is_pressed() or bumper_b.is_pressed()] :: custom-switch>

ブールブロックでは、比較演算子を使用して値を比較できます。

Pythonの比較演算子を使用したブールブロック#
<[drivetrain.get_heading()> 100] :: custom-switch>
起動時に、ブールブロックを使用して、6軸アームが特定の位置に到達できるかどうかを確認します。#
開始したとき
[Check if the 6-Axis Arm can move to a position.]
もし <<[arm.can_arm_reach_to(0, 0, 0)] :: custom-switch> でない> なら
[The 6-Axis Arm can't move to this position.] をコンソールに表示 ◀ カーソルを次の行へ移動する
終わり

記者#

Reporterブロックは、値を返すPythonコードを入力するスペースを提供します。循環入力を持つブロック内に配置できます。

記者ブロック#
([] :: custom-switch)

Reporterブロックには、値を返すPythonコードを含めることができます。

値を返すPythonコードを含むレポーターブロック#
([arm.get_y()] :: custom-switch)

Reporterブロックでは、数学演算子を使用できます。

  • 加算: +

  • 引き算: -

  • 分割: /

  • 乗算: *

Pythonを追加したレポーターブロック#
([arm.get_y() + 45] :: custom-switch)
起動すると、6軸アームが動作している間、現在のy座標を表示します。#
開始したとき
[Display the current y-position while the 6-Axis Arm moves.]
[アーム v] を位置 x:(-100) y:(200) z:(100) [mm v] へ移動 ◀ 待たない
<[アーム v] 移動完了?> まで繰り返す
([arm.get_y()] :: custom-switch) をコンソールに表示 ◀ カーソルを次の行へ移動する
[0.25] 秒待つ
終わり

帽子#

ハットブロックは、Python関数を定義するためのスペースを提供します。ハットの下に接続されたブロックは、その関数の本体となります。

帽子の形#
[] :: hat custom-switch

ハットブロックにPython関数の定義を入力します。引数は括弧の中に記述します。引数を使用しない関数の場合は、空の括弧を使用します。

Python関数定義を含むハットブロック#
[def function_name(parameter):] :: hat custom-switch
起動時に、ハットブロックを使用して関数を定義し、呼び出します。#
[def increment_y_100():] :: hat custom-switch
[Create a function to increment the 6-Axis Arm.]
[アーム v] の位置を x:[0] y:[100] z:[0] [mm v] 増分 ▶

開始したとき :: hat events
[increment_y_100()] :: custom-switch