在 VEXcode Pro V5 中寫出不帶參數的 Void 函數

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.

例:機器人直線行走的範例程序

以下函數將允許機器人向前運行TotalEnc編碼器。 它使用 V5 Clawbot 配置。

這個spinToPosition函數在移動完成前不會回傳。 這稱為“同步”操作 - 由最後一個參數true決定。

不使用使用者定義的函數:

int main() {
   vexcodeInit();  

   LeftMotor.resetPosition();
   LeftMotor.setVelocity(100.0, 百分比);
   LeftMotor.spinToPosition(500.0, deg, true);


   Brain.Screen.printAt(5,30, "秒轉速" );
   Brain.Screen.printAt(5,60, "%.2f: %8.2f %8.2f", 
     Brain.Timer.value(), 
     LeftMotor.position(rev), 
     LeftMotor.position(deg)); 
     等待(1000,毫秒);

   LeftMotor.resetPosition();
   LeftMotor.setVelocity(-100.0, 百分比);
   LeftMotor.spinToPosition(-1000.0, c, true);

   Brain.Screen.printAt(5,100, "秒轉度");
   Brain.Screen.printAt(5,130, "%%.2f: %8.2f %8.2f", 
     Brain.Timer.value(),
     LeftMotor.position(rev), 
     LeftMotor.position(deg)); 
     等待(1000,毫秒);
}

範例可能會給出以下結果:

秒 原轉速 度
2.06 5445 3.12 1089

請注意,您的測試結果在小數點後的變化幅度非常小。

使用使用者定義的函數:

以下範例展示如何將一組操作擷取到單一可重複使用的「函數」呼叫中。

void reportMotionValues() {

  Brain.Screen.printAt(5, 60, "%.2f: %8.2f %8.2f",
    Brain.Timer.value(), 
    LeftMotor.position(rev),
    LeftMotor.position(deg)); 
}


int main() {
   vexcodeInit();  

   左馬達.resetPosition();
   LeftMotor.setVelocity(50.0, 百分比);
   LeftMotor.spinToPosition(500.0, deg, true);
   報告運動值();

   LeftMotor.resetPosition();
   LeftMotor.setVelocity(-50.0,%);
   LeftMotor.spinToPosition(-500.0, deg, true);
   報告運動值();

}

下面顯示了呼叫函數 main() 的邏輯流程順序:

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

Last Updated: