This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jevois.java
267 lines (230 loc) · 7.1 KB
/
Jevois.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package com.team687.subsystems;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.team687.Robot;
import com.team687.constants.VisionConstants;
import edu.wpi.cscore.UsbCamera;
import edu.wpi.cscore.VideoMode.PixelFormat;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.hal.util.UncleanStatusException;
import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.SerialPort;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
public class Jevois extends Subsystem implements Runnable {
private SerialPort m_cam;
private UsbCamera m_visionCam;
private Thread m_stream;
private boolean m_send;
public double m_offset;
public double m_distinguish = 0.0;
private double m_threshold = 25.4;
private boolean writeException = false;
String[] parts;
private String sendValue;
private String m_filePath1 = "/media/sda1/logs/";
private String m_filePath2 = "/home/lvuser/logs/";
private File m_file;
public FileWriter m_writer;
private double m_logStartTime = 0;
// Jevois Serial Output Data
private double m_contourNum, m_area, m_centerX, m_centerY, m_distance;
public Jevois(int baud, SerialPort.Port port) {
m_send = false;
sendValue = "None";
try {
m_cam = new SerialPort(baud, port);
m_stream = new Thread(this);
m_stream.start();
} catch (UncleanStatusException e) {
e.printStackTrace();
}
}
public void startCameraStream() {
try {
m_visionCam = CameraServer.getInstance().startAutomaticCapture();
m_visionCam.setVideoMode(PixelFormat.kMJPEG, 320, 240, 30);
} catch (Exception e) {
}
}
public void run() {
while (m_stream.isAlive()) {
Timer.delay(0.001);
if (m_send) {
m_cam.writeString(sendValue);
m_send = false;
}
if (m_cam.getBytesReceived() > 0) {
String read = m_cam.readString();
if (read.charAt(0) == '/') {
parts = dataParse(read);
m_contourNum = Integer.parseInt(getData(1));
m_area = Double.parseDouble(getData(2));
m_centerX = Double.parseDouble(getData(3));
m_centerY = Double.parseDouble(getData(4));
m_distance = Double.parseDouble(getData(5));
} else {
System.out.println(read);
}
}
}
}
// use if jevois is centered on robot
public double getSimpleAngleToTurn() {
return xPixelToDegree(getTargetX());
}
// use if jevois is horizontally offset from center
public double getAngleToTurn() {
double radians = (Math.PI / 180) * (xPixelToDegree(getTargetX())
+ VisionConstants.kCameraHorizontalMountAngle);
double horizontalAngle = Math.PI / 2 - radians;
double distance = getDistance();
double f = Math.sqrt(distance * distance + Math.pow(VisionConstants.kCameraHorizontalOffset, 2)
- 2 * distance * VisionConstants.kCameraHorizontalOffset * Math.cos(horizontalAngle));
double c = Math.asin(VisionConstants.kCameraHorizontalOffset * Math.sin(horizontalAngle) / f);
double b = Math.PI - horizontalAngle - c;
double calculatedAngle = (180 / Math.PI) * (Math.PI / 2 - b);
if (getTargetX() == 0) {
return 0;
} else {
return calculatedAngle;
}
}
private double xPixelToDegree(double pixel) {
double radian = Math.signum(pixel) * Math.atan(Math.abs(pixel / VisionConstants.kXFocalLength));
double degree = 180 / Math.PI * radian;
return degree;
}
public double getDistance() {
return m_distance;
}
public double getDistanceFeet() {
return m_distance / 12;
}
// use if overshooting the turn
public double getOffset() {
double angularError = getSimpleAngleToTurn();
if (-m_threshold < angularError && angularError < m_threshold) {
m_offset = 0.0;
} else if (m_threshold > 0) {
m_offset = angularError - m_threshold;
} else if (m_threshold < 0) {
m_offset = angularError + m_threshold;
}
return m_offset;
}
public double getContourNum() {
return m_contourNum;
}
public double getTargetX() {
return m_centerX;
}
public double getTargetY() {
return m_centerY;
}
public double getTargetArea() {
return m_area;
}
public void end() {
m_stream.interrupt();
}
private void sendCommand(String value) {
sendValue = value + "\n";
m_send = true;
Timer.delay(0.1);
}
private String[] dataParse(String input) {
return input.split("/");
}
private String getData(int data) {
return parts[data];
}
public void ping() {
sendCommand("ping");
}
public void enableStream() {
sendCommand("streamon");
}
public void disableStream() {
sendCommand("streamoff");
}
public void reportToSmartDashboard() {
SmartDashboard.putNumber("Total contours", getContourNum());
SmartDashboard.putNumber("Area", getTargetArea());
SmartDashboard.putNumber("Y coord", getTargetY());
SmartDashboard.putNumber("X coord", getTargetX());
SmartDashboard.putNumber("Angle to Turn", getAngleToTurn());
SmartDashboard.putNumber("Distance", getDistance());
SmartDashboard.putBoolean("Contour detected", getContourNum() > 0);
SmartDashboard.putBoolean("Is Locked on", Math.abs(Robot.jevois.getAngleToTurn()) <= VisionConstants.kDriveRotationDeadband);
// SmartDashboard.putNumber("Exposure", getExp());
}
public void startLog() {
// Check to see if flash drive is mounted.
File logFolder1 = new File(m_filePath1);
File logFolder2 = new File(m_filePath2);
Path filePrefix = Paths.get("");
if (logFolder1.exists() && logFolder1.isDirectory()) {
filePrefix = Paths.get(logFolder1.toString(),
Robot.kDate + Robot.ds.getMatchType().toString() + Robot.ds.getMatchNumber() + "Jevois");
} else if (logFolder2.exists() && logFolder2.isDirectory()) {
filePrefix = Paths.get(logFolder2.toString(),
Robot.kDate + Robot.ds.getMatchType().toString() + Robot.ds.getMatchNumber() + "Jevois");
} else {
writeException = true;
}
if (!writeException) {
int counter = 0;
while (counter <= 99) {
m_file = new File(filePrefix.toString() + String.format("%02d", counter) + ".csv");
if (m_file.exists()) {
counter++;
} else {
break;
}
if (counter == 99) {
System.out.println("file creation counter at 99!");
}
}
try {
m_writer = new FileWriter(m_file);
m_writer.append("Time,Contours,Area,TargetX,TargetY,Distance,Angle\n");
m_logStartTime = Timer.getFPGATimestamp();
} catch (IOException e) {
e.printStackTrace();
writeException = true;
}
}
}
public void stopLog() {
try {
if (null != m_writer)
m_writer.close();
} catch (IOException e) {
e.printStackTrace();
writeException = true;
}
}
public void logToCSV() {
if (!writeException) {
try {
double timestamp = Timer.getFPGATimestamp() - m_logStartTime;
m_writer.append(String.valueOf(timestamp) + ","
+ String.valueOf(getContourNum()) + "," + String.valueOf(getTargetArea()) + ","
+ String.valueOf(getTargetX()) + "," + String.valueOf(getTargetY()) +","
+ String.valueOf(getDistance()) + "," + String.valueOf(getAngleToTurn()) + "\n");
m_writer.flush();
} catch (IOException e) {
e.printStackTrace();
writeException = true;
}
}
}
@Override
protected void initDefaultCommand() {
}
}