VCS Example Program - VEX C++ - V5 Clawbot - Using the Vision Sensor

This example program shows how to program the V5 Clawbot with VEX C++ to use data from the Vision Sensor. The program has the robot move forward when it detects a green object, turn right when it detects a yellow object, turn left when it detects a red object, or stop when it does not detect an object.

UsingVisionSensor.vex
robot-config.h
using namespace vex;
vex::brain Brain;
vex::motor LeftMotor     (vex::PORT1, vex::gearSetting::ratio18_1, false);
vex::motor RightMotor    (vex::PORT10, vex::gearSetting:ratio18_1, true);
vex::vision VisionSensor (vex::PORT5);
main.cpp
#include "robot-config.h"


int main() {
while (true) {
    VisionSensor.takeSnapshot(1);
    if (VisionSensor.largestObject.exists && VisionSensor.largestObject.width>5) {
        Brain.Screen.setPenColor(vex::color::white);
        Brain.Screen.setFillColor(vex::color::green);
        Brain.Screen.drawRectangle(0,0, 480, 240);
        Brain.Screen.setCursor(2,6);
        Brain.Screen.setFont(vex::fontType::mono40);
        Brain.Screen.print("Forward");
        LeftMotor.spin(vex::directionType::fwd);
        RightMotor.spin(vex::directionType::fwd);
    }
    	else  {
            VisionSensor.takeSnapshot(2);
    if (VisionSensor.largestObject.exists && VisionSensor.largestObject.width>5) {
        Brain.Screen.setPenColor(vex::color::black);
        Brain.Screen.setFillColor(vex::color::yellow);
        Brain.Screen.drawRectangle(0,0, 480, 240);
        Brain.Screen.setCursor(2,6);
        Brain.Screen.setFont(vex::fontType::mono40);
        Brain.Screen.print("Right");
        LeftMotor.spin(vex::directionType::fwd);
        RightMotor.spin(vex::directionType::rev);
    }
    	else {
            VisionSensor.takeSnapshot(3);
    if (VisionSensor.largestObject.exists && VisionSensor.largestObject.width>5) {
        Brain.Screen.setPenColor(vex::color::white);
        Brain.Screen.setFillColor(vex::color::red);
        Brain.Screen.drawRectangle(0,0, 480, 240);
        Brain.Screen.setCursor(2,6);
        Brain.Screen.setFont(vex::fontType::mono40);
        Brain.Screen.print("Left");
        LeftMotor.spin(vex::directionType::rev);
        RightMotor.spin(vex::directionType::fwd);
    }
    	else {
            LeftMotor.stop();
            RightMotor.stop();
            Brain.Screen.clearScreen();
        }
        }
        } 
    task::sleep(100);
}
}
How it works

This program checks the Vision Sensor data for Signatures 1 through 3, repeatedly. If one of those signatures is detected, the program carries out a series of commands that change the display on the brain and have the robot move forward, turn left or right, or stop (no signature detected). 

First, the while (true) line of code tells the robot to run this code repeatedly as long as the condition is true like a forever loop.

Then, the Vision Sensor takes its first  snapshot of Signature 1 (1 = green signature).

Then, the program decides (Boolean: 1-true / 0-false)   if the Vision Sensor detects that the largest green object exists and that the largest object's width is greater than 5

If both of those conditionals are true (1), then the program:

  • sets the brain's screen's pen color to white 
  • sets the brain's screen's fill color to green
  • then the brain's screen draws a rectangle starting at (0,0) that is 480 pixels wide and 240 pixels tall
  • then sets the brain's screen's cursor at (2,6)
  • sets the brain's screen's font to mono40
  • then the brain's screen prints "Forward"
  • and then the LeftMotor spins forward
  • and the RightMotor spins forward

If those two conditionals are not both true (0-false ---> else), the Vision Sensor takes a snapshot of Signature 2 (2 = yellow signature).

Then, the program decides (Boolean: 1-true / 0-false)   if the Vision Sensor detects that the largest yellow object exists and that the largest object's width is greater than 5.

If both of those conditionals are true (1), then the program:

  • sets the brain's screen's pen color to black 
  • sets the brain's screen's fill color to yellow
  • then the brain's screen draws a rectangle starting at (0,0) that is 480 pixels wide and 240 pixels tall
  • then sets the brain's screen's cursor at (2,6)
  • sets the brain's screen's font to mono40
  • then the brain's screen prints "Right"
  • and then the LeftMotor spins forward
  • and the RightMotor spins reverse

If those two conditionals are not both true (0-false ---> else), the Vision Sensor takes a snapshot of Signature 3 (3 = red signature).

Then, the program decides (Boolean: 1-true / 0-false)   if the Vision Sensor detects that the largest red object exists and that the largest object's width is greater than 5.

If both of those conditionals are true (1), then the program:

  • sets the brain's screen's pen color to white 
  • sets the brain's screen's fill color to red
  • then the brain's screen draws a rectangle starting at (0,0) that is 480 pixels wide and 240 pixels tall
  • then sets the brain's screen's cursor at (2,6)
  • sets the brain's screen's font to mono40
  • then the brain's screen prints "Left"
  • and then the LeftMotor spins reverse
  • and the RightMotor spins forward

If those two conditionals are  not both true (0-false --->else), the program:

  • stops the LeftMotor
  • stops the RightMotor
  • and clears the brain's screen

Then the program  waits 100 milliseconds and starts the loop again by taking a Snapshot of Signature 1 and continuing.

Still need help? Contact Us Contact Us