Skip to content

Commit adb404c

Browse files
committed
added .gitignore and project cleanup
1 parent 42172c3 commit adb404c

13 files changed

+600
-0
lines changed

.gitignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# Eclipse Project
8+
.classpath
9+
.project
10+
.settings/
11+
12+
# Mobile Tools for Java (J2ME)
13+
.mtj.tmp/
14+
15+
# Package Files #
16+
*.jar
17+
*.war
18+
*.nar
19+
*.ear
20+
*.zip
21+
*.tar.gz
22+
*.rar
23+

bin/ev3/robot/BarrierDetector.class

-2.64 KB
Binary file not shown.

bin/ev3/robot/EV3Exit.class

-615 Bytes
Binary file not shown.

bin/ev3/robot/Hello.class

-612 Bytes
Binary file not shown.

bin/ev3/robot/LineDetector.class

-4.49 KB
Binary file not shown.

bin/ev3/robot/SimpleMovements.class

-849 Bytes
Binary file not shown.

bin/ev3/robot/TestPilot.class

-4.59 KB
Binary file not shown.

src/ev3/robot/BarrierDetector.java

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package ev3.robot;
2+
3+
import lejos.hardware.Button;
4+
import lejos.hardware.lcd.LCD;
5+
import lejos.hardware.port.SensorPort;
6+
import lejos.hardware.sensor.EV3IRSensor;
7+
import lejos.robotics.SampleProvider;
8+
import lejos.hardware.motor.Motor;
9+
10+
public class BarrierDetector extends Thread {
11+
12+
EV3IRSensor ir = new EV3IRSensor(SensorPort.S4);
13+
SampleProvider distance = ir.getDistanceMode();
14+
15+
boolean stop = false;
16+
17+
public boolean getBarrier(){
18+
return stop;
19+
}
20+
21+
@Override
22+
public void run(){
23+
24+
System.out.println("Distance: starting sensor...");
25+
26+
while(true) {
27+
if(getDistance() < 10){
28+
Button.LEDPattern(2);
29+
System.out.println("Distance: barrier found!");
30+
System.out.println(" Moving camera up");
31+
stop = true;
32+
Motor.B.rotate(1000);
33+
34+
try {
35+
Thread.sleep(10);
36+
} catch (InterruptedException e) {
37+
System.out.println("Error sleeping!");
38+
}
39+
40+
while(getDistance() < 10){
41+
try {
42+
Thread.sleep(10);
43+
} catch (InterruptedException e) {
44+
System.out.println("Error sleeping!");
45+
}
46+
}
47+
48+
if(getDistance() >= 10){
49+
System.out.println(" Route free");
50+
System.out.println(" Moving camera down");
51+
Button.LEDPattern(1);
52+
Motor.B.rotate(-1000);
53+
54+
stop = false;
55+
}
56+
}
57+
}
58+
}
59+
60+
public float getDistance(){
61+
float[] sample = new float[distance.sampleSize()];
62+
distance.fetchSample(sample, 0);
63+
return sample[0];
64+
}
65+
66+
/**
67+
* Displays the current sensor values.
68+
*/
69+
public void printLiveData() {
70+
LCD.clearDisplay();
71+
while (true) {
72+
LCD.setAutoRefreshPeriod(2);
73+
LCD.drawString("- sonic sensor -", 0, 1);
74+
float[] sample = new float[distance.sampleSize()];
75+
distance.fetchSample(sample, 0);
76+
LCD.drawString( String.valueOf(sample[0]), 0, 3);
77+
78+
if(Button.readButtons() == Button.ID_ENTER){
79+
LCD.clearDisplay();
80+
break;
81+
}
82+
}
83+
}
84+
85+
/**
86+
* @param args
87+
*/
88+
public static void main(String[] args) {
89+
90+
BarrierDetector barrier = new BarrierDetector();
91+
new EV3Exit().start();
92+
93+
barrier.printLiveData();
94+
barrier.start();
95+
}
96+
97+
}

src/ev3/robot/EV3Exit.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ev3.robot;
2+
3+
import lejos.hardware.Button;
4+
5+
6+
/**
7+
* Simple thread which quits the application if a button is pressed
8+
* @author Oliver
9+
*/
10+
public class EV3Exit extends Thread{
11+
12+
private int exitButton;
13+
14+
/**
15+
* Default constructor with escape as exit button
16+
*/
17+
public EV3Exit(){
18+
this.exitButton = Button.ID_ESCAPE;
19+
}
20+
21+
/**
22+
* User defined exit button
23+
* @param exitButton
24+
*/
25+
public EV3Exit(int exitButton){
26+
this.exitButton = exitButton;
27+
}
28+
29+
/**
30+
* Starts the thread
31+
*/
32+
@Override
33+
public void run() {
34+
while (true) {
35+
if(Button.readButtons() == exitButton){
36+
System.exit(MAX_PRIORITY);
37+
}
38+
}
39+
}
40+
41+
}

src/ev3/robot/Hello.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ev3.robot;
2+
3+
import lejos.hardware.Button;
4+
import lejos.hardware.lcd.LCD;
5+
6+
public class Hello {
7+
8+
public static void main(String[] args) {
9+
LCD.clear();
10+
LCD.drawString("Hallo EV3...", 0, 5);
11+
Button.waitForAnyPress();
12+
LCD.clear();
13+
LCD.refresh();
14+
15+
}
16+
17+
}

src/ev3/robot/LineDetector.java

+209
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
package ev3.robot;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
import lejos.hardware.Button;
8+
import lejos.hardware.sensor.EV3ColorSensor;
9+
import lejos.hardware.lcd.LCD;
10+
import lejos.hardware.motor.Motor;
11+
import lejos.hardware.port.SensorPort;
12+
import lejos.robotics.navigation.DifferentialPilot;
13+
14+
15+
/**
16+
* Detects a line...
17+
*
18+
* @author Oliver
19+
*/
20+
public class LineDetector {
21+
22+
//private LightSensor light = new LightSensor(SensorPort.S3);
23+
private EV3ColorSensor light = new EV3ColorSensor(SensorPort.S3);
24+
25+
private float high = 0.0f;
26+
private float low = 2.0f;
27+
private float diff = 0;
28+
private float threshold = 0.5f;
29+
30+
/**
31+
* true : bright line on dark background
32+
* false : dark line on bright background
33+
*/
34+
private boolean lineColor = false;
35+
private boolean detected = false;
36+
37+
/**
38+
* @return LightSensor
39+
*/
40+
public EV3ColorSensor getLightSensor() {
41+
return light;
42+
}
43+
44+
/**
45+
* @return the computed threshold between the two "colors"
46+
*/
47+
public float getThreshold() {
48+
return threshold;
49+
}
50+
51+
/**
52+
* @return true if the line is bright and false if it is dark
53+
*/
54+
public boolean getLineColor() {
55+
return lineColor;
56+
}
57+
58+
/**
59+
* @return true if a line is detected during the calibration
60+
*/
61+
public boolean lineDetected() {
62+
return detected;
63+
}
64+
65+
/**
66+
* @return true if a line is found
67+
*/
68+
public boolean isLine(){
69+
if(getRedSensorValue() > threshold){
70+
if(lineColor){
71+
return true;
72+
} else {
73+
return false;
74+
}
75+
} else {
76+
if(lineColor){
77+
return false;
78+
} else {
79+
return true;
80+
}
81+
}
82+
}
83+
84+
/**
85+
* Measures the brightness of the surrounding underground
86+
* and computes threshold and line "color"
87+
*
88+
* @param pilot - necessary for the rotation
89+
* @return true if a line is detected
90+
*/
91+
public boolean calibrate(DifferentialPilot pilot){
92+
93+
List<Float> lightList = new ArrayList<Float>();
94+
float sum = 0;
95+
96+
high = 0.0f;
97+
low = 2.0f;
98+
diff = 0;
99+
threshold = 0.3f;
100+
pilot.setRotateSpeed(16);//12
101+
pilot.rotate(360, true);
102+
while (pilot.isMoving()) {
103+
lightList.add(getRedSensorValue());
104+
}
105+
106+
Iterator<Float> iter = lightList.iterator();
107+
108+
while(iter.hasNext()){
109+
float next = iter.next();
110+
sum += next;
111+
if(next < low) {
112+
low = next;
113+
}
114+
if(next > high) {
115+
high = next;
116+
}
117+
}
118+
119+
diff = high - low;
120+
121+
//line detected?
122+
if(diff < 0.2f) {
123+
detected = false;
124+
return false;
125+
}
126+
detected = true;
127+
128+
if((sum / lightList.size()) < threshold) lineColor = true; else lineColor = false;
129+
threshold = low + (diff/2);
130+
131+
return true;
132+
}
133+
134+
/**
135+
* Displays the current sensor values.
136+
*/
137+
public void printLiveData() {
138+
LCD.clearDisplay();
139+
while (true) {
140+
LCD.setAutoRefreshPeriod(2);
141+
LCD.drawString("- light sensor -", 0, 1);
142+
LCD.drawString(String.valueOf(getRedSensorValue()), 4, 3);
143+
144+
if(Button.readButtons() == Button.ID_ENTER){
145+
LCD.clearDisplay();
146+
break;
147+
}
148+
}
149+
}
150+
151+
/**
152+
* Read float value (Red mode)
153+
* @return
154+
*/
155+
public float getRedSensorValue(){
156+
float[] sample = new float[light.getRedMode().sampleSize()];
157+
light.getRedMode().fetchSample(sample, 0);
158+
return sample[0];
159+
}
160+
161+
/**
162+
* Displays the data collected during a calibration
163+
*/
164+
public void printInfo() {
165+
LCD.clearDisplay();
166+
167+
System.out.println("- light sensor -");
168+
System.out.println("high: " + high);
169+
System.out.println("low: " + low);
170+
System.out.println("diff: " + diff);
171+
System.out.println("average: " + threshold);
172+
173+
if(detected) {
174+
System.out.println("line found:");
175+
if(lineColor) {
176+
System.out.println("bright");
177+
} else {
178+
System.out.println("dark");
179+
}
180+
} else {
181+
System.out.println("no line found!");
182+
}
183+
}
184+
185+
/**
186+
* Test
187+
* @param args
188+
*/
189+
public static void main(String[] args) {
190+
LineDetector sensors = new LineDetector();
191+
new EV3Exit().start();
192+
193+
DifferentialPilot pilot = new DifferentialPilot(2.42f, 11.5f, Motor.A, Motor.D, true);
194+
sensors.printLiveData();
195+
sensors.calibrate(pilot);
196+
sensors.printInfo();
197+
Button.waitForAnyPress();
198+
199+
while(true) {
200+
if(sensors.isLine()){
201+
System.out.println("line");
202+
} else {
203+
System.out.println("no line");
204+
}
205+
Button.waitForAnyPress();
206+
}
207+
}
208+
209+
}

0 commit comments

Comments
 (0)