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.DriverStation;
|
9
|
|
|
import edu.wpi.first.wpilibj.IterativeRobot;
|
10
|
|
|
|
11
|
|
|
import org.opencv.core.Core;
|
12
|
|
|
import org.opencv.core.Mat;
|
13
|
|
|
|
14
|
|
|
import java.util.ArrayList;
|
15
|
|
|
import java.util.Collection;
|
16
|
|
|
import java.util.stream.Collectors;
|
17
|
|
|
|
18
|
|
|
/**
|
19
|
|
|
* Contains methods used for anything vision
|
20
|
|
|
*/
|
21
|
|
|
public class Vision extends IterativeRobot {
|
22
|
|
|
|
23
|
|
|
/// Two cameras for double FOV
|
24
|
|
|
private UsbCamera cameraLeft;
|
25
|
|
|
private UsbCamera cameraRight;
|
26
|
|
|
|
27
|
|
|
private UsbCamera cameraScrew;
|
28
|
|
|
|
29
|
|
|
void startConcatCameraThread(){
|
30
|
|
|
//Places the vision in a separate thread from everything else as recommended by FIRST.
|
31
|
|
|
new Thread(this::concatCameraStream).start();
|
32
|
|
|
}
|
33
|
|
|
|
34
|
|
|
void startScrewCameraThread(){
|
35
|
|
|
new Thread(this::screwCameraStream).start();
|
36
|
|
|
}
|
37
|
|
|
|
38
|
|
|
private void screwCameraStream(){
|
39
|
|
|
cameraScrew = CameraServer.getInstance().startAutomaticCapture("Screw", 0);
|
40
|
|
|
|
41
|
|
|
CvSink cvsinkScrew = new CvSink("screwSink");
|
42
|
|
|
cvsinkScrew.setSource(cameraScrew);
|
43
|
|
|
cvsinkScrew.setEnabled(true);
|
44
|
|
|
|
45
|
|
|
Mat streamImages = new Mat();
|
46
|
|
|
|
47
|
|
|
CvSource outputScrew = CameraServer.getInstance().putVideo("Screw", Constants.CAM_WIDTH, Constants.CAM_HEIGHT);
|
48
|
|
|
while (!Thread.interrupted()){
|
49
|
|
|
cvsinkScrew.grabFrame(streamImages);
|
50
|
|
|
outputScrew.putFrame(streamImages);
|
51
|
|
|
}
|
52
|
|
|
}
|
53
|
|
|
|
54
|
|
|
/**
|
55
|
|
|
* Start both the left and right camera streams and combine them into a single one which is then pushed
|
56
|
|
|
* to an output stream titled Concat.
|
57
|
|
|
* This method should only be used for starting the camera stream.
|
58
|
|
|
*/
|
59
|
|
|
private void concatCameraStream() {
|
60
|
|
|
cameraLeft = CameraServer.getInstance().startAutomaticCapture("Left", 0);
|
61
|
|
|
cameraRight = CameraServer.getInstance().startAutomaticCapture("Right", 1);
|
62
|
|
|
|
63
|
|
|
/// Dummy sinks to keep camera connections open.
|
64
|
|
|
CvSink cvsinkLeft = new CvSink("leftSink");
|
65
|
|
|
cvsinkLeft.setSource(cameraLeft);
|
66
|
|
|
cvsinkLeft.setEnabled(true);
|
67
|
|
|
CvSink cvsinkRight = new CvSink("rightSink");
|
68
|
|
|
cvsinkRight.setSource(cameraRight);
|
69
|
|
|
cvsinkRight.setEnabled(true);
|
70
|
|
|
|
71
|
|
|
/// Matrices to store each image from the cameras.
|
72
|
|
|
Mat leftSource = new Mat();
|
73
|
|
|
Mat rightSource = new Mat();
|
74
|
|
|
|
75
|
|
|
/// The ArrayList of left and right sources is needed for the hconcat method used to combine the streams
|
76
|
|
|
ArrayList<Mat> sources = new ArrayList<>();
|
77
|
|
|
sources.add(leftSource);
|
78
|
|
|
sources.add(rightSource);
|
79
|
|
|
|
80
|
|
|
/// Concatenation of both matrices
|
81
|
|
|
Mat concat = new Mat();
|
82
|
|
|
|
83
|
|
|
/// Puts the combined video on the SmartDashboard (I think)
|
84
|
|
|
/// The width is multiplied by 2 as the dimensions of the stream will have a width two times that of a single webcam
|
85
|
|
|
CvSource outputStream = CameraServer.getInstance().putVideo("Concat", 2*Constants.CAM_WIDTH, Constants.CAM_HEIGHT);
|
|
|
|
|
86
|
|
|
|
87
|
|
|
while (!Thread.interrupted()) {
|
88
|
|
|
/// Provide each mat with the current frame
|
89
|
|
|
cvsinkLeft.grabFrame(leftSource);
|
90
|
|
|
cvsinkRight.grabFrame(rightSource);
|
91
|
|
|
/// Combine the frames into a single mat in the Output and stream the image.
|
92
|
|
|
Core.hconcat(sources, concat);
|
93
|
|
|
outputStream.putFrame(concat);
|
94
|
|
|
}
|
95
|
|
|
}
|
96
|
|
|
}
|
97
|
|
|
|
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.