コントロール#

導入#

制御機能には、タイミング、プログラムの流れ、条件分岐、およびプロジェクト終了のための機能が含まれています。これらの制御機能を使用すると、実行の一時停止、ループの作成、ロジックパスの定義、およびプログラムの終了を行うことができます。

機能とキーワード#

以下に、API関数やC++キーワードを含む、利用可能なコントロールの一覧を示します。

  • wait – プロジェクトを一定時間一時停止します。

  • for – コードブロックを指定された回数繰り返します。

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

  • if/else – 条件が真の場合は 1 つのコードブロックを実行し、偽の場合は別のブロックを実行します。

  • if/else if/else – 複数の条件を順番にチェックし、条件が真である最初のブロックを実行します。

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

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

  • programStop – プロジェクトを即座に終了します。

wait#

次のコード行に進む前に、プロジェクトを一定時間一時停止します。

Available Functions
void wait(
  double time,
  timeUnits units );

Parameters

パラメータ

タイプ

説明

time

double

待機時間(正の値 double。

units

timeUnits

時間を表す単位: msec (デフォルト) – ミリ秒または seconds

Return Values

この関数は値を返しません。

Examples
// Stop driving after 1 second
Drivetrain.drive(forward);
wait(1, seconds);
Drivetrain.stop();

for#

コードブロックを繰り返します。カウント形式は指定された回数だけ実行され、範囲指定形式はコレクション内の各項目に対して1回ずつ実行されます。

Syntax

1 — カウントループ — 固定回数実行されます。

for (int i = 0; i < count; i++) {
   // code block
}

2 — 範囲ベースのループ — コレクション内の各項目に対して一度実行されます。

for (type element : collection) {
   // code block
}

Parameters

パラメータ

タイプ

説明

count

int

コードブロックが繰り返される回数。

type

タイプ名

コレクション内の各項目のデータ型(例 int double)。

element

variable

ループの現在の項目の値を保持する変数の名前。

collection

array

ループ処理の対象となる項目を保持する配列(またはベクトル)。

Return Values

この制御構造は値を返しません。

Examples
// Drive in a square
for (int i = 0; i < 4; i++) {
  Drivetrain.driveFor(forward, 100, mm);
  Drivetrain.turnFor(right, 90, degrees);
}

// Drive forward different distances
int distances[] = {100, 150, 200};
for (int d : distances) {
  Drivetrain.driveFor(forward, d, mm);
  wait(0.5, seconds);
}

if#

条件が trueの場合、囲まれたコードブロックを実行します。

Syntax
if (condition) {
  // code block
}

Parameters

パラメータ

タイプ

説明

condition

bool

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

Return Values

この制御構造は値を返しません。

Examples
while (true) {
  if (Brain.Screen.pressing()) {
    // Display a message once the screen is pressed
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("Screen pressed!");
    break;
  }
  wait(5, msec);
}

if/else#

条件が true または falseであるかどうかに基づいて、囲まれたコードブロックのどれを実行するかを決定します。

Syntax
if (condition) {
  // code block if true
}
else {
  // code block if false
}

Parameters

パラメータ

タイプ

説明

condition

bool

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

Return Values

この制御構造は値を返しません。

Examples
while (true) {
  if (Brain.Screen.pressing()) {
    // Turn the screen green when pressed
    Brain.Screen.clearScreen(green);
  } else {
    Brain.Screen.clearScreen();
  }
}

if/else if/else#

複数の条件に基づいて、囲まれたコードブロックのうちどれを実行するかを選択します。

Syntax
if (condition) {
  // code block if first condition is true
}
else if (condition) {
  // code block if second condition is true
}
else {
  // code block if no conditions are true
}

Parameters

パラメータ

タイプ

説明

condition

bool

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

Return Values

この制御構造は値を返しません。

Notes
  • if ブロックを実行します true。

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

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

Examples
while (true) {
  // Turn screen blue if pressed on the left half
  if (Brain.Screen.pressing() && Brain.Screen.xPosition() < 240) {
    Brain.Screen.clearScreen(blue);

  // Turn screen green if pressed on the right half
  } else if (Brain.Screen.pressing() && Brain.Screen.xPosition() > 240) {
    Brain.Screen.clearScreen(green);

  } else {
    Brain.Screen.clearScreen();
  }

  wait(20, msec);
}

while#

条件が trueである限り、コードを繰り返し実行します。

Syntax
while (condition) {
  // code block
}

Parameters

パラメータ

タイプ

説明

condition

bool

各反復処理の前に評価される式または変数。0 trueの場合はループが継続され、 falseの場合はループが停止します。

Return Values

この制御構造は値を返しません。

Notes
  • また、以下の例に示すように、条件の前に ! 追加することで、「待機」のように使用することもできます。

Examples
while (true) {
  if (Brain.Screen.pressing()) {
    // Turn the screen green whenever it's pressed
    Brain.Screen.clearScreen(green);
  } else {
    Brain.Screen.clearScreen();
  }
}

// Wait until the screen is pressed to turn it green
while (!Brain.Screen.pressing()) {
  wait(20, msec);
}

Brain.Screen.clearScreen(green);

break#

現在のループを即座に終了します。ネストされたループの場合、 break 最も内側のループを終了します。

Syntax
break;

Parameters

このステートメントはパラメータを受け付けません。

Return Values

このステートメントは値を返しません。

Examples
while (true) {

  // Press the screen to pause on a color
  Brain.Screen.clearScreen(red);
  wait(500, msec);

  if (Brain.Screen.pressing()) {
    break;
  }

  Brain.Screen.clearScreen(green);
  wait(500, msec);

  if (Brain.Screen.pressing()) {
    break;
  }
}

programStop#

プロジェクトを即座に終了します。

Syntax
Brain.programStop();

Parameters

このステートメントはパラメータを受け付けません。

Return Values

このステートメントは値を返しません。

Examples
while (true) {

  // Press the screen to end the project
  Brain.Screen.clearScreen(red);
  wait(500, msec);

  if (Brain.Screen.pressing()) {
    Brain.programStop();
  }

  Brain.Screen.clearScreen(green);
  wait(500, msec);

  if (Brain.Screen.pressing()) {
    Brain.programStop();
  }
}