-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NOTE: there is currently a race condition which may cause the shooter to be stuck on until one of its controls are pressed.
- Loading branch information
Showing
3 changed files
with
151 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,100 @@ | ||
package frc.system; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.opencv.core.Core; | ||
import org.opencv.core.Mat; | ||
import org.opencv.core.Rect; | ||
import org.opencv.core.Scalar; | ||
import org.opencv.imgproc.Imgproc; | ||
|
||
import edu.wpi.first.cameraserver.CameraServer; | ||
import edu.wpi.first.cscore.CvSink; | ||
import edu.wpi.first.cscore.CvSource; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
|
||
public class Vision extends Thread { | ||
final Target[] targets; | ||
final AtomicBoolean stop = new AtomicBoolean(false); | ||
|
||
final CvSink source = CameraServer.getVideo(); | ||
final CvSource stream = CameraServer.putVideo("Rectangle", 640, 480); | ||
|
||
public static class Target { | ||
// Top-left point | ||
public double x1; | ||
public double y1; | ||
// Bottom-right point | ||
public double x2; | ||
public double y2; | ||
// Detection threshold | ||
public int threshold; | ||
// The command to run during detection | ||
public Command cmd; | ||
|
||
public Target(double x1, double y1, double x2, double y2, int threshold, Command cmd) { | ||
this.x1 = x1; | ||
this.y1 = y1; | ||
this.x2 = x2; | ||
this.y2 = y2; | ||
this.threshold = threshold; | ||
this.cmd = cmd; | ||
} | ||
} | ||
|
||
public Vision(Target targets[]) { | ||
this.targets = targets; | ||
|
||
setDaemon(true); | ||
|
||
public class Vision { | ||
public Vision() { | ||
Thread visionThread = new Thread(() -> { | ||
// Get the UsbCamera from CameraServer | ||
CameraServer.startAutomaticCapture(); | ||
|
||
// Get a CvSink. This will capture Mats from the camera | ||
CvSink cvSink = CameraServer.getVideo(); | ||
|
||
// Setup a CvSource. This will send images back to the Dashboard | ||
CvSource outputStream = CameraServer.putVideo("Rectangle", 640, 480); | ||
|
||
// Mats are very memory expensive. Lets reuse this Mat. | ||
Mat mat1 = new Mat(); | ||
Mat mat2 = new Mat(); | ||
|
||
// This cannot be 'true'. The program will never exit if it is. This | ||
// lets the robot stop this thread when restarting robot code or | ||
// deploying. | ||
while (!Thread.interrupted()) { | ||
// Tell the CvSink to grab a frame from the camera and put it | ||
// in the source mat. If there is an error notify the output. | ||
if (cvSink.grabFrame(mat1) == 0) { | ||
// Send the output the error. | ||
outputStream.notifyError(cvSink.getError()); | ||
// skip the rest of the current iteration | ||
continue; | ||
} | ||
|
||
Core.inRange(mat1, new Scalar(245, 245, 245), new Scalar(255, 255, 255), mat2); | ||
|
||
// Give the output stream a new image to display | ||
outputStream.putFrame(mat2); | ||
CameraServer.startAutomaticCapture(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
// Mats are very memory expensive. Lets reuse this Mat. | ||
Mat mat = new Mat(); | ||
|
||
while (!Thread.interrupted() && !stop.compareAndSet(true, false)) { | ||
// Tell the CvSink to grab a frame from the camera and put it | ||
// in the source mat. If there is an error notify the output. | ||
if (source.grabFrame(mat) == 0) { | ||
// Send the output the error. | ||
stream.notifyError(source.getError()); | ||
// skip the rest of the current iteration | ||
continue; | ||
} | ||
|
||
int c = mat.cols(); | ||
int r = mat.rows(); | ||
|
||
for(Target t : targets) { | ||
Rect rect = new Rect( | ||
(int)(t.x1 * c), | ||
(int)(t.y1 * r), | ||
(int)(t.x2 * c), | ||
(int)(t.y2 * r) | ||
); | ||
|
||
// Slice the mat to the target rectangle | ||
Mat submat = mat.submat(rect); | ||
// Find the average pixel value | ||
double[] mean = Core.mean(submat).val; | ||
// Check if the threshold is met | ||
boolean detected = mean[0] > t.threshold && mean[1] > t.threshold && mean[2] > t.threshold; | ||
|
||
if (detected) t.cmd.schedule(); | ||
else t.cmd.cancel(); | ||
|
||
Imgproc.rectangle(mat, rect, new Scalar(0, detected ? 255 : 0, detected ? 0 : 255), 5); | ||
} | ||
}); | ||
|
||
// Give the output stream a new image to display | ||
stream.putFrame(mat); | ||
} | ||
} | ||
|
||
visionThread.setDaemon(true); | ||
visionThread.start(); | ||
public void requestStop() { | ||
stop.set(true); | ||
} | ||
} |