Void Functions() - Programming

 Sample: A sample program for a robot to go a straight distance

The following function will allow the robot to run forward for totalEnc encoder. It uses the V5 Clawbot configuration.

This spinToPosition function will not return until movement is complete. This is called “synchronized” action – determined by the last parameter true.

 

Without using a user-defined function:
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);
}

The sample may give the following result:

secs    raw   rev     deg
2.06    5445  3.12    1089

Do note that your test will vary within a very small margin in the decimal places.

With a user-defined function:

The following sample shows how to capture a set of operations into a single reusable “function” call.

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();

}

Here shows the sequence of logic flow from the caller function main():