My team is using a JeVois camera for the first time this year, and we’re trying to put the code that reads from the camera’s serial stream into a while loop that is running in a separate thread. The condition of the while loop is a boolean that can be a changed by a command so that we can control when we are reading from the camera, which is sending contour data. For some reason, when we change the value of the boolean (named runVisionThread), the while loop doesn’t start. Anyone have any ideas? Here’s our code:
// This is in robotInit
Thread visionThread = new Thread(() -> {
synchronized (imgLock) {
while (runVisionThread) {
String cameraOutput = jevois.readString();
System.out.println(cameraOutput);
// Other parsing code
}
}
});
visionThread.start();
// Elsewhere in Robot.java
public static void enableVision() {
synchronized (imgLock) {
runVisionThread = true;
}
}
public static void disableVision() {
synchronized (imgLock) {
runVisionThread = false;
}
}
The moment runVisionThread is equal to false, the while loop is over and the code in your Thread will finish executing and then be done. To have your vision code run only when runVisionThread is true AND not have the thread terminate when runVisionThread is false, you can put it all in a bigger while loop that keeps the Thread looping.
Also, I think with your current sychronization setup, you wouldn’t be able to call disableVision() once you enabled it because the while loop for vision is inside a synchronized block, so it would never let go of the imgLock and thus prevent disableVision() from ever getting access to imgLock. You would want to put the synchronized block inside the while loop so that in-between each loop there would be a chance for something else to use imgLock.
To put all of that into an example, I believe the tweaks you need to your Thread’s code would look like this:
while (true) {
while (runVisionThread) {
synchronized (imgLock) {
String cameraOutput = jevois.readString();
System.out.println(cameraOutput);
// Other parsing code
}
}
}