การเขียนฟังก์ชั่นโมฆะโดยไม่มีพารามิเตอร์ใน 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.

ตัวอย่าง: โปรแกรมตัวอย่างสำหรับหุ่นยนต์ที่จะเคลื่อนที่ไปในระยะทางตรง

ฟังก์ชันต่อไปนี้จะอนุญาตให้หุ่นยนต์วิ่งไปข้างหน้าเป็นเวลาtotalEncencoder มันใช้การกำหนดค่า V5 Clawbot

ฟังก์ชันspinToPositionนี้จะไม่ส่งค่ากลับจนกว่าการเคลื่อนไหวจะเสร็จสมบูรณ์ เรียกว่าการทำงานแบบ "ซิงโครไนซ์" ซึ่งกำหนดโดยพารามิเตอร์สุดท้ายtrue

โดยไม่ใช้ฟังก์ชันที่ผู้ใช้กำหนดเอง:

int main() {
   vexcodeInit();  

   LeftMotor.resetPosition();
   LeftMotor.setVelocity(100.0, percent);
   LeftMotor.spinToPosition(500.0, deg, true);


   Brain.Screen.printAt(5, 30, "secs rev deg");
   Brain.Screen.printAt(5, 60, "%.2f: %8.2f %8.2f", 
     Brain.Timer.value(), 
     LeftMotor.position(rev), 
     LeftMotor.position(deg)); 
     wait(1000, msec);

   LeftMotor.resetPosition();
   LeftMotor.setVelocity(-100.0, percent);
   LeftMotor.spinToPosition(-1000.0, c, true); }

   Brain.Screen.printAt(5,100, "secs rev deg" );
   Brain.Screen.printAt(5,130, "%%.2f: %8.2f %8.2f", 
     Brain.Timer.value(),
     LeftMotor.position(rev), 
     LeftMotor.position(deg)); 
     wait(1000, msec);
}

ตัวอย่างอาจให้ผลลัพธ์ดังต่อไปนี้:

วินาที รอบดิบ องศา
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();  

   LeftMotor.resetPosition();
   LeftMotor.setVelocity(50.0, percent);
   LeftMotor.spinToPosition(500.0, deg, true);
   reportMotionValues();

   LeftMotor.resetPosition ();
   LeftMotor.setVelocity(-50.0, percent);
   LeftMotor.spinToPosition(-500.0, deg, true);
   reportMotionValues();

}

วิดีโอด้านล่างแสดงลำดับการทำงานของตรรกะจากฟังก์ชันผู้เรียก main():

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

Last Updated: