Camera Vision Processing

Hi,

in my code i have


public void operatorControl(){
  while(isenabled() && isOperatorControl()){

         ColorImage img = Camera.getImage();
          .....and then i do a bunch of processing after i get the image.

  }
}

How do I get the camera to only take a picture 5 times per second? Doesn’t the loop repeat like once every 10ms? So wouldn’t that mean that the code I have now would take a picture once every 10ms?

You can’t rely on the loop being executed every 10ms, it’s bound to change as you put in more code (especially image processing). If you wanted to do something every 200ms, you might do something like this:


import edu.wpi.first.wpilibj.Timer;

public void operatorControl() {
  double time = Timer.getFPGATimestamp();
  
  while(isEnabled() && isOperatorControl()) {
    if (Timer.getFPGATimestamp() - time >= 0.2) {
      // CAMERA IMAGE PROCESSING HERE
      time = Timer.getFPGATimestamp();
    }
    // OTHER CODE HERE
  }
}

You might try running the image capture in a separate thread. I’ve had sketchy luck with threads and FRC Java, but your mileage may vary.

Like what jesusrambo said, running in a seperate thread might actually be a good idea… (Thanks rambo! ill try it). I don’t think that overhead would be a problem as long you aren’t using a seperate thread for each motor or something unsynchronized as that… If you need help on image tracking ill be more than happy to help. I haven’t used the javacv that everyone seems to be raving about but with the image processing functions tat FRC gave us i was able to successfully track imagery.

Im not sure what you guys mean by running it in a seperate thread

Having a separate thread makes it so that your code won’t be stuck on some piece of code until it finishes running. It makes it so that it can run multiple pieces of code (technically no on the low level, but for all practical purposes yes).

If you don’t stick a piece of particularly long running loop in a thread (say, one that deliberately waits for 5 seconds before it finishes executing), the rest of your program will get stuck on it, and your other code, like your drive/shoot code will stop executing until the 5 seconds is done.

Read this piece of documentation on how you can implement this in Java: http://docs.oracle.com/javase/tutorial/essential/concurrency/