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

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.

Still need help? Contact Us Contact Us