VCS Example Program - VEX C++ - V5 Clawbot - Opening and Closing the Claw

Get more with VEXcode
We recommend you transition to our new software. VEXcode has both block-based and text-based options, that work on Chromebooks, iPads, Android tablets, Windows and Macs. Visit code.vex.com for more details on how to download VEXcode.
VEX Coding studio has been discontinued and is no longer available for download. Already downloaded? The program will continue to function but will no longer be supported.
Users who transition to VEXcode can migrate their C++/C++ Pro projects to VEXcode Text by following this article.

This example program shows how to program the V5 Clawbot to open and close its claw using VEX C++.

BasicOpenCloseClaw.vex

robot-config.h
vex::brain Brain;
vex::motor LeftMotor (vex::PORT1, vex::gearSetting::ratio18_1,false);
vex::motor ClawMotor (vex::PORT3, vex::gearSetting::ratio18_1,false);
vex::motor RightMotor (vex::PORT10, vex::gearSetting::ratio18_1,true);
main.cpp
#include "robot-config.h"

vex::directionType reverse = vex::directionType::rev;
vex::directionType forward = vex::directionType::fwd;
vex::velocityUnits rpm = vex::velocityUnits::rpm;

int main() {

 while(true) {
  ClawMotor.spin(reverse);
  vex::task::sleep(200);
  while(ClawMotor.velocity(rpm) < 0) { }
  ClawMotor.stop();
    
  vex::task::sleep(1000);
    
  ClawMotor.spin(forward);
  vex::task::sleep(200);
  while(ClawMotor.velocity(rpm) > 0) { }
  ClawMotor.stop();
 }
}
How it works

First, for convenience, variables named reverse, forward, and rpm are declared. These are shorthand to make the program easier to read.

Next, the main function is declared.

Then, the claw motor is set to spin in reverse. Since spin is used, the motor will begin rotating and the program will continue immediately. Spinning in reverse causes the claw motor to open the claw jaws.

Then, the program will pause for two-tenths of a second ( 200 milliseconds) to allow the motor to start spinning.

Next, the program will enter a while loop, continuously checking if the motor's velocity is negative (< 0). When the claw jaws open fully, the motor's movement will be resisted. When this occurs, the velocity will become 0 and the loop will exit. Because the loop is empty, nothing else will happen while the claw is opening.

Then, when the claw has finished opening, the claw motor will be commanded to stop. This stops the motor's spin so it does not continue trying to open. When this happens, the claw jaws will spring back slightly.

Next, the program will pause for 1 second ( 1000 milliseconds).

Finally, the spin command, while loop, and stop command are repeated but spinning forward. Spinning the claw motor in the opposite direction will make the claw jaws close. When the claw's motion is resisted, the loop will finish and the motor will stop.