Vision sensor block finding program

So my team has been pushing for a vision sensor program. I coded a program that I thought would work but it had several errors. For example, when I start the program, the robots spins around and seems like it goes toward the block but then turns and goes toward a wall.
Here is my code:

void Visionalign(int block){
  int offset = 100;
  int count = 0;
  while(1){
  VisionSensor.takeSnapshot(block);
  
  if(VisionSensor.largestObject.exists and VisionSensor.largestObject.width<50 and  VisionSensor.largestObject.width>5 ){
    int offset = VisionSensor.largestObject.centerX - (centerFOV+offsetX);
    LeftMotorFront.spin(directionType::fwd, 20-offset/2, velocityUnits::pct);
    LeftMotorBack.spin(directionType::fwd, 20-offset/2, velocityUnits::pct );
    RightMotorFront.spin(directionType::fwd, 20+offset/2, velocityUnits::pct);
    RightMotorBack.spin(directionType::fwd, 20+offset/2, velocityUnits::pct);
    if(offset == 0){
      count++;
    }
  }
  else if(VisionSensor.largestObject.exists == false ){
    turn(true, 10);
  }

  }

}

I made a program that just ran this function so there are no other functions interfering with this code. Are there any errors in code logic that are throwing this off?

One of the errors I see is when you use “and” instead of &&.

&& means AND
|| means OR

Also I’m not sure if you defined centerFOV based on your code. If you defined it somewhere else you should be fine.

2 Likes