VCS Example Program - Modkit - V5 Clawbot - Using the Vision Sensor

This example program shows how to program the V5 Clawbot to use data from the Vision Sensor using Modkit. 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

Configuration
  • A Vision Sensor, on Port 5, named VisionSensor (configured to detect yellow, green, and red signatures)
  • A V5 Smart Motor, on Port 1, named LeftMotor
  • A V5 Smart Motor, on Port 10, named RightMotor
Program

How it works

This program uses a loop to check the Vision Sensor data for Signatures 1 through 3, repeatedly. If one of those signatures is detected, it broadcasts that there is an object of that color detected. That broadcast then triggers 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  when (STARTED) block tells the robot to run this code when the program starts.

Then, the program enters into a forever loop and the Vision Sensor takes its first snapshot of Signature 1 (SIG_1 = green).

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 that is true (1), it broadcasts GreenObject

When GreenObject is broadcasted, 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 (SIG_2 = yellow).

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 that is true (1), it  broadcasts YellowObject

When YellowObject is broadcasted, 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 takessnapshot of Signature 3 (SIG_3 = red).

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 that is true (1), it  broadcasts RedObject

When RedObject is broadcasted, 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 broadcasts NoObject.

When NoObject is broadcasted, the program:

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

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

Still need help? Contact Us Contact Us