VEXcode Pro V5 で値を返す関数を作成する

The VEX Visual Studio Code Extension has replaced VEXcode Pro V5, which is now end-of-life.

VEXcode Blocks and VEXcode Text remain actively developed and supported for all VEX platforms.

サンプル: 単純な最大公約数 (GCD) 関数 (Euclid アルゴリズムを使用)

次のサンプルでは、​​単純なループ構造での使用例も示しています。

ユーザー定義関数を使用しない場合:

int main() { 
  int 剰余 = 1;
  int a1、b1、a2、b2;
  vexcodeInit();
  a1 = a2 = 20;
  b1 = b2 = 64;
  while(剰余 > 0){
    剰余 = a1 % b1;
    a1 = b1;
    b1 = 余り。
  }
  Brain.Screen.printAt(5, 60, "GCD ( %d, %d ) = %d", a2, b2, a1);

  a1 = a2 = 60;
  b1 = b2 = 200;
  while(剰余 > 0){
    剰余 = a1 % b1;
    a1 = b1;
    b1 = 剰余;
  }
  Brain.Screen.printAt(5, 60, "GCD ( %d, %d ) = %d", a2, b2, a1);
}

ユーザー定義関数の使用:

int getGCD(int a, int b){
  int 剰余 = 1;
  while(剰余>0){
    剰余 = a % b;
    a = b;
    b = 余り。
  }
  を返します。
}

int main() { 
  vexcodeInit();
  Brain.Screen.printAt(5, 60, "GCD ( %d, %d ) = %d",getGCD(20, 64));
  Brain.Screen.printAt(5, 60, "GCD ( %d, %d ) = %d",getGCD(60, 100));
}

ここでは、呼び出し元関数 main() からのロジック フローのシーケンスを示します。

サンプル: 一連の数値を平均する

float doAverage(int n1, int n2, int n3){
  float avg = (n1 + n2 + n3)/3;平均
  のリターン。
}

int main() { 
  int n1、n2、n3;
  Brain.Screen.print("average(%d, %d, %d) = %d", 10, 20, 30); 
  doAverage(10, 20, 30);

  n1=10; n2=20; n3=40;
  Brain.Screen.print("average(%d, %d, %d) = %d", n1, n2, n3);
  doAverage(n1, n2, n3);
}

サンプル: 可変エンコーダ値を使用してロボットが前進できるようにする

void reportMotionValues(){
  Brain.Screen.printAt(5,100, "%8.2lf%8.2lf%8.2f",
    LeftMotor.position(rev),
    LeftMotor.position(deg));
}

void goStraight(float totalEnc, float vel){
  LeftMotor.resetPosition();
  LeftMotor.setVelocity(50.0, パーセント);
LeftMotor.spinToPosition(totalEnc, deg, true); 戻ります。 } int main() { int enc=1000; // >0: 前方、 <0: 逆方向 vexcodeInit(); for(int i=0; i<10; i++){ enc *= -1; goStraight(enc, 100); Brain.Screen.printAt(5,60, "完了"); レポートモーション値(); //この遅延は、これらのアクションを表示するためだけにここにあります。 //ここで遅延を設定する必要はありません。 wait(2000, ミリ秒); } }

ご覧のとおり、ロボットが可変の回数で前進および後退できるようにすることはほぼ不可能です。実行時にのみ決定できるためです。

For more information, help, and tips, check out the many resources at VEX Professional Development Plus

Last Updated: