Test Failed
Pull Request — master (#35)
by
unknown
01:35
created

org.usfirst.frc.team3695.robot.Vision   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
c 2
b 0
f 0
dl 0
loc 48
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B robotInit() 0 41 2
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);
0 ignored issues
show
Comprehensibility introduced by
Consider assigning this magic number 1080 to a constant.

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.

Loading history...
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