-
Notifications
You must be signed in to change notification settings - Fork 35
/
LidarProcessor.java
executable file
·249 lines (215 loc) · 8.21 KB
/
LidarProcessor.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
package com.team254.frc2018.lidar;
import com.team254.frc2018.Constants;
import com.team254.frc2018.RobotState;
import com.team254.frc2018.lidar.icp.ICP;
import com.team254.frc2018.lidar.icp.Point;
import com.team254.frc2018.lidar.icp.ReferenceModel;
import com.team254.frc2018.lidar.icp.Transform;
import com.team254.frc2018.loops.Loop;
import com.team254.lib.geometry.Pose2d;
import com.team254.lib.geometry.Translation2d;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.zip.GZIPOutputStream;
/**
* Receives LIDAR points from the {@link LidarServer}, stores a set number
* of scans/revolutions, and provides methods for processing the data.
* <p>
* All interfacing with the LIDAR should be done through this class.
*
* @see Constants.kChezyLidarNumScansToStore
* @see doICP()
* @see getTowerPosition()
*/
public class LidarProcessor implements Loop {
private static LidarProcessor mInstance = null;
public static LidarProcessor getInstance() {
if (mInstance == null) {
mInstance = new LidarProcessor();
}
return mInstance;
}
private RobotState mRobotState = RobotState.getInstance();
private LinkedList<LidarScan> mScans = new LinkedList<>();
private double prev_timestamp;
private ICP icp = new ICP(ReferenceModel.TOWER, 100);
private DataOutputStream dataLogFile;
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private static FileOutputStream newLogFile() throws IOException {
// delete old files if we're over the limit
File logDir = new File(Constants.kLidarLogDir);
File[] logFiles = logDir.listFiles();
if (logFiles == null) throw new IOException("List files in " + Constants.kLidarLogDir);
Arrays.sort(logFiles, (f1, f2) -> {
return Long.compare(f1.lastModified(), f2.lastModified());
});
for (int i = 0; i < logFiles.length - Constants.kNumLidarLogsToKeep + 1; i++) {
logFiles[i].delete();
}
// create the new file and return
String dateStr = new SimpleDateFormat("MM-dd-HH_mm_ss").format(new Date());
File newFile = new File(logDir, "lidarLog-" + dateStr + ".dat");
newFile.createNewFile();
return new FileOutputStream(newFile, false);
}
private LidarProcessor() {
mScans.add(new LidarScan());
try {
dataLogFile = new DataOutputStream(new GZIPOutputStream(newLogFile()));
} catch (IOException e) {
System.err.println("Failed to open lidar log file:");
e.printStackTrace();
}
}
private void logPoint(double angle, double dist, double x, double y) {
try {
dataLogFile.writeInt((int) (angle * 100));
dataLogFile.writeInt((int) (dist * 256));
dataLogFile.writeInt((int) (x * 256));
dataLogFile.writeInt((int) (y * 256));
} catch (IOException e) {
e.printStackTrace();
}
}
public void addPoint(LidarPoint point, boolean newScan) {
SmartDashboard.putNumber("LIDAR last_angle", point.angle);
Translation2d cartesian = point.toCartesian();
logPoint(point.angle, point.distance, cartesian.x(), cartesian.y());
lock.writeLock().lock();
try {
if (newScan) { // crosses the 360-0 threshold. start a new scan
prev_timestamp = Timer.getFPGATimestamp();
// long start = System.nanoTime();
// Translation2d towerPos = getTowerPosition();
// long end = System.nanoTime();
// SmartDashboard.putNumber("towerPos_ms", (end-start)/1000000);
// SmartDashboard.putNumber("towerPosX", towerPos.x());
// SmartDashboard.putNumber("towerPosY", towerPos.y());
mScans.add(new LidarScan());
if (mScans.size() > Constants.kChezyLidarNumScansToStore) {
mScans.removeFirst();
}
}
if (!excludePoint(cartesian.x(), cartesian.y())) {
getCurrentScan().addPoint(new Point(cartesian), point.timestamp);
}
} finally {
lock.writeLock().unlock();
}
}
private static final double FIELD_WIDTH = 27 * 12, FIELD_HEIGHT = 54 * 12;
private static final double RECT_RX = FIELD_WIDTH / 5, RECT_RY = FIELD_HEIGHT / 2;
private static final double FIELD_CX = FIELD_WIDTH / 2, FIELD_CY = FIELD_HEIGHT / 2;
private static final double RECT_X_MIN = FIELD_CX - RECT_RX, RECT_X_MAX = FIELD_CX + RECT_RX,
RECT_Y_MIN = FIELD_CY - RECT_RY, RECT_Y_MAX = FIELD_CY + RECT_RY;
private static boolean excludePoint(double x, double y) {
return x < RECT_X_MIN || x > RECT_X_MAX ||
y < RECT_Y_MIN || y > RECT_Y_MAX;
}
private LidarScan getCurrentScan() {
return mScans.getLast();
}
private ArrayList<Point> getAllPoints() {
ArrayList<Point> list = new ArrayList<>();
for (LidarScan scan : mScans) {
list.addAll(scan.getPoints());
}
return list;
}
private Point getAveragePoint() {
double sumX = 0, sumY = 0;
int n = 0;
for (Point p : getAllPoints()) {
sumX += p.x;
sumY += p.y;
n++;
}
return new Point(sumX / n, sumY / n);
}
private static final double BUCKET_SIZE = 3.0; // inches
/**
* Cantor pairing function (to bucket & hash two doubles)
*/
private int getBucket(double x, double y) {
int ix = (int) (x / BUCKET_SIZE);
int iy = (int) (y / BUCKET_SIZE);
int a = ix >= 0 ? 2 * ix : -2 * ix - 1;
int b = iy >= 0 ? 2 * iy : -2 * iy - 1;
int sum = a + b;
return sum * (sum + 1) / 2 + a;
}
/**
* Returns a list of points that have been thinned roughly uniformly.
*/
private ArrayList<Point> getCulledPoints() {
ArrayList<Point> list = new ArrayList<>();
HashSet<Integer> buckets = new HashSet<>();
for (Point p : getAllPoints()) {
if (buckets.add(getBucket(p.x, p.y)))
list.add(p);
}
return list;
}
public Pose2d doICP() {
lock.readLock().lock();
try {
Pose2d guess = mRobotState.getFieldToLidar(getCurrentScan().getTimestamp());
return icp.doICP(getCulledPoints(), new Transform(guess).inverse()).inverse().toPose2d();
} finally {
lock.readLock().unlock();
}
}
public Translation2d getTowerPosition() {
lock.readLock().lock();
try {
Point avg = getAveragePoint();
Transform trans = icp.doICP(getCulledPoints(), new Transform(0, avg.x, avg.y));
return trans.apply(icp.reference).getMidpoint().toTranslation2d();
} finally {
lock.readLock().unlock();
}
}
public void setPrevTimestamp(double time) {
lock.writeLock().lock();
try {
prev_timestamp = time;
} finally {
lock.writeLock().unlock();
}
}
public double getPrevTimestamp() {
lock.readLock().lock();
try {
return prev_timestamp;
} finally {
lock.readLock().unlock();
}
}
@Override
public void onStart(double timestamp) {
setPrevTimestamp(Double.NEGATIVE_INFINITY);
}
@Override
public void onLoop(double timestamp) {
LidarServer lidarServer = LidarServer.getInstance();
if (timestamp - getPrevTimestamp() > Constants.kChezyLidarRestartTime) {
if (lidarServer.isRunning()) {
System.err.println("Lidar timed out. Restarting");
lidarServer.stop();
} else if (!lidarServer.isEnding() && lidarServer.start()) {
setPrevTimestamp(timestamp);
}
}
}
@Override
public void onStop(double timestamp) {
}
}