VEX V5 Vision Sensor code help

GOAL: I would like to use a VEX V5 Vision sensor to move towards a game object.

PROBLEM: However, I am unsure on how to program this feature as I am fairly new to VEX V5 Sensors. Could you please help?

INFO: I am using VEXcode V5 Pro to program my vision sensor and all online tutorials seem to be only block code

1 Like

I can help you with your problem’s logic, but not the coding itself. https://api.vexcode.cloud/v5/namespace/namespacevex has documentation on the code.

For ease of use, place the vision sensor in the middle of your drivetrain, on the front end. This will make it much easier to perform orientation.

First of all, you need to let the vision sensor detect the specific object that you are looking for. This could be anything. You need to find the object signature’s color (purple for a ring, yellow for a mobile goal, etc). Once you have found that, you need to orient your robot to move to the object.

You need to rotate to the side of the robot that the vision center detects the object (turn right for an object on the right) until the center of the object (NOT the origin) matches up with the center of the vision sensor’s view.

You can find the center of the object by taking the height, dividing it by 2, and adding the final result to the object’s origin. This also must be completed for the width.

After that, it’s just moving forward and doing whatever operation you need to do.

Please read

I am not totally sure on all of this; it’s just my initial thoughts. Take with a grain of salt.

3 Likes

I already have the sensor trained to target a specific color and I have also programmed it to turn towards the color. As for the placement of the sensor, we currently plan to put it at the front of the robot.

About the moving forward part, say I want to go forward until I am close enough to pick up the object. But that distance changes every time depending on the placement of the robot. So what I want to do is make the bot go forward until the my color takes up say 80% of the sensor’s FOV and stop moving and then pick up the object( I have code for the pickup portion done).

80% of the sensor’s FOV

80% of the sensor’s FOV would be 53172 pixels. Compare height of the object multiplied by width with 53172 to figure out how far/close it is.

If (h times w) > 53172, preform x operation.

3 Likes

I have added this as well as more changes to my code.
When I ran it, it just spins in a circle to the left.
No matter how close the object is placed, it still spins in a circle.

I have included my code below:

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       C:\Users\vexrobotics                                      */
/*    Created:      Tue Jan 25 2022                                           */
/*    Description:  V5 project                                                */
/*                                                                            */
/*----------------------------------------------------------------------------*/
   
// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// VisionSensor         vision        1               
// FL                   motor         12              
// FR                   motor         11              
// BL                   motor         14              
// BR                   motor         13              
// ---- END VEXCODE CONFIGURED DEVICES ----
#include "vex.h"

using namespace vex;

#include "robot-config.h"

competition Competition;
int centerFOV = 158;
int offsetX = 15;

void auto_turn()
{
  while(true)
  {
   Brain.Screen.clearLine();
   VisionSensor.takeSnapshot(VisionSensor__SIG_YELLOW);

   if(VisionSensor.largestObject.exists)
   {
     if(VisionSensor.largestObject.centerX > centerFOV + offsetX)//If the object is to the left then turns right
     {
       FL.spin(forward, 100, percent);
       FR.spin(forward, 100, percent);
       BL.spin(reverse, 100, percent); 
       BR.spin(reverse, 100, percent);
     }
   }
   else if(VisionSensor.largestObject.centerX < centerFOV - offsetX) // If the object is to the right then turn left
   {
      FL.spin(reverse, 100, percent);
       FR.spin(reverse, 100, percent);
       BL.spin(forward, 100, percent); 
       BR.spin(forward, 100, percent);
     //Spin Motors
   }
    else
    {
      FL.setVelocity(0,percent);
      BL.setVelocity(0,percent);
      FR.setVelocity(0,percent);
      BR.setVelocity(0,percent);
    }
   }
  }

int hw = 0;
int h = 0;
int w = 0;
void movefwd()
{
  h = VisionSensor.objects[0].height;
  w = VisionSensor.objects[0].width;
  hw = h*w; 
  if(hw > 53172)
  {   
    FL.spin(forward, 100, percent);
       FR.spin(forward, 100, percent);
       BL.spin(forward, 100, percent); 
       BR.spin(forward, 100, percent);
  }
  else
  {
     FL.setVelocity(0,percent); 
      BL.setVelocity(0,percent);
      FR.setVelocity(0,percent);
      BR.setVelocity(0,percent);
  }
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  
  while(true)
  {
   auto_turn();
   movefwd();

  }
}

edit: code tags added by moderators

Add wait function.
Use motor. stop();
Combine forward and turn into one function

1 Like

NOTE: It is supposed to be SIG_YELLOW but it is set to SIG_1 so the code doesn’t give an error.

/----------------------------------------------------------------------------/
/* /
/
Module: main.cpp /
/
Author: C:\Users\vexrobotics /
/
Created: Tue Jan 25 2022 /
/
Description: V5 project /
/
/
/
----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name] [Type] [Port(s)]
// VisionSensor vision 1
// FL motor 12
// FR motor 11
// BL motor 14
// BR motor 13
// ---- END VEXCODE CONFIGURED DEVICES ----
#include “vex.h”

using namespace vex;

#include “robot-config.h”

competition Competition;
int centerFOV = 158;
int offsetX = 15;

int hw = 0;
int h = 0;
int w = 0;
void movement()
{
h = VisionSensor.objects[0].height;
w = VisionSensor.objects[0].width;
hw = h*w;
while(true)
{
Brain.Screen.clearLine();
VisionSensor.takeSnapshot(VisionSensor__SIG_1);//THIS HAS BEEN CHANGED SO THAT IT DOESNT THROW ERRORS. IT IS ACTUALLY SIG_ YELLOW

if(VisionSensor.largestObject.exists)
{
if(VisionSensor.largestObject.centerX > centerFOV + offsetX)//If the object is to the left then turns right
{
FL.spin(forward, 100, percent);
FR.spin(forward, 100, percent);
BL.spin(reverse, 100, percent);
BR.spin(reverse, 100, percent);
}
}
else if(VisionSensor.largestObject.centerX < centerFOV - offsetX) // If the object is to the right then turn left
{
FL.spin(reverse, 100, percent);
FR.spin(reverse, 100, percent);
BL.spin(forward, 100, percent);
BR.spin(forward, 100, percent);
//Spin Motors
}
else
{
FL.setVelocity(0,percent);
BL.setVelocity(0,percent);
FR.setVelocity(0,percent);
BR.setVelocity(0,percent);
}

if(hw > 53172)
{
FL.spin(forward, 100, percent);
FR.spin(forward, 100, percent);
BL.spin(forward, 100, percent);
BR.spin(forward, 100, percent);
}
else
{
FL.setVelocity(0,percent);
BL.setVelocity(0,percent);
FR.setVelocity(0,percent);
BR.setVelocity(0,percent);
}
}
}

int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();

while(true)
{
movement();
wait(150, msec);

}
}

Formated Code
NOTE: It is supposed to be SIG_YELLOW but it is set to SIG_1 so the code doesn’t give an error.

/----------------------------------------------------------------------------/
/* /
/ Module: main.cpp /
/ Author: C:\Users\vexrobotics /
/ Created: Tue Jan 25 2022 /
/ Description: V5 project /
/ /
/----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name] [Type] [Port(s)]
// VisionSensor vision 1
// FL motor 12
// FR motor 11
// BL motor 14
// BR motor 13
// ---- END VEXCODE CONFIGURED DEVICES ----
#include “vex.h”

using namespace vex;

#include “robot-config.h”

competition Competition;
int centerFOV = 158;
int offsetX = 15;

int hw = 0;
int h = 0;
int w = 0;
void movement()
{
h = VisionSensor.objects[0].height;
w = VisionSensor.objects[0].width;
hw = h*w;
while(true)
{
Brain.Screen.clearLine();
VisionSensor.takeSnapshot(VisionSensor__SIG_1);//THIS HAS BEEN CHANGED SO THAT IT DOESNT THROW ERRORS. IT IS ACTUALLY SIG_ YELLOW

if(VisionSensor.largestObject.exists)
{
if(VisionSensor.largestObject.centerX > centerFOV + offsetX)//If the object is to the left then turns right
{
FL.spin(forward, 100, percent);
FR.spin(forward, 100, percent);
BL.spin(reverse, 100, percent);
BR.spin(reverse, 100, percent);
}
}
else if(VisionSensor.largestObject.centerX < centerFOV - offsetX) // If the object is to the right then turn left
{
FL.spin(reverse, 100, percent);
FR.spin(reverse, 100, percent);
BL.spin(forward, 100, percent);
BR.spin(forward, 100, percent);
//Spin Motors
}
else
{
FL.setVelocity(0,percent);
BL.setVelocity(0,percent);
FR.setVelocity(0,percent);
BR.setVelocity(0,percent);
}

if(hw > 53172)
{
FL.spin(forward, 100, percent);
FR.spin(forward, 100, percent);
BL.spin(forward, 100, percent);
BR.spin(forward, 100, percent);
}
else
{
FL.setVelocity(0,percent);
BL.setVelocity(0,percent);
FR.setVelocity(0,percent);
BR.setVelocity(0,percent);
}
}
}

int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();

while(true)
{
movement();
wait(150, msec);

}
}
3 Likes

Please format your code by enclosing it with “```”. Thank @RoboKnight for formatting the code.

replace setVelocity(0,percent); with stop( );
I am sorry I may not make it clear. When I said “combine” I mean to combine the input into one value and feed to the motor. Each motor may have a different value of input though. To start with simple code, I would suggest you go back to two functions, one for turning and one for forward. However, you will need to wait until turning finished. You could use waitUntil() or wait(enough time). Please try turing function first, then add forward function when it works.

1 Like

I have replaced setVelocity(0, percent); with stop(); and I have also fixed an if statement typo so it included all of the else ifs(there was an extra curly). Lastly, I also added an extra precautionary if statement so that the forward code would only run after the color is in the center of the sensor’s FOV. I have attached my code below.

Lastly thanks to @RoboKnight and @jpearman for formatting the code(I didn’t know how cuz I am new to the forum)

{
/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       C:\Users\vexrobotics                                      */
/*    Created:      Tue Jan 25 2022                                           */
/*    Description:  V5 project                                                */
/*                                                                            */
/*----------------------------------------------------------------------------*/
   
// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// VisionSensor         vision        1               
// FL                   motor         12              
// FR                   motor         11              
// BL                   motor         14              
// BR                   motor         13              
// ---- END VEXCODE CONFIGURED DEVICES ----
#include "vex.h"

using namespace vex;

#include "robot-config.h"

competition Competition;
int centerFOV = 158;
int offsetX = 15;
int run = 0; // Will be used as a precaution before running forward code
void auto_turn()
{
  while(true)
  {
   Brain.Screen.clearLine();
   VisionSensor.takeSnapshot(VisionSensor__SIG_YELLOW);

   if(VisionSensor.largestObject.exists)
   {
     if(VisionSensor.largestObject.centerX > centerFOV + offsetX)//If the object is to the left then turns right
     {
       FL.spin(forward, 100, percent);
       FR.spin(forward, 100, percent);
       BL.spin(reverse, 100, percent); 
       BR.spin(reverse, 100, percent);
     }
   
   else if(VisionSensor.largestObject.centerX < centerFOV - offsetX) // If the object is to the right then turn left
   {
      FL.spin(reverse, 100, percent);
       FR.spin(reverse, 100, percent);
       BL.spin(forward, 100, percent); 
       BR.spin(forward, 100, percent);
     //Spin Motors
   }
    else
    {
       FL.stop();
     BL.stop();
     FR.stop();
    BL.stop();
    }
   }

   //Extra precaution for running the forward code
   if(VisionSensor.largestObject.centerX > centerFOV + offsetX || VisionSensor.largestObject.centerX == centerFOV - offsetX )//If the object is to the left then turns right
     {
       run = 3;
     }  
  }

int hw = 0;
int h = 0;
int w = 0;
void movefwd()
{
  h = VisionSensor.objects[0].height;
  w = VisionSensor.objects[0].width;
  hw = h*w; 
  if(hw > 53172)//Or would it be hw < 53172?
  {   
    FL.spin(forward, 100, percent);
       FR.spin(forward, 100, percent);
       BL.spin(forward, 100, percent); 
       BR.spin(forward, 100, percent);
  }
  else
  {
     FL.stop();
     BL.stop();
     FR.stop();
    BL.stop();
  }
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  
  while(true)
  {
   auto_turn();

   if(run == 3)
   {
    movefwd();
   }
   else
   {
     run == 0;//Not necessary bc run is being overwritten each time but just in case
   }
  }
}

}

Also, in my code I am saying if hw(the height and width of the mobile goal) is greater than 53172 it should move forward.
Should it be hw < 53172 because as the robot gets closer the height and width increases?

Yes, you are right. It should be hw < 53172

You have everything in the {}. Hope you notice this and correct it.

You need add wait() in the while loop of the main.

You do not need while in your auto_trun()

1 Like

Yep, I fixed my code acording to your suggestions and fixed other errors as well.
Fixed Errors:
Auto_turn wasn’t closed with }
While loop in auto_turn is deleted.
Outer {} are deleted
Motors have been programmed to turn properly(they were wrong before)
The if statement has been changed to hw < 53172
A wait statement has been added in the main method

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       C:\Users\vexrobotics                                      */
/*    Created:      Tue Jan 25 2022                                           */
/*    Description:  V5 project                                                */
/*                                                                            */
/*----------------------------------------------------------------------------*/
   
// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// VisionSensor         vision        1               
// FL                   motor         12              
// FR                   motor         11              
// BL                   motor         14              
// BR                   motor         13              
// ---- END VEXCODE CONFIGURED DEVICES ----
#include "vex.h"

using namespace vex;

#include "robot-config.h"

competition Competition;
int centerFOV = 158;
int offsetX = 15;
int run = 0; // Will be used as a precaution before running forward code
void auto_turn()
{
   Brain.Screen.clearLine();
   VisionSensor.takeSnapshot(VisionSensor__SIG_YELLOW);

   if(VisionSensor.largestObject.exists)
   {
     if(VisionSensor.largestObject.centerX > centerFOV + offsetX)//If the object is to the left then turns right
     {
       FL.spin(forward, 100, percent);
       FR.spin(reverse, 100, percent);
       BL.spin(forward, 100, percent); 
       BR.spin(reverse, 100, percent);
     }
   
   else if(VisionSensor.largestObject.centerX < centerFOV - offsetX) // If the object is to the right then turn left
   {
      FL.spin(reverse, 100, percent);
       FR.spin(forward, 100, percent);
       BL.spin(reverse, 100, percent); 
       BR.spin(forward, 100, percent);
     //Spin Motors
   }
    else
    {
       FL.stop();
     BL.stop();
     FR.stop();
    BL.stop();
    }
   }

   //Extra precaution for running the forward code
   if(VisionSensor.largestObject.centerX > centerFOV + offsetX || VisionSensor.largestObject.centerX == centerFOV - offsetX )//If the object is to the left then turns right
     {
       run = 3;
     }  
}

int hw = 0;
int h = 0;
int w = 0;
void movefwd()
{
  h = VisionSensor.objects[0].height;
  w = VisionSensor.objects[0].width;
  hw = h*w; 
  if(hw < 53172)// It is less than bc height and width gets bigger as you get closer so once you are close enough you can stop
  {   
    FL.spin(forward, 100, percent);
       FR.spin(forward, 100, percent);
       BL.spin(forward, 100, percent); 
       BR.spin(forward, 100, percent);
  }
  else
  {
     FL.stop();
     BL.stop();
     FR.stop();
    BL.stop();
  }
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  
  while(true)
  {
   auto_turn();
   wait(350,mscs) 
   if(run == 3)
   {
    movefwd();
   }
   else
   {
     run == 0;//Not necessary bc run is being overwritten each time but just in case
   }
  }
}
1 Like

I would really suggest using a distance sensor mounted on the manipulator if you have one available, they are far more reliable than pixel percentage. If implemented properly you can even have the bot slow down once it gets to a certain distance from the object, helps ensure a soft touch so that it doesn’t bump out of your clamp’s range.

Best of luck! I personally love vision sensors, my bot uses two and once they’re properly set up and figured out they’re invaluable for autonomous.

1 Like

Is it possible to restrict the field of vision sensor?
Say I want the top part of it to look for red and the bottom part of it to look for blue?(Assuming it’s FOV is a 2x3 Grid)

Yes, you can have it see patterns
So your gonna want to configure you sensor first by connecting to the sensor to your computer via micro usb to usb, then you can click the freeze button and set the individual colors.
image

Then you have to set the patterns like this, you click on the codes tab and
put in the two colors you named previously.

Then the code works by analyzing the frames and seeing the pattern you described to the sensor, and all you have to do is set the angle requirement, which basically is you telling it the red needs to be on top and the blue needs to be on the bottom.

1 Like