1
|
|
|
package org.usfirst.frc.team3695.robot; |
2
|
|
|
|
3
|
|
|
import edu.wpi.cscore.CvSink; |
4
|
|
|
import edu.wpi.cscore.CvSource; |
5
|
|
|
import edu.wpi.cscore.UsbCamera; |
6
|
|
|
|
7
|
|
|
import edu.wpi.first.wpilibj.CameraServer; |
8
|
|
|
import edu.wpi.first.wpilibj.IterativeRobot; |
9
|
|
|
|
10
|
|
|
import org.opencv.core.Core; |
11
|
|
|
import org.opencv.core.Mat; |
12
|
|
|
|
13
|
|
|
import java.util.ArrayList; |
14
|
|
|
|
15
|
|
|
//As I don't know where I should be putting this, |
16
|
|
|
//I made a different class to store this. |
17
|
|
|
//Move it wherever it should be. |
18
|
|
|
public class Vision extends IterativeRobot { |
19
|
|
|
|
20
|
|
|
//Two cameras for double FOV |
21
|
|
|
private UsbCamera cameraLeft; |
22
|
|
|
private UsbCamera cameraRight; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
public void robotInit() { |
26
|
|
|
//Places the vision in a separate thread from everything else as recommended by FIRST |
27
|
|
|
//It should never be accessed by other code, so protection isn't necessary. |
28
|
|
|
new Thread(() -> { |
29
|
|
|
//Initialize cameras as the left and right respectively. |
30
|
|
|
cameraLeft = CameraServer.getInstance().startAutomaticCapture("Left", 0); |
31
|
|
|
cameraRight = CameraServer.getInstance().startAutomaticCapture("Right", 1); |
32
|
|
|
|
33
|
|
|
//Dummy sinks to keep camera connections open. |
34
|
|
|
CvSink cvsinkLeft = new CvSink("leftSink"); |
35
|
|
|
cvsinkLeft.setSource(cameraLeft); |
36
|
|
|
cvsinkLeft.setEnabled(true); |
37
|
|
|
CvSink cvsinkRight = new CvSink("rightSink"); |
38
|
|
|
cvsinkRight.setSource(cameraRight); |
39
|
|
|
cvsinkRight.setEnabled(true); |
40
|
|
|
|
41
|
|
|
//Matrices to store each image from the cameras. |
42
|
|
|
//Labeled left and right respectively. |
43
|
|
|
Mat leftSource = new Mat(); |
44
|
|
|
Mat rightSource = new Mat(); |
45
|
|
|
|
46
|
|
|
//The arraylist of left and right sources is needed for concatenating |
47
|
|
|
ArrayList<Mat> sources = new ArrayList<>(); |
48
|
|
|
sources.add(leftSource); |
49
|
|
|
sources.add(rightSource); |
50
|
|
|
|
51
|
|
|
//Concatenation of both matrices |
52
|
|
|
Mat concat = new Mat(); |
53
|
|
|
|
54
|
|
|
//Puts the combined video on the SmartDashboard (I think) |
55
|
|
|
CvSource outputStream = CameraServer.getInstance().putVideo("Concat", 3840, 1080); |
|
|
|
|
56
|
|
|
|
57
|
|
|
while (!Thread.interrupted()) { |
58
|
|
|
//Provide each mat with the current frame |
59
|
|
|
cvsinkLeft.grabFrame(leftSource); |
60
|
|
|
cvsinkRight.grabFrame(rightSource); |
61
|
|
|
//Combine the frames into a single mat in the Output |
62
|
|
|
Core.hconcat(sources, concat); |
63
|
|
|
outputStream.putFrame(concat); |
64
|
|
|
} |
65
|
|
|
}).start(); |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
Using constants for hard-coded numbers is a best practice. A constant’s name can explain the rationale behind this magic number. It is also easier to find if you ever need to change it.