Test Failed
Push — master ( a16f5a...8ddd40 )
by
unknown
04:31
created

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

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 3
c 6
b 0
f 0
dl 0
loc 51
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A startCameraStream() 0 3 1
B concatStreamStart() 0 35 2
startCameraStream 0 3 ?
concatStreamStart 0 35 ?
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
/**
16
 * Contains methods used for anything vision
17
 */
18
public class Vision extends IterativeRobot {
19
20
    /// Two cameras for double FOV
21
    private UsbCamera cameraLeft;
22
    private UsbCamera cameraRight;
23
24
    void startCameraStream(){
25
        //Places the vision in a separate thread from everything else as recommended by FIRST.
26
        new Thread(this::concatStreamStart).start();
27
    }
28
29
    /**
30
     * Start both the left and right camera streams and combine them into a single one which is then pushed
31
     * to an output stream titled Concat.
32
     * This method should only be used for starting the camera stream.
33
     */
34
    private void concatStreamStart() {
35
        cameraLeft = CameraServer.getInstance().startAutomaticCapture("Left", 0);
36
        cameraRight = CameraServer.getInstance().startAutomaticCapture("Right", 1);
37
38
        /// Dummy sinks to keep camera connections open.
39
        CvSink cvsinkLeft = new CvSink("leftSink");
40
        cvsinkLeft.setSource(cameraLeft);
41
        cvsinkLeft.setEnabled(true);
42
        CvSink cvsinkRight = new CvSink("rightSink");
43
        cvsinkRight.setSource(cameraRight);
44
        cvsinkRight.setEnabled(true);
45
46
        /// Matrices to store each image from the cameras.
47
        Mat leftSource = new Mat();
48
        Mat rightSource = new Mat();
49
50
        /// The ArrayList of left and right sources is needed for the hconcat method used to combine the streams
51
        ArrayList<Mat> sources = new ArrayList<>();
52
        sources.add(leftSource);
53
        sources.add(rightSource);
54
55
        /// Concatenation of both matrices
56
        Mat concat = new Mat();
57
58
        /// Puts the combined video on the SmartDashboard (I think)
59
        /// The width is multiplied by 2 as the dimensions of the stream will have a width two times that of a single webcam
60
        CvSource outputStream = CameraServer.getInstance().putVideo("Concat", 2*Constants.CAM_WIDTH, Constants.CAM_HEIGHT);
0 ignored issues
show
Comprehensibility introduced by
Consider assigning this magic number 2 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...
61
62
        while (!Thread.interrupted()) {
63
            /// Provide each mat with the current frame
64
            cvsinkLeft.grabFrame(leftSource);
65
            cvsinkRight.grabFrame(rightSource);
66
            /// Combine the frames into a single mat in the Output and stream the image.
67
            Core.hconcat(sources, concat);
68
            outputStream.putFrame(concat);
69
        }
70
    }
71
}
72