VCS Example Program - VEX C++ - V5 Clawbot - Turning Left and Right

This example program shows how to program the V5 Clawbot to turn left and right using VEX C++.


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

vex::rotationUnits revolutions = vex::rotationUnits::rev;

int main() {
    float revsPerClawbot90Turn = 1.7;
    LeftMotor.startRotateFor(-1 * revsPerClawbot90Turn, revolutions);
    RightMotor.rotateFor(revsPerClawbot90Turn, revolutions);
    LeftMotor.startRotateFor(2 * revsPerClawbot90Turn, revolutions);
    RightMotor.rotateFor(-2 * revsPerClawbot90Turn, revolutions);
How it works

First, for convenience, a variable named revolutions is declared. This is a shorthand to make the program easier to read.

Next, the main function is declared. A floating point number variable, revsPerClawbot90Turn is declared and assigned to 1.7.

This value should turn a V5 Clawbot with 18:1 gear motors approximately 90 degrees. Based on the weight of the robot and the friction of the wheels, the amount of turn may be slightly more or less than 90 degrees. You can adjust  the  revsPerClawbot90Turn  variable if desired.

Then, the left motor is set to start rotating with a negative number of revolutions. Since startRotateFor is used the motor will begin rotating and the program will continue immediately.

Next, the right motor is set to rotate - but with a positive number of revolutions. Since rotateFor is used the program will not continue until the motor completes its rotation.

Since the motors are set to rotate in opposite directions, with the left motor rotating backward, the robot will turn in place to the left. The facing of the V5 Clawbot will change by about 90 degrees.

Then, when the right motor has finished rotating, the program will pause for 1 second ( 1000 milliseconds).

Finally, the motor rotation commands are repeated. This time, the positive and negative rotations are switched and multiplied by 2. The motors will spin in the opposite direction, and twice as far, which will turn the robot to face in the opposite direction.

Still need help? Contact Us Contact Us