Vision Sensor VexCode Pro C++

I have added a vision sensor to my robot and configured two color signature for red and blue balls. The next goal is to use the color signature to automatically sort balls to go to the tower or out the back of the robot. I assume you would the code would look something like this:

if(Vision.Sig1){
put balls into tower
}else if(Vision.Sig2){
send balls out the back
}

I don’t know what the arguments in the if/else statements would be so if someone could provide an example, your help would be much appreciated.

So, the vision get signature function a boolean that says whether or not the signature specified in the function is found. So, assuming that the signature 1 is the color you want to spit out, you would:

if(Vision.takeSnapshot(blue) >= 1){
    send balls out the back
}
else{
    put balls in tower
}
1 Like

That’s completely wrong, getSignature does not tell you anything about objects. You need to use takeSnapshot. See the examples included with VEXcode, or perhaps search the forum, here is an old example (written for VCS but should still work).

7 Likes

Sorry, my mistake. I guess I read the api wrong. What would the get signature function do then since it returns a boolean?

It lets you read back a signature that was sent to the vision sensor. It has no practical use in VEXcode. You pass it an id and signature instance by reference.

bool getSignature( uint32_t id, signature &sig );

The return value just lets you know that the result is valid.

3 Likes

Ok thank you. I should probably edit the post above then.

So uint32_t is the value and signature is the name of the “box” we put the value in right?

In the driver control then I would use this code right? I would probably have to define a red and blue uint number to compare to in preaton right?
while(1) {
// request any objects with color code C1
int numberObjects = Vision1.takeSnapshot( C1 );
bool getSignature(uint32_t id, sig1 &sig)
if(id == red){
mainConveyor.spin(forward);
sorter.spin(reverse)
}else if(id == blue){
mainConveyor.spin(forward);
sorter.spin(reverse);
}
// run 10 times/second
this_thread::sleep_for(100);
}

As I said earlier, getSignature has no use in your program.

takeSnapshot is returning the number of objects found with the provided signature or color code.

int numberObjects = Vision1.takeSnapshot( C1 );

you most likely do not want to be using color codes, just signatures (a color code is 2 or more adjacent color objects)

3 Likes

Do something like this.

int main() {   
    int nObjects;

    while(1) {
      // Allow other tasks to run
      this_thread::sleep_for(10);

      nObjects = Vision1.takeSnapshot(SIG_BLUE);
      if( nObjects ) {
        Brain.Screen.printAt( 10, 10, "Blue found    ");
        // we found a blue object !
        continue;
      }

      nObjects = Vision1.takeSnapshot(SIG_RED);
      if( nObjects ) {
        Brain.Screen.printAt( 10, 10, "Red found    "); 
        // we found a red object
        continue;
      }

      Brain.Screen.printAt( 10, 10, "Nothing found");        
    }
}

that’s assuming you have calibrated the vision sensor and named the two signatures SIG_RED and SIG_BLUE like this.
image

6 Likes

Ok, I’ll try this tonight and ask any more questions if i need more help.