-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChiliVisionFull.java
158 lines (122 loc) · 3.52 KB
/
ChiliVisionFull.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.team2576.lib;
import java.util.TimerTask;
import com.ni.vision.NIVision.Image;
import com.team2576.lib.util.ChiliConstants;
import com.team2576.robot.io.SensorInput;
import com.team2576.robot.io.SensorInput.Cameras;
import edu.wpi.first.wpilibj.CameraServer;
@SuppressWarnings("unused")
public class ChiliVisionFull {
private class VisionTask extends TimerTask {
private VisionCamera visor;
public VisionTask (VisionCamera visor) {
this.visor = visor;
}
@Override
public void run() {
this.visor.view();
}
}
private class VisionCamera {
private java.util.Timer visionLoop;
private USBCamera camera;
//private SensorInput sensor;
//private Cameras cam;
private boolean is_ready;
private Image frame;
private int delay, period;
private boolean abort_vision;
/*
private VisionCamera (Cameras descriptor, int delay, int period) {
this.sensor = SensorInput.getInstance();
this.cam = descriptor;
this.delay = delay;
this.period = period;
this.visionLoop = new java.util.Timer();
this.visionLoop.schedule(new VisionTask(this), (long) (this.delay), (long) (this.period));
this.is_ready = false;
}
*/
private VisionCamera (USBCamera cam, int delay, int period) {
this.camera = cam;
this.delay = delay;
this.period = period;
this.visionLoop = new java.util.Timer();
this.visionLoop.schedule(new VisionTask(this), (long) (this.delay), (long) (this.period));
this.is_ready = false;
this.abort_vision = false;
}
private Image getImage() {
Image image = NIVision.imaqCreateImage(NIVision.ImageType.IMAGE_RGB, 0);
try {
this.camera.getImage(image);
} catch (VisionException ve) {
ve.printStackTrace();
this.abort_vision = true;
}
return image;
}
private void view() {
if (this.abort_vision) {
this.visionLoop.cancel();
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
this.visionLoop.schedule(new VisionTask(this), (long) (this.delay), (long) (this.period));
this.abort_vision = false;
}
this.frame = this.getImage();
this.is_ready = true;
}
public synchronized boolean isReady() {
return this.is_ready;
}
public synchronized Image get() {
this.is_ready = false;
return this.frame;
}
}
private class ChiliVisionTask extends TimerTask {
private ChiliVisionFull vision;
public ChiliVisionTask (ChiliVisionFull vision) {
this.vision = vision;
}
@Override
public void run() {
this.vision.serve();
}
}
private VisionCamera camThread;
private java.util.Timer visiontask;
private static ChiliVisionFull instance;
private USBCamera cam;
public static ChiliVisionFull getInstance() {
if (instance == null) {
instance = new ChiliVisionFull();
}
return instance;
}
private ChiliVisionFull() {
//puede que sea "cam1"
this.cam = USBCamera("cam0");
this.camThread = new VisionCamera(Cameras.LEFT_CAM, 0, 66);
this.visiontask = new java.util.Timer();
this.visiontask.schedule(new ChiliVisionTask(this), 0L, (long) 10);
}
public synchronized void serve() {
while (true) {
Image img;
img = camThread.isReady() ? camThread.get() : null;
try {
if (img != null) {
CameraServer.getInstance().setImage(img);
Thread.sleep(5);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}