3 Commits

8 changed files with 315 additions and 138 deletions

185
map.txt
View File

@@ -1,79 +1,112 @@
numVerts,43 numVerts,60
numEdges,34 numEdges,50
vert,601.0,491.0
vert,602.0,652.0
vert,300.0,277.0
vert,213.0,812.0
vert,715.0,655.0
vert,294.0,378.0
vert,259.0,476.0
vert,224.0,247.0
vert,304.0,479.0
vert,485.0,693.0
vert,713.0,810.0
vert,367.0,347.0
vert,223.0,273.0
vert,404.0,666.0
vert,329.0,345.0
vert,482.0,615.0
vert,606.0,811.0
vert,483.0,810.0
vert,267.0,616.0
vert,603.0,537.0
vert,330.0,376.0
vert,267.0,723.0
vert,714.0,488.0
vert,401.0,616.0
vert,294.0,347.0
vert,206.0,522.0 vert,206.0,522.0
vert,711.0,234.0 vert,554.0,422.0
vert,493.0,238.0 vert,525.0,281.0
vert,302.0,249.0 vert,404.0,666.0
vert,403.0,483.0 vert,482.0,615.0
vert,334.0,727.0 vert,545.0,326.0
vert,168.0,471.0 vert,520.0,338.0
vert,487.0,485.0 vert,713.0,810.0
vert,327.0,615.0 vert,401.0,616.0
vert,405.0,237.0 vert,520.0,399.0
vert,211.0,346.0
vert,210.0,380.0
vert,165.0,235.0
vert,411.0,811.0
vert,165.0,809.0
vert,713.0,539.0
vert,367.0,375.0 vert,367.0,375.0
vert,599.0,418.0
vert,591.0,332.0
vert,483.0,810.0
vert,602.0,652.0
vert,711.0,234.0
vert,366.0,275.0
vert,611.0,359.0
vert,570.0,324.0
vert,302.0,249.0
vert,259.0,476.0
vert,411.0,811.0
vert,503.0,375.0
vert,168.0,471.0
vert,603.0,346.0
vert,601.0,491.0
vert,165.0,235.0
vert,405.0,237.0
vert,606.0,811.0
vert,165.0,809.0
vert,213.0,812.0
vert,165.0,521.0 vert,165.0,521.0
edge,start,1,end,4 vert,223.0,273.0
edge,start,1,end,16 vert,294.0,378.0
edge,start,3,end,25 vert,267.0,616.0
edge,start,9,end,17 vert,493.0,238.0
edge,start,10,end,26 vert,403.0,483.0
edge,start,11,end,14 vert,210.0,380.0
edge,start,11,end,41 vert,330.0,376.0
edge,end,7,start,12 vert,714.0,488.0
edge,end,2,start,12 vert,603.0,537.0
edge,start,13,end,38 vert,715.0,655.0
edge,end,0,start,19 vert,224.0,247.0
edge,start,19,end,40 vert,267.0,723.0
edge,end,14,start,20 vert,503.0,357.0
edge,start,20,end,41 vert,618.0,397.0
edge,end,18,start,21 vert,713.0,539.0
edge,start,21,end,30 vert,334.0,727.0
edge,start,24,end,35 vert,485.0,693.0
edge,end,5,start,24 vert,211.0,346.0
edge,start,26,end,27 vert,300.0,277.0
edge,end,7,start,28 vert,294.0,347.0
edge,end,2,start,28 vert,577.0,423.0
edge,end,23,start,29 vert,531.0,414.0
edge,end,8,start,29 vert,329.0,345.0
edge,end,6,start,31 vert,327.0,615.0
edge,end,15,start,32 vert,367.0,347.0
edge,end,22,start,32 vert,304.0,479.0
edge,end,18,start,33 vert,621.0,379.0
edge,end,30,start,33 vert,487.0,485.0
edge,start,34,end,37 edge,start,1,end,53
edge,end,35,start,36 edge,start,2,end,16
edge,end,5,start,36 edge,start,3,end,21
edge,start,37,end,39 edge,start,5,end,18
edge,end,10,start,39 edge,end,5,start,6
edge,end,25,start,42 edge,start,7,end,15
edge,start,9,end,22
edge,start,11,end,52
edge,start,12,end,24
edge,start,14,end,41
edge,start,14,end,28
edge,start,15,end,35
edge,start,17,end,58
edge,end,12,start,18
edge,start,19,end,42
edge,start,19,end,50
edge,start,22,end,44
edge,end,20,start,23
edge,end,17,start,24
edge,start,26,end,29
edge,end,26,start,27
edge,end,7,start,29
edge,end,0,start,30
edge,end,0,start,31
edge,start,32,end,42
edge,start,32,end,50
edge,end,8,start,36
edge,start,36,end,57
edge,start,37,end,49
edge,end,33,start,37
edge,start,38,end,54
edge,end,10,start,38
edge,end,25,start,40
edge,start,40,end,46
edge,end,34,start,43
edge,start,43,end,47
edge,end,6,start,44
edge,end,11,start,45
edge,end,13,start,48
edge,end,49,start,51
edge,end,33,start,51
edge,end,1,start,52
edge,end,9,start,53
edge,end,34,start,55
edge,end,47,start,55
edge,end,54,start,56
edge,end,10,start,56
edge,end,45,start,58
edge,end,4,start,59
edge,end,39,start,59

View File

@@ -38,22 +38,21 @@ public class Car{
} }
//draw the car and its views //draw the car and its views
public void drawCar(PointGraph g){ public void drawCar(PointGraph map, boolean SLAMIsHidden){
proc.stroke(255); proc.stroke(255);
proc.ellipse(pose.x, pose.y, carWidth, carLength); proc.ellipse(pose.x, pose.y, carWidth, carLength);
this.updateScan(g); this.updateScan(map);
// this.slam.drawLines(); if(!SLAMIsHidden){
this.slam.drawFeatures(proc);
}
} }
//With all the views that the car has, get their point list //With all the views that the car has, get their point list
void updateScan(PointGraph map){ void updateScan(PointGraph map){
for(View view : views){ for(View view : views){
view.look(map); view.look(map);
} slam.RANSAC(view);
for(View view : views){
ArrayList<Vector> pointList = view.getPoints();
// slam.RANSAC(pointList, view.getFOV() / view.getRayNum());
} }
} }

View File

@@ -12,6 +12,8 @@ public class Processing extends PApplet {
public static PApplet processing; public static PApplet processing;
PointGraph map = new PointGraph(); PointGraph map = new PointGraph();
boolean mapIsHidden = false;
boolean SLAMIsHidden = false;
public static void main(String[] args) { public static void main(String[] args) {
PApplet.main("Processing"); PApplet.main("Processing");
@@ -21,7 +23,7 @@ public class Processing extends PApplet {
processing = this; processing = this;
car = new Car(processing, 100,100,50,40); car = new Car(processing, 100,100,50,40);
size(1000, 1000); size(1000, 1000);
car.addView(360,180); car.addView(360,360);
// for(int i = 0; i < 10; i++){ // for(int i = 0; i < 10; i++){
// PointVertex vStart = new PointVertex(random(50, 950), random(50, 950)); // PointVertex vStart = new PointVertex(random(50, 950), random(50, 950));
@@ -32,8 +34,10 @@ public class Processing extends PApplet {
} }
public void draw(){ public void draw(){
background(0); background(0);
map.draw(processing); if(!mapIsHidden){
car.drawCar(map); map.draw(processing);
}
car.drawCar(map, SLAMIsHidden);
strokeWeight(2); strokeWeight(2);
stroke(255); stroke(255);
//car.drive(new int[] {0, 0}); //car.drive(new int[] {0, 0});
@@ -89,6 +93,12 @@ public class Processing extends PApplet {
e.printStackTrace(); e.printStackTrace();
} }
} }
if(key == 'h'){
mapIsHidden = !mapIsHidden;
}
if(key == 'j'){
SLAMIsHidden = !SLAMIsHidden;
}
} }
public void mousePressed(){ public void mousePressed(){

View File

@@ -1,14 +1,17 @@
import Vector.*; import Vector.*;
import processing.core.*; import processing.core.*;
import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static java.lang.Math.random;
import static processing.core.PApplet.*; import static processing.core.PApplet.*;
public class SLAM{ public class SLAM{
ArrayList<Line> lines = new ArrayList<>(); ArrayList<Line> lines = new ArrayList<>();
ShortTermMem unassociatedPoints = new ShortTermMem();
private static PApplet proc; private static PApplet proc;
SLAM(PApplet processing){ SLAM(PApplet processing){
@@ -17,38 +20,41 @@ public class SLAM{
/** /**
* @param set the set to take a sub sample of * @param set the set to take a sub sample of
* @param indexRange the range within to take the sub sample
* @param subSampleSize the size of the sub sample * @param subSampleSize the size of the sub sample
* @return A random subset of the set within an indexRange and of size: subSampleSize * @param minAngle the minimum angle allowed in the subset
* @param maxAngle the maximum angle allowed in the subset
* @return A random subset of the set within the angle range
*/ */
private List<Vector> randomSample(ArrayList<Vector> set, int indexRange, int subSampleSize){ private List<Vector> randomSampleInAngleRange(ArrayList<Vector> set, int subSampleSize, float minAngle, float maxAngle){
// select a random laser data reading
int randomIdx = (int) proc.random(set.size() - 1); // index of starter reading
Vector point = set.get(randomIdx); // point of starter reading
// get a random sample of size numSampleReadings within degreeRange degrees of this laser reading. // create an arraylist with all points within the angle range from the given set
List<Vector> subSample; ArrayList<Vector> pointsInAngleRange = new ArrayList<>();
int rangeStart = randomIdx - indexRange >= 0 ? randomIdx - indexRange : 0; for(Vector point : set){
int rangeEnd = randomIdx + indexRange < set.size() ? randomIdx + indexRange : set.size()-1; if(minAngle <= point.z && point.z <= maxAngle){
subSample = set.subList(rangeStart, rangeEnd); // get the sub-sample pointsInAngleRange.add(point);
Collections.shuffle(subSample); // shuffle the list }
List<Vector> randomSample = subSample.subList(0, rangeEnd-rangeStart); // get our random sample
if (!randomSample.contains(point)) {
randomSample.add(point);
} }
return randomSample; // shuffle the list to randomize it
Collections.shuffle(pointsInAngleRange);
// if the list is too small, just return the whole list
if(pointsInAngleRange.size() < subSampleSize){
return pointsInAngleRange;
}
// return a subSample of the list
return pointsInAngleRange.subList(0, subSampleSize);
} }
/** /**
* @param originalList the list which the randomSample of points originated from
* @param randomSample a random subsampling of points from the originalList * @param randomSample a random subsampling of points from the originalList
* @param maxRange the maximum distance away from the line of best fit of the subSample of points for a given point's consensus to count. * @param maxRange the maximum distance away from the line of best fit of the subSample of points for a given point's consensus to count.
* @param consensus the number of points that have to give their consensus for the line of best fit to count as a valid feature. * @param consensus the number of points that have to give their consensus for the line of best fit to count as a valid feature.
*/ */
private void extractFeature(ArrayList<Vector> originalList, List<Vector> randomSample, float maxRange, int consensus){ private void extractFeature(List<Vector> randomSample, float maxRange, int consensus){
// get a line of best fit for this list. // get a line of best fit for this list.
Line bestFit = new Line(randomSample); Line bestFit = new Line(randomSample);
// check that there are enough points in the sample that are less than the maxRange away to form a consensus
int count = 0; int count = 0;
ArrayList<Vector> newRandomSample = new ArrayList<>(); ArrayList<Vector> newRandomSample = new ArrayList<>();
for (Vector v : randomSample) { for (Vector v : randomSample) {
@@ -63,41 +69,69 @@ public class SLAM{
lines.add(bestFit); lines.add(bestFit);
// remove the associated readings from the total available readings. // remove the associated readings from the total available readings.
for (Vector v : newRandomSample) { for (Vector v : newRandomSample) {
originalList.remove(v); this.unassociatedPoints.remove(v);
} }
} }
} }
/** private void fitToPreviousReadings(List<Vector> sample, float maxRange){
* @param newPoints a new scan of points to perform feature detection on // keep track of points that were succesffully associated so they can be removed from the sample at the end
* @param raysPerDegree How many degrees apart are each ray that was cast ArrayList<Vector> pointsToRemove = new ArrayList<>();
*/ // try to associate points from the smaple with pre-existing lines
public void RANSAC(ArrayList<Vector> newPoints, float raysPerDegree){ for(Vector v: sample){
float degreeRange = radians(10/2); // range to randomly sample readings within for(Line l : lines){
int indexRange = (int) (degreeRange / raysPerDegree); if(l.getDistance(v) < maxRange){
int numSampleReadings = 10; // number of readings to randomly sample l.refitLine(v);
// constrain numSampleReadings so that it cant be higher than possible pointsToRemove.add(v);
if(numSampleReadings >= 2 * indexRange){ }
numSampleReadings = 2 * indexRange; }
} }
int consensus = 6; // the number of points that need to lie near a line for it to be considered valid.
for(Vector v : pointsToRemove){
sample.remove(v);
}
}
/**
* @param view a laser scan view
*/
public void RANSAC(View view){
unassociatedPoints.addScan(view.getPos(), view.getPoints());
float degreeRange = radians(5); // range to randomly sample readings within
int numSampleReadings = 15; // number of readings to randomly sample
int consensus = 10; // the number of points that need to lie near a line for it to be considered valid.
float maxRange = 10; // the maximum distance a point can be away from the line for it to count as a consensus float maxRange = 10; // the maximum distance a point can be away from the line for it to count as a consensus
// this for loop determines the maximum number of trials we're willing to do. // this for loop determines the maximum number of trials we're willing to do.
for(int j = 0; j < 20; j++) { for(int j = 0; j < 20; j++) {
// if there aren't enough points left in the set to form a consensus, we're done. // if there aren't enough points left in the set to form a consensus, we're done.
if(newPoints.size() < consensus){ if(this.unassociatedPoints.size() < maxRange){
break; break;
} }
// get a random sub sample of newPoints within the index range of a given size // get a random angle between -PI and PI
List<Vector> randomSample = this.randomSample(newPoints, indexRange, numSampleReadings); float randomAngle = (float) (2*PI*(random()) - 0.5);
// check if the sub sample forms a valid line and remove the randomSample points if it does. // get a random sub sample of newPoints within the index range of a given size
extractFeature(newPoints, randomSample, maxRange, consensus); List<Vector> randomSample = this.randomSampleInAngleRange(this.unassociatedPoints.getPoints(), numSampleReadings, randomAngle-degreeRange, randomAngle+degreeRange);
if(randomSample.size() >= numSampleReadings){
// try to associate points from the sample with previously made lines
fitToPreviousReadings(randomSample, maxRange);
// check if the sub sample forms a valid line and remove the randomSample points if it does.
extractFeature(randomSample, maxRange, consensus);
}
} }
} }
public void drawFeatures(PApplet proc){
for(Line line : lines){
line.draw(proc);
}
}
} }

59
src/ShortTermMem.java Normal file
View File

@@ -0,0 +1,59 @@
import java.util.ArrayList;
import Vector.Vector;
public class ShortTermMem {
ArrayList<ArrayList<Vector>> scans = new ArrayList<>();
ArrayList<Vector> scanPositions = new ArrayList<>();
ArrayList<Long> scanTimes = new ArrayList<>();
private int size = 0;
public void addScan(Vector scanPosition, ArrayList<Vector> scan){
size += scan.size();
scans.add(scan);
scanPositions.add(scanPosition);
scanTimes.add(System.currentTimeMillis());
purgeScans();
}
public ArrayList<Vector> getPoints(){
ArrayList<Vector> points = new ArrayList<>();
for(ArrayList<Vector> pointList : this.scans){
points.addAll(pointList);
}
return points;
}
public void remove(Vector point){
for(ArrayList<Vector> pointList : this.scans){
int listSize = pointList.size();
pointList.remove(point);
if(listSize - pointList.size() != 0){
size--;
break;
}
}
}
private void purgeScans(){
long currentTime = System.currentTimeMillis();
int i = scanTimes.size();
// loop through the list backwards and remove all scans that are over second old
// we loop backwards to avoid removal conflicts
while(i > 0){
i--;
long dt = currentTime - scanTimes.get(i);
if(dt < 1000){
continue;
}
size -= scans.get(i).size();
scanTimes.remove(i);
scanPositions.remove(i);
scans.remove(i);
}
}
public int size(){
return this.size;
}
}

View File

@@ -3,6 +3,7 @@ package Vector;
import Vector.Vector; import Vector.Vector;
import processing.core.PApplet; import processing.core.PApplet;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import static processing.core.PApplet.*; import static processing.core.PApplet.*;
@@ -13,6 +14,8 @@ public class Line implements LineInterface{
// store the starting position of the line // store the starting position of the line
protected Vector position = new Vector(0,0); protected Vector position = new Vector(0,0);
protected ArrayList<Vector> points = new ArrayList<>();
public Line(Vector startPosition, Vector endPosition){ public Line(Vector startPosition, Vector endPosition){
direction = endPosition.sub(startPosition); direction = endPosition.sub(startPosition);
position = startPosition; position = startPosition;
@@ -26,8 +29,16 @@ public class Line implements LineInterface{
bestFit(points); bestFit(points);
} }
public void refitLine(Vector newPoint){
// add the new point to our list
this.points.add(newPoint);
// rerun the bestFit algorithm with the new point
bestFit(new ArrayList<>());
}
// least squares line of best fit algorithm // least squares line of best fit algorithm
private void bestFit(List<Vector> points){ public void bestFit(List<Vector> fitPoints){
this.points.addAll(fitPoints);
// get the mean of all the points // get the mean of all the points
Vector mean = new Vector(); Vector mean = new Vector();
for(Vector point : points){ for(Vector point : points){
@@ -37,26 +48,28 @@ public class Line implements LineInterface{
// this section calculates the direction vector of the line of best fit // this section calculates the direction vector of the line of best fit
Vector direction = new Vector(); Vector direction = new Vector();
float length = 1; float length = 0;
// get the rise and run of the line of best fit // get the rise and run of the line of best fit
for(Vector point : points){ for(Vector point : points){
direction.y += (point.x - mean.x)*(point.y - mean.y); // rise direction.y += (point.x - mean.x)*(point.y - mean.y); // rise
direction.x += pow((point.x - mean.x),2); direction.x += pow((point.x - mean.x),2);
// find the point that's furthest from the mean and use it to set the line length. // get the average distance avery point is away from the mean.
float dist = abs(point.sub(mean).mag()); float dist = abs(point.sub(mean).mag());
if(dist > length){ length += dist;
length = 2*dist;
}
} }
length = 2f*length/points.size();
// if the direction is perfectly vertical create a line to represent that.
if(direction.y == 0){ if(direction.y == 0){
this.direction = new Vector(0, 1); this.direction = new Vector(0, 1);
} }
else{ else{
this.direction = new Vector(1, direction.y/direction.x); this.direction = new Vector(1, direction.y/direction.x);
} }
// scale the direction vector to be the correct length of the line.
this.direction = this.direction.normalize().mul(length); this.direction = this.direction.normalize().mul(length);
this.position = mean.sub(this.direction.div(2)); this.position = mean.sub(this.direction.div(2));
} }
@@ -84,12 +97,15 @@ public class Line implements LineInterface{
return this.position.add(this.direction); return this.position.add(this.direction);
} }
/**
* @param point
* @return the smallest distance from the point to this line
*/
public float getDistance(Vector point){ public float getDistance(Vector point){
return (point.sub(position).cross(direction)).mag() / direction.mag(); float line_dist = direction.mag();
if(line_dist == 0) return this.position.sub(point).mag();
Vector l2 = this.endPoint();
float t = ((point.x - position.x) * (l2.x - position.x) + (point.y - position.y) * (l2.y - position.y) + (point.z - position.z) * (l2.z - position.z)) / line_dist;
t = constrain(t, 0, 1);
Vector closestPoint = new Vector(position.x + t * (l2.x - position.x), position.y + t * (l2.y - position.y), position.z + t * (l2.z - position.z));
return closestPoint.sub(point).mag();
} }
public void draw(PApplet proc){ public void draw(PApplet proc){

View File

@@ -1,5 +1,7 @@
package Vector; package Vector;
import processing.core.PApplet;
import static java.lang.Math.*; import static java.lang.Math.*;
import static processing.core.PApplet.cos; import static processing.core.PApplet.cos;
import static processing.core.PApplet.sin; import static processing.core.PApplet.sin;
@@ -90,4 +92,8 @@ public class Vector {
float currentAngle = this.angle(); float currentAngle = this.angle();
return new Vector(cos(currentAngle + angle), sin(currentAngle + angle)).mul(distance); return new Vector(cos(currentAngle + angle), sin(currentAngle + angle)).mul(distance);
} }
public void draw(PApplet proc){
proc.circle(this.x, this.y, 8);
}
} }

View File

@@ -22,7 +22,7 @@ public class View {
public void setRayNum(int numberOfRays, float FOV, float angleOffset) { public void setRayNum(int numberOfRays, float FOV, float angleOffset) {
float rayStep = FOV / numberOfRays; float rayStep = FOV / numberOfRays;
rays.clear(); rays.clear();
float angle = (float) (0.01 - angleOffset); //the 0.01 fixes some bugs float angle = (float) (angleOffset); //the 0.01 fixes some bugs
for (int i = 0; i < numberOfRays; i++) { for (int i = 0; i < numberOfRays; i++) {
Ray ray = new Ray(pose, angle); Ray ray = new Ray(pose, angle);
angle = angle + rayStep; angle = angle + rayStep;
@@ -35,7 +35,8 @@ public class View {
for (Ray ray : rays) { for (Ray ray : rays) {
ray.castRay(map); ray.castRay(map);
if(ray.hasCollided()){ if(ray.hasCollided()){
ray.drawRay(proc); ray.getPoint().draw(proc);
// ray.drawRay(proc);
} }
} }
} }
@@ -51,7 +52,10 @@ public class View {
//changes the angle of the view //changes the angle of the view
public void setAngle(float angle) { public void setAngle(float angle) {
this.angle = angle; this.angle = angle;
this.setRayNum(rays.size(), this.FOV, angle); for(Ray ray : rays){
float angleOffset = ray.getAngle() - this.angle;
ray.setAngle(this.angle+angleOffset);
}
} }
//changes the field of view of the view //changes the field of view of the view
@@ -81,11 +85,27 @@ public class View {
ArrayList<Vector> points = new ArrayList<>(); ArrayList<Vector> points = new ArrayList<>();
for (Ray ray : rays) { for (Ray ray : rays) {
if (ray.hasCollided()){ if(ray.hasCollided()){
points.add(ray.getPoint()); Vector point = ray.getPoint();
// store the angle information for that point in the z coordinate
point.z = ray.getAngle();
points.add(point);
} }
} }
return points; return points;
} }
/**
* @return A list of the angles where a collision was detected
*/
public ArrayList<Float> getAngles(){
ArrayList<Float> angles = new ArrayList<>();
for (Ray ray : rays) {
if (ray.hasCollided()){
angles.add(ray.getAngle());
}
}
return angles;
}
} }