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.

主な目的は、エンコーダの計算やモーション操作ではなく、基本的なモーション コードの一部を関数にカプセル化する方法を示すことです。

次のサンプル モーション関数は、基本的なモーション コードの作成に役立ちます。

  • より整理された
  • より読みやすくなりました
  • エラーが起こりにくい

まず最初に、次の基本変数を定義する必要があります。

フロート ホイール = 10.16;
フロート WB = 36.75; //V5 Clawbot 設定に基づく

注: ギア比を変更した場合、以下の計算はギア比に基づいて若干変更される場合があります。

これについては、モーター エンコーダに焦点を当てた別の記事で説明します。

float EncPerCm = 360/(wheel* M_PI);
float EncPerDeg = WB/ホイール;

このサンプルはピボットターン専用です。 例えば 右モーター = 100、左モーター = -100 など。

次に、次のサンプルを確認するときは、パラメータとその仕様についてオンライン API リファレンスを参照することをお勧めします。 パラメータについて詳しく知るには、次のサイトにアクセスしてください:https://api.vexcode.cloud/v5/html/そして、たとえばrotateToを検索してください。


サンプル 1: さまざまな距離に基づいて直進運動をカプセル化する

float Wheel = 10.16; 
フロート WB = 36.75; 
float EncPerCm = 360.0 / (ホイール* M_PI); 

void reportMotionValues(motor m, int line){

  Brain.Screen.printAt(5,line, "%8.2lf%8.2lf%8.2f", 
    m.position(rev), 
    m.position(deg)); 
}

// 距離はセンチメートルです
void goStraight( float distance ){
	
	LeftMotor.resetPosition();
	LeftMotor.resetRotation();
	RightMotor.resetPosition();
	RightMotor.resetRotation();
  	
	float totalEnc = 距離 * EncPerCm;
	
	// 注: 「deg」は vex:: 名前空間からのものです。 したがって、 
        // 「deg」という名前の別の変数を作成しないでください。 

       LeftMotor.setVelocity(50.0, パーセント);
LeftMotor.spinToPosition(totalEnc, deg, false);

RightMotor.setVelocity(50.0, パーセント);
RightMotor.spinToPosition(totalEnc, deg, false); while (LeftMotor.isSpinning() || RightMotor.isSpinning() ) wait(50, ミリ秒); 戻ります。 } int main() { vexcodeInit(); ゴーストレート(100.0); Brain.Screen.printAt(5,60, "完了"); reportMotionValues(LeftMotor, 30); reportMotionValues(RightMotor, 60); }

サンプル 2: さまざまな角度に基づいて左折をカプセル化する

フロート ホイール = 10.16; 
フロート WB = 36.75; 
float EncPerCm = 360.0 / (ホイール* M_PI); 
float EncPerDeg = WB / ホイール;

voidturnRight(float 度){
	
	LeftMotor.resetPosition();
	LeftMotor.resetRotation();
	RightMotor.resetPosition();
	RightMotor.resetRotation();
  	
	float totalEnc = 度 * EncPerDeg;
	
       // 注: この「度」は vex 名前空間からのものです
       LeftMotor.setVelocity(100.0, パーセント);
LeftMotor.spinToPosition(totalEnc, deg, false);

RightMotor.setVelocity(-100.0, パーセント);
RightMotor.spinToPosition(-totalEnc, 度, false); while(LeftMotor.isSpinning() || RightMotor.isSpinning() ) wait(50, ミリ秒); 戻ります。 } int main(){ ... 右折(90.0); ... }

サンプル 3: 左と右の両方を 1 つの関数にカプセル化する

void doTurning( floatdegrees ) {
	// 上記のturnRightとまったく同じコード
}

int main() {
...
   doTurning(90.0); // 右ピボットターン
...
   doTurning(-90.0); // 左ピボットターン
}

より高度なプログラミングで、コードをさらに読みやすくしたい場合は、次のことを試してください。

#define doLeftTurn(d) doTurning(d)
#define doRigtTurn(d) doTurning(-d)

int main() {
...
   doLeftTurn(90.0);  
   doRightTurning(90.0);
...
}

#defineをプリプロセッサマクロ式と呼びます。 この表現を記号として見てください。 コード内でこの記号が出現する場合、という式doTurning(指定された値)に置き換えられます。

プリプロセッサ マクロについては、別の広範なトピックであるため、この記事では説明しません。

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

Last Updated: