Skip to main content

Vision

Vision Sensor Presentation

Code Implementations

The functions needed to seek an object are presented below. Most of this code is set up for you to be able to copy and paste into your competition template. Just check to make sure naming is consistent etc.

We will be using this drive function to move the robot:

void drive(int lspeed, int rspeed, int wt) {
  LeftFront.spin(forward, lspeed, pct);
  LeftBack.spin(forward, lspeed, pct);
  RightFront.spin(forward, rspeed, pct);
  RightBack.spin(forward, rspeed, pct);
  wait(wt, msec);
}
Locate an Object

this function will return the value of x pixel on the screen. The screen is approximately 300 pixels wide.

int searchTriball() {
  int x = 0;
  Brain.Screen.printAt(1, 20, "triball  SEARCH");
  Vision.takeSnapshot(Vision__SIG_1);
  x = Vision.largestObject.centerX;
  return x;
}

In this function we have already calibrated the vision sensor with SIG_1 being a green game object.

Move robot to Center Object

We will use of standard control process to move the robot until the game object indicated by SIG_1 is centered on the screen.

void seekTriball(float target=150) {
  float x = 0;
  float error = x - target;
  float kp = 1;
  int speed = 0.0;
  float accuracy=10;
  while (fabs(error)>accuracy) {
    x = searchTriball();
    error = x - target;
    speed = kp * error;
    drive(speed, -speed, 10);
  }
  drive(0,0,0);
}

here we have set the target center to be 150 but you can choose any position on the screen when you call the function by including a value for target.

Autonomous Object Tracking

We can call the functions from the autonomous routine. Here we repeatedly call the function. This way we can slowly move a game object and the robot will track it.

void autonomous(void) {

 
while(true){
 
    Brain.Screen.printAt(80, 100, "Auton.   ");

    Brain.Screen.printAt(1, 60, "triball  x = %d  ", searchTriball());
    seekTriball();
    wait(100, msec);
}
}